mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-26 00:02:43 -04:00
This commit adds the virtual method for running processing algs to the base c++ class, and adds the initial framework for c++ algorithm parameters. When running an algorithm, a QVariantMap is passed as the algorithm parameters. The high level API provided by QgsProcessingParameters should be used to retrieve strings/layers/doubles/etc from this QVariantMap. This allows advanced use cases, such as passing QgsProperty with the QVariantMap for "dynamic" parameters, where the value should be evaluated for every feature processed. E.g. if the buffer algorithm uses a dynamic property for distance, then the distance could be bound to either a field value or to a custom expression. This gets evaluated before buffering each feature to allow for advanced variable buffering. Support for dynamic parameters will be "opt in", and non default. So algorithms will need to specifically add support for dynamic properties as required.
75 lines
3.1 KiB
C++
75 lines
3.1 KiB
C++
/***************************************************************************
|
|
qgsprocessingparameters.cpp
|
|
---------------------------
|
|
begin : April 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 "qgsprocessingparameters.h"
|
|
#include "qgsprocessingcontext.h"
|
|
#include "qgsprocessingutils.h"
|
|
|
|
bool QgsProcessingParameters::isDynamic( const QVariantMap ¶meters, const QString &name )
|
|
{
|
|
QVariant val = parameters.value( name );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
return val.value< QgsProperty >().propertyType() != QgsProperty::StaticProperty;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
QString QgsProcessingParameters::parameterAsString( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context )
|
|
{
|
|
QVariant val = parameters.value( name );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
return val.value< QgsProperty >().valueAsString( context.expressionContext() );
|
|
else
|
|
return val.toString();
|
|
}
|
|
|
|
double QgsProcessingParameters::parameterAsDouble( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context )
|
|
{
|
|
QVariant val = parameters.value( name );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
return val.value< QgsProperty >().valueAsDouble( context.expressionContext(), -1 );
|
|
else
|
|
return val.toDouble();
|
|
}
|
|
|
|
int QgsProcessingParameters::parameterAsInt( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context )
|
|
{
|
|
QVariant val = parameters.value( name );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
return val.value< QgsProperty >().valueAsInt( context.expressionContext(), -1 );
|
|
else
|
|
return val.toInt();
|
|
}
|
|
|
|
bool QgsProcessingParameters::parameterAsBool( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context )
|
|
{
|
|
QVariant val = parameters.value( name );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
return val.value< QgsProperty >().valueAsBool( context.expressionContext(), -1 );
|
|
else
|
|
return val.toBool();
|
|
}
|
|
|
|
QgsMapLayer *QgsProcessingParameters::parameterAsLayer( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context )
|
|
{
|
|
QString layerRef = parameterAsString( parameters, name, context );
|
|
if ( layerRef.isEmpty() )
|
|
return nullptr;
|
|
|
|
return QgsProcessingUtils::mapLayerFromString( layerRef, context );
|
|
}
|