Add framework for algorithm outputs

This somewhat changes the meaning of outputs from processing 2.x.
In 2.x processing outputs were used both as a method of specifying
inputs to algorithms (file paths to destination layers created
by the algorithm) AND pure outputs (such as statistics calculated
by the algorithm).

This is now split. The old input-type-outputs (destination layers)
are now input parameters (since the parameter value IS an input to the
algorithm). To differentiate them from parameters indicating pure
input layers a new "isDestination()" method was added to
QgsProcessingParameterDefinition.

Output definitions are now purely indications of values CREATED
by the algorithms. Suitable candidates are the existing calculated
stats and actual file path/URI of any layers created by the algorithm.
Moving forward we should ensure all algorithms output as much
useful information as possible - e.g. number of features processed,
number of skipped features, count null geometries encountered, etc...
This commit is contained in:
Nyall Dawson 2017-05-15 12:29:44 +10:00
parent fac8ca4d4f
commit fb811766f8
20 changed files with 600 additions and 44 deletions

View File

@ -287,6 +287,7 @@
%Include processing/qgsprocessingalgorithm.sip
%Include processing/qgsprocessingcontext.sip
%Include processing/qgsprocessingfeedback.sip
%Include processing/qgsprocessingoutputs.sip
%Include processing/qgsprocessingparameters.sip
%Include processing/qgsprocessingprovider.sip
%Include processing/qgsprocessingregistry.sip

View File

