Skip to content
Permalink
Browse files

Port algorithm countVisibleParameters to c++

  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent 2da85f8 commit b64a71df615fe688d3888b67b49f2edbe8e5aa03
@@ -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
@@ -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.
"""
@@ -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'),
@@ -102,7 +102,7 @@ def initWidgets(self):
nOutputs = 0

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

# Table headers
column = 0
@@ -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)
@@ -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)
@@ -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 );
@@ -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.
@@ -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 );
}

};

0 comments on commit b64a71d

Please sign in to comment.
You can’t perform that action at this time.