2012-10-05 23:28:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2013-08-12 20:44:27 +02:00
|
|
|
ProcessingPlugin.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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2013-10-01 20:52:22 +03: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$'
|
|
|
|
|
2013-03-28 21:25:02 +01:00
|
|
|
import shutil
|
2012-09-18 14:47:49 +03:00
|
|
|
import inspect
|
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
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2017-06-21 18:23:45 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
2017-09-15 13:29:21 +10:00
|
|
|
QgsProcessingUtils,
|
2017-09-20 14:05:31 +10:00
|
|
|
QgsProcessingModelAlgorithm,
|
|
|
|
QgsDataItemProvider,
|
|
|
|
QgsDataProvider,
|
|
|
|
QgsDataItem,
|
|
|
|
QgsMimeDataUtils)
|
2017-09-15 13:29:21 +10:00
|
|
|
from qgis.gui import (QgsOptionsWidgetFactory,
|
|
|
|
QgsCustomDropHandler)
|
2017-09-20 14:05:31 +10:00
|
|
|
from qgis.PyQt.QtCore import Qt, QCoreApplication, QDir, QFileInfo
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtWidgets import QMenu, QAction
|
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2015-02-03 11:22:34 +02:00
|
|
|
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.Processing import Processing
|
2017-09-15 13:29:21 +10:00
|
|
|
from processing.gui.AlgorithmDialog import AlgorithmDialog
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.gui.ProcessingToolbox import ProcessingToolbox
|
|
|
|
from processing.gui.HistoryDialog import HistoryDialog
|
2017-03-13 10:00:51 +01:00
|
|
|
from processing.gui.ConfigDialog import ConfigOptionsPage
|
2017-02-10 13:35:58 +02:00
|
|
|
from processing.gui.ResultsDock import ResultsDock
|
2017-05-11 11:49:07 +10:00
|
|
|
from processing.gui.AlgorithmLocatorFilter import AlgorithmLocatorFilter
|
2015-02-03 11:22:34 +02:00
|
|
|
from processing.modeler.ModelerDialog import ModelerDialog
|
2017-06-21 18:23:45 +10:00
|
|
|
from processing.tools.system import tempHelpFolder
|
2016-02-03 07:54:42 +01:00
|
|
|
from processing.gui.menus import removeMenus, initializeMenus, createMenus
|
2017-02-10 13:35:58 +02:00
|
|
|
from processing.core.ProcessingResults import resultsList
|
2012-09-18 20:02:43 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]
|
2012-09-15 18:25:25 +03:00
|
|
|
if cmd_folder not in sys.path:
|
2013-03-31 21:18:27 +02:00
|
|
|
sys.path.insert(0, cmd_folder)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2017-03-07 18:12:06 +10:00
|
|
|
class ProcessingOptionsFactory(QgsOptionsWidgetFactory):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(QgsOptionsWidgetFactory, self).__init__()
|
|
|
|
|
|
|
|
def icon(self):
|
|
|
|
return QgsApplication.getThemeIcon('/processingAlgorithm.svg')
|
|
|
|
|
|
|
|
def createWidget(self, parent):
|
|
|
|
return ConfigOptionsPage(parent)
|
|
|
|
|
|
|
|
|
2017-09-15 13:29:21 +10:00
|
|
|
class ProcessingDropHandler(QgsCustomDropHandler):
|
|
|
|
|
|
|
|
def handleFileDrop(self, file):
|
|
|
|
if not file.lower().endswith('.model3'):
|
|
|
|
return False
|
2017-09-20 14:05:31 +10:00
|
|
|
self.runAlg(file)
|
2017-09-15 13:29:21 +10:00
|
|
|
|
2017-09-20 14:05:31 +10:00
|
|
|
@staticmethod
|
|
|
|
def runAlg(file):
|
2017-09-15 13:29:21 +10:00
|
|
|
alg = QgsProcessingModelAlgorithm()
|
|
|
|
if not alg.fromFile(file):
|
|
|
|
return False
|
|
|
|
|
|
|
|
alg.setProvider(QgsApplication.processingRegistry().providerById('model'))
|
|
|
|
dlg = AlgorithmDialog(alg)
|
|
|
|
dlg.setAttribute(Qt.WA_DeleteOnClose)
|
|
|
|
dlg.show()
|
|
|
|
return True
|
|
|
|
|
2017-09-20 14:05:31 +10:00
|
|
|
def customUriProviderKey(self):
|
|
|
|
return 'processing'
|
|
|
|
|
|
|
|
def handleCustomUriDrop(self, uri):
|
|
|
|
path = uri.uri
|
|
|
|
self.runAlg(path)
|
|
|
|
|
|
|
|
|
|
|
|
class ProcessingModelItem(QgsDataItem):
|
|
|
|
|
|
|
|
def __init__(self, parent, name, path):
|
2017-09-20 18:41:42 +10:00
|
|
|
super(ProcessingModelItem, self).__init__(QgsDataItem.Custom, parent, name, path)
|
|
|
|
self.setState(QgsDataItem.Populated) # no children
|
2017-09-20 14:05:31 +10:00
|
|
|
self.setIconName(":/images/themes/default/processingModel.svg")
|
2017-09-20 18:41:42 +10:00
|
|
|
self.setToolTip(QDir.toNativeSeparators(path))
|
2017-09-20 14:05:31 +10:00
|
|
|
|
|
|
|
def hasDragEnabled(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def handleDoubleClick(self):
|
|
|
|
self.runModel()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def mimeUri(self):
|
|
|
|
u = QgsMimeDataUtils.Uri()
|
|
|
|
u.layerType = "custom"
|
|
|
|
u.providerKey = "processing"
|
|
|
|
u.name = self.name()
|
|
|
|
u.uri = self.path()
|
|
|
|
return u
|
|
|
|
|
|
|
|
def runModel(self):
|
|
|
|
ProcessingDropHandler.runAlg(self.path())
|
|
|
|
|
|
|
|
def editModel(self):
|
|
|
|
dlg = ModelerDialog()
|
|
|
|
dlg.loadModel(self.path())
|
|
|
|
dlg.show()
|
|
|
|
|
|
|
|
def actions(self):
|
2017-09-22 11:38:42 +02:00
|
|
|
run_model_action = QAction(QCoreApplication.translate('ProcessingPlugin', '&Run Model…'), self)
|
2017-09-20 14:05:31 +10:00
|
|
|
run_model_action.triggered.connect(self.runModel)
|
2017-09-22 11:38:42 +02:00
|
|
|
edit_model_action = QAction(QCoreApplication.translate('ProcessingPlugin', '&Edit Model…'), self)
|
2017-09-20 14:05:31 +10:00
|
|
|
edit_model_action.triggered.connect(self.editModel)
|
|
|
|
return [run_model_action, edit_model_action]
|
|
|
|
|
|
|
|
|
|
|
|
class ProcessingDataItemProvider(QgsDataItemProvider):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(ProcessingDataItemProvider, self).__init__()
|
|
|
|
|
|
|
|
def name(self):
|
|
|
|
return 'processing'
|
|
|
|
|
|
|
|
def capabilities(self):
|
|
|
|
return QgsDataProvider.File
|
|
|
|
|
|
|
|
def createDataItem(self, path, parentItem):
|
|
|
|
file_info = QFileInfo(path)
|
|
|
|
|
|
|
|
if file_info.suffix().lower() == 'model3':
|
|
|
|
alg = QgsProcessingModelAlgorithm()
|
|
|
|
if alg.fromFile(path):
|
|
|
|
return ProcessingModelItem(parentItem, alg.name(), path)
|
|
|
|
return None
|
2017-09-15 13:29:21 +10:00
|
|
|
|
2017-09-20 18:41:42 +10:00
|
|
|
|
2017-11-27 13:23:49 +02:00
|
|
|
class ProcessingPlugin:
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def __init__(self, iface):
|
2014-05-20 16:30:59 +02:00
|
|
|
self.iface = iface
|
2017-03-07 18:12:06 +10:00
|
|
|
self.options_factory = ProcessingOptionsFactory()
|
|
|
|
self.options_factory.setTitle(self.tr('Processing'))
|
|
|
|
iface.registerOptionsWidgetFactory(self.options_factory)
|
2017-09-15 13:29:21 +10:00
|
|
|
self.drop_handler = ProcessingDropHandler()
|
|
|
|
iface.registerCustomDropHandler(self.drop_handler)
|
2017-09-20 14:05:31 +10:00
|
|
|
self.item_provider = ProcessingDataItemProvider()
|
|
|
|
QgsApplication.dataItemProviderRegistry().addProvider(self.item_provider)
|
2017-05-11 11:49:07 +10:00
|
|
|
self.locator_filter = AlgorithmLocatorFilter()
|
|
|
|
iface.registerLocatorFilter(self.locator_filter)
|
2014-09-24 10:40:36 +02:00
|
|
|
Processing.initialize()
|
|
|
|
|
2016-05-23 16:57:59 +02:00
|
|
|
def initGui(self):
|
2013-09-12 13:19:00 +02:00
|
|
|
self.toolbox = ProcessingToolbox()
|
2014-05-20 16:30:59 +02:00
|
|
|
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.toolbox)
|
2012-10-30 15:00:07 +02:00
|
|
|
self.toolbox.hide()
|
2013-05-01 23:45:20 +02:00
|
|
|
|
2017-02-10 13:35:58 +02:00
|
|
|
self.resultsDock = ResultsDock()
|
|
|
|
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.resultsDock)
|
|
|
|
self.resultsDock.hide()
|
|
|
|
|
|
|
|
resultsList.resultAdded.connect(self.resultsDock.fillTree)
|
|
|
|
|
2014-05-20 16:30:59 +02:00
|
|
|
self.menu = QMenu(self.iface.mainWindow().menuBar())
|
2015-02-03 11:22:34 +02:00
|
|
|
self.menu.setObjectName('processing')
|
|
|
|
self.menu.setTitle(self.tr('Pro&cessing'))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2012-12-03 20:13:34 +02:00
|
|
|
self.toolboxAction = self.toolbox.toggleViewAction()
|
2015-02-03 11:22:34 +02:00
|
|
|
self.toolboxAction.setObjectName('toolboxAction')
|
2015-05-18 19:51:26 +03:00
|
|
|
self.toolboxAction.setIcon(
|
2017-01-09 12:34:46 +10:00
|
|
|
QgsApplication.getThemeIcon("/processingAlgorithm.svg"))
|
2015-02-03 11:22:34 +02:00
|
|
|
self.toolboxAction.setText(self.tr('&Toolbox'))
|
2016-01-20 14:47:58 +02:00
|
|
|
self.iface.registerMainWindowAction(self.toolboxAction, 'Ctrl+Alt+T')
|
2012-09-15 18:25:25 +03:00
|
|
|
self.menu.addAction(self.toolboxAction)
|
|
|
|
|
2015-05-18 19:51:26 +03:00
|
|
|
self.modelerAction = QAction(
|
2017-01-09 12:34:46 +10:00
|
|
|
QgsApplication.getThemeIcon("/processingModel.svg"),
|
2015-02-03 11:22:34 +02:00
|
|
|
self.tr('Graphical &Modeler...'), self.iface.mainWindow())
|
|
|
|
self.modelerAction.setObjectName('modelerAction')
|
2012-12-03 20:13:34 +02:00
|
|
|
self.modelerAction.triggered.connect(self.openModeler)
|
2016-01-20 14:47:58 +02:00
|
|
|
self.iface.registerMainWindowAction(self.modelerAction, 'Ctrl+Alt+M')
|
2012-09-15 18:25:25 +03:00
|
|
|
self.menu.addAction(self.modelerAction)
|
|
|
|
|
2015-05-18 19:51:26 +03:00
|
|
|
self.historyAction = QAction(
|
2016-12-31 11:00:16 +07:00
|
|
|
QIcon(os.path.join(cmd_folder, 'images', 'history.svg')),
|
2015-05-20 19:33:47 +02:00
|
|
|
self.tr('&History...'), self.iface.mainWindow())
|
2015-02-03 11:22:34 +02:00
|
|
|
self.historyAction.setObjectName('historyAction')
|
2012-12-03 20:13:34 +02:00
|
|
|
self.historyAction.triggered.connect(self.openHistory)
|
2016-01-20 14:47:58 +02:00
|
|
|
self.iface.registerMainWindowAction(self.historyAction, 'Ctrl+Alt+H')
|
2012-09-15 18:25:25 +03:00
|
|
|
self.menu.addAction(self.historyAction)
|
|
|
|
|
2017-02-10 13:35:58 +02:00
|
|
|
self.resultsAction = self.resultsDock.toggleViewAction()
|
2015-02-03 11:22:34 +02:00
|
|
|
self.resultsAction.setObjectName('resultsAction')
|
2017-02-10 13:35:58 +02:00
|
|
|
self.resultsAction.setIcon(
|
2017-02-16 10:30:46 +02:00
|
|
|
QgsApplication.getThemeIcon("/processingResult.svg"))
|
|
|
|
self.resultsAction.setText(self.tr('&Results Viewer'))
|
2016-01-20 14:47:58 +02:00
|
|
|
self.iface.registerMainWindowAction(self.resultsAction, 'Ctrl+Alt+R')
|
2012-09-15 18:25:25 +03:00
|
|
|
self.menu.addAction(self.resultsAction)
|
|
|
|
|
2014-05-20 16:30:59 +02:00
|
|
|
menuBar = self.iface.mainWindow().menuBar()
|
2013-10-01 20:52:22 +03:00
|
|
|
menuBar.insertMenu(
|
2014-05-20 16:30:59 +02:00
|
|
|
self.iface.firstRightStandardMenu().menuAction(), self.menu)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2016-12-30 09:38:23 +07:00
|
|
|
self.menu.addSeparator()
|
|
|
|
|
2016-02-03 07:54:42 +01:00
|
|
|
initializeMenus()
|
|
|
|
createMenus()
|
2016-01-27 15:00:13 +02:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def unload(self):
|
|
|
|
self.toolbox.setVisible(False)
|
2016-05-13 08:58:47 +02:00
|
|
|
self.iface.removeDockWidget(self.toolbox)
|
2017-02-10 13:35:58 +02:00
|
|
|
|
|
|
|
self.resultsDock.setVisible(False)
|
|
|
|
self.iface.removeDockWidget(self.resultsDock)
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
self.menu.deleteLater()
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
|
|
# delete temporary output files
|
2017-06-21 18:23:45 +10:00
|
|
|
folder = QgsProcessingUtils.tempFolder()
|
2012-12-20 10:42:44 +02:00
|
|
|
if QDir(folder).exists():
|
2013-03-24 15:27:43 +01:00
|
|
|
shutil.rmtree(folder, True)
|
2013-05-01 23:45:20 +02:00
|
|
|
|
2017-06-06 10:13:18 +03:00
|
|
|
# also delete temporary help files
|
|
|
|
folder = tempHelpFolder()
|
|
|
|
if QDir(folder).exists():
|
|
|
|
shutil.rmtree(folder, True)
|
|
|
|
|
2016-01-27 09:49:25 +02:00
|
|
|
self.iface.unregisterMainWindowAction(self.toolboxAction)
|
|
|
|
self.iface.unregisterMainWindowAction(self.modelerAction)
|
|
|
|
self.iface.unregisterMainWindowAction(self.historyAction)
|
|
|
|
self.iface.unregisterMainWindowAction(self.resultsAction)
|
2013-05-01 23:45:20 +02:00
|
|
|
|
2017-03-07 18:12:06 +10:00
|
|
|
self.iface.unregisterOptionsWidgetFactory(self.options_factory)
|
2017-05-11 11:49:07 +10:00
|
|
|
self.iface.deregisterLocatorFilter(self.locator_filter)
|
2017-09-15 13:29:21 +10:00
|
|
|
self.iface.unregisterCustomDropHandler(self.drop_handler)
|
2017-09-20 14:05:31 +10:00
|
|
|
QgsApplication.dataItemProviderRegistry().removeProvider(self.item_provider)
|
2017-03-07 18:12:06 +10:00
|
|
|
|
2016-02-03 07:54:42 +01:00
|
|
|
removeMenus()
|
2017-04-04 13:28:44 +10:00
|
|
|
Processing.deinitialize()
|
2016-02-03 07:54:42 +01:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def openToolbox(self):
|
2012-10-30 15:00:07 +02:00
|
|
|
if self.toolbox.isVisible():
|
|
|
|
self.toolbox.hide()
|
|
|
|
else:
|
|
|
|
self.toolbox.show()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def openModeler(self):
|
|
|
|
dlg = ModelerDialog()
|
2016-10-31 14:37:12 +10:00
|
|
|
dlg.update_model.connect(self.updateModel)
|
|
|
|
dlg.show()
|
|
|
|
|
|
|
|
def updateModel(self):
|
2017-04-04 11:28:28 +10:00
|
|
|
model_provider = QgsApplication.processingRegistry().providerById('model')
|
|
|
|
model_provider.refreshAlgorithms()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def openResults(self):
|
2017-02-10 13:35:58 +02:00
|
|
|
if self.resultsDock.isVisible():
|
|
|
|
self.resultsDock.hide()
|
|
|
|
else:
|
|
|
|
self.resultsDock.show()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def openHistory(self):
|
|
|
|
dlg = HistoryDialog()
|
|
|
|
dlg.exec_()
|
|
|
|
|
2015-02-03 11:22:34 +02:00
|
|
|
def tr(self, message):
|
2015-02-13 19:48:17 +01:00
|
|
|
return QCoreApplication.translate('ProcessingPlugin', message)
|