Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make algorithm dialog use background tasks to execute algorithms
  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent 8cfcf57 commit 1b2086e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
30 changes: 19 additions & 11 deletions python/plugins/processing/gui/AlgorithmDialog.py
Expand Up @@ -34,11 +34,13 @@
from qgis.PyQt.QtGui import QCursor, QColor, QPalette

from qgis.core import (QgsProject,
QgsApplication,
QgsProcessingUtils,
QgsMessageLog,
QgsProcessingParameterDefinition,
QgsProcessingOutputRasterLayer,
QgsProcessingOutputVectorLayer,
QgsProcessingAlgRunnerTask,
QgsProcessingOutputHtml,
QgsProcessingParameterVectorOutput,
QgsProcessingOutputLayerDefinition,
Expand Down Expand Up @@ -244,18 +246,24 @@ def accept(self):
if command:
ProcessingLog.addToLog(command)
self.buttonCancel.setEnabled(self.alg.flags() & QgsProcessingAlgorithm.FlagCanCancel)
result, ok = executeAlgorithm(self.alg, parameters, context, feedback)
if ok:
feedback.pushInfo(self.tr('Execution completed in {0:0.2f} seconds'.format(time.time() - start_time)))
feedback.pushInfo(self.tr('Results:'))
feedback.pushCommandInfo(pformat(result))
else:
feedback.reportError(
self.tr('Execution failed after {0:0.2f} seconds'.format(time.time() - start_time)))
feedback.pushInfo('')

self.buttonCancel.setEnabled(False)
self.finish(result, context, feedback)
def on_complete(ok, results):
if ok:
feedback.pushInfo(self.tr('Execution completed in {0:0.2f} seconds'.format(time.time() - start_time)))
feedback.pushInfo(self.tr('Results:'))
feedback.pushCommandInfo(pformat(results))
else:
feedback.reportError(
self.tr('Execution failed after {0:0.2f} seconds'.format(time.time() - start_time)))
feedback.pushInfo('')

self.buttonCancel.setEnabled(False)
self.finish(results, context, feedback)

task = QgsProcessingAlgRunnerTask(self.alg, parameters, context, feedback)
task.executed.connect(on_complete)
QgsApplication.taskManager().addTask(task)

except AlgorithmDialogBase.InvalidParameterValue as e:
try:
self.buttonBox.accepted.connect(lambda e=e:
Expand Down
28 changes: 21 additions & 7 deletions python/plugins/processing/gui/AlgorithmDialogBase.py
Expand Up @@ -31,7 +31,7 @@
import html

from qgis.PyQt import uic
from qgis.PyQt.QtCore import Qt, QCoreApplication, QByteArray, QUrl
from qgis.PyQt.QtCore import pyqtSignal, Qt, QCoreApplication, QByteArray, QUrl
from qgis.PyQt.QtWidgets import QApplication, QDialogButtonBox, QVBoxLayout, QToolButton

from qgis.utils import iface
Expand All @@ -52,27 +52,34 @@ class AlgorithmDialogFeedback(QgsProcessingFeedback):
Directs algorithm feedback to an algorithm dialog
"""

error = pyqtSignal(str)
progress_text = pyqtSignal(str)
info = pyqtSignal(str)
command_info = pyqtSignal(str)
debug_info = pyqtSignal(str)
console_info = pyqtSignal(str)

def __init__(self, dialog):
QgsProcessingFeedback.__init__(self)
self.dialog = dialog

def reportError(self, msg):
self.dialog.error(msg)
self.error.emit(msg)

def setProgressText(self, text):
self.dialog.setText(text)
self.progress_text.emit(text)

def pushInfo(self, msg):
self.dialog.setInfo(msg)
self.info.emit(msg)

def pushCommandInfo(self, msg):
self.dialog.setCommand(msg)
self.command_info.emit(msg)

def pushDebugInfo(self, msg):
self.dialog.setDebugInfo(msg)
self.debug_info.emit(msg)

def pushConsoleInfo(self, msg):
self.dialog.setConsoleInfo(msg)
self.console_info.emit(msg)


class AlgorithmDialogBase(BASE, WIDGET):
Expand Down Expand Up @@ -151,6 +158,13 @@ def linkClicked(url):
def createFeedback(self):
feedback = AlgorithmDialogFeedback(self)
feedback.progressChanged.connect(self.setPercentage)
feedback.error.connect(self.error)
feedback.progress_text.connect(self.setText)
feedback.info.connect(self.setInfo)
feedback.command_info.connect(self.setCommand)
feedback.debug_info.connect(self.setDebugInfo)
feedback.console_info.connect(self.setConsoleInfo)

self.buttonCancel.clicked.connect(feedback.cancel)
return feedback

Expand Down

0 comments on commit 1b2086e

Please sign in to comment.