# -*- 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. * * * *************************************************************************** """ __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 PyQt4 import uic from PyQt4.QtCore import Qt, QSettings, QCoreApplication from PyQt4.QtGui import QMenu, QAction, QTreeWidgetItem from qgis.utils import iface from processing.modeler.ModelerUtils import ModelerUtils from processing.core.Processing import Processing from processing.core.ProcessingLog import ProcessingLog from processing.core.ProcessingConfig import ProcessingConfig from processing.core.GeoAlgorithm import GeoAlgorithm from processing.gui.MessageDialog import MessageDialog from processing.gui import AlgorithmClassification from processing.gui.AlgorithmDialog import AlgorithmDialog from processing.gui.BatchAlgorithmDialog import BatchAlgorithmDialog from processing.gui.EditRenderingStylesDialog import EditRenderingStylesDialog pluginPath = os.path.split(os.path.dirname(__file__))[0] WIDGET, BASE = uic.loadUiType( os.path.join(pluginPath, 'ui', 'ProcessingToolbox.ui')) class ProcessingToolbox(BASE, WIDGET): USE_CATEGORIES = '/Processing/UseSimplifiedInterface' updateAlgList = True def __init__(self): super(ProcessingToolbox, self).__init__(None) self.setupUi(self) self.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea) self.modeComboBox.clear() self.modeComboBox.addItems([self.tr('Simplified interface'), self.tr('Advanced interface')]) settings = QSettings() if not settings.contains(self.USE_CATEGORIES): settings.setValue(self.USE_CATEGORIES, True) useCategories = settings.value(self.USE_CATEGORIES, type=bool) if useCategories: self.modeComboBox.setCurrentIndex(0) else: self.modeComboBox.setCurrentIndex(1) self.modeComboBox.currentIndexChanged.connect(self.modeHasChanged) self.searchBox.textChanged.connect(self.textChanged) self.algorithmTree.customContextMenuRequested.connect( self.showPopupMenu) self.algorithmTree.doubleClicked.connect(self.executeAlgorithm) if hasattr(self.searchBox, 'setPlaceholderText'): self.searchBox.setPlaceholderText(self.tr('Search...')) self.fillTree() def textChanged(self): text = self.searchBox.text().strip(' ').lower() self._filterItem(self.algorithmTree.invisibleRootItem(), text) if text: self.algorithmTree.expandAll() else: self.algorithmTree.collapseAll() self.algorithmTree.invisibleRootItem().child(0).setExpanded(True) def _filterItem(self, item, text): if (item.childCount() > 0): show = False for i in xrange(item.childCount()): child = item.child(i) showChild = self._filterItem(child, text) show = showChild or show item.setHidden(not show) return show elif isinstance(item, (TreeAlgorithmItem, TreeActionItem)): hide = bool(text) and (text not in item.text(0).lower()) item.setHidden(hide) return not hide else: item.setHidden(True) return False def modeHasChanged(self): idx = self.modeComboBox.currentIndex() settings = QSettings() if idx == 0: # Simplified settings.setValue(self.USE_CATEGORIES, True) else: settings.setValue(self.USE_CATEGORIES, False) self.fillTree() def algsListHasChanged(self): if self.updateAlgList: self.fillTree() def updateProvider(self, providerName, updateAlgsList=True): if updateAlgsList: self.updateAlgList = False Processing.updateAlgsList() self.updateAlgList = True for i in xrange(self.algorithmTree.invisibleRootItem().childCount()): child = self.algorithmTree.invisibleRootItem().child(i) if isinstance(child, TreeProviderItem): if child.providerName == providerName: child.refresh() # sort categories and items in categories child.sortChildren(0, Qt.AscendingOrder) for i in xrange(child.childCount()): child.child(i).sortChildren(0, Qt.AscendingOrder) break self.addRecentAlgorithms(True) def showPopupMenu(self, point): item = self.algorithmTree.itemAt(point) if isinstance(item, TreeAlgorithmItem): alg = item.alg popupmenu = QMenu() 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) actions = Processing.contextMenuActions if len(actions) > 0: popupmenu.addSeparator() for action in actions: action.setData(alg, 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('Missing dependency')) dlg.setMessage( self.tr('