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

260 lines
11 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
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
from qgis.utils import iface
2018-07-23 13:58:11 +10:00
from qgis.core import (QgsWkbTypes,
QgsMapLayer,
QgsApplication,
QgsProcessingAlgorithm)
from qgis.gui import (QgsGui,
2018-07-08 16:54:09 +10:00
QgsDockWidget,
QgsProcessingToolboxProxyModel)
2015-05-18 21:04:20 +03:00
from processing.gui.Postprocessing import handleAlgorithmResults
2018-07-08 17:24:23 +10:00
from processing.core.ProcessingConfig import ProcessingConfig
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.MessageBarProgress import MessageBarProgress
from processing.gui.AlgorithmExecutor import execute
from processing.gui.ProviderActions import (ProviderActions,
ProviderContextMenuActions)
from processing.tools import dataobjects
2018-07-23 13:58:11 +10:00
from processing.gui.AlgorithmExecutor import execute_in_place
2015-05-18 21:04:20 +03:00
pluginPath = os.path.split(os.path.dirname(__file__))[0]
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
WIDGET, BASE = uic.loadUiType(
os.path.join(pluginPath, 'ui', 'ProcessingToolbox.ui'))
2012-09-15 18:25:25 +03:00
class ProcessingToolbox(QgsDockWidget, 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
2018-07-23 13:58:11 +10:00
self.in_place_mode = False
self.setupUi(self)
self.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
self.processingToolbar.setIconSize(iface.iconSize(True))
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
self.searchBox.setShowSearchIcon(True)
2018-07-08 17:24:23 +10:00
self.searchBox.textChanged.connect(self.algorithmTree.setFilterString)
self.searchBox.returnPressed.connect(self.activateCurrent)
self.algorithmTree.customContextMenuRequested.connect(
self.showPopupMenu)
self.algorithmTree.doubleClicked.connect(self.executeAlgorithm)
self.txtTip.setVisible(self.disabledProviders())
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())
2017-12-01 08:22:37 +10: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
# connect to existing providers
for p in QgsApplication.processingRegistry().providers():
if p.isActive():
self.addProviderActions(p)
2018-07-08 16:54:09 +10:00
QgsApplication.processingRegistry().providerRemoved.connect(self.addProvider)
QgsApplication.processingRegistry().providerRemoved.connect(self.removeProvider)
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()))
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() and provider.canBeActivated():
return True
return False
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)
act = QAction(action.name, menu)
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)
def removeProvider(self, provider_id):
button = self.findChild(QToolButton, 'provideraction-' + provider_id)
if button:
self.processingToolbar.removeChild(button)
def showPopupMenu(self, point):
2018-07-08 16:54:09 +10:00
index = self.algorithmTree.indexAt(point)
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)
if alg.flags() & QgsProcessingAlgorithm.FlagSupportsBatch:
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)
executeBatchAction.triggered.connect(
self.executeAlgorithmAsBatchProcess)
popupmenu.addAction(executeBatchAction)
popupmenu.addSeparator()
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)
editRenderingStylesAction.triggered.connect(
self.editRenderingStyles)
2012-09-15 18:25:25 +03:00
popupmenu.addAction(editRenderingStylesAction)
actions = ProviderContextMenuActions.actions
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():
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_()
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:
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
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
if alg.countVisibleParameters() > 0:
dlg = alg.createCustomParametersWidget(self)
if not dlg:
2018-07-23 13:58:11 +10:00
dlg = AlgorithmDialog(alg, self.in_place_mode)
canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec_()
if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)
else:
feedback = MessageBarProgress()
context = dataobjects.createContext(feedback)
parameters = {}
ret, results = execute(alg, parameters, context, feedback)
handleAlgorithmResults(alg, context, feedback)
feedback.close()