Skip to content

Commit

Permalink
Port algorithm countVisibleParameters to c++
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 14, 2017
1 parent 280ca31 commit c3c694f
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 13 deletions.
7 changes: 7 additions & 0 deletions python/core/processing/qgsprocessingalgorithm.sip
Expand Up @@ -128,6 +128,13 @@ class QgsProcessingAlgorithm
:rtype: QgsProcessingParameterDefinition
%End

int countVisibleParameters() const;
%Docstring
Returns the number of visible (non-hidden) parameters defined by this
algorithm.
:rtype: int
%End

virtual QVariantMap run( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
%Docstring
Expand Down
9 changes: 0 additions & 9 deletions python/plugins/processing/core/GeoAlgorithm.py
Expand Up @@ -430,15 +430,6 @@ def getVisibleOutputsCount(self):
i += 1
return i

def getVisibleParametersCount(self):
"""Returns the number of non-hidden parameters.
"""
i = 0
for param in self.parameters:
if not param.hidden:
i += 1
return i

def getHTMLOutputsCount(self):
"""Returns the number of HTML outputs.
"""
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/core/Processing.py
Expand Up @@ -170,7 +170,7 @@ def runAlgorithm(algOrName, onFinish, *args, **kwargs):
Processing.tr("Processing"))
return
else:
if len(args) != alg.getVisibleParametersCount() + alg.getVisibleOutputsCount():
if len(args) != alg.countVisibleParameters() + alg.getVisibleOutputsCount():
# fix_print_with_import
print('Error: Wrong number of parameters')
QgsMessageLog.logMessage(Processing.tr('Error: Wrong number of parameters'),
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/gui/BatchPanel.py
Expand Up @@ -102,7 +102,7 @@ def initWidgets(self):
nOutputs = 0

self.tblParameters.setColumnCount(
self.alg.getVisibleParametersCount() + nOutputs)
self.alg.countVisibleParameters() + nOutputs)

# Table headers
column = 0
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/gui/ProcessingToolbox.py
Expand Up @@ -275,7 +275,7 @@ def executeAlgorithm(self):
alg = alg.getCopy()
alg.setProvider(provider)

if (alg.getVisibleParametersCount() + alg.getVisibleOutputsCount()) > 0:
if (alg.countVisibleParameters() + alg.getVisibleOutputsCount()) > 0:
dlg = alg.getCustomParametersDialog()
if not dlg:
dlg = AlgorithmDialog(alg)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/gui/menus.py
Expand Up @@ -210,7 +210,7 @@ def _executeAlgorithm(alg):
alg.setProvider(provider)

context = dataobjects.createContext()
if (alg.getVisibleParametersCount() + alg.getVisibleOutputsCount()) > 0:
if (alg.countVisibleParameters() + alg.getVisibleOutputsCount()) > 0:
dlg = alg.getCustomParametersDialog()
if not dlg:
dlg = AlgorithmDialog(alg)
Expand Down
11 changes: 11 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -87,6 +87,17 @@ const QgsProcessingParameterDefinition *QgsProcessingAlgorithm::parameterDefinit
return nullptr;
}

int QgsProcessingAlgorithm::countVisibleParameters() const
{
int count = 0;
Q_FOREACH ( const QgsProcessingParameterDefinition *def, mParameters )
{
if ( !( def->flags() & QgsProcessingParameterDefinition::FlagHidden ) )
count++;
}
return count;
}

QString QgsProcessingAlgorithm::parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
{
return QgsProcessingParameters::parameterAsString( parameterDefinition( name ), parameters, name, context );
Expand Down
6 changes: 6 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.h
Expand Up @@ -142,6 +142,12 @@ class CORE_EXPORT QgsProcessingAlgorithm
*/
const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const;

/**
* Returns the number of visible (non-hidden) parameters defined by this
* algorithm.
*/
int countVisibleParameters() const;

/**
* Runs the algorithm using the specified \a parameters. Algorithms should implement
* their custom processing logic here.
Expand Down
9 changes: 9 additions & 0 deletions tests/src/core/testqgsprocessing.cpp
Expand Up @@ -62,6 +62,15 @@ class DummyAlgorithm : public QgsProcessingAlgorithm
// parameterDefinition should be case insensitive
QCOMPARE( parameterDefinition( "P1" ), parameterDefinitions().at( 0 ) );
QVERIFY( !parameterDefinition( "invalid" ) );

QCOMPARE( countVisibleParameters(), 1 );
QgsProcessingParameterBoolean *p3 = new QgsProcessingParameterBoolean( "p3" );
QVERIFY( addParameter( p3 ) );
QCOMPARE( countVisibleParameters(), 2 );
QgsProcessingParameterBoolean *p4 = new QgsProcessingParameterBoolean( "p4" );
p4->setFlags( QgsProcessingParameterDefinition::FlagHidden );
QVERIFY( addParameter( p4 ) );
QCOMPARE( countVisibleParameters(), 2 );
}

};
Expand Down

0 comments on commit c3c694f

Please sign in to comment.