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
|
2018-05-21 14:23:26 +10:00
|
|
|
import warnings
|
2015-05-18 21:04:20 +03:00
|
|
|
|
2016-04-29 11:39:26 +02:00
|
|
|
from qgis.PyQt import uic
|
2018-07-08 17:24:23 +10:00
|
|
|
from qgis.PyQt.QtCore import Qt, QCoreApplication
|
|
|
|
from qgis.PyQt.QtWidgets import QToolButton, QMenu, QAction
|
2014-05-20 16:30:59 +02:00
|
|
|
from qgis.utils import iface
|
2018-07-23 13:58:11 +10:00
|
|
|
from qgis.core import (QgsWkbTypes,
|
|
|
|
QgsMapLayer,
|
|
|
|
QgsApplication,
|
2017-03-29 15:00:20 +10:00
|
|
|
QgsProcessingAlgorithm)
|
2018-07-04 16:38:46 +10:00
|
|
|
from qgis.gui import (QgsGui,
|
2018-07-08 16:54:09 +10:00
|
|
|
QgsDockWidget,
|
|
|
|
QgsProcessingToolboxProxyModel)
|
2015-05-18 21:04:20 +03:00
|
|
|
|
2016-04-06 10:50:54 +02:00
|
|
|
from processing.gui.Postprocessing import handleAlgorithmResults
|
2018-07-08 17:24:23 +10:00
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig
|
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-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
|
2018-07-23 13:58:11 +10:00
|
|
|
from processing.gui.AlgorithmExecutor import execute_in_place
|
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]
|
2018-05-21 14:23:26 +10:00
|
|
|
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
|
|
|
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
|
|
|
|
2018-01-24 07:34:42 +10:00
|
|
|
class ProcessingToolbox(QgsDockWidget, WIDGET):
|
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
|
2018-07-23 13:58:11 +10:00
|
|
|
self.in_place_mode = False
|
2012-10-30 15:00:07 +02:00
|
|
|
self.setupUi(self)
|
|
|
|
self.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
|
2018-01-26 12:37:55 +07:00
|
|
|
self.processingToolbar.setIconSize(iface.iconSize(True))
|
2012-10-30 15:00:07 +02:00
|
|
|
|
2018-07-08 17:24:23 +10:00
|
|
|
self.algorithmTree.setRegistry(QgsApplication.processingRegistry(),
|
|
|
|
QgsGui.instance().processingRecentAlgorithmLog())
|
|
|
|
self.algorithmTree.setFilters(QgsProcessingToolboxProxyModel.FilterToolbox)
|
2018-07-08 16:54:09 +10:00
|
|
|
|
2018-05-31 12:51:53 +10:00
|
|
|
self.searchBox.setShowSearchIcon(True)
|
|
|
|
|
2018-07-08 17:24:23 +10:00
|
|
|
self.searchBox.textChanged.connect(self.algorithmTree.setFilterString)
|
2018-02-23 08:27:40 +10:00
|
|
|
self.searchBox.returnPressed.connect(self.activateCurrent)
|
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.txtTip.setVisible(self.disabledProviders())
|
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())
|
2017-12-01 08:22:37 +10:00
|
|
|
|
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'):
|
2018-02-15 22:30:52 +01:00
|
|
|
self.searchBox.setPlaceholderText(QCoreApplication.translate('ProcessingToolbox', 'Search…'))
|
2012-12-01 20:22:21 +02:00
|
|
|
|
2017-05-01 17:12:33 +10:00
|
|
|
# connect to existing providers
|
|
|
|
for p in QgsApplication.processingRegistry().providers():
|
2018-01-25 16:16:44 +07:00
|
|
|
if p.isActive():
|
|
|
|
self.addProviderActions(p)
|
2017-05-01 17:12:33 +10:00
|
|
|
|
2018-07-08 16:54:09 +10:00
|
|
|
QgsApplication.processingRegistry().providerRemoved.connect(self.addProvider)
|
2017-01-09 09:59:35 +10:00
|
|
|
QgsApplication.processingRegistry().providerRemoved.connect(self.removeProvider)
|
2016-01-08 08:27:22 +01:00
|
|
|
|
2018-07-23 13:58:11 +10:00
|
|
|
iface.currentLayerChanged.connect(self.layer_changed)
|
|
|
|
|
|
|
|
def set_in_place_edit_mode(self, enabled):
|
|
|
|
if enabled:
|
|
|
|
self.algorithmTree.setFilters(QgsProcessingToolboxProxyModel.Filters(QgsProcessingToolboxProxyModel.FilterToolbox | QgsProcessingToolboxProxyModel.FilterInPlace))
|
|
|
|
else:
|
|
|
|
self.algorithmTree.setFilters(QgsProcessingToolboxProxyModel.FilterToolbox)
|
|
|
|
self.in_place_mode = enabled
|
|
|
|
|
|
|
|
def layer_changed(self, layer):
|
|
|
|
if layer is None or layer.type() != QgsMapLayer.VectorLayer:
|
|
|
|
return
|
|
|
|
self.algorithmTree.setInPlaceLayerType(QgsWkbTypes.geometryType(layer.wkbType()))
|
|
|
|
|
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():
|
2018-07-10 10:22:43 +10:00
|
|
|
if not provider.isActive() and provider.canBeActivated():
|
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
|
|
|
|
|
2018-01-25 16:16:44 +07:00
|
|
|
def addProviderActions(self, provider):
|
|
|
|
if provider.id() in ProviderActions.actions:
|
|
|
|
toolbarButton = QToolButton()
|
|
|
|
toolbarButton.setObjectName('provideraction_' + provider.id())
|
|
|
|
toolbarButton.setIcon(provider.icon())
|
|
|
|
toolbarButton.setToolTip(provider.name())
|
|
|
|
toolbarButton.setPopupMode(QToolButton.InstantPopup)
|
|
|
|
|
|
|
|
actions = ProviderActions.actions[provider.id()]
|
|
|
|
menu = QMenu(provider.name(), self)
|
|
|
|
for action in actions:
|
|
|
|
action.setData(self)
|
2018-01-29 16:03:01 +02:00
|
|
|
act = QAction(action.name, menu)
|
2018-01-25 16:16:44 +07:00
|
|
|
act.triggered.connect(action.execute)
|
|
|
|
menu.addAction(act)
|
|
|
|
toolbarButton.setMenu(menu)
|
|
|
|
self.processingToolbar.addWidget(toolbarButton)
|
|
|
|
|
2018-07-08 16:54:09 +10:00
|
|
|
def addProvider(self, provider_id):
|
|
|
|
provider = QgsApplication.processingRegistry().providerById(provider_id)
|
2018-07-08 17:33:02 +10:00
|
|
|
if provider is not None:
|
|
|
|
self.addProviderActions(provider)
|
2016-04-27 10:09:28 +02:00
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
def removeProvider(self, provider_id):
|
2018-01-25 16:16:44 +07:00
|
|
|
button = self.findChild(QToolButton, 'provideraction-' + provider_id)
|
|
|
|
if button:
|
|
|
|
self.processingToolbar.removeChild(button)
|
2016-04-27 10:09:28 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
def showPopupMenu(self, point):
|
2018-07-08 16:54:09 +10:00
|
|
|
index = self.algorithmTree.indexAt(point)
|
2016-08-01 00:09:23 +02:00
|
|
|
popupmenu = QMenu()
|
2018-07-08 17:24:23 +10:00
|
|
|
alg = self.algorithmTree.algorithmForIndex(index)
|
2018-07-08 16:54:09 +10:00
|
|
|
if alg is not None:
|
|
|
|
executeAction = QAction(QCoreApplication.translate('ProcessingToolbox', 'Execute…'), popupmenu)
|
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(
|
2018-02-22 00:36:30 +01:00
|
|
|
QCoreApplication.translate('ProcessingToolbox', 'Execute as Batch Process…'),
|
2018-07-08 16:54:09 +10:00
|
|
|
popupmenu)
|
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(
|
2018-02-22 00:36:30 +01:00
|
|
|
QCoreApplication.translate('ProcessingToolbox', 'Edit Rendering Styles for Outputs…'),
|
2018-07-08 16:54:09 +10:00
|
|
|
popupmenu)
|
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)
|
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:
|
2018-07-08 16:54:09 +10:00
|
|
|
action.setData(alg, self)
|
2012-09-15 18:25:25 +03:00
|
|
|
if action.isEnabled():
|
2013-10-01 20:52:22 +03:00
|
|
|
contextMenuAction = QAction(action.name,
|
2018-07-08 16:54:09 +10:00
|
|
|
popupmenu)
|
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):
|
2018-07-08 17:24:23 +10:00
|
|
|
alg = self.algorithmTree.selectedAlgorithm()
|
2018-07-08 16:54:09 +10:00
|
|
|
if alg is not None:
|
2012-09-15 18:25:25 +03:00
|
|
|
dlg = EditRenderingStylesDialog(alg)
|
|
|
|
dlg.exec_()
|
|
|
|
|
2018-02-23 08:27:40 +10:00
|
|
|
def activateCurrent(self):
|
|
|
|
self.executeAlgorithm()
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def executeAlgorithmAsBatchProcess(self):
|
2018-07-08 17:24:23 +10:00
|
|
|
alg = self.algorithmTree.selectedAlgorithm()
|
2018-07-08 16:54:09 +10:00
|
|
|
if alg is not None:
|
|
|
|
dlg = BatchAlgorithmDialog(alg)
|
|
|
|
dlg.show()
|
|
|
|
dlg.exec_()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def executeAlgorithm(self):
|
2018-07-08 17:24:23 +10:00
|
|
|
alg = self.algorithmTree.selectedAlgorithm()
|
2018-07-08 16:54:09 +10:00
|
|
|
if alg is not None:
|
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
|
|
|
|
2018-07-23 13:58:11 +10:00
|
|
|
if self.in_place_mode and len(alg.parameterDefinitions()) <= 2:
|
|
|
|
# hack
|
|
|
|
parameters = {}
|
|
|
|
execute_in_place(alg, parameters)
|
|
|
|
return
|
|
|
|
|
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:
|
2018-07-23 13:58:11 +10:00
|
|
|
dlg = AlgorithmDialog(alg, self.in_place_mode)
|
2016-04-06 10:50:54 +02:00
|
|
|
canvas = iface.mapCanvas()
|
|
|
|
prevMapTool = canvas.mapTool()
|
|
|
|
dlg.show()
|
|
|
|
dlg.exec_()
|
|
|
|
if canvas.mapTool() != prevMapTool:
|
|
|
|
try:
|
|
|
|
canvas.mapTool().reset()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
canvas.setMapTool(prevMapTool)
|
|
|
|
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()
|