Skip to content

Commit

Permalink
Add a cancel button for algorithms which support cancelation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 6, 2017
1 parent 39d20a4 commit ab64428
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
2 changes: 2 additions & 0 deletions python/core/processing/qgsprocessingalgorithm.sip
Expand Up @@ -28,6 +28,7 @@ class QgsProcessingAlgorithm
FlagHideFromToolbox,
FlagHideFromModeler,
FlagSupportsBatch,
FlagCanCancel,
FlagDeprecated,
};
typedef QFlags<QgsProcessingAlgorithm::Flag> Flags;
Expand Down Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion python/plugins/processing/gui/AlgorithmDialog.py
Expand Up @@ -36,7 +36,8 @@
QgsProcessingParameterDefinition,
QgsProcessingOutputVectorLayer,
QgsProcessingFeatureSinkDefinition,
QgsProcessingParameterFeatureSink)
QgsProcessingParameterFeatureSink,
QgsProcessingAlgorithm)
from qgis.gui import QgsMessageBar
from qgis.utils import iface

Expand Down Expand Up @@ -229,7 +230,9 @@ def accept(self):
#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:
Expand Down
3 changes: 3 additions & 0 deletions python/plugins/processing/gui/AlgorithmDialogBase.py
Expand Up @@ -84,6 +84,7 @@ def __init__(self, alg):

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()))
Expand All @@ -96,6 +97,8 @@ def __init__(self, alg):
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())
Expand Down
53 changes: 47 additions & 6 deletions python/plugins/processing/ui/DlgAlgorithmBase.ui
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -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">
Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -64,7 +64,7 @@ QString QgsProcessingAlgorithm::svgIconPath() const

QgsProcessingAlgorithm::Flags QgsProcessingAlgorithm::flags() const
{
return FlagSupportsBatch;
return FlagSupportsBatch | FlagCanCancel;
}

bool QgsProcessingAlgorithm::canExecute( QString * ) const
Expand Down
2 changes: 2 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.h
Expand Up @@ -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 )
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit ab64428

Please sign in to comment.