Navigation Menu

Skip to content

Commit

Permalink
Move method to evaluate a variant to a feature source to QgsProcessin…
Browse files Browse the repository at this point in the history
…gUtils
  • Loading branch information
nyalldawson committed Jul 13, 2017
1 parent 8f19f74 commit b7ae44f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 49 deletions.
14 changes: 14 additions & 0 deletions python/core/processing/qgsprocessingutils.sip
Expand Up @@ -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
Expand Down
50 changes: 1 addition & 49 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -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 )
Expand Down
55 changes: 55 additions & 0 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -25,6 +25,7 @@
#include "qgsmemoryproviderutils.h"
#include "qgsprocessingparameters.h"
#include "qgsprocessingalgorithm.h"
#include "qgsvectorlayerfeatureiterator.h"

QList<QgsRasterLayer *> QgsProcessingUtils::compatibleRasterLayers( QgsProject *project, bool sort )
{
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/core/processing/qgsprocessingutils.h
Expand Up @@ -29,6 +29,7 @@ class QgsProject;
class QgsProcessingContext;
class QgsMapLayerStore;
class QgsProcessingFeedback;
class QgsProcessingFeatureSource;

#include <QString>
#include <QVariant>
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit b7ae44f

Please sign in to comment.