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

View File

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

View File

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

View File

@ -29,7 +29,16 @@
<property name="spacing"> <property name="spacing">
<number>2</number> <number>2</number>
</property> </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> <number>0</number>
</property> </property>
</layout> </layout>
@ -42,7 +51,16 @@
<property name="spacing"> <property name="spacing">
<number>2</number> <number>2</number>
</property> </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> <number>0</number>
</property> </property>
<item> <item>
@ -62,7 +80,16 @@
<property name="spacing"> <property name="spacing">
<number>0</number> <number>0</number>
</property> </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> <number>0</number>
</property> </property>
<item> <item>
@ -104,11 +131,25 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QProgressBar" name="progressBar"> <layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="value"> <property name="topMargin">
<number>0</number> <number>0</number>
</property> </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>
<item> <item>
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">

View File

@ -64,7 +64,7 @@ QString QgsProcessingAlgorithm::svgIconPath() const
QgsProcessingAlgorithm::Flags QgsProcessingAlgorithm::flags() const QgsProcessingAlgorithm::Flags QgsProcessingAlgorithm::flags() const
{ {
return FlagSupportsBatch; return FlagSupportsBatch | FlagCanCancel;
} }
bool QgsProcessingAlgorithm::canExecute( QString * ) const 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 FlagHideFromToolbox = 1 << 1, //!< Algorithm should be hidden from the toolbox
FlagHideFromModeler = 1 << 2, //!< Algorithm should be hidden from the modeler FlagHideFromModeler = 1 << 2, //!< Algorithm should be hidden from the modeler
FlagSupportsBatch = 1 << 3, //!< Algorithm supports batch mode FlagSupportsBatch = 1 << 3, //!< Algorithm supports batch mode
FlagCanCancel = 1 << 4, //!< Algorithm can be canceled
FlagDeprecated = FlagHideFromToolbox | FlagHideFromModeler, //!< Algorithm is deprecated FlagDeprecated = FlagHideFromToolbox | FlagHideFromModeler, //!< Algorithm is deprecated
}; };
Q_DECLARE_FLAGS( Flags, Flag ) 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. * 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; virtual Flags flags() const;