Skip to content

Commit

Permalink
Move execution check to QgsProcessingAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent 03e40f7 commit fac8ca4
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 24 deletions.
9 changes: 9 additions & 0 deletions python/core/processing/qgsprocessingalgorithm.sip
Expand Up @@ -104,6 +104,15 @@ class QgsProcessingAlgorithm
:rtype: Flags
%End

virtual bool canExecute( QString *errorMessage /Out/ = 0 ) const;
%Docstring
Returns true if the algorithm can execute. Algorithm subclasses can return false
here to indicate that they are not able to execute, e.g. as a result of unmet
external dependencies. If specified, the ``errorMessage`` argument will be filled
with a localised error message describing why the algorithm cannot execute.
:rtype: bool
%End

QgsProcessingProvider *provider() const;
%Docstring
Returns the provider to which this algorithm belongs.
Expand Down
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/grass7/Grass7Algorithm.py
Expand Up @@ -572,8 +572,9 @@ def exportRasterLayer(self, layer):
def getTempFilename(self):
return system.getTempFilename()

def checkBeforeOpeningParametersDialog(self):
return Grass7Utils.checkGrass7IsInstalled()
def canExecute(self):
message = Grass7Utils.checkGrass7IsInstalled()
return not message, message

def checkParameterValuesBeforeExecuting(self):
if self.module:
Expand Down
14 changes: 0 additions & 14 deletions python/plugins/processing/core/GeoAlgorithm.py
Expand Up @@ -130,20 +130,6 @@ def getParameterDescriptions(self):
descs = {}
return descs

def checkBeforeOpeningParametersDialog(self):
"""If there is any check to perform before the parameters
dialog is opened, it should be done here.
This method returns an error message string if there is any
problem (for instance, an external app not configured yet),
or None if the parameters dialog can be opened.
Note that this check should also be done in the
processAlgorithm method, since algorithms can be called without
opening the parameters dialog.
"""
return None

def checkParameterValuesBeforeExecuting(self):
"""If there is any check to do before launching the execution
of the algorithm, it should be done here.
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/gui/ProcessingToolbox.py
Expand Up @@ -255,8 +255,8 @@ def executeAlgorithm(self):
if isinstance(item, TreeAlgorithmItem):
context = dataobjects.createContext()
alg = QgsApplication.processingRegistry().algorithmById(item.alg.id())
message = alg.checkBeforeOpeningParametersDialog()
if message:
ok, message = alg.canExecute()
if not ok:
dlg = MessageDialog()
dlg.setTitle(self.tr('Error executing algorithm'))
dlg.setMessage(
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/gui/menus.py
Expand Up @@ -193,8 +193,8 @@ def removeAlgorithmEntry(alg, menuName, submenuName, actionText=None, delButton=


def _executeAlgorithm(alg):
message = alg.checkBeforeOpeningParametersDialog()
if message:
ok, message = alg.canExecute()
if not ok:
dlg = MessageDialog()
dlg.setTitle(Processing.tr('Missing dependency'))
dlg.setMessage(
Expand Down
5 changes: 3 additions & 2 deletions python/plugins/processing/modeler/ModelerAlgorithm.py
Expand Up @@ -525,11 +525,12 @@ def getAsCommand(self):
else:
return None

def checkBeforeOpeningParametersDialog(self):
def canExecute(self):
for alg in list(self.algs.values()):
algInstance = QgsApplication.processingRegistry().algorithmById(alg.consoleName)
if algInstance is None:
return self.tr("The model you are trying to run contains an algorithm that is not available: <i>{0}</i>").format(alg.consoleName)
return False, self.tr("The model you are trying to run contains an algorithm that is not available: <i>{0}</i>").format(alg.consoleName)
return True, None

def setModelerView(self, dialog):
self.modelerdialog = dialog
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/script/ScriptAlgorithm.py
Expand Up @@ -122,8 +122,8 @@ def defineCharacteristicsFromScript(self):
except:
pass

def checkBeforeOpeningParametersDialog(self):
return self.error
def canExecute(self):
return not self.error, self.error

def checkInputCRS(self):
if self.noCRSWarning:
Expand Down
5 changes: 5 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -49,6 +49,11 @@ QgsProcessingAlgorithm::Flags QgsProcessingAlgorithm::flags() const
return FlagSupportsBatch;
}

bool QgsProcessingAlgorithm::canExecute( QString * ) const
{
return true;
}

QgsProcessingProvider *QgsProcessingAlgorithm::provider() const
{
return mProvider;
Expand Down
8 changes: 8 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.h
Expand Up @@ -117,6 +117,14 @@ class CORE_EXPORT QgsProcessingAlgorithm
*/
virtual Flags flags() const;

/**
* Returns true if the algorithm can execute. Algorithm subclasses can return false
* here to indicate that they are not able to execute, e.g. as a result of unmet
* external dependencies. If specified, the \a errorMessage argument will be filled
* with a localised error message describing why the algorithm cannot execute.
*/
virtual bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const;

/**
* Returns the provider to which this algorithm belongs.
*/
Expand Down

0 comments on commit fac8ca4

Please sign in to comment.