QGIS/python/plugins/processing/gui/ProcessingToolbox.py

413 lines
16 KiB
Python
Raw Normal View History

2012-10-05 23:28:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
2013-08-12 20:44:27 +02:00
ProcessingToolbox.py
2012-10-05 23:28:47 +02:00
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
2013-09-21 12:44:51 +02:00
2012-10-05 23:28:47 +02:00
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
2012-10-05 23:28:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-05 23:28:47 +02:00
__revision__ = '$Format:%H$'
2012-09-15 18:25:25 +03:00
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.utils import iface
from processing.modeler.ModelerUtils import ModelerUtils
2013-08-12 20:44:27 +02:00
from processing.core.Processing import Processing
from processing.core.ProcessingLog import ProcessingLog
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.gui.MessageDialog import MessageDialog
from processing.gui.AlgorithmClassification import AlgorithmDecorator
from processing.gui.AlgorithmDialog import AlgorithmDialog
from processing.gui.BatchAlgorithmDialog import BatchAlgorithmDialog
2013-08-12 20:44:27 +02:00
from processing.gui.EditRenderingStylesDialog import EditRenderingStylesDialog
2013-08-12 20:44:27 +02:00
from processing.ui.ui_ProcessingToolbox import Ui_ProcessingToolbox
2012-09-15 18:25:25 +03:00
2013-08-12 20:44:27 +02:00
class ProcessingToolbox(QDockWidget, Ui_ProcessingToolbox):
USE_CATEGORIES = '/Processing/UseSimplifiedInterface'
def __init__(self):
QDockWidget.__init__(self, None)
self.setupUi(self)
self.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
self.modeComboBox.clear()
self.modeComboBox.addItems(['Simplified interface',
'Advanced interface'])
settings = QSettings()
if not settings.contains(self.USE_CATEGORIES):
settings.setValue(self.USE_CATEGORIES, True)
useCategories = settings.value(self.USE_CATEGORIES, type=bool)
if useCategories:
self.modeComboBox.setCurrentIndex(0)
else:
self.modeComboBox.setCurrentIndex(1)
self.modeComboBox.currentIndexChanged.connect(self.modeHasChanged)
2013-08-25 10:45:52 +02:00
self.searchBox.textChanged.connect(self.textChanged)
self.algorithmTree.customContextMenuRequested.connect(
self.showPopupMenu)
self.algorithmTree.doubleClicked.connect(self.executeAlgorithm)
2012-12-01 20:22:21 +02:00
if hasattr(self.searchBox, 'setPlaceholderText'):
self.searchBox.setPlaceholderText(self.tr('Search...'))
2012-12-01 20:22:21 +02:00
self.fillTree()
2014-04-06 12:27:51 +02:00
def textChanged(self):
text = self.searchBox.text().strip(' ').lower()
self._filterItem(self.algorithmTree.invisibleRootItem(), text)
if text:
self.algorithmTree.expandAll()
else:
self.algorithmTree.collapseAll()
self.algorithmTree.invisibleRootItem().child(0).setExpanded(True)
2014-04-06 12:27:51 +02:00
def _filterItem(self, item, text):
if (item.childCount() > 0):
show = False
2014-04-06 12:27:51 +02:00
for i in xrange(item.childCount()):
child = item.child(i)
showChild = self._filterItem(child, text)
2014-04-06 12:27:51 +02:00
show = showChild or show
item.setHidden(not show)
return show
2014-04-06 12:27:51 +02:00
elif isinstance(item, (TreeAlgorithmItem, TreeActionItem)):
hide = bool(text) and (text not in item.text(0).lower())
item.setHidden(hide)
return not hide
else:
2014-04-06 12:27:51 +02:00
item.setHidden(True)
return False
2014-04-06 12:27:51 +02:00
def modeHasChanged(self):
idx = self.modeComboBox.currentIndex()
settings = QSettings()
if idx == 0:
# Simplified
settings.setValue(self.USE_CATEGORIES, True)
else:
settings.setValue(self.USE_CATEGORIES, False)
self.fillTree()
2012-09-15 18:25:25 +03:00
def algsListHasChanged(self):
self.fillTree()
2014-04-06 12:27:51 +02:00
def updateProvider(self, providerName, updateAlgsList = True):
if updateAlgsList:
Processing.updateAlgsList()
for i in xrange(self.algorithmTree.invisibleRootItem().childCount()):
child = self.algorithmTree.invisibleRootItem().child(i)
if isinstance(child, TreeProviderItem):
if child.providerName == providerName:
child.refresh()
# sort categories and items in categories
child.sortChildren(0, Qt.AscendingOrder)
for i in xrange(child.childCount()):
child.child(i).sortChildren(0, Qt.AscendingOrder)
break
2012-09-15 18:25:25 +03:00
def showPopupMenu(self, point):
2012-09-15 18:25:25 +03:00
item = self.algorithmTree.itemAt(point)
if isinstance(item, TreeAlgorithmItem):
alg = item.alg
popupmenu = QMenu()
executeAction = QAction(self.tr('Execute'), self.algorithmTree)
2012-09-15 18:25:25 +03:00
executeAction.triggered.connect(self.executeAlgorithm)
popupmenu.addAction(executeAction)
2013-10-07 18:40:37 +02:00
if alg.canRunInBatchMode and not alg.allowOnlyOpenedLayers:
executeBatchAction = QAction(
self.tr('Execute as batch process'),
self.algorithmTree)
executeBatchAction.triggered.connect(
self.executeAlgorithmAsBatchProcess)
popupmenu.addAction(executeBatchAction)
popupmenu.addSeparator()
editRenderingStylesAction = QAction(
self.tr('Edit rendering styles for outputs'),
self.algorithmTree)
editRenderingStylesAction.triggered.connect(
self.editRenderingStyles)
2012-09-15 18:25:25 +03:00
popupmenu.addAction(editRenderingStylesAction)
2013-08-12 20:44:27 +02:00
actions = Processing.contextMenuActions
if len(actions) > 0:
popupmenu.addSeparator()
2012-09-15 18:25:25 +03:00
for action in actions:
action.setData(alg, self)
2012-09-15 18:25:25 +03:00
if action.isEnabled():
contextMenuAction = QAction(action.name,
self.algorithmTree)
2012-09-15 18:25:25 +03:00
contextMenuAction.triggered.connect(action.execute)
popupmenu.addAction(contextMenuAction)
popupmenu.exec_(self.algorithmTree.mapToGlobal(point))
def editRenderingStyles(self):
item = self.algorithmTree.currentItem()
if isinstance(item, TreeAlgorithmItem):
2013-08-12 20:44:27 +02:00
alg = Processing.getAlgorithm(item.alg.commandLineName())
2012-09-15 18:25:25 +03:00
dlg = EditRenderingStylesDialog(alg)
dlg.exec_()
def executeAlgorithmAsBatchProcess(self):
item = self.algorithmTree.currentItem()
if isinstance(item, TreeAlgorithmItem):
2013-08-12 20:44:27 +02:00
alg = Processing.getAlgorithm(item.alg.commandLineName())
alg = alg.getCopy()
dlg = BatchAlgorithmDialog(alg)
2012-09-15 18:25:25 +03:00
dlg.exec_()
def executeAlgorithm(self):
item = self.algorithmTree.currentItem()
if isinstance(item, TreeAlgorithmItem):
2013-08-12 20:44:27 +02:00
alg = Processing.getAlgorithm(item.alg.commandLineName())
2012-09-15 18:25:25 +03:00
message = alg.checkBeforeOpeningParametersDialog()
if message:
dlg = MessageDialog()
dlg.setTitle(self.tr('Missing dependency'))
dlg.setMessage(
self.tr('<h3>Missing dependency. This algorithm cannot '
'be run :-( </h3>\n%s') % message)
dlg.exec_()
2012-09-15 18:25:25 +03:00
return
2013-01-26 23:55:14 +01:00
alg = alg.getCopy()
2012-09-15 18:25:25 +03:00
dlg = alg.getCustomParametersDialog()
if not dlg:
dlg = AlgorithmDialog(alg)
canvas = iface.mapCanvas()
2012-09-15 18:25:25 +03:00
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec_()
if canvas.mapTool() != prevMapTool:
2012-09-15 18:25:25 +03:00
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)
if dlg.executed:
showRecent = ProcessingConfig.getSetting(
ProcessingConfig.SHOW_RECENT_ALGORITHMS)
2012-09-15 18:25:25 +03:00
if showRecent:
self.addRecentAlgorithms(True)
2012-09-15 18:25:25 +03:00
if isinstance(item, TreeActionItem):
action = item.action
action.setData(self)
action.execute()
def fillTree(self):
settings = QSettings()
useCategories = settings.value(self.USE_CATEGORIES, type=bool)
if useCategories:
self.fillTreeUsingCategories()
else:
self.fillTreeUsingProviders()
2013-03-26 14:15:12 +01:00
self.algorithmTree.sortItems(0, Qt.AscendingOrder)
self.addRecentAlgorithms(False)
2013-03-26 14:15:12 +01:00
def addRecentAlgorithms(self, updating):
showRecent = ProcessingConfig.getSetting(
ProcessingConfig.SHOW_RECENT_ALGORITHMS)
2013-04-02 22:34:21 +02:00
if showRecent:
2013-08-12 20:44:27 +02:00
recent = ProcessingLog.getRecentAlgorithms()
if len(recent) != 0:
found = False
if updating:
recentItem = self.algorithmTree.topLevelItem(0)
2013-04-02 22:34:21 +02:00
treeWidget = recentItem.treeWidget()
treeWidget.takeTopLevelItem(
treeWidget.indexOfTopLevelItem(recentItem))
2013-04-02 22:34:21 +02:00
recentItem = QTreeWidgetItem()
recentItem.setText(0, self.tr('Recently used algorithms'))
for algname in recent:
2013-08-12 20:44:27 +02:00
alg = Processing.getAlgorithm(algname)
if alg is not None:
algItem = TreeAlgorithmItem(alg)
recentItem.addChild(algItem)
found = True
2013-04-02 22:34:21 +02:00
if found:
self.algorithmTree.insertTopLevelItem(0, recentItem)
recentItem.setExpanded(True)
self.algorithmTree.setWordWrap(True)
2012-12-10 00:12:07 +01:00
def fillTreeUsingCategories(self):
providersToExclude = ['model', 'script']
self.algorithmTree.clear()
text = unicode(self.searchBox.text())
groups = {}
2013-08-12 20:44:27 +02:00
for providerName in Processing.algs.keys():
provider = Processing.algs[providerName]
name = 'ACTIVATE_' + providerName.upper().replace(' ', '_')
2013-08-12 20:44:27 +02:00
if not ProcessingConfig.getSetting(name):
continue
if providerName in providersToExclude \
or len(ModelerUtils.providers[providerName].actions) != 0:
continue
algs = provider.values()
# add algorithms
for alg in algs:
if not alg.showInToolbox:
continue
(altgroup, altsubgroup, altname) = \
AlgorithmDecorator.getGroupsAndName(alg)
2013-07-21 21:53:27 +02:00
if altgroup is None:
continue
if text == '' or text.lower() in altname.lower():
if altgroup not in groups:
groups[altgroup] = {}
group = groups[altgroup]
if altsubgroup not in group:
groups[altgroup][altsubgroup] = []
subgroup = groups[altgroup][altsubgroup]
2012-12-10 00:12:07 +01:00
subgroup.append(alg)
if len(groups) > 0:
mainItem = QTreeWidgetItem()
mainItem.setText(0, 'Geoalgorithms')
mainItem.setIcon(0, GeoAlgorithm.getDefaultIcon())
2013-02-28 22:08:32 +01:00
mainItem.setToolTip(0, mainItem.text(0))
for (groupname, group) in groups.items():
groupItem = QTreeWidgetItem()
groupItem.setText(0, groupname)
groupItem.setIcon(0, GeoAlgorithm.getDefaultIcon())
groupItem.setToolTip(0, groupItem.text(0))
mainItem.addChild(groupItem)
for (subgroupname, subgroup) in group.items():
subgroupItem = QTreeWidgetItem()
subgroupItem.setText(0, subgroupname)
subgroupItem.setIcon(0, GeoAlgorithm.getDefaultIcon())
subgroupItem.setToolTip(0, subgroupItem.text(0))
groupItem.addChild(subgroupItem)
for alg in subgroup:
algItem = TreeAlgorithmItem(alg)
2013-02-28 22:08:32 +01:00
subgroupItem.addChild(algItem)
self.algorithmTree.addTopLevelItem(mainItem)
2012-12-10 00:12:07 +01:00
2013-08-12 20:44:27 +02:00
for providerName in Processing.algs.keys():
if providerName not in providersToExclude:
continue
name = 'ACTIVATE_' + providerName.upper().replace(' ', '_')
2013-08-12 20:44:27 +02:00
if not ProcessingConfig.getSetting(name):
continue
2014-04-06 12:27:51 +02:00
providerItem = TreeProviderItem(providerName)
self.algorithmTree.addTopLevelItem(providerItem)
2014-04-06 12:27:51 +02:00
def fillTreeUsingProviders(self):
2012-09-15 18:25:25 +03:00
self.algorithmTree.clear()
2013-08-12 20:44:27 +02:00
for providerName in Processing.algs.keys():
name = 'ACTIVATE_' + providerName.upper().replace(' ', '_')
2013-08-12 20:44:27 +02:00
if not ProcessingConfig.getSetting(name):
2012-09-15 18:25:25 +03:00
continue
providerItem = TreeProviderItem(providerName)
self.algorithmTree.addTopLevelItem(providerItem)
providerItem.setHidden(providerItem.childCount() == 0)
2014-04-06 12:27:51 +02:00
2012-09-15 18:25:25 +03:00
2012-09-18 21:53:25 +03:00
class TreeAlgorithmItem(QTreeWidgetItem):
2012-09-15 18:25:25 +03:00
def __init__(self, alg):
settings = QSettings()
useCategories = settings.value(ProcessingToolbox.USE_CATEGORIES,
type=bool)
2012-09-15 18:25:25 +03:00
QTreeWidgetItem.__init__(self)
2013-02-28 22:08:32 +01:00
self.alg = alg
2012-12-10 00:12:07 +01:00
icon = alg.getIcon()
name = alg.name
if useCategories:
2012-12-10 00:12:07 +01:00
icon = GeoAlgorithm.getDefaultIcon()
(group, subgroup, name) = AlgorithmDecorator.getGroupsAndName(alg)
self.setIcon(0, icon)
self.setToolTip(0, name)
self.setText(0, name)
2012-09-15 18:25:25 +03:00
2012-09-18 21:53:25 +03:00
class TreeActionItem(QTreeWidgetItem):
2012-09-15 18:25:25 +03:00
def __init__(self, action):
QTreeWidgetItem.__init__(self)
self.action = action
self.setText(0, action.name)
self.setIcon(0, action.getIcon())
2014-04-06 12:27:51 +02:00
class TreeProviderItem(QTreeWidgetItem):
def __init__(self, providerName):
QTreeWidgetItem.__init__(self)
2014-04-06 12:27:51 +02:00
self.providerName = providerName
self.provider = Processing.getProviderFromName(providerName)
self.setIcon(0, self.provider.getIcon())
self.populate()
2014-04-06 12:27:51 +02:00
def refresh(self):
self.takeChildren()
self.populate()
2014-04-06 12:27:51 +02:00
def populate(self):
groups = {}
count = 0
provider = Processing.algs[self.providerName]
algs = provider.values()
# Add algorithms
for alg in algs:
if not alg.showInToolbox:
continue
if alg.group in groups:
groupItem = groups[alg.group]
else:
groupItem = QTreeWidgetItem()
groupItem.setText(0, alg.group)
groupItem.setToolTip(0, alg.group)
groups[alg.group] = groupItem
algItem = TreeAlgorithmItem(alg)
groupItem.addChild(algItem)
count += 1
actions = Processing.actions[self.providerName]
2014-04-06 12:27:51 +02:00
for action in actions:
if action.group in groups:
groupItem = groups[action.group]
else:
groupItem = QTreeWidgetItem()
groupItem.setText(0, action.group)
groups[action.group] = groupItem
algItem = TreeActionItem(action)
2014-04-06 12:27:51 +02:00
groupItem.addChild(algItem)
self.setText(0, self.provider.getDescription()
+ ' [' + str(count) + ' geoalgorithms]')
self.setToolTip(0, self.text(0))
for groupItem in groups.values():
self.addChild(groupItem)
2014-04-06 12:27:51 +02:00