Skip to content

Commit

Permalink
Fix context going out of scope
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 23, 2018
1 parent 6fff62e commit 2617fd5
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions python/plugins/processing/gui/AlgorithmDialog.py
Expand Up @@ -73,6 +73,9 @@ def __init__(self, alg, in_place=False, parent=None):
self.in_place = in_place
self.active_layer = None

self.context = None
self.feedback = None

self.setAlgorithm(alg)
self.setMainWidget(self.getParametersPanel(alg, self))

Expand Down Expand Up @@ -160,14 +163,14 @@ def getParameterValues(self):
return self.algorithm().preprocessParameters(parameters)

def runAlgorithm(self):
feedback = self.createFeedback()
context = dataobjects.createContext(feedback)
self.feedback = self.createFeedback()
self.context = dataobjects.createContext(self.feedback)

checkCRS = ProcessingConfig.getSetting(ProcessingConfig.WARN_UNMATCHING_CRS)
try:
parameters = self.getParameterValues()

if checkCRS and not self.algorithm().validateInputCrs(parameters, context):
if checkCRS and not self.algorithm().validateInputCrs(parameters, self.context):
reply = QMessageBox.question(self, self.tr("Unmatching CRS's"),
self.tr('Parameters do not all use the same CRS. This can '
'cause unexpected results.\nDo you want to '
Expand All @@ -176,7 +179,7 @@ def runAlgorithm(self):
QMessageBox.No)
if reply == QMessageBox.No:
return
ok, msg = self.algorithm().checkParameterValues(parameters, context)
ok, msg = self.algorithm().checkParameterValues(parameters, self.context)
if not ok:
QMessageBox.warning(
self, self.tr('Unable to execute algorithm'), msg)
Expand All @@ -198,12 +201,12 @@ def runAlgorithm(self):
self.setInfo(
QCoreApplication.translate('AlgorithmDialog', '<b>Algorithm \'{0}\' starting&hellip;</b>').format(self.algorithm().displayName()), escapeHtml=False)

feedback.pushInfo(self.tr('Input parameters:'))
self.feedback.pushInfo(self.tr('Input parameters:'))
display_params = []
for k, v in parameters.items():
display_params.append("'" + k + "' : " + self.algorithm().parameterDefinition(k).valueAsPythonString(v, context))
feedback.pushCommandInfo('{ ' + ', '.join(display_params) + ' }')
feedback.pushInfo('')
display_params.append("'" + k + "' : " + self.algorithm().parameterDefinition(k).valueAsPythonString(v, self.context))
self.feedback.pushCommandInfo('{ ' + ', '.join(display_params) + ' }')
self.feedback.pushInfo('')
start_time = time.time()

if self.iterateParam:
Expand All @@ -215,30 +218,30 @@ def runAlgorithm(self):
pass

self.cancelButton().setEnabled(self.algorithm().flags() & QgsProcessingAlgorithm.FlagCanCancel)
if executeIterating(self.algorithm(), parameters, self.iterateParam, context, feedback):
feedback.pushInfo(
if executeIterating(self.algorithm(), parameters, self.iterateParam, self.context, self.feedback):
self.feedback.pushInfo(
self.tr('Execution completed in {0:0.2f} seconds').format(time.time() - start_time))
self.cancelButton().setEnabled(False)
self.finish(True, parameters, context, feedback)
self.finish(True, parameters, self.context, self.feedback)
else:
self.cancelButton().setEnabled(False)
self.resetGui()
else:
command = self.algorithm().asPythonCommand(parameters, context)
command = self.algorithm().asPythonCommand(parameters, self.context)
if command:
ProcessingLog.addToLog(command)
QgsGui.instance().processingRecentAlgorithmLog().push(self.algorithm().id())
self.cancelButton().setEnabled(self.algorithm().flags() & QgsProcessingAlgorithm.FlagCanCancel)

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))
self.feedback.pushInfo(self.tr('Execution completed in {0:0.2f} seconds').format(time.time() - start_time))
self.feedback.pushInfo(self.tr('Results:'))
self.feedback.pushCommandInfo(pformat(results))
else:
feedback.reportError(
self.feedback.reportError(
self.tr('Execution failed after {0:0.2f} seconds').format(time.time() - start_time))
feedback.pushInfo('')
self.feedback.pushInfo('')

if self.feedback_dialog is not None:
self.feedback_dialog.close()
Expand All @@ -248,28 +251,31 @@ def on_complete(ok, results):
self.cancelButton().setEnabled(False)

if not self.in_place:
self.finish(ok, results, context, feedback)
self.finish(ok, results, self.context, self.feedback)
elif ok:
self.close()

self.feedback = None
self.context = None

if not self.in_place and not (self.algorithm().flags() & QgsProcessingAlgorithm.FlagNoThreading):
# Make sure the Log tab is visible before executing the algorithm
self.showLog()

task = QgsProcessingAlgRunnerTask(self.algorithm(), parameters, context, feedback)
task = QgsProcessingAlgRunnerTask(self.algorithm(), parameters, self.context, self.feedback)
task.executed.connect(on_complete)
self.setCurrentTask(task)
else:
self.proxy_progress = QgsProxyProgressTask(QCoreApplication.translate("AlgorithmDialog", "Executing “{}”").format(self.algorithm().displayName()))
QgsApplication.taskManager().addTask(self.proxy_progress)
feedback.progressChanged.connect(self.proxy_progress.setProxyProgress)
self.feedback.progressChanged.connect(self.proxy_progress.setProxyProgress)
self.feedback_dialog = self.createProgressDialog()
self.feedback_dialog.show()
if self.in_place:
ok, results = execute_in_place(self.algorithm(), parameters, context, feedback)
ok, results = execute_in_place(self.algorithm(), parameters, self.context, self.feedback)
else:
ok, results = execute(self.algorithm(), parameters, context, feedback)
feedback.progressChanged.disconnect()
ok, results = execute(self.algorithm(), parameters, self.context, self.feedback)
self.feedback.progressChanged.disconnect()
self.proxy_progress.finalize(ok)
on_complete(ok, results)

Expand Down

0 comments on commit 2617fd5

Please sign in to comment.