Add a cancel button for algorithms which support cancelation

This commit is contained in:
Nyall Dawson 2017-06-06 11:40:33 +10:00
parent 39d20a4cb4
commit ab64428891
6 changed files with 59 additions and 8 deletions

View File

@ -28,6 +28,7 @@ class QgsProcessingAlgorithm
FlagHideFromToolbox,
FlagHideFromModeler,
FlagSupportsBatch,
FlagCanCancel,
FlagDeprecated,
};
typedef QFlags<QgsProcessingAlgorithm::Flag> Flags;
@ -128,6 +129,7 @@ class QgsProcessingAlgorithm
virtual Flags flags() const;
%Docstring
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Default flags are FlagSupportsBatch and FlagCanCancel.
:rtype: Flags
%End

View File

@ -36,7 +36,8 @@ from qgis.core import (QgsProject,
QgsProcessingParameterDefinition,
QgsProcessingOutputVectorLayer,
QgsProcessingFeatureSinkDefinition,
QgsProcessingParameterFeatureSink)
QgsProcessingParameterFeatureSink,
QgsProcessingAlgorithm)
from qgis.gui import QgsMessageBar
from qgis.utils import iface
@ -229,7 +230,9 @@ class AlgorithmDialog(AlgorithmDialogBase):
#command = self.alg.getAsCommand()
#if command:
# ProcessingLog.addToLog(command)
self.buttonCancel.setEnabled(self.alg.flags() & QgsProcessingAlgorithm.FlagCanCancel)
result = executeAlgorithm(self.alg, parameters, context, self.feedback)
self.buttonCancel.setEnabled(False)
self.finish(result, context)
#TODO
#else:

View File

@ -84,6 +84,7 @@ class AlgorithmDialogBase(BASE, WIDGET):
self.feedback = AlgorithmDialogFeedback(self)
self.feedback.progressChanged.connect(self.setPercentage)
self.buttonCancel.clicked.connect(self.feedback.cancel)
self.settings = QgsSettings()
self.restoreGeometry(self.settings.value("/Processing/dialogBase", QByteArray()))
@ -96,6 +97,8 @@ class AlgorithmDialogBase(BASE, WIDGET):
self.btnRun = self.buttonBox.button(QDialogButtonBox.Ok)
self.btnRun.setText(self.tr('Run'))
self.buttonCancel.setEnabled(False)
self.btnClose = self.buttonBox.button(QDialogButtonBox.Close)
self.setWindowTitle(self.alg.displayName())

View File

@ -29,7 +29,16 @@
<property name="spacing">
<number>2</number>
</property>
<property name="margin">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
</layout>
@ -42,7 +51,16 @@
<property name="spacing">
<number>2</number>
</property>
<property name="margin">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
@ -62,7 +80,16 @@
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
@ -104,11 +131,25 @@
</widget>
</item>
<item>
<widget class="QProgressBar" name="progressBar">
<property name="value">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="topMargin">
<number>0</number>
</property>
</widget>
<item>
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>0</number>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonCancel">
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">

View File

@ -64,7 +64,7 @@ QString QgsProcessingAlgorithm::svgIconPath() const
QgsProcessingAlgorithm::Flags QgsProcessingAlgorithm::flags() const
{
return FlagSupportsBatch;
return FlagSupportsBatch | FlagCanCancel;
}
bool QgsProcessingAlgorithm::canExecute( QString * ) const

View File

@ -48,6 +48,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
FlagHideFromToolbox = 1 << 1, //!< Algorithm should be hidden from the toolbox
FlagHideFromModeler = 1 << 2, //!< Algorithm should be hidden from the modeler
FlagSupportsBatch = 1 << 3, //!< Algorithm supports batch mode
FlagCanCancel = 1 << 4, //!< Algorithm can be canceled
FlagDeprecated = FlagHideFromToolbox | FlagHideFromModeler, //!< Algorithm is deprecated
};
Q_DECLARE_FLAGS( Flags, Flag )
@ -140,6 +141,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
/**
* Returns the flags indicating how and when the algorithm operates and should be exposed to users.
* Default flags are FlagSupportsBatch and FlagCanCancel.
*/
virtual Flags flags() const;