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. *
|
|
|
|
|
* *
|
|
|
|
|
***************************************************************************
|
|
|
|
|
"""
|
2016-04-06 10:50:54 +02:00
|
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
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
|
2014-05-20 16:30:59 +02:00
|
|
|
|
from qgis.utils import iface
|
2017-03-29 15:00:20 +10:00
|
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
|
QgsProcessingAlgorithm)
|
2015-05-18 21:04:20 +03:00
|
|
|
|
|
2016-04-06 10:50:54 +02:00
|
|
|
|
from processing.gui.Postprocessing import handleAlgorithmResults
|
2016-05-19 12:25:04 +02:00
|
|
|
|
from processing.core.Processing import Processing
|
2013-08-12 20:44:27 +02:00
|
|
|
|
from processing.core.ProcessingLog import ProcessingLog
|
2016-04-27 13:36:28 +02:00
|
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig, settingsWatcher
|
2014-10-04 14:04:39 +03:00
|
|
|
|
from processing.gui.MessageDialog import MessageDialog
|
2014-11-12 11:42:29 +02:00
|
|
|
|
from processing.gui.AlgorithmDialog import AlgorithmDialog
|
2014-11-12 19:36:12 +02:00
|
|
|
|
from processing.gui.BatchAlgorithmDialog import BatchAlgorithmDialog
|
2013-08-12 20:44:27 +02:00
|
|
|
|
from processing.gui.EditRenderingStylesDialog import EditRenderingStylesDialog
|
2016-01-08 08:27:22 +01:00
|
|
|
|
from processing.gui.ConfigDialog import ConfigDialog
|
2016-04-06 10:50:54 +02:00
|
|
|
|
from processing.gui.MessageBarProgress import MessageBarProgress
|
2017-03-22 17:17:14 +02:00
|
|
|
|
from processing.gui.AlgorithmExecutor import execute
|
2017-04-04 12:52:21 +10:00
|
|
|
|
from processing.gui.ProviderActions import (ProviderActions,
|
|
|
|
|
ProviderContextMenuActions)
|
2017-04-26 14:33:53 +10:00
|
|
|
|
from processing.tools import dataobjects
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
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-10-30 15:00:07 +02:00
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
2015-05-18 21:04:20 +03:00
|
|
|
|
class ProcessingToolbox(BASE, WIDGET):
|
2013-04-24 13:34:34 +04:00
|
|
|
|
|
2017-10-15 19:33:49 +10:00
|
|
|
|
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
|
|
|
|
|
|
2013-09-12 13:19:00 +02:00
|
|
|
|
def __init__(self):
|
2015-05-18 21:04:20 +03:00
|
|
|
|
super(ProcessingToolbox, self).__init__(None)
|
2016-01-29 14:07:47 +01:00
|
|
|
|
self.tipWasClosed = False
|
2012-10-30 15:00:07 +02:00
|
|
|
|
self.setupUi(self)
|
|
|
|
|
self.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
|
|
|
|
|
|
2014-03-26 10:18:27 +01:00
|
|
|
|
self.searchBox.textChanged.connect(self.textChanged)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
self.algorithmTree.customContextMenuRequested.connect(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
|
self.showPopupMenu)
|
2012-10-30 15:00:07 +02:00
|
|
|
|
self.algorithmTree.doubleClicked.connect(self.executeAlgorithm)
|
2016-01-08 08:27:22 +01:00
|
|
|
|
self.txtDisabled.setVisible(False)
|
|
|
|
|
self.txtTip.setVisible(self.disabledProviders())
|
2016-01-11 09:38:41 +01:00
|
|
|
|
self.txtDisabled.linkActivated.connect(self.showDisabled)
|
2016-01-12 08:24:13 +11:00
|
|
|
|
|
2016-01-29 14:07:47 +01:00
|
|
|
|
def openSettings(url):
|
|
|
|
|
if url == "close":
|
|
|
|
|
self.txtTip.setVisible(False)
|
|
|
|
|
self.tipWasClosed = True
|
|
|
|
|
else:
|
2017-03-24 11:16:49 +10:00
|
|
|
|
iface.showOptionsDialog(iface.mainWindow(), 'processingOptions')
|
2016-01-29 14:07:47 +01:00
|
|
|
|
self.txtTip.setVisible(self.disabledProviders())
|
2016-01-11 09:38:41 +01:00
|
|
|
|
self.txtTip.linkActivated.connect(openSettings)
|
2012-12-01 20:22:21 +02:00
|
|
|
|
if hasattr(self.searchBox, 'setPlaceholderText'):
|
2013-10-01 20:52:22 +03:00
|
|
|
|
self.searchBox.setPlaceholderText(self.tr('Search...'))
|
2012-12-01 20:22:21 +02:00
|
|
|
|
|
2012-10-30 15:00:07 +02:00
|
|
|
|
self.fillTree()
|
2014-04-06 12:27:51 +02:00
|
|
|
|
|
2017-05-01 17:12:33 +10:00
|
|
|
|
# connect to existing providers
|
|
|
|
|
for p in QgsApplication.processingRegistry().providers():
|
|
|
|
|
p.algorithmsLoaded.connect(self.updateProvider)
|
|
|
|
|
|
2017-01-09 09:59:35 +10:00
|
|
|
|
QgsApplication.processingRegistry().providerRemoved.connect(self.removeProvider)
|
|
|
|
|
QgsApplication.processingRegistry().providerAdded.connect(self.addProvider)
|
2016-04-27 13:36:28 +02:00
|
|
|
|
settingsWatcher.settingsChanged.connect(self.fillTree)
|
2016-04-27 10:09:28 +02:00
|
|
|
|
|
2016-01-08 08:27:22 +01:00
|
|
|
|
def showDisabled(self):
|
2016-01-08 12:52:19 +01:00
|
|
|
|
self.txtDisabled.setVisible(False)
|
2017-01-09 09:18:25 +10:00
|
|
|
|
for provider_id in self.disabledWithMatchingAlgs:
|
|
|
|
|
self.disabledProviderItems[provider_id].setHidden(False)
|
2016-01-08 12:52:19 +01:00
|
|
|
|
self.algorithmTree.expandAll()
|
2016-01-08 08:27:22 +01:00
|
|
|
|
|
|
|
|
|
def disabledProviders(self):
|
2016-01-29 14:07:47 +01:00
|
|
|
|
showTip = ProcessingConfig.getSetting(ProcessingConfig.SHOW_PROVIDERS_TOOLTIP)
|
|
|
|
|
if not showTip or self.tipWasClosed:
|
|
|
|
|
return False
|
|
|
|
|
|
2017-04-04 11:04:36 +10:00
|
|
|
|
for provider in QgsApplication.processingRegistry().providers():
|
2017-04-04 12:22:29 +10:00
|
|
|
|
if not provider.isActive():
|
2016-01-08 08:27:22 +01:00
|
|
|
|
return True
|
2017-04-04 12:22:29 +10:00
|
|
|
|
|
2016-01-08 08:27:22 +01:00
|
|
|
|
return False
|
|
|
|
|
|
2014-03-26 10:18:27 +01:00
|
|
|
|
def textChanged(self):
|
2014-03-26 14:56:17 +01:00
|
|
|
|
text = self.searchBox.text().strip(' ').lower()
|
2016-09-21 18:24:26 +02:00
|
|
|
|
for item in list(self.disabledProviderItems.values()):
|
2016-01-08 08:27:22 +01:00
|
|
|
|
item.setHidden(True)
|
2016-11-08 09:30:52 +10:00
|
|
|
|
self._filterItem(self.algorithmTree.invisibleRootItem(), [t for t in text.split(' ') if t])
|
2014-03-26 10:18:27 +01:00
|
|
|
|
if text:
|
|
|
|
|
self.algorithmTree.expandAll()
|
2016-01-08 08:27:22 +01:00
|
|
|
|
self.disabledWithMatchingAlgs = []
|
2017-04-04 11:04:36 +10:00
|
|
|
|
for provider in QgsApplication.processingRegistry().providers():
|
2017-04-04 12:22:29 +10:00
|
|
|
|
if not provider.isActive():
|
2017-04-04 11:04:36 +10:00
|
|
|
|
for alg in provider.algorithms():
|
2017-03-29 12:51:59 +10:00
|
|
|
|
if text in alg.name():
|
2017-04-04 11:04:36 +10:00
|
|
|
|
self.disabledWithMatchingAlgs.append(provider.id())
|
2016-01-08 08:27:22 +01:00
|
|
|
|
break
|
2016-03-09 14:20:48 +02:00
|
|
|
|
showTip = ProcessingConfig.getSetting(ProcessingConfig.SHOW_PROVIDERS_TOOLTIP)
|
|
|
|
|
if showTip:
|
|
|
|
|
self.txtDisabled.setVisible(bool(self.disabledWithMatchingAlgs))
|
2014-03-26 10:18:27 +01:00
|
|
|
|
else:
|
|
|
|
|
self.algorithmTree.collapseAll()
|
|
|
|
|
self.algorithmTree.invisibleRootItem().child(0).setExpanded(True)
|
2016-01-08 08:27:22 +01:00
|
|
|
|
self.txtDisabled.setVisible(False)
|
2014-04-06 12:27:51 +02:00
|
|
|
|
|
|
|
|
|
def _filterItem(self, item, text):
|
2014-03-26 10:18:27 +01:00
|
|
|
|
if (item.childCount() > 0):
|
|
|
|
|
show = False
|
2016-09-21 18:24:26 +02:00
|
|
|
|
for i in range(item.childCount()):
|
2014-03-26 10:18:27 +01:00
|
|
|
|
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)
|
2014-03-26 10:18:27 +01:00
|
|
|
|
return show
|
2014-04-06 12:27:51 +02:00
|
|
|
|
elif isinstance(item, (TreeAlgorithmItem, TreeActionItem)):
|
2016-11-08 09:30:52 +10:00
|
|
|
|
# hide if every part of text is not contained somewhere in either the item text or item user role
|
2017-10-15 19:33:49 +10:00
|
|
|
|
item_text = [item.text(0).lower(), item.data(0, ProcessingToolbox.NAME_ROLE).lower()]
|
2016-01-08 08:27:22 +01:00
|
|
|
|
if isinstance(item, TreeAlgorithmItem):
|
2017-04-03 20:35:03 +10:00
|
|
|
|
item_text.append(item.alg.id())
|
2017-10-15 19:33:49 +10:00
|
|
|
|
item_text.extend(item.data(0, ProcessingToolbox.TAG_ROLE))
|
2016-11-08 09:30:52 +10:00
|
|
|
|
|
|
|
|
|
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)
|
2014-03-26 10:18:27 +01:00
|
|
|
|
return not hide
|
|
|
|
|
else:
|
2014-04-06 12:27:51 +02:00
|
|
|
|
item.setHidden(True)
|
2014-03-26 10:18:27 +01:00
|
|
|
|
return False
|
2014-04-06 12:27:51 +02:00
|
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
|
def activateProvider(self, id):
|
2017-01-09 09:59:35 +10:00
|
|
|
|
provider = QgsApplication.processingRegistry().providerById(id)
|
2016-01-08 08:27:22 +01:00
|
|
|
|
if not provider.canBeActivated():
|
2017-04-04 12:22:29 +10:00
|
|
|
|
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
|
|
|
|
|
2017-04-04 11:28:28 +10:00
|
|
|
|
def updateProvider(self):
|
|
|
|
|
provider = self.sender()
|
|
|
|
|
item = self._providerItem(provider.id())
|
2016-04-27 10:09:28 +02:00
|
|
|
|
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()):
|
2016-04-27 10:09:28 +02:00
|
|
|
|
item.child(i).sortChildren(0, Qt.AscendingOrder)
|
|
|
|
|
self.addRecentAlgorithms(True)
|
|
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
|
def removeProvider(self, provider_id):
|
|
|
|
|
item = self._providerItem(provider_id)
|
2016-04-27 10:09:28 +02:00
|
|
|
|
if item is not None:
|
|
|
|
|
self.algorithmTree.invisibleRootItem().removeChild(item)
|
|
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
|
def _providerItem(self, provider_id):
|
2016-09-21 18:24:26 +02:00
|
|
|
|
for i in range(self.algorithmTree.invisibleRootItem().childCount()):
|
2014-03-26 10:18:27 +01:00
|
|
|
|
child = self.algorithmTree.invisibleRootItem().child(i)
|
|
|
|
|
if isinstance(child, TreeProviderItem):
|
2017-04-04 11:04:36 +10:00
|
|
|
|
if child.provider.id() == provider_id:
|
2016-04-27 10:09:28 +02:00
|
|
|
|
return child
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
def showPopupMenu(self, point):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
item = self.algorithmTree.itemAt(point)
|
2016-08-01 00:09:23 +02:00
|
|
|
|
popupmenu = QMenu()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
if isinstance(item, TreeAlgorithmItem):
|
|
|
|
|
alg = item.alg
|
2013-10-01 20:52:22 +03:00
|
|
|
|
executeAction = QAction(self.tr('Execute'), self.algorithmTree)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
executeAction.triggered.connect(self.executeAlgorithm)
|
|
|
|
|
popupmenu.addAction(executeAction)
|
2017-03-29 15:00:20 +10:00
|
|
|
|
if alg.flags() & QgsProcessingAlgorithm.FlagSupportsBatch:
|
2013-10-01 20:52:22 +03:00
|
|
|
|
executeBatchAction = QAction(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
|
self.tr('Execute as batch process'),
|
|
|
|
|
self.algorithmTree)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
executeBatchAction.triggered.connect(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
|
self.executeAlgorithmAsBatchProcess)
|
2013-02-21 22:12:01 +01:00
|
|
|
|
popupmenu.addAction(executeBatchAction)
|
2013-04-24 13:34:34 +04:00
|
|
|
|
popupmenu.addSeparator()
|
2013-10-01 20:52:22 +03:00
|
|
|
|
editRenderingStylesAction = QAction(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
|
self.tr('Edit rendering styles for outputs'),
|
|
|
|
|
self.algorithmTree)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
editRenderingStylesAction.triggered.connect(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
|
self.editRenderingStyles)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
popupmenu.addAction(editRenderingStylesAction)
|
2016-04-06 10:50:54 +02:00
|
|
|
|
|
|
|
|
|
if isinstance(item, (TreeAlgorithmItem, TreeActionItem)):
|
|
|
|
|
data = item.alg if isinstance(item, TreeAlgorithmItem) else item.action
|
2017-04-04 12:52:21 +10:00
|
|
|
|
actions = ProviderContextMenuActions.actions
|
2013-04-24 13:34:34 +04:00
|
|
|
|
if len(actions) > 0:
|
|
|
|
|
popupmenu.addSeparator()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
for action in actions:
|
2016-04-06 10:50:54 +02:00
|
|
|
|
action.setData(data, self)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
if action.isEnabled():
|
2013-10-01 20:52:22 +03:00
|
|
|
|
contextMenuAction = QAction(action.name,
|
2015-08-22 14:29:41 +02:00
|
|
|
|
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):
|
2017-07-10 16:31:14 +10:00
|
|
|
|
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):
|
2017-07-10 16:31:14 +10:00
|
|
|
|
alg = QgsApplication.processingRegistry().createAlgorithmById(item.alg.id())
|
|
|
|
|
if alg:
|
|
|
|
|
dlg = BatchAlgorithmDialog(alg)
|
|
|
|
|
dlg.show()
|
|
|
|
|
dlg.exec_()
|
2017-07-12 18:52:53 +10:00
|
|
|
|
# have to manually delete the dialog - otherwise it's owned by the
|
|
|
|
|
# iface mainWindow and never deleted
|
2017-08-05 08:54:28 +02:00
|
|
|
|
dlg.deleteLater()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
|
|
def executeAlgorithm(self):
|
|
|
|
|
item = self.algorithmTree.currentItem()
|
|
|
|
|
if isinstance(item, TreeAlgorithmItem):
|
2017-07-10 16:31:14 +10:00
|
|
|
|
alg = QgsApplication.processingRegistry().createAlgorithmById(item.alg.id())
|
|
|
|
|
if not alg:
|
|
|
|
|
return
|
|
|
|
|
|
2017-05-15 12:08:26 +10:00
|
|
|
|
ok, message = alg.canExecute()
|
|
|
|
|
if not ok:
|
2014-10-04 14:04:39 +03:00
|
|
|
|
dlg = MessageDialog()
|
2016-01-19 12:32:25 +01:00
|
|
|
|
dlg.setTitle(self.tr('Error executing algorithm'))
|
2014-10-04 14:04:39 +03:00
|
|
|
|
dlg.setMessage(
|
2016-01-19 12:32:25 +01:00
|
|
|
|
self.tr('<h3>This algorithm cannot '
|
2017-03-04 16:23:36 +01:00
|
|
|
|
'be run :-( </h3>\n{0}').format(message))
|
2013-04-12 17:18:51 +02:00
|
|
|
|
dlg.exec_()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
return
|
2017-05-01 17:00:10 +10:00
|
|
|
|
|
2017-05-15 12:29:44 +10:00
|
|
|
|
if alg.countVisibleParameters() > 0:
|
2017-05-15 12:46:59 +10:00
|
|
|
|
dlg = alg.createCustomParametersWidget(self)
|
2017-06-11 17:53:53 +10:00
|
|
|
|
|
2016-04-06 10:50:54 +02:00
|
|
|
|
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)
|
2017-07-12 18:52:53 +10:00
|
|
|
|
# have to manually delete the dialog - otherwise it's owned by the
|
|
|
|
|
# iface mainWindow and never deleted
|
2017-08-05 08:54:28 +02:00
|
|
|
|
dlg.deleteLater()
|
2016-04-06 10:50:54 +02:00
|
|
|
|
else:
|
2017-01-06 20:04:00 +10:00
|
|
|
|
feedback = MessageBarProgress()
|
2017-06-23 10:32:51 +10:00
|
|
|
|
context = dataobjects.createContext(feedback)
|
2017-05-15 16:19:46 +10:00
|
|
|
|
parameters = {}
|
2017-05-22 15:46:50 +10:00
|
|
|
|
ret, results = execute(alg, parameters, context, feedback)
|
2017-08-15 03:56:32 +10:00
|
|
|
|
handleAlgorithmResults(alg, context, feedback)
|
2017-01-06 20:04:00 +10:00
|
|
|
|
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):
|
2016-01-08 08:27:22 +01:00
|
|
|
|
self.fillTreeUsingProviders()
|
2013-03-15 09:28:56 +01:00
|
|
|
|
self.addRecentAlgorithms(False)
|
2013-03-26 14:15:12 +01:00
|
|
|
|
|
2013-03-15 09:28:56 +01:00
|
|
|
|
def addRecentAlgorithms(self, updating):
|
2013-10-01 20:52:22 +03:00
|
|
|
|
showRecent = ProcessingConfig.getSetting(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
|
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()
|
2012-12-07 21:11:56 +01:00
|
|
|
|
if len(recent) != 0:
|
|
|
|
|
found = False
|
2013-04-01 00:29:07 +02:00
|
|
|
|
if updating:
|
|
|
|
|
recentItem = self.algorithmTree.topLevelItem(0)
|
2017-08-23 17:35:06 +10:00
|
|
|
|
if recentItem.text(0) == self.tr('Recently used'):
|
2017-03-02 12:45:59 +01:00
|
|
|
|
treeWidget = recentItem.treeWidget()
|
|
|
|
|
treeWidget.takeTopLevelItem(
|
2017-03-03 08:07:48 +10:00
|
|
|
|
treeWidget.indexOfTopLevelItem(recentItem))
|
2013-04-02 22:34:21 +02:00
|
|
|
|
|
2012-12-07 21:11:56 +01:00
|
|
|
|
recentItem = QTreeWidgetItem()
|
2017-08-23 17:35:06 +10:00
|
|
|
|
recentItem.setText(0, self.tr('Recently used'))
|
2012-12-07 21:11:56 +01:00
|
|
|
|
for algname in recent:
|
2017-07-10 16:31:14 +10:00
|
|
|
|
alg = QgsApplication.processingRegistry().createAlgorithmById(algname)
|
2012-12-07 21:11:56 +01:00
|
|
|
|
if alg is not None:
|
|
|
|
|
algItem = TreeAlgorithmItem(alg)
|
|
|
|
|
recentItem.addChild(algItem)
|
|
|
|
|
found = True
|
2013-04-02 22:34:21 +02:00
|
|
|
|
if found:
|
2012-12-07 21:11:56 +01:00
|
|
|
|
self.algorithmTree.insertTopLevelItem(0, recentItem)
|
|
|
|
|
recentItem.setExpanded(True)
|
|
|
|
|
|
|
|
|
|
self.algorithmTree.setWordWrap(True)
|
2012-12-10 00:12:07 +01:00
|
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
|
def addProvider(self, provider_id):
|
2017-04-04 11:04:36 +10:00
|
|
|
|
provider = QgsApplication.processingRegistry().providerById(provider_id)
|
|
|
|
|
providerItem = TreeProviderItem(provider, self.algorithmTree, self)
|
2017-04-04 12:22:29 +10:00
|
|
|
|
if not provider.isActive():
|
2016-04-27 10:09:28 +02:00
|
|
|
|
providerItem.setHidden(True)
|
2017-04-04 11:04:36 +10:00
|
|
|
|
self.disabledProviderItems[provider.id()] = providerItem
|
2016-04-27 10:09:28 +02:00
|
|
|
|
|
2016-09-21 18:24:26 +02:00
|
|
|
|
for i in range(self.algorithmTree.invisibleRootItem().childCount()):
|
2016-04-27 10:09:28 +02:00
|
|
|
|
child = self.algorithmTree.invisibleRootItem().child(i)
|
|
|
|
|
if isinstance(child, TreeProviderItem):
|
|
|
|
|
if child.text(0) > providerItem.text(0):
|
|
|
|
|
break
|
|
|
|
|
self.algorithmTree.insertTopLevelItem(i, providerItem)
|
2017-04-04 11:28:28 +10:00
|
|
|
|
provider.algorithmsLoaded.connect(self.updateProvider)
|
2016-04-27 10:09:28 +02:00
|
|
|
|
|
2013-04-24 13:34:34 +04:00
|
|
|
|
def fillTreeUsingProviders(self):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
self.algorithmTree.clear()
|
2016-01-08 08:27:22 +01:00
|
|
|
|
self.disabledProviderItems = {}
|
2017-10-15 19:33:49 +10:00
|
|
|
|
|
|
|
|
|
# TODO - replace with proper model for toolbox!
|
|
|
|
|
|
|
|
|
|
# first add qgis/native providers, since they create top level groups
|
2017-04-04 11:04:36 +10:00
|
|
|
|
for provider in QgsApplication.processingRegistry().providers():
|
2017-11-25 08:12:27 +10:00
|
|
|
|
if provider.id() in ('qgis', 'native', '3d'):
|
2017-06-24 11:53:20 +10:00
|
|
|
|
self.addAlgorithmsFromProvider(provider, self.algorithmTree.invisibleRootItem())
|
2016-01-08 08:27:22 +01:00
|
|
|
|
else:
|
2017-10-15 19:33:49 +10:00
|
|
|
|
continue
|
2016-01-08 08:27:22 +01:00
|
|
|
|
self.algorithmTree.sortItems(0, Qt.AscendingOrder)
|
2017-10-15 19:33:49 +10:00
|
|
|
|
|
|
|
|
|
for provider in QgsApplication.processingRegistry().providers():
|
2017-11-25 08:12:27 +10:00
|
|
|
|
if provider.id() in ('qgis', 'native', '3d'):
|
2017-10-15 19:33:49 +10:00
|
|
|
|
# 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)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
2017-06-24 11:53:20 +10:00
|
|
|
|
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:
|
2017-10-15 19:33:49 +10:00
|
|
|
|
groupItem = TreeGroupItem(alg.group())
|
2017-06-24 11:53:20 +10:00
|
|
|
|
if not active:
|
2017-10-15 19:33:49 +10:00
|
|
|
|
groupItem.setInactive()
|
2017-11-25 08:12:27 +10:00
|
|
|
|
if provider.id() in ('qgis', 'native', '3d'):
|
2017-06-24 11:53:20 +10:00
|
|
|
|
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:
|
2017-10-15 19:33:49 +10:00
|
|
|
|
groupItem = TreeGroupItem(action.group)
|
2017-06-24 11:53:20 +10:00
|
|
|
|
groups[action.group] = groupItem
|
|
|
|
|
algItem = TreeActionItem(action)
|
|
|
|
|
groupItem.addChild(algItem)
|
|
|
|
|
|
|
|
|
|
text = provider.name()
|
|
|
|
|
|
2017-11-25 08:12:27 +10:00
|
|
|
|
if not provider.id() in ('qgis', 'native', '3d'):
|
2017-06-24 11:53:20 +10:00
|
|
|
|
if not active:
|
|
|
|
|
def activateProvider():
|
|
|
|
|
self.activateProvider(provider.id())
|
|
|
|
|
|
|
|
|
|
label = QLabel(text + " <a href='%s'>Activate</a>")
|
|
|
|
|
label.setStyleSheet("QLabel {background-color: white; color: grey;}")
|
|
|
|
|
label.linkActivated.connect(activateProvider)
|
2017-09-04 11:16:17 +10:00
|
|
|
|
self.algorithmTree.setItemWidget(parent, 0, label)
|
2017-06-24 11:53:20 +10:00
|
|
|
|
else:
|
|
|
|
|
parent.setText(0, text)
|
|
|
|
|
|
2017-11-24 17:58:43 +07:00
|
|
|
|
for group, groupItem in sorted(groups.items(), key=operator.itemgetter(1)):
|
2017-06-24 11:53:20 +10:00
|
|
|
|
parent.addChild(groupItem)
|
|
|
|
|
|
2017-11-25 08:12:27 +10:00
|
|
|
|
if not provider.id() in ('qgis', 'native', '3d'):
|
2017-06-24 11:53:20 +10:00
|
|
|
|
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
|
2017-03-29 10:42:42 +10:00
|
|
|
|
icon = alg.icon()
|
2017-03-29 12:51:59 +10:00
|
|
|
|
nameEn = alg.name()
|
|
|
|
|
name = alg.displayName()
|
2016-04-28 14:53:27 +03:00
|
|
|
|
name = name if name != '' else nameEn
|
2012-12-07 21:11:56 +01:00
|
|
|
|
self.setIcon(0, icon)
|
2017-11-30 15:36:55 +10:00
|
|
|
|
self.setToolTip(0, self.formatAlgorithmTooltip(alg))
|
2013-02-21 22:12:01 +01:00
|
|
|
|
self.setText(0, name)
|
2017-10-15 19:33:49 +10:00
|
|
|
|
self.setData(0, ProcessingToolbox.NAME_ROLE, nameEn)
|
|
|
|
|
self.setData(0, ProcessingToolbox.TAG_ROLE, alg.tags())
|
|
|
|
|
self.setData(0, ProcessingToolbox.TYPE_ROLE, ProcessingToolbox.ALG_ITEM)
|
|
|
|
|
|
2017-11-30 15:36:55 +10:00
|
|
|
|
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()))
|
|
|
|
|
)
|
|
|
|
|
|
2017-10-15 19:33:49 +10:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-10-01 20:52:22 +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
|
2016-04-11 15:55:49 +03:00
|
|
|
|
self.setText(0, action.i18n_name)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
self.setIcon(0, action.getIcon())
|
2017-10-15 19:33:49 +10:00
|
|
|
|
self.setData(0, ProcessingToolbox.NAME_ROLE, action.name)
|
2014-04-06 12:27:51 +02:00
|
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
|
2014-03-26 10:18:27 +01:00
|
|
|
|
class TreeProviderItem(QTreeWidgetItem):
|
|
|
|
|
|
2017-04-04 11:04:36 +10:00
|
|
|
|
def __init__(self, provider, tree, toolbox):
|
2017-10-15 19:33:49 +10:00
|
|
|
|
QTreeWidgetItem.__init__(self, None)
|
2016-01-08 08:27:22 +01:00
|
|
|
|
self.tree = tree
|
|
|
|
|
self.toolbox = toolbox
|
2017-04-04 11:04:36 +10:00
|
|
|
|
self.provider = provider
|
2017-01-09 09:18:25 +10:00
|
|
|
|
self.setIcon(0, self.provider.icon())
|
2017-10-15 19:33:49 +10:00
|
|
|
|
self.setData(0, ProcessingToolbox.TYPE_ROLE, ProcessingToolbox.PROVIDER_ITEM)
|
2017-10-19 13:12:31 +10:00
|
|
|
|
self.setToolTip(0, self.provider.longName())
|
2014-03-26 10:18:27 +01:00
|
|
|
|
self.populate()
|
2014-04-06 12:27:51 +02:00
|
|
|
|
|
2014-03-26 10:18:27 +01:00
|
|
|
|
def refresh(self):
|
|
|
|
|
self.takeChildren()
|
|
|
|
|
self.populate()
|
2014-04-06 12:27:51 +02:00
|
|
|
|
|
2014-03-26 10:18:27 +01:00
|
|
|
|
def populate(self):
|
2017-06-24 11:53:20 +10:00
|
|
|
|
self.toolbox.addAlgorithmsFromProvider(self.provider, self)
|