# -*- coding: utf-8 -*- """ *************************************************************************** ProcessingToolbox.py --------------------- 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. * * * *************************************************************************** """ from builtins import range __author__ = 'Victor Olaya' __date__ = 'August 2012' __copyright__ = '(C) 2012, Victor Olaya' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' import os from qgis.PyQt import uic from qgis.PyQt.QtCore import Qt, QCoreApplication from qgis.PyQt.QtWidgets import QMenu, QAction, QTreeWidgetItem, QLabel, QMessageBox from qgis.utils import iface from processing.gui.Postprocessing import handleAlgorithmResults from processing.core.Processing import Processing 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 from processing.gui.EditRenderingStylesDialog import EditRenderingStylesDialog from processing.gui.ConfigDialog import ConfigDialog from processing.gui.MessageBarProgress import MessageBarProgress from processing.gui.AlgorithmExecutor import runalg from processing.core.alglist import algList pluginPath = os.path.split(os.path.dirname(__file__))[0] WIDGET, BASE = uic.loadUiType( os.path.join(pluginPath, 'ui', 'ProcessingToolbox.ui')) class ProcessingToolbox(BASE, WIDGET): def __init__(self): 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) def openSettings(url): if url == "close": self.txtTip.setVisible(False) self.tipWasClosed = True else: dlg = ConfigDialog(self) dlg.exec_() self.txtTip.setVisible(self.disabledProviders()) self.txtTip.linkActivated.connect(openSettings) if hasattr(self.searchBox, 'setPlaceholderText'): self.searchBox.setPlaceholderText(self.tr('Search...')) self.fillTree() algList.providerRemoved.connect(self.removeProvider) algList.providerAdded.connect(self.addProvider) algList.providerUpdated.connect(self.updateProvider) settingsWatcher.settingsChanged.connect(self.fillTree) def showDisabled(self): self.txtDisabled.setVisible(False) for providerName in self.disabledWithMatchingAlgs: self.disabledProviderItems[providerName].setHidden(False) self.algorithmTree.expandAll() def disabledProviders(self): showTip = ProcessingConfig.getSetting(ProcessingConfig.SHOW_PROVIDERS_TOOLTIP) if not showTip or self.tipWasClosed: return False for providerName in list(algList.algs.keys()): name = 'ACTIVATE_' + providerName.upper().replace(' ', '_') if not ProcessingConfig.getSetting(name): return True return False def textChanged(self): text = self.searchBox.text().strip(' ').lower() for item in list(self.disabledProviderItems.values()): item.setHidden(True) self._filterItem(self.algorithmTree.invisibleRootItem(), text) if text: self.algorithmTree.expandAll() self.disabledWithMatchingAlgs = [] for providerName, provider in algList.algs.items(): name = 'ACTIVATE_' + providerName.upper().replace(' ', '_') if not ProcessingConfig.getSetting(name): for alg in list(provider.values()): if text in alg.name: self.disabledWithMatchingAlgs.append(providerName) 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) def _filterItem(self, item, text): if (item.childCount() > 0): show = False for i in range(item.childCount()): child = item.child(i) showChild = self._filterItem(child, text) show = (showChild or show) and item not in list(self.disabledProviderItems.values()) item.setHidden(not show) return show elif isinstance(item, (TreeAlgorithmItem, TreeActionItem)): # hide = bool(text) and (text not in item.text(0).lower()) hide = bool(text) and not any(text in t for t in [item.text(0).lower(), item.data(0, Qt.UserRole).lower()]) if isinstance(item, TreeAlgorithmItem): hide = hide and (text not in item.alg.commandLineName()) item.setHidden(hide) return not hide else: item.setHidden(True) return False def activateProvider(self, providerName): name = 'ACTIVATE_' + providerName.upper().replace(' ', '_') ProcessingConfig.setSettingValue(name, True) self.fillTree() self.textChanged() self.showDisabled() provider = Processing.getProviderFromName(providerName) if not provider.canBeActivated(): QMessageBox.warning(self, "Activate provider", "The provider has been activated, but it might need additional configuration.") def updateProvider(self, providerName): item = self._providerItem(providerName) if item is not None: item.refresh() item.sortChildren(0, Qt.AscendingOrder) for i in range(item.childCount()): item.child(i).sortChildren(0, Qt.AscendingOrder) self.addRecentAlgorithms(True) def removeProvider(self, providerName): item = self._providerItem(providerName) if item is not None: self.algorithmTree.invisibleRootItem().removeChild(item) def _providerItem(self, providerName): for i in range(self.algorithmTree.invisibleRootItem().childCount()): child = self.algorithmTree.invisibleRootItem().child(i) if isinstance(child, TreeProviderItem): if child.providerName == providerName: return child def showPopupMenu(self, point): item = self.algorithmTree.itemAt(point) popupmenu = QMenu() if isinstance(item, TreeAlgorithmItem): alg = item.alg executeAction = QAction(self.tr('Execute'), self.algorithmTree) executeAction.triggered.connect(self.executeAlgorithm) popupmenu.addAction(executeAction) 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) popupmenu.addAction(editRenderingStylesAction) if isinstance(item, (TreeAlgorithmItem, TreeActionItem)): data = item.alg if isinstance(item, TreeAlgorithmItem) else item.action actions = Processing.contextMenuActions if len(actions) > 0: popupmenu.addSeparator() for action in actions: action.setData(data, self) if action.isEnabled(): contextMenuAction = QAction(action.name, self.algorithmTree) 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 = Processing.getAlgorithm(item.alg.commandLineName()) dlg = EditRenderingStylesDialog(alg) dlg.exec_() def executeAlgorithmAsBatchProcess(self): item = self.algorithmTree.currentItem() if isinstance(item, TreeAlgorithmItem): alg = Processing.getAlgorithm(item.alg.commandLineName()) alg = alg.getCopy() dlg = BatchAlgorithmDialog(alg) dlg.show() dlg.exec_() def executeAlgorithm(self): item = self.algorithmTree.currentItem() if isinstance(item, TreeAlgorithmItem): alg = Processing.getAlgorithm(item.alg.commandLineName()) message = alg.checkBeforeOpeningParametersDialog() if message: dlg = MessageDialog() dlg.setTitle(self.tr('Error executing algorithm')) dlg.setMessage( self.tr('