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

528 lines
21 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. *
* *
***************************************************************************
"""
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$'
2017-11-24 16:03:19 +01:00
import operator
2015-05-18 21:04:20 +03:00
import os
2016-04-29 11:39:26 +02:00
from qgis.PyQt import uic
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtCore import Qt, QCoreApplication
from qgis.PyQt.QtWidgets import QMenu, QAction, QTreeWidgetItem, QLabel, QMessageBox
from qgis.utils import iface
from qgis.core import (QgsApplication,
QgsProcessingAlgorithm)
2015-05-18 21:04:20 +03:00
from processing.gui.Postprocessing import handleAlgorithmResults
from processing.core.Processing import Processing
2013-08-12 20:44:27 +02:00
from processing.core.ProcessingLog import ProcessingLog
from processing.core.ProcessingConfig import ProcessingConfig, settingsWatcher
from processing.gui.MessageDialog import MessageDialog
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
from processing.gui.ConfigDialog import ConfigDialog
from processing.gui.MessageBarProgress import MessageBarProgress
from processing.gui.AlgorithmExecutor import execute
from processing.gui.ProviderActions import (ProviderActions,
ProviderContextMenuActions)
from processing.tools import dataobjects
2015-05-18 21:04:20 +03:00
pluginPath = os.path.split(os.path.dirname(__file__))[0]
WIDGET, BASE = uic.loadUiType(
os.path.join(pluginPath, 'ui', 'ProcessingToolbox.ui'))
2012-09-15 18:25:25 +03:00
2015-05-18 21:04:20 +03:00
class ProcessingToolbox(BASE, WIDGET):
ALG_ITEM = 'ALG_ITEM'
PROVIDER_ITEM = 'PROVIDER_ITEM'
GROUP_ITEM = 'GROUP_ITEM'
NAME_ROLE = Qt.UserRole
TAG_ROLE = Qt.UserRole + 1
TYPE_ROLE = Qt.UserRole + 2
def __init__(self):
2015-05-18 21:04:20 +03:00
super(ProcessingToolbox, self).__init__(None)
self.tipWasClosed = False
self.setupUi(self)
self.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
self.searchBox.textChanged.connect(self.textChanged)
self.algorithmTree.customContextMenuRequested.connect(
self.showPopupMenu)
self.algorithmTree.doubleClicked.connect(self.executeAlgorithm)
self.txtDisabled.setVisible(False)
self.txtTip.setVisible(self.disabledProviders())
self.txtDisabled.linkActivated.connect(self.showDisabled)
2016-01-12 08:24:13 +11:00
def openSettings(url):
if url == "close":
self.txtTip.setVisible(False)
self.tipWasClosed = True
else:
iface.showOptionsDialog(iface.mainWindow(), 'processingOptions')
self.txtTip.setVisible(self.disabledProviders())
self.txtTip.linkActivated.connect(openSettings)
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
# connect to existing providers
for p in QgsApplication.processingRegistry().providers():
p.algorithmsLoaded.connect(self.updateProvider)
QgsApplication.processingRegistry().providerRemoved.connect(self.removeProvider)
QgsApplication.processingRegistry().providerAdded.connect(self.addProvider)
settingsWatcher.settingsChanged.connect(self.fillTree)
def showDisabled(self):
2016-01-08 12:52:19 +01:00
self.txtDisabled.setVisible(False)
for provider_id in self.disabledWithMatchingAlgs:
self.disabledProviderItems[provider_id].setHidden(False)
2016-01-08 12:52:19 +01:00
self.algorithmTree.expandAll()
def disabledProviders(self):
showTip = ProcessingConfig.getSetting(ProcessingConfig.SHOW_PROVIDERS_TOOLTIP)
if not showTip or self.tipWasClosed:
return False
for provider in QgsApplication.processingRegistry().providers():
if not provider.isActive():
return True
return False
def textChanged(self):
text = self.searchBox.text().strip(' ').lower()
2016-09-21 18:24:26 +02:00
for item in list(self.disabledProviderItems.values()):
item.setHidden(True)
self._filterItem(self.algorithmTree.invisibleRootItem(), [t for t in text.split(' ') if t])
if text:
self.algorithmTree.expandAll()
self.disabledWithMatchingAlgs = []
for provider in QgsApplication.processingRegistry().providers():
if not provider.isActive():
for alg in provider.algorithms():
if text in alg.name():
self.disabledWithMatchingAlgs.append(provider.id())
break
showTip = ProcessingConfig.getSetting(ProcessingConfig.SHOW_PROVIDERS_TOOLTIP)
if showTip:
self.txtDisabled.setVisible(bool(self.disabledWithMatchingAlgs))
else:
self.algorithmTree.collapseAll()
self.algorithmTree.invisibleRootItem().child(0).setExpanded(True)
self.txtDisabled.setVisible(False)
2014-04-06 12:27:51 +02:00
def _filterItem(self, item, text):
if (item.childCount() > 0):
show = False
2016-09-21 18:24:26 +02:00
for i in range(item.childCount()):
child = item.child(i)
showChild = self._filterItem(child, text)
2016-09-21 18:24:26 +02:00
show = (showChild or show) and item not in list(self.disabledProviderItems.values())
2014-04-06 12:27:51 +02:00
item.setHidden(not show)
return show
2014-04-06 12:27:51 +02:00
elif isinstance(item, (TreeAlgorithmItem, TreeActionItem)):
# hide if every part of text is not contained somewhere in either the item text or item user role
item_text = [item.text(0).lower(), item.data(0, ProcessingToolbox.NAME_ROLE).lower()]
if isinstance(item, TreeAlgorithmItem):
item_text.append(item.alg.id())
item_text.extend(item.data(0, ProcessingToolbox.TAG_ROLE))
hide = bool(text) and not all(
any(part in t for t in item_text)
for part in text)
2014-04-06 12:27:51 +02:00
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 activateProvider(self, id):
provider = QgsApplication.processingRegistry().providerById(id)
if not provider.canBeActivated():
QMessageBox.warning(self, self.tr("Activate provider"),
self.tr("The provider has been activated, but it might need additional configuration."))
return
try:
# not part of the base class - only some providers have a setActive member
provider.setActive(True)
self.fillTree()
self.textChanged()
self.showDisabled()
except:
QMessageBox.warning(self, self.tr("Activate provider"),
self.tr("The provider could not be activated."))
2012-09-15 18:25:25 +03:00
def updateProvider(self):
provider = self.sender()
item = self._providerItem(provider.id())
if item is not None:
item.refresh()
item.sortChildren(0, Qt.AscendingOrder)
2016-09-21 18:24:26 +02:00
for i in range(item.childCount()):
item.child(i).sortChildren(0, Qt.AscendingOrder)
self.addRecentAlgorithms(True)
def removeProvider(self, provider_id):
item = self._providerItem(provider_id)
if item is not None:
self.algorithmTree.invisibleRootItem().removeChild(item)
def _providerItem(self, provider_id):
2016-09-21 18:24:26 +02:00
for i in range(self.algorithmTree.invisibleRootItem().childCount()):
child = self.algorithmTree.invisibleRootItem().child(i)
if isinstance(child, TreeProviderItem):
if child.provider.id() == provider_id:
return child
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)
popupmenu = QMenu()
2012-09-15 18:25:25 +03:00
if isinstance(item, TreeAlgorithmItem):
alg = item.alg
executeAction = QAction(self.tr('Execute'), self.algorithmTree)
2012-09-15 18:25:25 +03:00
executeAction.triggered.connect(self.executeAlgorithm)
popupmenu.addAction(executeAction)
if alg.flags() & QgsProcessingAlgorithm.FlagSupportsBatch:
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)
if isinstance(item, (TreeAlgorithmItem, TreeActionItem)):
data = item.alg if isinstance(item, TreeAlgorithmItem) else item.action
actions = ProviderContextMenuActions.actions
if len(actions) > 0:
popupmenu.addSeparator()
2012-09-15 18:25:25 +03:00
for action in actions:
action.setData(data, 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):
alg = QgsApplication.processingRegistry().createAlgorithmById(item.alg.id())
2012-09-15 18:25:25 +03:00
dlg = EditRenderingStylesDialog(alg)
dlg.exec_()
def executeAlgorithmAsBatchProcess(self):
item = self.algorithmTree.currentItem()
if isinstance(item, TreeAlgorithmItem):
alg = QgsApplication.processingRegistry().createAlgorithmById(item.alg.id())
if alg:
dlg = BatchAlgorithmDialog(alg)
dlg.show()
dlg.exec_()
# have to manually delete the dialog - otherwise it's owned by the
# iface mainWindow and never deleted
dlg.deleteLater()
2012-09-15 18:25:25 +03:00
def executeAlgorithm(self):
item = self.algorithmTree.currentItem()
if isinstance(item, TreeAlgorithmItem):
alg = QgsApplication.processingRegistry().createAlgorithmById(item.alg.id())
if not alg:
return
ok, message = alg.canExecute()
if not ok:
dlg = MessageDialog()
dlg.setTitle(self.tr('Error executing algorithm'))
dlg.setMessage(
self.tr('<h3>This algorithm cannot '
2017-03-04 16:23:36 +01:00
'be run :-( </h3>\n{0}').format(message))
dlg.exec_()
2012-09-15 18:25:25 +03:00
return
2017-05-01 17:00:10 +10:00
if alg.countVisibleParameters() > 0:
dlg = alg.createCustomParametersWidget(self)
if not dlg:
dlg = AlgorithmDialog(alg)
canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec_()
if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)
if dlg.executed:
showRecent = ProcessingConfig.getSetting(
ProcessingConfig.SHOW_RECENT_ALGORITHMS)
if showRecent:
self.addRecentAlgorithms(True)
# have to manually delete the dialog - otherwise it's owned by the
# iface mainWindow and never deleted
dlg.deleteLater()
else:
feedback = MessageBarProgress()
context = dataobjects.createContext(feedback)
parameters = {}
ret, results = execute(alg, parameters, context, feedback)
handleAlgorithmResults(alg, context, feedback)
feedback.close()
2012-09-15 18:25:25 +03:00
if isinstance(item, TreeActionItem):
action = item.action
action.setData(self)
action.execute()
def fillTree(self):
self.fillTreeUsingProviders()
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)
if recentItem.text(0) == self.tr('Recently used'):
treeWidget = recentItem.treeWidget()
treeWidget.takeTopLevelItem(
2017-03-03 08:07:48 +10:00
treeWidget.indexOfTopLevelItem(recentItem))
2013-04-02 22:34:21 +02:00
recentItem = QTreeWidgetItem()
recentItem.setText(0, self.tr('Recently used'))
for algname in recent:
alg = QgsApplication.processingRegistry().createAlgorithmById(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 addProvider(self, provider_id):
provider = QgsApplication.processingRegistry().providerById(provider_id)
providerItem = TreeProviderItem(provider, self.algorithmTree, self)
if not provider.isActive():
providerItem.setHidden(True)
self.disabledProviderItems[provider.id()] = providerItem
2016-09-21 18:24:26 +02:00
for i in range(self.algorithmTree.invisibleRootItem().childCount()):
child = self.algorithmTree.invisibleRootItem().child(i)
if isinstance(child, TreeProviderItem):
if child.text(0) > providerItem.text(0):
break
self.algorithmTree.insertTopLevelItem(i, providerItem)
provider.algorithmsLoaded.connect(self.updateProvider)
def fillTreeUsingProviders(self):
2012-09-15 18:25:25 +03:00
self.algorithmTree.clear()
self.disabledProviderItems = {}
# TODO - replace with proper model for toolbox!
# first add qgis/native providers, since they create top level groups
for provider in QgsApplication.processingRegistry().providers():
if provider.id() in ('qgis', 'native', '3d'):
self.addAlgorithmsFromProvider(provider, self.algorithmTree.invisibleRootItem())
else:
continue
self.algorithmTree.sortItems(0, Qt.AscendingOrder)
for provider in QgsApplication.processingRegistry().providers():
if provider.id() in ('qgis', 'native', '3d'):
# already added
continue
else:
providerItem = TreeProviderItem(provider, self.algorithmTree, self)
if not provider.isActive():
providerItem.setHidden(True)
self.disabledProviderItems[provider.id()] = providerItem
# insert non-native providers at end of tree, alphabetically
for i in range(self.algorithmTree.invisibleRootItem().childCount()):
child = self.algorithmTree.invisibleRootItem().child(i)
if isinstance(child, TreeProviderItem):
if child.text(0) > providerItem.text(0):
break
self.algorithmTree.insertTopLevelItem(i + 1, providerItem)
def addAlgorithmsFromProvider(self, provider, parent):
groups = {}
count = 0
algs = provider.algorithms()
active = provider.isActive()
# Add algorithms
for alg in algs:
if alg.flags() & QgsProcessingAlgorithm.FlagHideFromToolbox:
continue
groupItem = None
if alg.group() in groups:
groupItem = groups[alg.group()]
else:
# check if group already exists
for i in range(parent.childCount()):
if parent.child(i).text(0) == alg.group():
groupItem = parent.child(i)
groups[alg.group()] = groupItem
break
if not groupItem:
groupItem = TreeGroupItem(alg.group())
if not active:
groupItem.setInactive()
if provider.id() in ('qgis', 'native', '3d'):
groupItem.setIcon(0, provider.icon())
groups[alg.group()] = groupItem
algItem = TreeAlgorithmItem(alg)
if not active:
algItem.setForeground(0, Qt.darkGray)
groupItem.addChild(algItem)
count += 1
if provider.id() in ProviderActions.actions:
actions = ProviderActions.actions[provider.id()]
for action in actions:
if action.group in groups:
groupItem = groups[action.group]
else:
groupItem = TreeGroupItem(action.group)
groups[action.group] = groupItem
algItem = TreeActionItem(action)
groupItem.addChild(algItem)
text = provider.name()
if not provider.id() in ('qgis', 'native', '3d'):
if not active:
def activateProvider():
self.activateProvider(provider.id())
label = QLabel(text + "&nbsp;&nbsp;&nbsp;&nbsp;<a href='%s'>Activate</a>")
label.setStyleSheet("QLabel {background-color: white; color: grey;}")
label.linkActivated.connect(activateProvider)
self.algorithmTree.setItemWidget(parent, 0, label)
else:
parent.setText(0, text)
for group, groupItem in sorted(groups.items(), key=operator.itemgetter(1)):
parent.addChild(groupItem)
if not provider.id() in ('qgis', 'native', '3d'):
parent.setHidden(parent.childCount() == 0)
2016-01-08 10:32:43 +02:00
2012-09-18 21:53:25 +03:00
class TreeAlgorithmItem(QTreeWidgetItem):
2012-09-15 18:25:25 +03:00
def __init__(self, alg):
QTreeWidgetItem.__init__(self)
2013-02-28 22:08:32 +01:00
self.alg = alg
icon = alg.icon()
nameEn = alg.name()
name = alg.displayName()
name = name if name != '' else nameEn
self.setIcon(0, icon)
self.setToolTip(0, self.formatAlgorithmTooltip(alg))
self.setText(0, name)
self.setData(0, ProcessingToolbox.NAME_ROLE, nameEn)
self.setData(0, ProcessingToolbox.TAG_ROLE, alg.tags())
self.setData(0, ProcessingToolbox.TYPE_ROLE, ProcessingToolbox.ALG_ITEM)
def formatAlgorithmTooltip(self, alg):
return '<p><b>{}</b></p><p>{}</p>'.format(
alg.displayName(),
QCoreApplication.translate('Toolbox','Algorithm ID: {}').format('<i>{}</i>'.format(alg.id()))
)
class TreeGroupItem(QTreeWidgetItem):
def __init__(self, name):
QTreeWidgetItem.__init__(self)
self.setToolTip(0, name)
self.setText(0, name)
self.setData(0, ProcessingToolbox.NAME_ROLE, name)
self.setData(0, ProcessingToolbox.TYPE_ROLE, ProcessingToolbox.GROUP_ITEM)
def setInactive(self):
self.setForeground(0, Qt.darkGray)
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.i18n_name)
2012-09-15 18:25:25 +03:00
self.setIcon(0, action.getIcon())
self.setData(0, ProcessingToolbox.NAME_ROLE, action.name)
2014-04-06 12:27:51 +02:00
class TreeProviderItem(QTreeWidgetItem):
def __init__(self, provider, tree, toolbox):
QTreeWidgetItem.__init__(self, None)
self.tree = tree
self.toolbox = toolbox
self.provider = provider
self.setIcon(0, self.provider.icon())
self.setData(0, ProcessingToolbox.TYPE_ROLE, ProcessingToolbox.PROVIDER_ITEM)
self.setToolTip(0, self.provider.longName())
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):
self.toolbox.addAlgorithmsFromProvider(self.provider, self)