mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-19 00:04:52 -04:00
This adds a lot of flexibility to algorithms, as it makes output sinks truely optional. For instance, the various "Extract by..." algorithms could add a new optional sink for features which 'fail' the extraction criteria. This effectively allows these algorithms to become feature 'routers', directing features onto other parts of a model depending on whether they pass or fail the test. But in this situation we don't always care about these failing features, and we don't want to force them to always be fetched from the provider. By making the outputs truely optional, the algorithm can tweak its logic to either fetch all features and send them to the correct output, or only fetch matching features from the provider in the first place (a big speed boost).
1599 lines
46 KiB
C++
1599 lines
46 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"
|
|
#include "qgsvectorlayerfeatureiterator.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 QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, const QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return QString();
|
|
|
|
QVariant val = parameters.value( definition->name() );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
return val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
|
|
if ( !val.isValid() || val.toString().isEmpty() )
|
|
{
|
|
// fall back to default
|
|
val = definition->defaultValue();
|
|
}
|
|
|
|
return val.toString();
|
|
}
|
|
|
|
QString QgsProcessingParameters::parameterAsExpression( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, const QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return QString();
|
|
|
|
QVariant val = parameters.value( definition->name() );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
return val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
|
|
if ( val.isValid() && !val.toString().isEmpty() )
|
|
{
|
|
QgsExpression e( val.toString() );
|
|
if ( e.isValid() )
|
|
return val.toString();
|
|
}
|
|
|
|
// fall back to default
|
|
return definition->defaultValue().toString();
|
|
}
|
|
|
|
double QgsProcessingParameters::parameterAsDouble( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, const QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return 0;
|
|
|
|
QVariant val = parameters.value( definition->name() );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
return val.value< QgsProperty >().valueAsDouble( context.expressionContext(), definition->defaultValue().toDouble() );
|
|
|
|
bool ok = false;
|
|
double res = val.toDouble( &ok );
|
|
if ( ok )
|
|
return res;
|
|
|
|
// fall back to default
|
|
val = definition->defaultValue();
|
|
return val.toDouble();
|
|
}
|
|
|
|
int QgsProcessingParameters::parameterAsInt( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, const QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return 0;
|
|
|
|
QVariant val = parameters.value( definition->name() );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
return val.value< QgsProperty >().valueAsInt( context.expressionContext(), definition->defaultValue().toInt() );
|
|
|
|
bool ok = false;
|
|
double dbl = val.toDouble( &ok );
|
|
if ( !ok )
|
|
{
|
|
// fall back to default
|
|
val = definition->defaultValue();
|
|
dbl = val.toDouble( &ok );
|
|
}
|
|
|
|
//String representations of doubles in QVariant will not convert to int
|
|
//work around this by first converting to double, and then checking whether the double is convertible to int
|
|
if ( ok )
|
|
{
|
|
double round = qgsRound( dbl );
|
|
if ( round > INT_MAX || round < -INT_MAX )
|
|
{
|
|
//double too large to fit in int
|
|
return 0;
|
|
}
|
|
return qRound( dbl );
|
|
}
|
|
|
|
return val.toInt();
|
|
}
|
|
|
|
int QgsProcessingParameters::parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, const QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return 0;
|
|
|
|
int val = parameterAsInt( definition, parameters, context );
|
|
const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
|
|
if ( enumDef && val >= enumDef->options().size() )
|
|
{
|
|
return enumDef->defaultValue().toInt();
|
|
}
|
|
return val;
|
|
}
|
|
|
|
QList<int> QgsProcessingParameters::parameterAsEnums( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, const QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return QList<int>();
|
|
|
|
QVariantList resultList;
|
|
QVariant val = parameters.value( definition->name() );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
resultList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
else if ( val.type() == QVariant::List )
|
|
{
|
|
Q_FOREACH ( const QVariant &var, val.toList() )
|
|
resultList << var;
|
|
}
|
|
else if ( val.type() == QVariant::String )
|
|
{
|
|
Q_FOREACH ( const QString &var, val.toString().split( ',' ) )
|
|
resultList << var;
|
|
}
|
|
else
|
|
resultList << val;
|
|
|
|
if ( ( !val.isValid() || !resultList.at( 0 ).isValid() ) && definition )
|
|
{
|
|
resultList.clear();
|
|
// check default
|
|
if ( definition->defaultValue().type() == QVariant::List )
|
|
{
|
|
Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
|
|
resultList << var;
|
|
}
|
|
else if ( definition->defaultValue().type() == QVariant::String )
|
|
{
|
|
Q_FOREACH ( const QString &var, definition->defaultValue().toString().split( ',' ) )
|
|
resultList << var;
|
|
}
|
|
else
|
|
resultList << definition->defaultValue();
|
|
}
|
|
|
|
QList< int > result;
|
|
const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
|
|
Q_FOREACH ( const QVariant &var, resultList )
|
|
{
|
|
int resInt = var.toInt();
|
|
if ( !enumDef || resInt < enumDef->options().size() )
|
|
{
|
|
result << resInt;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
bool QgsProcessingParameters::parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, const QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return false;
|
|
|
|
QVariant def = definition->defaultValue();
|
|
|
|
QVariant val = parameters.value( definition->name() );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
return val.value< QgsProperty >().valueAsBool( context.expressionContext(), def.toBool() );
|
|
else if ( val.isValid() )
|
|
return val.toBool();
|
|
else
|
|
return def.toBool();
|
|
}
|
|
|
|
QgsFeatureSink *QgsProcessingParameters::parameterAsSink( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, const QgsFields &fields,
|
|
QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs,
|
|
QgsProcessingContext &context, QString &destinationIdentifier )
|
|
{
|
|
QVariant val;
|
|
if ( definition )
|
|
{
|
|
val = parameters.value( definition->name() );
|
|
}
|
|
|
|
QgsProject *destinationProject = nullptr;
|
|
QVariantMap createOptions;
|
|
if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
|
|
{
|
|
// input is a QgsProcessingOutputLayerDefinition - get extra properties from it
|
|
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
|
|
destinationProject = fromVar.destinationProject;
|
|
createOptions = fromVar.createOptions;
|
|
val = fromVar.sink;
|
|
}
|
|
|
|
QString dest;
|
|
if ( val.canConvert<QgsProperty>() )
|
|
{
|
|
dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
}
|
|
else if ( !val.isValid() || val.toString().isEmpty() )
|
|
{
|
|
if ( definition && definition->flags() & QgsProcessingParameterDefinition::FlagOptional && !definition->defaultValue().isValid() )
|
|
{
|
|
// unset, optional sink, no default => no sink
|
|
return nullptr;
|
|
}
|
|
// fall back to default
|
|
dest = definition->defaultValue().toString();
|
|
}
|
|
else
|
|
{
|
|
dest = val.toString();
|
|
}
|
|
|
|
std::unique_ptr< QgsFeatureSink > sink( QgsProcessingUtils::createFeatureSink( dest, context, fields, geometryType, crs, createOptions ) );
|
|
destinationIdentifier = dest;
|
|
|
|
if ( destinationProject )
|
|
context.addLayerToLoadOnCompletion( destinationIdentifier, QgsProcessingContext::LayerDetails( definition ? definition->description() : QString(), destinationProject ) );
|
|
|
|
return sink.release();
|
|
}
|
|
|
|
QgsFeatureSource *QgsProcessingParameters::parameterAsSource( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return nullptr;
|
|
|
|
QVariant val = parameters.value( definition->name() );
|
|
|
|
bool selectedFeaturesOnly = false;
|
|
if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
|
|
{
|
|
// input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
|
|
QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
|
|
selectedFeaturesOnly = fromVar.selectedFeaturesOnly;
|
|
val = fromVar.source;
|
|
}
|
|
|
|
QString layerRef;
|
|
if ( val.canConvert<QgsProperty>() )
|
|
{
|
|
layerRef = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
}
|
|
else if ( !val.isValid() || val.toString().isEmpty() )
|
|
{
|
|
// fall back to default
|
|
layerRef = definition->defaultValue().toString();
|
|
}
|
|
else
|
|
{
|
|
layerRef = val.toString();
|
|
}
|
|
|
|
if ( layerRef.isEmpty() )
|
|
return nullptr;
|
|
|
|
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context ) );
|
|
if ( !vl )
|
|
return nullptr;
|
|
|
|
if ( selectedFeaturesOnly )
|
|
{
|
|
return new QgsProcessingFeatureSource( new QgsVectorLayerSelectedFeatureSource( vl ), context, true );
|
|
}
|
|
else
|
|
{
|
|
return new QgsProcessingFeatureSource( vl, context );
|
|
}
|
|
}
|
|
|
|
QgsMapLayer *QgsProcessingParameters::parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return nullptr;
|
|
|
|
QVariant val = parameters.value( definition->name() );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
{
|
|
val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
}
|
|
|
|
if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
|
|
{
|
|
return layer;
|
|
}
|
|
|
|
if ( !val.isValid() || val.toString().isEmpty() )
|
|
{
|
|
// fall back to default
|
|
val = definition->defaultValue();
|
|
}
|
|
|
|
if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
|
|
{
|
|
return layer;
|
|
}
|
|
|
|
QString layerRef = val.toString();
|
|
if ( layerRef.isEmpty() )
|
|
layerRef = definition->defaultValue().toString();
|
|
|
|
if ( layerRef.isEmpty() )
|
|
return nullptr;
|
|
|
|
return QgsProcessingUtils::mapLayerFromString( layerRef, context );
|
|
}
|
|
|
|
QgsRasterLayer *QgsProcessingParameters::parameterAsRasterLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
return qobject_cast< QgsRasterLayer *>( parameterAsLayer( definition, parameters, context ) );
|
|
}
|
|
|
|
QString QgsProcessingParameters::parameterAsRasterOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
QVariant val;
|
|
if ( definition )
|
|
{
|
|
val = parameters.value( definition->name() );
|
|
}
|
|
|
|
QgsProject *destinationProject = nullptr;
|
|
QVariantMap createOptions;
|
|
if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
|
|
{
|
|
// input is a QgsProcessingOutputLayerDefinition - get extra properties from it
|
|
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
|
|
destinationProject = fromVar.destinationProject;
|
|
createOptions = fromVar.createOptions;
|
|
val = fromVar.sink;
|
|
}
|
|
|
|
QString dest;
|
|
if ( val.canConvert<QgsProperty>() )
|
|
{
|
|
dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
}
|
|
else if ( !val.isValid() || val.toString().isEmpty() )
|
|
{
|
|
// fall back to default
|
|
dest = definition->defaultValue().toString();
|
|
}
|
|
else
|
|
{
|
|
dest = val.toString();
|
|
}
|
|
|
|
if ( destinationProject )
|
|
context.addLayerToLoadOnCompletion( dest, QgsProcessingContext::LayerDetails( definition ? definition->description() : QString(), destinationProject ) );
|
|
|
|
return dest;
|
|
}
|
|
|
|
QString QgsProcessingParameters::parameterAsFileOutput( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
QVariant val;
|
|
if ( definition )
|
|
{
|
|
val = parameters.value( definition->name() );
|
|
}
|
|
|
|
if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
|
|
{
|
|
// input is a QgsProcessingOutputLayerDefinition - get extra properties from it
|
|
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
|
|
val = fromVar.sink;
|
|
}
|
|
|
|
QString dest;
|
|
if ( val.canConvert<QgsProperty>() )
|
|
{
|
|
dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
}
|
|
else if ( !val.isValid() || val.toString().isEmpty() )
|
|
{
|
|
// fall back to default
|
|
dest = definition->defaultValue().toString();
|
|
}
|
|
else
|
|
{
|
|
dest = val.toString();
|
|
}
|
|
|
|
return dest;
|
|
}
|
|
|
|
QgsVectorLayer *QgsProcessingParameters::parameterAsVectorLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
return qobject_cast< QgsVectorLayer *>( parameterAsLayer( definition, parameters, context ) );
|
|
}
|
|
|
|
QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return QgsCoordinateReferenceSystem();
|
|
|
|
QString crsText = parameterAsString( definition, parameters, context );
|
|
if ( crsText.isEmpty() )
|
|
crsText = definition->defaultValue().toString();
|
|
|
|
if ( crsText.isEmpty() )
|
|
return QgsCoordinateReferenceSystem();
|
|
|
|
// maybe special string
|
|
if ( context.project() && crsText.compare( QStringLiteral( "ProjectCrs" ), Qt::CaseInsensitive ) == 0 )
|
|
return context.project()->crs();
|
|
|
|
// maybe a map layer reference
|
|
if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( crsText, context ) )
|
|
return layer->crs();
|
|
|
|
// else CRS from string
|
|
QgsCoordinateReferenceSystem crs;
|
|
crs.createFromString( crsText );
|
|
return crs;
|
|
}
|
|
|
|
QgsRectangle QgsProcessingParameters::parameterAsExtent( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return QgsRectangle();
|
|
|
|
QVariant val = parameters.value( definition->name() );
|
|
QString rectText;
|
|
if ( val.canConvert<QgsProperty>() )
|
|
rectText = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
else
|
|
rectText = val.toString();
|
|
|
|
if ( rectText.isEmpty() )
|
|
return QgsRectangle();
|
|
|
|
QStringList parts = rectText.split( ',' );
|
|
if ( parts.count() == 4 )
|
|
{
|
|
bool xMinOk = false;
|
|
double xMin = parts.at( 0 ).toDouble( &xMinOk );
|
|
bool xMaxOk = false;
|
|
double xMax = parts.at( 1 ).toDouble( &xMaxOk );
|
|
bool yMinOk = false;
|
|
double yMin = parts.at( 2 ).toDouble( &yMinOk );
|
|
bool yMaxOk = false;
|
|
double yMax = parts.at( 3 ).toDouble( &yMaxOk );
|
|
if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
|
|
return QgsRectangle( xMin, yMin, xMax, yMax );
|
|
}
|
|
|
|
// try as layer extent
|
|
if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( rectText, context ) )
|
|
return layer->extent();
|
|
|
|
return QgsRectangle();
|
|
}
|
|
|
|
QgsPointXY QgsProcessingParameters::parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return QgsPointXY();
|
|
|
|
QString pointText = parameterAsString( definition, parameters, context );
|
|
if ( pointText.isEmpty() )
|
|
pointText = definition->defaultValue().toString();
|
|
|
|
if ( pointText.isEmpty() )
|
|
return QgsPointXY();
|
|
|
|
QStringList parts = pointText.split( ',' );
|
|
if ( parts.count() == 2 )
|
|
{
|
|
bool xOk = false;
|
|
double x = parts.at( 0 ).toDouble( &xOk );
|
|
bool yOk = false;
|
|
double y = parts.at( 1 ).toDouble( &yOk );
|
|
if ( xOk && yOk )
|
|
return QgsPointXY( x, y );
|
|
}
|
|
|
|
return QgsPointXY();
|
|
}
|
|
|
|
QString QgsProcessingParameters::parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return QString();
|
|
|
|
QString fileText = parameterAsString( definition, parameters, context );
|
|
if ( fileText.isEmpty() )
|
|
fileText = definition->defaultValue().toString();
|
|
return fileText;
|
|
}
|
|
|
|
QVariantList QgsProcessingParameters::parameterAsMatrix( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return QVariantList();
|
|
|
|
QString resultString;
|
|
QVariant val = parameters.value( definition->name() );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
resultString = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
else if ( val.type() == QVariant::List )
|
|
return val.toList();
|
|
else
|
|
resultString = val.toString();
|
|
|
|
if ( resultString.isEmpty() )
|
|
{
|
|
// check default
|
|
if ( definition->defaultValue().type() == QVariant::List )
|
|
return definition->defaultValue().toList();
|
|
else
|
|
resultString = definition->defaultValue().toString();
|
|
}
|
|
|
|
QVariantList result;
|
|
Q_FOREACH ( const QString &s, resultString.split( ',' ) )
|
|
result << s;
|
|
|
|
return result;
|
|
}
|
|
|
|
QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return QList<QgsMapLayer *>();
|
|
|
|
QVariant val = parameters.value( definition->name() );
|
|
if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
|
|
{
|
|
return QList<QgsMapLayer *>() << layer;
|
|
}
|
|
|
|
QList<QgsMapLayer *> layers;
|
|
|
|
QStringList resultStringList;
|
|
|
|
if ( val.canConvert<QgsProperty>() )
|
|
resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
else if ( val.type() == QVariant::List )
|
|
{
|
|
Q_FOREACH ( const QVariant &var, val.toList() )
|
|
{
|
|
if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
|
|
{
|
|
layers << layer;
|
|
}
|
|
else
|
|
{
|
|
resultStringList << var.toString();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
resultStringList << val.toString();
|
|
|
|
if ( ( resultStringList.isEmpty() || resultStringList.at( 0 ).isEmpty() ) )
|
|
{
|
|
resultStringList.clear();
|
|
// check default
|
|
if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( definition->defaultValue() ) ) )
|
|
{
|
|
layers << layer;
|
|
}
|
|
else if ( definition->defaultValue().type() == QVariant::List )
|
|
{
|
|
Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
|
|
{
|
|
if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
|
|
{
|
|
layers << layer;
|
|
}
|
|
else
|
|
{
|
|
resultStringList << var.toString();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
resultStringList << definition->defaultValue().toString();
|
|
}
|
|
|
|
Q_FOREACH ( const QString &s, resultStringList )
|
|
{
|
|
QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( s, context );
|
|
if ( layer )
|
|
layers << layer;
|
|
}
|
|
|
|
return layers;
|
|
}
|
|
|
|
QList<double> QgsProcessingParameters::parameterAsRange( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return QList<double>();
|
|
|
|
QStringList resultStringList;
|
|
QVariant val = parameters.value( definition->name() );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
else if ( val.type() == QVariant::List )
|
|
{
|
|
Q_FOREACH ( const QVariant &var, val.toList() )
|
|
resultStringList << var.toString();
|
|
}
|
|
else
|
|
resultStringList << val.toString();
|
|
|
|
if ( ( resultStringList.isEmpty() || ( resultStringList.size() == 1 && resultStringList.at( 0 ).isEmpty() ) ) )
|
|
{
|
|
resultStringList.clear();
|
|
// check default
|
|
if ( definition->defaultValue().type() == QVariant::List )
|
|
{
|
|
Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
|
|
resultStringList << var.toString();
|
|
}
|
|
else
|
|
resultStringList << definition->defaultValue().toString();
|
|
}
|
|
|
|
if ( resultStringList.size() == 1 )
|
|
{
|
|
resultStringList = resultStringList.at( 0 ).split( ',' );
|
|
}
|
|
|
|
if ( resultStringList.size() < 2 )
|
|
return QList< double >() << 0.0 << 0.0;
|
|
|
|
return QList< double >() << resultStringList.at( 0 ).toDouble() << resultStringList.at( 1 ).toDouble();
|
|
}
|
|
|
|
QStringList QgsProcessingParameters::parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
|
{
|
|
if ( !definition )
|
|
return QStringList();
|
|
|
|
QStringList resultStringList;
|
|
QVariant val = parameters.value( definition->name() );
|
|
if ( val.canConvert<QgsProperty>() )
|
|
resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
|
|
else if ( val.type() == QVariant::List )
|
|
{
|
|
Q_FOREACH ( const QVariant &var, val.toList() )
|
|
resultStringList << var.toString();
|
|
}
|
|
else
|
|
resultStringList.append( val.toString().split( ';' ) );
|
|
|
|
if ( ( resultStringList.isEmpty() || resultStringList.at( 0 ).isEmpty() ) )
|
|
{
|
|
resultStringList.clear();
|
|
// check default
|
|
if ( definition->defaultValue().type() == QVariant::List )
|
|
{
|
|
Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
|
|
resultStringList << var.toString();
|
|
}
|
|
else
|
|
resultStringList.append( definition->defaultValue().toString().split( ';' ) );
|
|
}
|
|
|
|
return resultStringList;
|
|
}
|
|
|
|
//
|
|
// QgsProcessingParameterDefinition
|
|
//
|
|
|
|
QgsProcessingParameterDefinition::QgsProcessingParameterDefinition( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
|
|
: mName( name )
|
|
, mDescription( description )
|
|
, mDefault( defaultValue )
|
|
, mFlags( optional ? FlagOptional : 0 )
|
|
{}
|
|
|
|
bool QgsProcessingParameterDefinition::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
|
|
{
|
|
if ( !input.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( input.type() == QVariant::String && input.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
|
|
return true;
|
|
}
|
|
|
|
QgsProcessingParameterBoolean::QgsProcessingParameterBoolean( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
{}
|
|
|
|
QgsProcessingParameterCrs::QgsProcessingParameterCrs( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterCrs::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
|
|
{
|
|
if ( !input.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( input.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( input.type() != QVariant::String || input.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
|
|
return true;
|
|
}
|
|
|
|
QgsProcessingParameterMapLayer::QgsProcessingParameterMapLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterMapLayer::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context ) const
|
|
{
|
|
if ( !input.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( input.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( input.type() != QVariant::String || input.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( !context )
|
|
{
|
|
// that's as far as we can get without a context
|
|
return true;
|
|
}
|
|
|
|
// try to load as layer
|
|
if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context ) )
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
QgsProcessingParameterExtent::QgsProcessingParameterExtent( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterExtent::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context ) const
|
|
{
|
|
if ( !input.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( input.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( input.type() != QVariant::String || input.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( !context )
|
|
{
|
|
// that's as far as we can get without a context
|
|
return true;
|
|
}
|
|
|
|
QStringList parts = input.toString().split( ',' );
|
|
if ( parts.count() == 4 )
|
|
{
|
|
bool xMinOk = false;
|
|
( void )parts.at( 0 ).toDouble( &xMinOk );
|
|
bool xMaxOk = false;
|
|
( void )parts.at( 1 ).toDouble( &xMaxOk );
|
|
bool yMinOk = false;
|
|
( void )parts.at( 2 ).toDouble( &yMinOk );
|
|
bool yMaxOk = false;
|
|
( void )parts.at( 3 ).toDouble( &yMaxOk );
|
|
if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
|
|
return true;
|
|
}
|
|
|
|
// try as layer extent
|
|
if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context ) )
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
QgsProcessingParameterPoint::QgsProcessingParameterPoint( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterPoint::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
|
|
{
|
|
if ( !input.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( input.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( input.type() == QVariant::String )
|
|
{
|
|
if ( input.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
}
|
|
|
|
QStringList parts = input.toString().split( ',' );
|
|
if ( parts.count() == 2 )
|
|
{
|
|
bool xOk = false;
|
|
( void )parts.at( 0 ).toDouble( &xOk );
|
|
bool yOk = false;
|
|
( void )parts.at( 1 ).toDouble( &yOk );
|
|
return xOk && yOk;
|
|
}
|
|
else
|
|
return false;
|
|
}
|
|
|
|
QgsProcessingParameterFile::QgsProcessingParameterFile( const QString &name, const QString &description, Behavior behavior, const QString &extension, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
, mBehavior( behavior )
|
|
, mExtension( extension )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterFile::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
|
|
{
|
|
if ( !input.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( input.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
QString string = input.toString().trimmed();
|
|
|
|
if ( input.type() != QVariant::String || string.isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
|
|
switch ( mBehavior )
|
|
{
|
|
case File:
|
|
{
|
|
if ( !mExtension.isEmpty() )
|
|
return string.endsWith( mExtension, Qt::CaseInsensitive );
|
|
return true;
|
|
}
|
|
|
|
case Folder:
|
|
return true;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
QgsProcessingParameterMatrix::QgsProcessingParameterMatrix( const QString &name, const QString &description, int numberRows, bool fixedNumberRows, const QStringList &headers, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
, mHeaders( headers )
|
|
, mNumberRows( numberRows )
|
|
, mFixedNumberRows( fixedNumberRows )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterMatrix::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
|
|
{
|
|
if ( !input.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( input.type() == QVariant::String )
|
|
{
|
|
if ( input.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
return true;
|
|
}
|
|
else if ( input.type() == QVariant::List )
|
|
{
|
|
if ( input.toList().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
return true;
|
|
}
|
|
else if ( input.type() == QVariant::Double || input.type() == QVariant::Int )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
QStringList QgsProcessingParameterMatrix::headers() const
|
|
{
|
|
return mHeaders;
|
|
}
|
|
|
|
void QgsProcessingParameterMatrix::setHeaders( const QStringList &headers )
|
|
{
|
|
mHeaders = headers;
|
|
}
|
|
|
|
int QgsProcessingParameterMatrix::numberRows() const
|
|
{
|
|
return mNumberRows;
|
|
}
|
|
|
|
void QgsProcessingParameterMatrix::setNumberRows( int numberRows )
|
|
{
|
|
mNumberRows = numberRows;
|
|
}
|
|
|
|
bool QgsProcessingParameterMatrix::hasFixedNumberRows() const
|
|
{
|
|
return mFixedNumberRows;
|
|
}
|
|
|
|
void QgsProcessingParameterMatrix::setHasFixedNumberRows( bool fixedNumberRows )
|
|
{
|
|
mFixedNumberRows = fixedNumberRows;
|
|
}
|
|
|
|
QgsProcessingParameterMultipleLayers::QgsProcessingParameterMultipleLayers( const QString &name, const QString &description, LayerType layerType, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
, mLayerType( layerType )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterMultipleLayers::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context ) const
|
|
{
|
|
if ( !input.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( input.type() == QVariant::String )
|
|
{
|
|
if ( input.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( mMinimumNumberInputs > 1 )
|
|
return false;
|
|
|
|
if ( !context )
|
|
return true;
|
|
|
|
return QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
|
|
}
|
|
else if ( input.type() == QVariant::List )
|
|
{
|
|
if ( input.toList().count() < mMinimumNumberInputs )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( mMinimumNumberInputs > input.toList().count() )
|
|
return false;
|
|
|
|
if ( !context )
|
|
return true;
|
|
|
|
Q_FOREACH ( const QVariant &v, input.toList() )
|
|
{
|
|
if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( v ) ) )
|
|
continue;
|
|
|
|
if ( !QgsProcessingUtils::mapLayerFromString( v.toString(), *context ) )
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
else if ( input.type() == QVariant::StringList )
|
|
{
|
|
if ( input.toStringList().count() < mMinimumNumberInputs )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( mMinimumNumberInputs > input.toStringList().count() )
|
|
return false;
|
|
|
|
if ( !context )
|
|
return true;
|
|
|
|
Q_FOREACH ( const QString &v, input.toStringList() )
|
|
{
|
|
if ( !QgsProcessingUtils::mapLayerFromString( v, *context ) )
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
QgsProcessingParameterDefinition::LayerType QgsProcessingParameterMultipleLayers::layerType() const
|
|
{
|
|
return mLayerType;
|
|
}
|
|
|
|
void QgsProcessingParameterMultipleLayers::setLayerType( LayerType type )
|
|
{
|
|
mLayerType = type;
|
|
}
|
|
|
|
int QgsProcessingParameterMultipleLayers::minimumNumberInputs() const
|
|
{
|
|
return mMinimumNumberInputs;
|
|
}
|
|
|
|
void QgsProcessingParameterMultipleLayers::setMinimumNumberInputs( int minimumNumberInputs )
|
|
{
|
|
if ( mMinimumNumberInputs >= 1 || !( flags() & QgsProcessingParameterDefinition::FlagOptional ) )
|
|
mMinimumNumberInputs = minimumNumberInputs;
|
|
}
|
|
|
|
QgsProcessingParameterNumber::QgsProcessingParameterNumber( const QString &name, const QString &description, Type type, const QVariant &defaultValue, bool optional, double minValue, double maxValue )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
, mMin( minValue )
|
|
, mMax( maxValue )
|
|
, mDataType( type )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterNumber::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
|
|
{
|
|
if ( !input.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( input.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
bool ok = false;
|
|
double res = input.toDouble( &ok );
|
|
if ( !ok )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( res < mMin || res > mMax )
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
double QgsProcessingParameterNumber::minimum() const
|
|
{
|
|
return mMin;
|
|
}
|
|
|
|
void QgsProcessingParameterNumber::setMinimum( double min )
|
|
{
|
|
mMin = min;
|
|
}
|
|
|
|
double QgsProcessingParameterNumber::maximum() const
|
|
{
|
|
return mMax;
|
|
}
|
|
|
|
void QgsProcessingParameterNumber::setMaximum( double max )
|
|
{
|
|
mMax = max;
|
|
}
|
|
|
|
QgsProcessingParameterNumber::Type QgsProcessingParameterNumber::dataType() const
|
|
{
|
|
return mDataType;
|
|
}
|
|
|
|
void QgsProcessingParameterNumber::setDataType( const Type &dataType )
|
|
{
|
|
mDataType = dataType;
|
|
}
|
|
|
|
QgsProcessingParameterRange::QgsProcessingParameterRange( const QString &name, const QString &description, QgsProcessingParameterNumber::Type type, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
, mDataType( type )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterRange::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
|
|
{
|
|
if ( !input.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( input.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( input.type() == QVariant::String )
|
|
{
|
|
QStringList list = input.toString().split( ',' );
|
|
if ( list.count() != 2 )
|
|
return mFlags & FlagOptional;
|
|
bool ok = false;
|
|
list.at( 0 ).toDouble( &ok );
|
|
bool ok2 = false;
|
|
list.at( 1 ).toDouble( &ok2 );
|
|
if ( !ok || !ok2 )
|
|
return mFlags & FlagOptional;
|
|
return true;
|
|
}
|
|
else if ( input.type() == QVariant::List )
|
|
{
|
|
if ( input.toList().count() != 2 )
|
|
return mFlags & FlagOptional;
|
|
|
|
bool ok = false;
|
|
input.toList().at( 0 ).toDouble( &ok );
|
|
bool ok2 = false;
|
|
input.toList().at( 1 ).toDouble( &ok2 );
|
|
if ( !ok || !ok2 )
|
|
return mFlags & FlagOptional;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
QgsProcessingParameterNumber::Type QgsProcessingParameterRange::dataType() const
|
|
{
|
|
return mDataType;
|
|
}
|
|
|
|
void QgsProcessingParameterRange::setDataType( const QgsProcessingParameterNumber::Type &dataType )
|
|
{
|
|
mDataType = dataType;
|
|
}
|
|
|
|
QgsProcessingParameterRasterLayer::QgsProcessingParameterRasterLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterRasterLayer::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context ) const
|
|
{
|
|
if ( !input.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( input.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( qobject_cast< QgsRasterLayer * >( qvariant_cast<QObject *>( input ) ) )
|
|
return true;
|
|
|
|
if ( input.type() != QVariant::String || input.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( !context )
|
|
{
|
|
// that's as far as we can get without a context
|
|
return true;
|
|
}
|
|
|
|
// try to load as layer
|
|
if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context ) )
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
QgsProcessingParameterEnum::QgsProcessingParameterEnum( const QString &name, const QString &description, const QStringList &options, bool allowMultiple, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
, mOptions( options )
|
|
, mAllowMultiple( allowMultiple )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterEnum::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
|
|
{
|
|
if ( !input.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( input.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( input.type() == QVariant::List )
|
|
{
|
|
if ( !mAllowMultiple )
|
|
return false;
|
|
|
|
Q_FOREACH ( const QVariant &val, input.toList() )
|
|
{
|
|
bool ok = false;
|
|
int res = val.toInt( &ok );
|
|
if ( !ok )
|
|
return false;
|
|
else if ( res < 0 || res >= mOptions.count() )
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
else if ( input.type() == QVariant::String )
|
|
{
|
|
QStringList parts = input.toString().split( ',' );
|
|
if ( parts.count() > 1 && !mAllowMultiple )
|
|
return false;
|
|
|
|
Q_FOREACH ( const QString &part, parts )
|
|
{
|
|
bool ok = false;
|
|
int res = part.toInt( &ok );
|
|
if ( !ok )
|
|
return false;
|
|
else if ( res < 0 || res >= mOptions.count() )
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
else if ( input.type() == QVariant::Int || input.type() == QVariant::Double )
|
|
{
|
|
bool ok = false;
|
|
int res = input.toInt( &ok );
|
|
if ( !ok )
|
|
return false;
|
|
else if ( res >= 0 && res < mOptions.count() )
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
QStringList QgsProcessingParameterEnum::options() const
|
|
{
|
|
return mOptions;
|
|
}
|
|
|
|
void QgsProcessingParameterEnum::setOptions( const QStringList &options )
|
|
{
|
|
mOptions = options;
|
|
}
|
|
|
|
bool QgsProcessingParameterEnum::allowMultiple() const
|
|
{
|
|
return mAllowMultiple;
|
|
}
|
|
|
|
void QgsProcessingParameterEnum::setAllowMultiple( bool allowMultiple )
|
|
{
|
|
mAllowMultiple = allowMultiple;
|
|
}
|
|
|
|
QgsProcessingParameterString::QgsProcessingParameterString( const QString &name, const QString &description, const QVariant &defaultValue, bool multiLine, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
, mMultiLine( multiLine )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterString::multiLine() const
|
|
{
|
|
return mMultiLine;
|
|
}
|
|
|
|
void QgsProcessingParameterString::setMultiLine( bool multiLine )
|
|
{
|
|
mMultiLine = multiLine;
|
|
}
|
|
|
|
QgsProcessingParameterExpression::QgsProcessingParameterExpression( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
, mParentLayerParameter( parentLayerParameterName )
|
|
{
|
|
|
|
}
|
|
|
|
QString QgsProcessingParameterExpression::parentLayerParameter() const
|
|
{
|
|
return mParentLayerParameter;
|
|
}
|
|
|
|
void QgsProcessingParameterExpression::setParentLayerParameter( const QString &parentLayerParameter )
|
|
{
|
|
mParentLayerParameter = parentLayerParameter;
|
|
}
|
|
|
|
QgsProcessingParameterTable::QgsProcessingParameterTable( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
{
|
|
|
|
}
|
|
|
|
QgsProcessingParameterTableField::QgsProcessingParameterTableField( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, DataType type, bool allowMultiple, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
, mParentLayerParameter( parentLayerParameterName )
|
|
, mDataType( type )
|
|
, mAllowMultiple( allowMultiple )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterTableField::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
|
|
{
|
|
if ( !input.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( input.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( input.type() == QVariant::List || input.type() == QVariant::StringList )
|
|
{
|
|
if ( !mAllowMultiple )
|
|
return false;
|
|
}
|
|
else if ( input.type() == QVariant::String )
|
|
{
|
|
if ( input.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
|
|
QStringList parts = input.toString().split( ';' );
|
|
if ( parts.count() > 1 && !mAllowMultiple )
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
if ( input.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
QString QgsProcessingParameterTableField::parentLayerParameter() const
|
|
{
|
|
return mParentLayerParameter;
|
|
}
|
|
|
|
void QgsProcessingParameterTableField::setParentLayerParameter( const QString &parentLayerParameter )
|
|
{
|
|
mParentLayerParameter = parentLayerParameter;
|
|
}
|
|
|
|
QgsProcessingParameterTableField::DataType QgsProcessingParameterTableField::dataType() const
|
|
{
|
|
return mDataType;
|
|
}
|
|
|
|
void QgsProcessingParameterTableField::setDataType( const DataType &dataType )
|
|
{
|
|
mDataType = dataType;
|
|
}
|
|
|
|
bool QgsProcessingParameterTableField::allowMultiple() const
|
|
{
|
|
return mAllowMultiple;
|
|
}
|
|
|
|
void QgsProcessingParameterTableField::setAllowMultiple( bool allowMultiple )
|
|
{
|
|
mAllowMultiple = allowMultiple;
|
|
}
|
|
|
|
QgsProcessingParameterFeatureSource::QgsProcessingParameterFeatureSource( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
, mDataTypes( types )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterFeatureSource::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context ) const
|
|
{
|
|
QVariant var = input;
|
|
if ( !var.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( var.canConvert<QgsProcessingFeatureSourceDefinition>() )
|
|
{
|
|
QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( var );
|
|
var = fromVar.source;
|
|
}
|
|
|
|
if ( var.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( input ) ) )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( var.type() != QVariant::String || var.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( !context )
|
|
{
|
|
// that's as far as we can get without a context
|
|
return true;
|
|
}
|
|
|
|
// try to load as layer
|
|
if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context ) )
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
QList< int > QgsProcessingParameterFeatureSource::dataTypes() const
|
|
{
|
|
return mDataTypes;
|
|
}
|
|
|
|
void QgsProcessingParameterFeatureSource::setDataTypes( const QList<int> &types )
|
|
{
|
|
mDataTypes = types;
|
|
}
|
|
|
|
|
|
QgsProcessingParameterFeatureSink::QgsProcessingParameterFeatureSink( const QString &name, const QString &description, QgsProcessingParameterDefinition::LayerType type, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
, mDataType( type )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterFeatureSink::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
|
|
{
|
|
QVariant var = input;
|
|
if ( !var.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
|
|
{
|
|
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
|
|
var = fromVar.sink;
|
|
}
|
|
|
|
if ( var.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( var.type() != QVariant::String )
|
|
return false;
|
|
|
|
if ( var.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
|
|
return true;
|
|
}
|
|
|
|
QgsProcessingParameterDefinition::LayerType QgsProcessingParameterFeatureSink::dataType() const
|
|
{
|
|
return mDataType;
|
|
}
|
|
|
|
bool QgsProcessingParameterFeatureSink::hasGeometry() const
|
|
{
|
|
switch ( mDataType )
|
|
{
|
|
case TypeAny:
|
|
case TypeVectorAny:
|
|
case TypeVectorPoint:
|
|
case TypeVectorLine:
|
|
case TypeVectorPolygon:
|
|
case TypeTable:
|
|
return true;
|
|
|
|
case TypeRaster:
|
|
case TypeFile:
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void QgsProcessingParameterFeatureSink::setDataType( QgsProcessingParameterDefinition::LayerType type )
|
|
{
|
|
mDataType = type;
|
|
}
|
|
|
|
QgsProcessingParameterRasterOutput::QgsProcessingParameterRasterOutput( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
{}
|
|
|
|
bool QgsProcessingParameterRasterOutput::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
|
|
{
|
|
QVariant var = input;
|
|
if ( !var.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
|
|
{
|
|
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
|
|
var = fromVar.sink;
|
|
}
|
|
|
|
if ( var.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( var.type() != QVariant::String )
|
|
return false;
|
|
|
|
if ( var.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
QgsProcessingParameterFileOutput::QgsProcessingParameterFileOutput( const QString &name, const QString &description, const QString &fileFilter, const QVariant &defaultValue, bool optional )
|
|
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
|
|
, mFileFilter( fileFilter.isEmpty() ? QObject::tr( "All files (*.*)" ) : fileFilter )
|
|
{
|
|
|
|
}
|
|
|
|
bool QgsProcessingParameterFileOutput::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
|
|
{
|
|
QVariant var = input;
|
|
if ( !var.isValid() )
|
|
return mFlags & FlagOptional;
|
|
|
|
if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
|
|
{
|
|
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
|
|
var = fromVar.sink;
|
|
}
|
|
|
|
if ( var.canConvert<QgsProperty>() )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if ( var.type() != QVariant::String )
|
|
return false;
|
|
|
|
if ( var.toString().isEmpty() )
|
|
return mFlags & FlagOptional;
|
|
|
|
// possible enhancement - check that value is compatible with file filter?
|
|
|
|
return true;
|
|
}
|
|
|
|
QString QgsProcessingParameterFileOutput::fileFilter() const
|
|
{
|
|
return mFileFilter;
|
|
}
|
|
|
|
void QgsProcessingParameterFileOutput::setFileFilter( const QString &fileFilter )
|
|
{
|
|
mFileFilter = fileFilter;
|
|
}
|