@ -124,6 +124,7 @@ class QgsProcessingAlgorithm
Returns an ordered list of parameter definitions utilized by the algorithm.
.. seealso:: addParameter()
.. seealso:: parameterDefinition()
.. seealso:: destinationParameterDefinitions()
:rtype: QgsProcessingParameterDefinitions
%End
@ -142,6 +143,30 @@ class QgsProcessingAlgorithm
:rtype: int
%End
QgsProcessingParameterDefinitions destinationParameterDefinitions() const;
%Docstring
Returns a list of destination parameters definitions utilized by the algorithm.
.. seealso:: QgsProcessingParameterDefinition.isDestination()
.. seealso:: parameterDefinitions()
:rtype: QgsProcessingParameterDefinitions
%End
QgsProcessingOutputDefinitions outputDefinitions() const;
%Docstring
Returns an ordered list of output definitions utilized by the algorithm.
.. seealso:: addOutput()
.. seealso:: outputDefinition()
:rtype: QgsProcessingOutputDefinitions
%End
const QgsProcessingOutputDefinition *outputDefinition( const QString &name ) const;
%Docstring
Returns a matching output by ``name``. Matching is done in a case-insensitive
manner.
.. seealso:: outputDefinitions()
:rtype: QgsProcessingOutputDefinition
%End
virtual QVariantMap run( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
%Docstring
@ -166,6 +191,16 @@ class QgsProcessingAlgorithm
Adds a parameter ``definition`` to the algorithm. Ownership of the definition is transferred to the algorithm.
Returns true if parameter could be successfully added, or false if the parameter could not be added (e.g.
as a result of a duplicate name).
.. seealso:: addOutput()
:rtype: bool
%End
bool addOutput( QgsProcessingOutputDefinition *outputDefinition /Transfer/ );
%Docstring
Adds an output ``definition`` to the algorithm. Ownership of the definition is transferred to the algorithm.
Returns true if the output could be successfully added, or false if the output could not be added (e.g.
as a result of a duplicate name).
.. seealso:: addParameter()
:rtype: bool
%End

View File

@ -0,0 +1,125 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/processing/qgsprocessingoutputs.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
class QgsProcessingOutputDefinition
{
%Docstring
Base class for the definition of processing outputs.
Output definitions encapsulate the properties regarding the outputs from algorithms, such
as generated layers or calculated values.
.. versionadded:: 3.0
%End
%TypeHeaderCode
#include "qgsprocessingoutputs.h"
%End
public:
QgsProcessingOutputDefinition( const QString &name, const QString &description = QString() );
%Docstring
Constructor for QgsProcessingOutputDefinition.
%End
virtual ~QgsProcessingOutputDefinition();
virtual QString type() const = 0;
%Docstring
Unique output type name.
:rtype: str
%End
QString name() const;
%Docstring
Returns the name of the output. This is the internal identifier by which
algorithms access this output.
@see setName()
:rtype: str
%End
void setName( const QString &name );
%Docstring
Sets the ``name`` of the output. This is the internal identifier by which
algorithms access this output.
@see name()
%End
QString description() const;
%Docstring
Returns the description for the output. This is the user-visible string
used to identify this output.
@see setDescription()
:rtype: str
%End
void setDescription( const QString &description );
%Docstring
Sets the ``description`` for the output. This is the user-visible string
used to identify this output.
@see description()
%End
protected:
};
typedef QList< const QgsProcessingOutputDefinition * > QgsProcessingOutputDefinitions;
class QgsProcessingOutputVectorLayer : QgsProcessingOutputDefinition
{
%Docstring
A vector layer output for processing algorithms.
.. versionadded:: 3.0
%End
%TypeHeaderCode
#include "qgsprocessingoutputs.h"
%End
public:
QgsProcessingOutputVectorLayer( const QString &name, const QString &description = QString(), QgsProcessingParameterDefinition::LayerType type = QgsProcessingParameterDefinition::TypeVectorAny );
%Docstring
Constructor for QgsProcessingOutputVectorLayer.
%End
virtual QString type() const;
QgsProcessingParameterDefinition::LayerType dataType() const;
%Docstring
Returns the layer type for the output layer.
.. seealso:: setDataType()
:rtype: QgsProcessingParameterDefinition.LayerType
%End
void setDataType( QgsProcessingParameterDefinition::LayerType type );
%Docstring
Sets the layer ``type`` for the output layer.
.. seealso:: dataType()
%End
};
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/processing/qgsprocessingoutputs.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/

View File

@ -64,6 +64,14 @@ class QgsProcessingParameterDefinition
:rtype: str
%End
virtual bool isDestination() const;
%Docstring
Returns true if this parameter represents a file or layer destination, e.g. parameters
which are used for the destination for layers output by an algorithm will return
true.
:rtype: bool
%End
QString name() const;
%Docstring
Returns the name of the parameter. This is the internal identifier by which
@ -906,7 +914,7 @@ class QgsProcessingParameterTableField : QgsProcessingParameterDefinition
};
class QgsProcessingParameterVector : QgsProcessingParameterDefinition
class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition
{
%Docstring
A vector layer parameter for processing algorithms.
@ -918,10 +926,10 @@ class QgsProcessingParameterVector : QgsProcessingParameterDefinition
%End
public:
QgsProcessingParameterVector( const QString &name, const QString &description = QString(), QgsProcessingParameterDefinition::LayerType type = QgsProcessingParameterDefinition::TypeVectorAny, const QVariant &defaultValue = QVariant(),
bool optional = false );
QgsProcessingParameterVectorLayer( const QString &name, const QString &description = QString(), QgsProcessingParameterDefinition::LayerType type = QgsProcessingParameterDefinition::TypeVectorAny, const QVariant &defaultValue = QVariant(),
bool optional = false );
%Docstring
Constructor for QgsProcessingParameterVector.
Constructor for QgsProcessingParameterVectorLayer.
%End
virtual QString type() const;
@ -943,6 +951,45 @@ class QgsProcessingParameterVector : QgsProcessingParameterDefinition
};
class QgsProcessingParameterOutputVectorLayer : QgsProcessingParameterDefinition
{
%Docstring
A vector layer output for processing algorithms.
A parameter which represents the destination for a vector layer created by an algorithm.
.. versionadded:: 3.0
%End
%TypeHeaderCode
#include "qgsprocessingparameters.h"
%End
public:
QgsProcessingParameterOutputVectorLayer( const QString &name, const QString &description = QString(), QgsProcessingParameterDefinition::LayerType type = QgsProcessingParameterDefinition::TypeVectorAny, const QVariant &defaultValue = QVariant(),
bool optional = false );
%Docstring
Constructor for QgsProcessingParameterOutputVectorLayer.
%End
virtual QString type() const;
virtual bool isDestination() const;
QgsProcessingParameterDefinition::LayerType dataType() const;
%Docstring
Returns the layer type for the output layer associated with the parameter.
.. seealso:: setDataType()
:rtype: QgsProcessingParameterDefinition.LayerType
%End
void setDataType( QgsProcessingParameterDefinition::LayerType type );
%Docstring
Sets the layer ``type`` for the output layer associated with the parameter.
.. seealso:: dataType()
%End
};
/************************************************************************
* This file has been generated automatically from *

View File

@ -398,15 +398,6 @@ class GeoAlgorithm(QgsProcessingAlgorithm):
if out.name == outputName:
out.setValue(value)
def getVisibleOutputsCount(self):
"""Returns the number of non-hidden outputs.
"""
i = 0
for out in self.outputs:
if not out.hidden:
i += 1
return i
def getHTMLOutputsCount(self):
"""Returns the number of HTML outputs.
"""

View File

@ -165,7 +165,7 @@ class Processing(object):
Processing.tr("Processing"))
return
else:
if len(args) != alg.countVisibleParameters() + alg.getVisibleOutputsCount():
if len(args) != alg.countVisibleParameters():
# fix_print_with_import
print('Error: Wrong number of parameters')
QgsMessageLog.logMessage(Processing.tr('Error: Wrong number of parameters'),

View File

@ -74,8 +74,8 @@ class BatchAlgorithmDialog(AlgorithmDialogBase):
for row in range(self.mainWidget.tblParameters.rowCount()):
alg = self.alg
col = 0
for param in alg.parameters:
if param.hidden:
for param in alg.parameterDefinitions():
if param.hidden or param.isDestination():
continue
wrapper = self.mainWidget.wrappers[row][col]
if not self.mainWidget.setParamValue(param, wrapper, alg):
@ -85,7 +85,7 @@ class BatchAlgorithmDialog(AlgorithmDialogBase):
self.algs = None
return
col += 1
for out in alg.outputs:
for out in alg.destinationParameterDefinitions():
if out.hidden:
continue
@ -102,7 +102,7 @@ class BatchAlgorithmDialog(AlgorithmDialogBase):
return
self.algs.append(alg)
if self.alg.getVisibleOutputsCount():
if len(self.alg.destinationParameterDefinitions()) > 0:
widget = self.mainWidget.tblParameters.cellWidget(row, col)
self.load.append(widget.currentIndex() == 0)
else:

View File

@ -97,30 +97,32 @@ class BatchPanel(BASE, WIDGET):
break
# Determine column count
nOutputs = self.alg.getVisibleOutputsCount() + 1
nOutputs = len(self.alg.destinationParameterDefinitions()) + 1
if nOutputs == 1:
nOutputs = 0
self.tblParameters.setColumnCount(
self.alg.countVisibleParameters() + nOutputs)
self.alg.countVisibleParameters())
# Table headers
column = 0
for param in self.alg.parameters:
for param in self.alg.parameterDefinitions():
if param.isDestination():
continue
self.tblParameters.setHorizontalHeaderItem(
column, QTableWidgetItem(param.description))
if param.isAdvanced:
self.tblParameters.setColumnHidden(column, True)
column += 1
for out in self.alg.outputs:
for out in self.alg.destinationParameterDefinitions():
if not out.hidden:
self.tblParameters.setHorizontalHeaderItem(
column, QTableWidgetItem(out.description))
column += 1
# Last column for indicating if output will be added to canvas
if self.alg.getVisibleOutputsCount():
if len(self.alg.destinationParameterDefinitions()) > 0:
self.tblParameters.setHorizontalHeaderItem(
column, QTableWidgetItem(self.tr('Load in QGIS')))
@ -233,8 +235,8 @@ class BatchPanel(BASE, WIDGET):
wrappers = {}
row = self.tblParameters.rowCount() - 1
column = 0
for param in self.alg.parameters:
if param.hidden:
for param in self.alg.parameterDefinitions():
if param.hidden or param.isDestination():
continue
wrapper = param.wrapper(self.parent, row, column)
@ -242,7 +244,7 @@ class BatchPanel(BASE, WIDGET):
self.setCellWrapper(row, column, wrapper)
column += 1
for out in self.alg.outputs:
for out in self.alg.destinationParameterDefinitions():
if out.hidden:
continue
@ -251,7 +253,7 @@ class BatchPanel(BASE, WIDGET):
out, self.alg, row, column, self))
column += 1
if self.alg.getVisibleOutputsCount():
if len(self.alg.destinationParameterDefinitions()) > 0:
item = QComboBox()
item.addItem(self.tr('Yes'))
item.addItem(self.tr('No'))

View File

@ -265,7 +265,7 @@ class ProcessingToolbox(BASE, WIDGET):
dlg.exec_()
return
if (alg.countVisibleParameters() + alg.getVisibleOutputsCount()) > 0:
if alg.countVisibleParameters() > 0:
dlg = alg.getCustomParametersDialog()
if not dlg:
dlg = AlgorithmDialog(alg)

View File

@ -145,8 +145,8 @@ def createTest(text):
results = {}
i = 0
for param in alg.parameters:
if param.hidden:
for param in alg.parameterDefinitions():
if param.hidden or param.isDestination():
continue
i += 1
@ -238,8 +238,8 @@ def createTest(text):
definition['params'] = params
for i, out in enumerate([out for out in alg.outputs if not out.hidden]):
token = tokens[i - alg.getVisibleOutputsCount()]
for i, out in enumerate([out for out in alg.destinationParameterDefinitions() if not out.hidden]):
token = tokens[i - len(alg.destinationParameterDefinitions())]
if isinstance(out, (OutputNumber, OutputString)):
results[out.name] = str(out)

View File

@ -204,7 +204,7 @@ def _executeAlgorithm(alg):
return
context = dataobjects.createContext()
if (alg.countVisibleParameters() + alg.getVisibleOutputsCount()) > 0:
if (alg.countVisibleParameters()) > 0:
dlg = alg.getCustomParametersDialog()
if not dlg:
dlg = AlgorithmDialog(alg)

View File

@ -95,6 +95,7 @@ SET(QGIS_CORE_SRCS
processing/qgsnativealgorithms.cpp
processing/qgsprocessingalgorithm.cpp
processing/qgsprocessingoutputs.cpp
processing/qgsprocessingparameters.cpp
processing/qgsprocessingprovider.cpp
processing/qgsprocessingregistry.cpp
@ -893,6 +894,7 @@ SET(QGIS_CORE_HDRS
processing/qgsnativealgorithms.h
processing/qgsprocessingalgorithm.h
processing/qgsprocessingcontext.h
processing/qgsprocessingoutputs.h
processing/qgsprocessingparameters.h
processing/qgsprocessingutils.h

View File

@ -65,7 +65,9 @@ void QgsNativeAlgorithms::loadAlgorithms()
QgsCentroidAlgorithm::QgsCentroidAlgorithm()
{
addParameter( new QgsProcessingParameterVector( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterOutputVectorLayer( QStringLiteral( "OUTPUT_LAYER" ), QObject::tr( "Centroids" ), QgsProcessingParameterDefinition::TypeVectorPoint ) );
addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT_LAYER" ), QObject::tr( "Centroids" ), QgsProcessingParameterDefinition::TypeVectorPoint ) );
}
QVariantMap QgsCentroidAlgorithm::run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
@ -119,10 +121,12 @@ QVariantMap QgsCentroidAlgorithm::run( const QVariantMap &parameters, QgsProcess
QgsBufferAlgorithm::QgsBufferAlgorithm()
{
addParameter( new QgsProcessingParameterVector( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), QgsProcessingParameterNumber::Double, 10 ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), QgsProcessingParameterNumber::Integer, 5, false, 1 ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "DISSOLVE" ), QObject::tr( "Dissolve result" ), false ) );
addParameter( new QgsProcessingParameterOutputVectorLayer( QStringLiteral( "OUTPUT_LAYER" ), QObject::tr( "Buffered" ), QgsProcessingParameterDefinition::TypeVectorPolygon ) );
addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT_LAYER" ), QObject::tr( "Buffered" ), QgsProcessingParameterDefinition::TypeVectorPoint ) );
}
QVariantMap QgsBufferAlgorithm::run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const

View File

@ -19,11 +19,13 @@
#include "qgsapplication.h"
#include "qgsprocessingprovider.h"
#include "qgsprocessingparameters.h"
#include "qgsprocessingoutputs.h"
#include "qgsrectangle.h"
QgsProcessingAlgorithm::~QgsProcessingAlgorithm()
{
qDeleteAll( mParameters );
qDeleteAll( mOutputs );
}
QString QgsProcessingAlgorithm::id() const
@ -82,6 +84,19 @@ bool QgsProcessingAlgorithm::addParameter( QgsProcessingParameterDefinition *def
return true;
}
bool QgsProcessingAlgorithm::addOutput( QgsProcessingOutputDefinition *definition )
{
if ( !definition )
return false;
// check for duplicate named outputs
if ( QgsProcessingAlgorithm::outputDefinition( definition->name() ) )
return false;
mOutputs << definition;
return true;
}
const QgsProcessingParameterDefinition *QgsProcessingAlgorithm::parameterDefinition( const QString &name ) const
{
Q_FOREACH ( const QgsProcessingParameterDefinition *def, mParameters )
@ -103,6 +118,29 @@ int QgsProcessingAlgorithm::countVisibleParameters() const
return count;
}
QgsProcessingParameterDefinitions QgsProcessingAlgorithm::destinationParameterDefinitions() const
{
QgsProcessingParameterDefinitions result;
Q_FOREACH ( const QgsProcessingParameterDefinition *def, mParameters )
{
if ( !def->isDestination() )
continue;
result << def;
}
return result;
}
const QgsProcessingOutputDefinition *QgsProcessingAlgorithm::outputDefinition( const QString &name ) const
{
Q_FOREACH ( const QgsProcessingOutputDefinition *def, mOutputs )
{
if ( def->name().compare( name, Qt::CaseInsensitive ) == 0 )
return def;
}
return nullptr;
}
QString QgsProcessingAlgorithm::parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
{
return QgsProcessingParameters::parameterAsString( parameterDefinition( name ), parameters, name, context );

View File

@ -21,6 +21,7 @@
#include "qgis_core.h"
#include "qgis.h"
#include "qgsprocessingparameters.h"
#include "qgsprocessingoutputs.h"
#include <QString>
#include <QVariant>
#include <QIcon>
@ -134,6 +135,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
* Returns an ordered list of parameter definitions utilized by the algorithm.
* \see addParameter()
* \see parameterDefinition()
* \see destinationParameterDefinitions()
*/
QgsProcessingParameterDefinitions parameterDefinitions() const { return mParameters; }
@ -150,6 +152,27 @@ class CORE_EXPORT QgsProcessingAlgorithm
*/
int countVisibleParameters() const;
/**
* Returns a list of destination parameters definitions utilized by the algorithm.
* \see QgsProcessingParameterDefinition::isDestination()
* \see parameterDefinitions()
*/
QgsProcessingParameterDefinitions destinationParameterDefinitions() const;
/**
* Returns an ordered list of output definitions utilized by the algorithm.
* \see addOutput()
* \see outputDefinition()
*/
QgsProcessingOutputDefinitions outputDefinitions() const { return mOutputs; }
/**
* Returns a matching output by \a name. Matching is done in a case-insensitive
* manner.
* \see outputDefinitions()
*/
const QgsProcessingOutputDefinition *outputDefinition( const QString &name ) const;
/**
* Runs the algorithm using the specified \a parameters. Algorithms should implement
* their custom processing logic here.
@ -172,9 +195,18 @@ class CORE_EXPORT QgsProcessingAlgorithm
* Adds a parameter \a definition to the algorithm. Ownership of the definition is transferred to the algorithm.
* Returns true if parameter could be successfully added, or false if the parameter could not be added (e.g.
* as a result of a duplicate name).
* \see addOutput()
*/
bool addParameter( QgsProcessingParameterDefinition *parameterDefinition SIP_TRANSFER );
/**
* Adds an output \a definition to the algorithm. Ownership of the definition is transferred to the algorithm.
* Returns true if the output could be successfully added, or false if the output could not be added (e.g.
* as a result of a duplicate name).
* \see addParameter()
*/
bool addOutput( QgsProcessingOutputDefinition *outputDefinition SIP_TRANSFER );
/**
* Evaluates the parameter with matching \a name to a static string value.
*/
@ -283,6 +315,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
QgsProcessingProvider *mProvider = nullptr;
QgsProcessingParameterDefinitions mParameters;
QgsProcessingOutputDefinitions mOutputs;
/**
* Associates this algorithm with its provider. No transfer of ownership is involved.

View File

@ -0,0 +1,40 @@
/***************************************************************************
qgsprocessingoutputs.cpp
-------------------------
begin : May 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsprocessingoutputs.h"
QgsProcessingOutputDefinition::QgsProcessingOutputDefinition( const QString &name, const QString &description )
: mName( name )
, mDescription( description )
{
}
QgsProcessingOutputVectorLayer::QgsProcessingOutputVectorLayer( const QString &name, const QString &description, QgsProcessingParameterDefinition::LayerType type )
: QgsProcessingOutputDefinition( name, description )
, mDataType( type )
{}
QgsProcessingParameterDefinition::LayerType QgsProcessingOutputVectorLayer::dataType() const
{
return mDataType;
}
void QgsProcessingOutputVectorLayer::setDataType( QgsProcessingParameterDefinition::LayerType type )
{
mDataType = type;
}

View File

@ -0,0 +1,135 @@
/***************************************************************************
qgsprocessingoutputs.h
-------------------------
begin : May 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSPROCESSINGOUTPUTS_H
#define QGSPROCESSINGOUTPUTS_H
#include "qgis_core.h"
#include "qgis.h"
#include "qgsprocessingparameters.h"
//
// Output definitions
//
/**
* \class QgsProcessingOutputDefinition
* \ingroup core
*
* Base class for the definition of processing outputs.
*
* Output definitions encapsulate the properties regarding the outputs from algorithms, such
* as generated layers or calculated values.
*
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingOutputDefinition
{
public:
/**
* Constructor for QgsProcessingOutputDefinition.
*/
QgsProcessingOutputDefinition( const QString &name, const QString &description = QString() );
virtual ~QgsProcessingOutputDefinition() = default;
/**
* Unique output type name.
*/
virtual QString type() const = 0;
/**
* Returns the name of the output. This is the internal identifier by which
* algorithms access this output.
* @see setName()
*/
QString name() const { return mName; }
/**
* Sets the \a name of the output. This is the internal identifier by which
* algorithms access this output.
* @see name()
*/
void setName( const QString &name ) { mName = name; }
/**
* Returns the description for the output. This is the user-visible string
* used to identify this output.
* @see setDescription()
*/
QString description() const { return mDescription; }
/**
* Sets the \a description for the output. This is the user-visible string
* used to identify this output.
* @see description()
*/
void setDescription( const QString &description ) { mDescription = description; }
protected:
//! Output name
QString mName;
//! Output description
QString mDescription;
};
//! List of processing parameters
typedef QList< const QgsProcessingOutputDefinition * > QgsProcessingOutputDefinitions;
/**
* \class QgsProcessingOutputVectorLayer
* \ingroup core
* A vector layer output for processing algorithms.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingOutputVectorLayer : public QgsProcessingOutputDefinition
{
public:
/**
* Constructor for QgsProcessingOutputVectorLayer.
*/
QgsProcessingOutputVectorLayer( const QString &name, const QString &description = QString(), QgsProcessingParameterDefinition::LayerType type = QgsProcessingParameterDefinition::TypeVectorAny );
QString type() const override { return QStringLiteral( "outputVector" ); }
/**
* Returns the layer type for the output layer.
* \see setDataType()
*/
QgsProcessingParameterDefinition::LayerType dataType() const;
/**
* Sets the layer \a type for the output layer.
* \see dataType()
*/
void setDataType( QgsProcessingParameterDefinition::LayerType type );
private:
QgsProcessingParameterDefinition::LayerType mDataType = QgsProcessingParameterDefinition::TypeVectorAny;
};
#endif // QGSPROCESSINGOUTPUTS_H

