Move method to evaluate a variant to a feature source to QgsProcessingUtils

This commit is contained in:
Nyall Dawson 2017-07-13 20:51:47 +10:00
parent 8f19f74893
commit b7ae44fb30
4 changed files with 84 additions and 49 deletions

View File

@ -79,6 +79,20 @@ class QgsProcessingUtils
:rtype: QgsMapLayer
%End
static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) /Factory/;
%Docstring
Converts a variant ``value`` to a new feature source.
Sources will either be taken from ``context``'s active project, or loaded from external
sources and stored temporarily in the ``context``.
The optional ``fallbackValue`` can be used to specify a "default" value which is used
if ``value`` cannot be successfully converted to a source.
This function creates a new object and the caller takes responsibility for deleting the returned object.
:rtype: QgsProcessingFeatureSource
%End
static QString normalizeLayerSource( const QString &source );
%Docstring
Normalizes a layer ``source`` string for safe comparison across different

View File

@ -277,55 +277,7 @@ QgsProcessingFeatureSource *QgsProcessingParameters::parameterAsSource( const Qg
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;
}
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) ) )
{
return new QgsProcessingFeatureSource( layer, context );
}
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
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( definition->defaultValue() ) ) )
{
return new QgsProcessingFeatureSource( layer, context );
}
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 );
}
return QgsProcessingUtils::variantToSource( val, context, definition->defaultValue() );
}
QString QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback )

View File

@ -25,6 +25,7 @@
#include "qgsmemoryproviderutils.h"
#include "qgsprocessingparameters.h"
#include "qgsprocessingalgorithm.h"
#include "qgsvectorlayerfeatureiterator.h"
QList<QgsRasterLayer *> QgsProcessingUtils::compatibleRasterLayers( QgsProject *project, bool sort )
{
@ -208,6 +209,60 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string, QgsP
}
}
QgsProcessingFeatureSource *QgsProcessingUtils::variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue )
{
QVariant val = value;
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;
}
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) ) )
{
return new QgsProcessingFeatureSource( layer, context );
}
QString layerRef;
if ( val.canConvert<QgsProperty>() )
{
layerRef = val.value< QgsProperty >().valueAsString( context.expressionContext(), fallbackValue.toString() );
}
else if ( !val.isValid() || val.toString().isEmpty() )
{
// fall back to default
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( fallbackValue ) ) )
{
return new QgsProcessingFeatureSource( layer, context );
}
layerRef = fallbackValue.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 );
}
}
bool QgsProcessingUtils::canUseLayer( const QgsRasterLayer *layer )
{
// only gdal file-based layers

View File

@ -29,6 +29,7 @@ class QgsProject;
class QgsProcessingContext;
class QgsMapLayerStore;
class QgsProcessingFeedback;
class QgsProcessingFeatureSource;
#include <QString>
#include <QVariant>
@ -95,6 +96,19 @@ class CORE_EXPORT QgsProcessingUtils
*/
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true );
/**
* Converts a variant \a value to a new feature source.
*
* Sources will either be taken from \a context's active project, or loaded from external
* sources and stored temporarily in the \a context.
*
* The optional \a fallbackValue can be used to specify a "default" value which is used
* if \a value cannot be successfully converted to a source.
*
* This function creates a new object and the caller takes responsibility for deleting the returned object.
*/
static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) SIP_FACTORY;
/**
* Normalizes a layer \a source string for safe comparison across different
* operating system environments.