2014-11-12 11:42:29 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
AlgorithmDialogBase.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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import str
|
2014-11-12 11:42:29 +02:00
|
|
|
|
|
|
|
__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$'
|
|
|
|
|
2015-05-18 21:04:20 +03:00
|
|
|
import os
|
2016-01-08 08:27:22 +01:00
|
|
|
import webbrowser
|
2015-05-18 21:04:20 +03:00
|
|
|
|
2016-04-29 11:39:26 +02:00
|
|
|
from qgis.PyQt import uic
|
2017-03-03 20:39:17 +01:00
|
|
|
from qgis.PyQt.QtCore import QCoreApplication, QByteArray, QUrl
|
|
|
|
from qgis.PyQt.QtWidgets import QApplication, QDialogButtonBox
|
2016-05-27 19:04:25 +03:00
|
|
|
from qgis.PyQt.QtNetwork import QNetworkRequest, QNetworkReply
|
2014-11-12 11:42:29 +02:00
|
|
|
|
|
|
|
from qgis.utils import iface
|
2017-01-06 20:04:00 +10:00
|
|
|
from qgis.core import (QgsNetworkAccessManager,
|
|
|
|
QgsProject,
|
2017-03-03 20:39:17 +01:00
|
|
|
QgsProcessingFeedback,
|
|
|
|
QgsSettings)
|
2017-01-06 20:04:00 +10:00
|
|
|
|
2014-11-12 11:42:29 +02:00
|
|
|
|
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig
|
2015-05-18 21:04:20 +03:00
|
|
|
|
|
|
|
pluginPath = os.path.split(os.path.dirname(__file__))[0]
|
|
|
|
WIDGET, BASE = uic.loadUiType(
|
|
|
|
os.path.join(pluginPath, 'ui', 'DlgAlgorithmBase.ui'))
|
2014-11-12 11:42:29 +02:00
|
|
|
|
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
class AlgorithmDialogFeedback(QgsProcessingFeedback):
|
|
|
|
"""
|
|
|
|
Directs algorithm feedback to an algorithm dialog
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, dialog):
|
|
|
|
QgsProcessingFeedback.__init__(self)
|
|
|
|
self.dialog = dialog
|
|
|
|
|
|
|
|
def reportError(self, msg):
|
|
|
|
self.dialog.error(msg)
|
|
|
|
|
|
|
|
def setProgressText(self, text):
|
|
|
|
self.dialog.setText(text)
|
|
|
|
|
|
|
|
def setProgress(self, i):
|
|
|
|
self.dialog.setPercentage(i)
|
|
|
|
|
|
|
|
def pushInfo(self, msg):
|
|
|
|
self.dialog.setInfo(msg)
|
|
|
|
|
|
|
|
def pushCommandInfo(self, msg):
|
|
|
|
self.dialog.setCommand(msg)
|
|
|
|
|
|
|
|
def pushDebugInfo(self, msg):
|
|
|
|
self.dialog.setDebugInfo(msg)
|
|
|
|
|
|
|
|
def pushConsoleInfo(self, msg):
|
|
|
|
self.dialog.setConsoleInfo(msg)
|
|
|
|
|
|
|
|
|
2015-05-18 21:04:20 +03:00
|
|
|
class AlgorithmDialogBase(BASE, WIDGET):
|
2014-11-12 11:42:29 +02:00
|
|
|
|
|
|
|
def __init__(self, alg):
|
2015-05-18 21:04:20 +03:00
|
|
|
super(AlgorithmDialogBase, self).__init__(iface.mainWindow())
|
2014-11-12 11:42:29 +02:00
|
|
|
self.setupUi(self)
|
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
self.feedback = AlgorithmDialogFeedback(self)
|
|
|
|
|
2017-03-03 20:39:17 +01:00
|
|
|
self.settings = QgsSettings()
|
2015-09-11 09:33:51 +02:00
|
|
|
self.restoreGeometry(self.settings.value("/Processing/dialogBase", QByteArray()))
|
|
|
|
|
2014-11-12 11:42:29 +02:00
|
|
|
self.executed = False
|
|
|
|
self.mainWidget = None
|
|
|
|
self.alg = alg
|
|
|
|
|
|
|
|
# Rename OK button to Run
|
|
|
|
self.btnRun = self.buttonBox.button(QDialogButtonBox.Ok)
|
|
|
|
self.btnRun.setText(self.tr('Run'))
|
|
|
|
|
|
|
|
self.btnClose = self.buttonBox.button(QDialogButtonBox.Close)
|
|
|
|
|
2016-04-28 18:27:45 +02:00
|
|
|
self.setWindowTitle(self.alg.displayName())
|
2014-11-12 11:42:29 +02:00
|
|
|
|
2017-03-03 20:44:03 +01:00
|
|
|
# desktop = QDesktopWidget()
|
|
|
|
# if desktop.physicalDpiX() > 96:
|
|
|
|
# self.txtHelp.setZoomFactor(desktop.physicalDpiX() / 96)
|
2016-01-11 14:14:09 +02:00
|
|
|
|
2016-01-08 08:27:22 +01:00
|
|
|
algHelp = self.alg.shortHelp()
|
|
|
|
if algHelp is None:
|
|
|
|
self.textShortHelp.setVisible(False)
|
|
|
|
else:
|
|
|
|
self.textShortHelp.document().setDefaultStyleSheet('''.summary { margin-left: 10px; margin-right: 10px; }
|
|
|
|
h2 { color: #555555; padding-bottom: 15px; }
|
|
|
|
a { text-decoration: none; color: #3498db; font-weight: bold; }
|
|
|
|
p { color: #666666; }
|
|
|
|
b { color: #333333; }
|
|
|
|
dl dd { margin-bottom: 5px; }''')
|
|
|
|
self.textShortHelp.setHtml(algHelp)
|
|
|
|
|
|
|
|
self.textShortHelp.setOpenLinks(False)
|
2016-01-08 10:32:43 +02:00
|
|
|
|
2016-01-08 08:27:22 +01:00
|
|
|
def linkClicked(url):
|
|
|
|
webbrowser.open(url.toString())
|
|
|
|
|
2016-05-27 19:04:25 +03:00
|
|
|
self.textShortHelp.anchorClicked.connect(linkClicked)
|
2015-10-24 12:41:08 +02:00
|
|
|
|
2014-11-12 11:42:29 +02:00
|
|
|
isText, algHelp = self.alg.help()
|
|
|
|
if algHelp is not None:
|
|
|
|
algHelp = algHelp if isText else QUrl(algHelp)
|
2016-01-08 08:27:22 +01:00
|
|
|
try:
|
|
|
|
if isText:
|
2016-05-27 19:04:25 +03:00
|
|
|
self.txtHelp.setHtml(algHelp)
|
2016-01-08 08:27:22 +01:00
|
|
|
else:
|
2016-05-27 19:04:25 +03:00
|
|
|
html = self.tr('<p>Downloading algorithm help... Please wait.</p>')
|
|
|
|
self.txtHelp.setHtml(html)
|
|
|
|
rq = QNetworkRequest(algHelp)
|
|
|
|
self.reply = QgsNetworkAccessManager.instance().get(rq)
|
|
|
|
self.reply.finished.connect(self.requestFinished)
|
2017-03-03 20:44:03 +01:00
|
|
|
except Exception:
|
2016-01-08 08:27:22 +01:00
|
|
|
self.tabWidget.removeTab(2)
|
2014-11-12 11:42:29 +02:00
|
|
|
else:
|
2016-01-08 08:27:22 +01:00
|
|
|
self.tabWidget.removeTab(2)
|
2014-11-12 11:42:29 +02:00
|
|
|
|
|
|
|
self.showDebug = ProcessingConfig.getSetting(
|
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
|
|
|
ProcessingConfig.SHOW_DEBUG_IN_DIALOG)
|
2014-11-12 11:42:29 +02:00
|
|
|
|
2016-05-27 19:04:25 +03:00
|
|
|
def requestFinished(self):
|
|
|
|
"""Change the webview HTML content"""
|
|
|
|
reply = self.sender()
|
|
|
|
if reply.error() != QNetworkReply.NoError:
|
|
|
|
html = self.tr('<h2>No help available for this algorithm</h2><p>{}</p>'.format(reply.errorString()))
|
|
|
|
else:
|
2016-09-21 18:24:26 +02:00
|
|
|
html = str(reply.readAll())
|
2016-05-27 19:04:25 +03:00
|
|
|
reply.deleteLater()
|
|
|
|
self.txtHelp.setHtml(html)
|
|
|
|
|
2015-09-11 09:33:51 +02:00
|
|
|
def closeEvent(self, evt):
|
|
|
|
self.settings.setValue("/Processing/dialogBase", self.saveGeometry())
|
2016-01-11 16:38:10 +01:00
|
|
|
super(AlgorithmDialogBase, self).closeEvent(evt)
|
2015-09-11 09:33:51 +02:00
|
|
|
|
2016-09-14 09:51:17 +02:00
|
|
|
def setMainWidget(self, widget):
|
|
|
|
if self.mainWidget is not None:
|
2016-12-10 15:18:12 +08:00
|
|
|
QgsProject.instance().layerWasAdded.disconnect(self.mainWidget.layerRegistryChanged)
|
|
|
|
QgsProject.instance().layersWillBeRemoved.disconnect(self.mainWidget.layerRegistryChanged)
|
2016-09-14 09:51:17 +02:00
|
|
|
self.mainWidget = widget
|
2014-11-12 11:42:29 +02:00
|
|
|
self.tabWidget.widget(0).layout().addWidget(self.mainWidget)
|
2016-12-10 15:18:12 +08:00
|
|
|
QgsProject.instance().layerWasAdded.connect(self.mainWidget.layerRegistryChanged)
|
|
|
|
QgsProject.instance().layersWillBeRemoved.connect(self.mainWidget.layerRegistryChanged)
|
2016-09-14 09:51:17 +02:00
|
|
|
|
2014-11-12 11:42:29 +02:00
|
|
|
def error(self, msg):
|
|
|
|
QApplication.restoreOverrideCursor()
|
|
|
|
self.setInfo(msg, True)
|
|
|
|
self.resetGUI()
|
|
|
|
self.tabWidget.setCurrentIndex(1)
|
|
|
|
|
|
|
|
def resetGUI(self):
|
|
|
|
QApplication.restoreOverrideCursor()
|
|
|
|
self.lblProgress.setText('')
|
|
|
|
self.progressBar.setMaximum(100)
|
|
|
|
self.progressBar.setValue(0)
|
|
|
|
self.btnRun.setEnabled(True)
|
|
|
|
self.btnClose.setEnabled(True)
|
|
|
|
|
|
|
|
def setInfo(self, msg, error=False):
|
|
|
|
if error:
|
2015-09-29 17:50:53 +02:00
|
|
|
self.txtLog.append('<span style="color:red"><br>%s<br></span>' % msg)
|
2014-11-12 11:42:29 +02:00
|
|
|
else:
|
|
|
|
self.txtLog.append(msg)
|
|
|
|
QCoreApplication.processEvents()
|
|
|
|
|
|
|
|
def setCommand(self, cmd):
|
|
|
|
if self.showDebug:
|
|
|
|
self.setInfo('<code>%s<code>' % cmd)
|
|
|
|
QCoreApplication.processEvents()
|
|
|
|
|
|
|
|
def setDebugInfo(self, msg):
|
|
|
|
if self.showDebug:
|
|
|
|
self.setInfo('<span style="color:blue">%s</span>' % msg)
|
|
|
|
QCoreApplication.processEvents()
|
|
|
|
|
|
|
|
def setConsoleInfo(self, msg):
|
|
|
|
if self.showDebug:
|
|
|
|
self.setCommand('<span style="color:darkgray">%s</span>' % msg)
|
|
|
|
QCoreApplication.processEvents()
|
|
|
|
|
|
|
|
def setPercentage(self, value):
|
|
|
|
if self.progressBar.maximum() == 0:
|
|
|
|
self.progressBar.setMaximum(100)
|
|
|
|
self.progressBar.setValue(value)
|
|
|
|
QCoreApplication.processEvents()
|
|
|
|
|
|
|
|
def setText(self, text):
|
|
|
|
self.lblProgress.setText(text)
|
|
|
|
self.setInfo(text, False)
|
|
|
|
QCoreApplication.processEvents()
|
|
|
|
|
|
|
|
def setParamValues(self):
|
|
|
|
pass
|
|
|
|
|
2014-11-12 19:36:12 +02:00
|
|
|
def setParamValue(self, param, widget, alg=None):
|
2014-11-12 11:42:29 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def finish(self):
|
|
|
|
pass
|
2016-02-15 00:16:51 +01:00
|
|
|
|
|
|
|
class InvalidParameterValue(Exception):
|
|
|
|
|
|
|
|
def __init__(self, param, widget):
|
|
|
|
(self.parameter, self.widget) = (param, widget)
|