View File

@ -706,19 +706,38 @@ void QgsProcessingParameterTableField::setAllowMultiple( bool allowMultiple )
mAllowMultiple = allowMultiple;
}
QgsProcessingParameterVector::QgsProcessingParameterVector( const QString &name, const QString &description, LayerType type, const QVariant &defaultValue, bool optional )
QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QString &name, const QString &description, LayerType type, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
, mDataType( type )
{
}
QgsProcessingParameterDefinition::LayerType QgsProcessingParameterVector::dataType() const
QgsProcessingParameterDefinition::LayerType QgsProcessingParameterVectorLayer::dataType() const
{
return mDataType;
}
void QgsProcessingParameterVector::setDataType( QgsProcessingParameterDefinition::LayerType dataType )
void QgsProcessingParameterVectorLayer::setDataType( QgsProcessingParameterDefinition::LayerType dataType )
{
mDataType = dataType;
}
QgsProcessingParameterOutputVectorLayer::QgsProcessingParameterOutputVectorLayer( const QString &name, const QString &description, QgsProcessingParameterDefinition::LayerType type, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
, mDataType( type )
{
}
QgsProcessingParameterDefinition::LayerType QgsProcessingParameterOutputVectorLayer::dataType() const
{
return mDataType;
}
void QgsProcessingParameterOutputVectorLayer::setDataType( QgsProcessingParameterDefinition::LayerType type )
{
mDataType = type;
}

View File

@ -84,6 +84,13 @@ class CORE_EXPORT QgsProcessingParameterDefinition
*/
virtual QString type() const = 0;
/**
* Returns true if this parameter represents a file or layer destination, e.g. parameters
* which are used for the destination for layers output by an algorithm will return
* true.
*/
virtual bool isDestination() const { return false; }
/**
* Returns the name of the parameter. This is the internal identifier by which
* algorithms access this parameter.
@ -906,20 +913,20 @@ class CORE_EXPORT QgsProcessingParameterTableField : public QgsProcessingParamet
};
/**
* \class QgsProcessingParameterVector
* \class QgsProcessingParameterVectorLayer
* \ingroup core
* A vector layer parameter for processing algorithms.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingParameterVector : public QgsProcessingParameterDefinition
class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParameterDefinition
{
public:
/**
* Constructor for QgsProcessingParameterVector.
* Constructor for QgsProcessingParameterVectorLayer.
*/
QgsProcessingParameterVector( const QString &name, const QString &description = QString(), QgsProcessingParameterDefinition::LayerType type = QgsProcessingParameterDefinition::TypeVectorAny, const QVariant &defaultValue = QVariant(),
bool optional = false );
QgsProcessingParameterVectorLayer( const QString &name, const QString &description = QString(), QgsProcessingParameterDefinition::LayerType type = QgsProcessingParameterDefinition::TypeVectorAny, const QVariant &defaultValue = QVariant(),
bool optional = false );
QString type() const override { return QStringLiteral( "vector" ); }
@ -938,10 +945,49 @@ class CORE_EXPORT QgsProcessingParameterVector : public QgsProcessingParameterDe
private:
QgsProcessingParameterDefinition::LayerType mDataType;
QgsProcessingParameterDefinition::LayerType mDataType = QgsProcessingParameterDefinition::TypeVectorAny;
};
/**
* \class QgsProcessingParameterOutputVectorLayer
* \ingroup core
* A vector layer output for processing algorithms.
*
* A parameter which represents the destination for a vector layer created by an algorithm.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingParameterOutputVectorLayer : public QgsProcessingParameterDefinition
{
public:
/**
* Constructor for QgsProcessingParameterOutputVectorLayer.
*/
QgsProcessingParameterOutputVectorLayer( const QString &name, const QString &description = QString(), QgsProcessingParameterDefinition::LayerType type = QgsProcessingParameterDefinition::TypeVectorAny, const QVariant &defaultValue = QVariant(),
bool optional = false );
QString type() const override { return QStringLiteral( "vectorOut" ); }
bool isDestination() const override { return true; }
/**
* Returns the layer type for the output layer associated with the parameter.
* \see setDataType()
*/
QgsProcessingParameterDefinition::LayerType dataType() const;
/**
* Sets the layer \a type for the output layer associated with the parameter.
* \see dataType()
*/
void setDataType( QgsProcessingParameterDefinition::LayerType type );
private:
QgsProcessingParameterDefinition::LayerType mDataType = QgsProcessingParameterDefinition::TypeVectorAny;
};
#endif // QGSPROCESSINGPARAMETERS_H

View File

@ -71,6 +71,37 @@ class DummyAlgorithm : public QgsProcessingAlgorithm
p4->setFlags( QgsProcessingParameterDefinition::FlagHidden );
QVERIFY( addParameter( p4 ) );
QCOMPARE( countVisibleParameters(), 2 );
//destination styleparameters
QVERIFY( destinationParameterDefinitions().isEmpty() );
QgsProcessingParameterOutputVectorLayer *p5 = new QgsProcessingParameterOutputVectorLayer( "p5" );
QVERIFY( addParameter( p5 ) );
QCOMPARE( destinationParameterDefinitions(), QgsProcessingParameterDefinitions() << p5 );
QgsProcessingParameterOutputVectorLayer *p6 = new QgsProcessingParameterOutputVectorLayer( "p6" );
QVERIFY( addParameter( p6 ) );
QCOMPARE( destinationParameterDefinitions(), QgsProcessingParameterDefinitions() << p5 << p6 );
}
void runOutputChecks()
{
QVERIFY( outputDefinitions().isEmpty() );
QVERIFY( addOutput( new QgsProcessingOutputVectorLayer( "p1" ) ) );
QCOMPARE( outputDefinitions().count(), 1 );
QCOMPARE( outputDefinitions().at( 0 )->name(), QString( "p1" ) );
QVERIFY( !addOutput( nullptr ) );
QCOMPARE( outputDefinitions().count(), 1 );
// duplicate name!
QgsProcessingOutputVectorLayer *p2 = new QgsProcessingOutputVectorLayer( "p1" );
QVERIFY( !addOutput( p2 ) );
delete p2;
QCOMPARE( outputDefinitions().count(), 1 );
QCOMPARE( outputDefinition( "p1" ), outputDefinitions().at( 0 ) );
// parameterDefinition should be case insensitive
QCOMPARE( outputDefinition( "P1" ), outputDefinitions().at( 0 ) );
QVERIFY( !outputDefinition( "invalid" ) );
}
};
@ -149,6 +180,7 @@ class TestQgsProcessing: public QObject
void createFeatureSink();
void parameters();
void algorithmParameters();
void algorithmOutputs();
void parameterGeneral();
void parameterBoolean();
void parameterCrs();
@ -978,6 +1010,12 @@ void TestQgsProcessing::algorithmParameters()
alg.runParameterChecks();
}
void TestQgsProcessing::algorithmOutputs()
{
DummyAlgorithm alg( "test" );
alg.runOutputChecks();
}
void TestQgsProcessing::parameterGeneral()
{
// test constructor