Skip to content
Permalink
Browse files

[FEATURE][processing][API] New parameter type QgsProcessingParameterM…

…apTheme

Allows for selection from existing map themes
  • Loading branch information
nyalldawson committed Jan 4, 2020
1 parent 41cdd40 commit 54629b8f21db897fef6bfc14278d63589aff9c70
@@ -193,6 +193,8 @@ their acceptable ranges, defaults, etc.
sipType = sipType_QgsProcessingParameterColor;
else if ( sipCpp->type() == QgsProcessingParameterCoordinateOperation::typeName() )
sipType = sipType_QgsProcessingParameterCoordinateOperation;
else if ( sipCpp->type() == QgsProcessingParameterMapTheme::typeName() )
sipType = sipType_QgsProcessingParameterMapTheme;
else
sipType = nullptr;
%End
@@ -3243,6 +3245,8 @@ class QgsProcessingParameterCoordinateOperation : QgsProcessingParameterDefiniti
A coordinate operation parameter for processing algorithms, for selection between available
coordinate operations to use when projecting between a source and destination coordinate reference system.

QgsProcessingParameterCoordinateOperation should be evaluated by calling :py:func:`QgsProcessingAlgorithm.parameterAsString()`

.. versionadded:: 3.12
%End

@@ -3358,6 +3362,56 @@ Sets the static destination ``crs``.
};


class QgsProcessingParameterMapTheme : QgsProcessingParameterDefinition
{
%Docstring
A map theme parameter for processing algorithms, allowing users to select an existing map theme from a project.

QgsProcessingParameterMapTheme should be evaluated by calling :py:func:`QgsProcessingAlgorithm.parameterAsString()`

.. versionadded:: 3.12
%End

%TypeHeaderCode
#include "qgsprocessingparameters.h"
%End
public:

QgsProcessingParameterMapTheme( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
bool optional = false );
%Docstring
Constructor for QgsProcessingParameterMapTheme.
%End

static QString typeName();
%Docstring
Returns the type name for the parameter class.
%End
virtual QgsProcessingParameterDefinition *clone() const /Factory/;

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;

virtual QString asScriptCode() const;

virtual QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const;

virtual QVariantMap toVariantMap() const;

virtual bool fromVariantMap( const QVariantMap &map );


static QgsProcessingParameterMapTheme *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/;
%Docstring
Creates a new parameter using the definition from a script code.
%End

};






@@ -94,6 +94,12 @@ void QgsRasterizeAlgorithm::initAlgorithm( const QVariantMap & )
QStringLiteral( "MAP_THEME" ),
QObject::tr( "Map theme to render" ),
QVariant(), false, true );

QVariantMap metadataClass;
metadataClass.insert( QStringLiteral( "class" ), QStringLiteral( "processing.gui.wrappers_map_theme.MapThemeWrapper" ) );
QVariantMap metadata;
metadata.insert( QStringLiteral( "widget_wrapper" ), metadataClass );
mapThemeParam->setMetadata( metadata );
addParameter( mapThemeParam.release() );

QList<QgsMapLayer *> projectLayers { QgsProject::instance()->mapLayers().values() };
@@ -1708,7 +1708,6 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromVariantM
def.reset( new QgsProcessingParameterColor( name ) );
else if ( type == QgsProcessingParameterCoordinateOperation::typeName() )
def.reset( new QgsProcessingParameterCoordinateOperation( name ) );
else
{
QgsProcessingParameterType *paramType = QgsApplication::instance()->processingRegistry()->parameterType( type );
if ( paramType )
@@ -1804,6 +1803,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromScriptCo
return QgsProcessingParameterColor::fromScriptCode( name, description, isOptional, definition );
else if ( type == QStringLiteral( "coordinateoperation" ) )
return QgsProcessingParameterCoordinateOperation::fromScriptCode( name, description, isOptional, definition );
else if ( type == QStringLiteral( "maptheme" ) )
return QgsProcessingParameterMapTheme::fromScriptCode( name, description, isOptional, definition );

return nullptr;
}
@@ -6033,3 +6034,103 @@ QgsProcessingParameterCoordinateOperation *QgsProcessingParameterCoordinateOpera

return new QgsProcessingParameterCoordinateOperation( name, description, defaultValue, QString(), QString(), QVariant(), QVariant(), isOptional );
}


//
// QgsProcessingParameterMapTheme
//

QgsProcessingParameterMapTheme::QgsProcessingParameterMapTheme( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
{

}


QgsProcessingParameterDefinition *QgsProcessingParameterMapTheme::clone() const
{
return new QgsProcessingParameterMapTheme( *this );
}

bool QgsProcessingParameterMapTheme::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
{
if ( !input.isValid() && !mDefault.isValid() )
return mFlags & FlagOptional;

if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
|| ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
return mFlags & FlagOptional;

return true;
}

QString QgsProcessingParameterMapTheme::valueAsPythonString( const QVariant &value, QgsProcessingContext & ) const
{
if ( !value.isValid() )
return QStringLiteral( "None" );

if ( value.canConvert<QgsProperty>() )
return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );

return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
}

QString QgsProcessingParameterMapTheme::asScriptCode() const
{
QString code = QStringLiteral( "##%1=" ).arg( mName );
if ( mFlags & FlagOptional )
code += QStringLiteral( "optional " );
code += QStringLiteral( "maptheme " );

code += mDefault.toString();
return code.trimmed();
}

QString QgsProcessingParameterMapTheme::asPythonString( const QgsProcessing::PythonOutputType outputType ) const
{
switch ( outputType )
{
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
{
QString code = QStringLiteral( "QgsProcessingParameterMapTheme('%1', '%2'" ).arg( name(), description() );
if ( mFlags & FlagOptional )
code += QStringLiteral( ", optional=True" );

QgsProcessingContext c;
code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );

return code;
}
}
return QString();
}

QVariantMap QgsProcessingParameterMapTheme::toVariantMap() const
{
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
return map;
}

bool QgsProcessingParameterMapTheme::fromVariantMap( const QVariantMap &map )
{
QgsProcessingParameterDefinition::fromVariantMap( map );
return true;
}

QgsProcessingParameterMapTheme *QgsProcessingParameterMapTheme::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
{
QString parent;

QString def = definition;
if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
def = def.mid( 1 );
if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
def.chop( 1 );

QVariant defaultValue = def;

if ( defaultValue == QStringLiteral( "None" ) || defaultValue.toString().isEmpty() )
defaultValue = QVariant();

return new QgsProcessingParameterMapTheme( name, description, defaultValue, isOptional );
}
@@ -270,6 +270,8 @@ class CORE_EXPORT QgsProcessingParameterDefinition
sipType = sipType_QgsProcessingParameterColor;
else if ( sipCpp->type() == QgsProcessingParameterCoordinateOperation::typeName() )
sipType = sipType_QgsProcessingParameterCoordinateOperation;
else if ( sipCpp->type() == QgsProcessingParameterMapTheme::typeName() )
sipType = sipType_QgsProcessingParameterMapTheme;
else
sipType = nullptr;
SIP_END
@@ -3033,7 +3035,9 @@ class CORE_EXPORT QgsProcessingParameterColor : public QgsProcessingParameterDef
* \ingroup core
* A coordinate operation parameter for processing algorithms, for selection between available
* coordinate operations to use when projecting between a source and destination coordinate reference system.
* \since QGIS 3.12
*
* QgsProcessingParameterCoordinateOperation should be evaluated by calling QgsProcessingAlgorithm::parameterAsString().
* \since QGIS 3.12
*/
class CORE_EXPORT QgsProcessingParameterCoordinateOperation : public QgsProcessingParameterDefinition
{
@@ -3131,6 +3135,49 @@ class CORE_EXPORT QgsProcessingParameterCoordinateOperation : public QgsProcessi
};


/**
* \class QgsProcessingParameterMapTheme
* \ingroup core
* A map theme parameter for processing algorithms, allowing users to select an existing map theme from a project.
*
* QgsProcessingParameterMapTheme should be evaluated by calling QgsProcessingAlgorithm::parameterAsString().
*
* \since QGIS 3.12
*/
class CORE_EXPORT QgsProcessingParameterMapTheme : public QgsProcessingParameterDefinition
{
public:

/**
* Constructor for QgsProcessingParameterMapTheme.
*/
QgsProcessingParameterMapTheme( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
bool optional = false );

/**
* Returns the type name for the parameter class.
*/
static QString typeName() { return QStringLiteral( "maptheme" ); }
QgsProcessingParameterDefinition *clone() const override SIP_FACTORY;
QString type() const override { return typeName(); }
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
QString asScriptCode() const override;
QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const override;
QVariantMap toVariantMap() const override;
bool fromVariantMap( const QVariantMap &map ) override;

/**
* Creates a new parameter using the definition from a script code.
*/
static QgsProcessingParameterMapTheme *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) SIP_FACTORY;

private:

};



// clazy:excludeall=qstring-allocations

#endif // QGSPROCESSINGPARAMETERS_H
@@ -1729,5 +1729,58 @@ class CORE_EXPORT QgsProcessingParameterTypeCoordinateOperation : public QgsProc
}


};


/**
* A map themeparameter for Processing algorithms.
*
* \ingroup core
* \note No Python bindings available. Get your copy from QgsApplication.processingRegistry().parameterType('maptheme')
* \since QGIS 3.12
*/
class CORE_EXPORT QgsProcessingParameterTypeMapTheme: public QgsProcessingParameterType
{
QgsProcessingParameterDefinition *create( const QString &name ) const override SIP_FACTORY
{
return new QgsProcessingParameterMapTheme( name );
}

QString description() const override
{
return QCoreApplication::translate( "Processing", "A map theme parameter." );
}

QString name() const override
{
return QCoreApplication::translate( "Processing", "Map Theme" );
}

QString id() const override
{
return QStringLiteral( "maptheme" );
}

QString pythonImportString() const override
{
return QStringLiteral( "from qgis.core import QgsProcessingParameterMapTheme" );
}

QString className() const override
{
return QStringLiteral( "QgsProcessingParameterMapTheme" );
}

QStringList acceptedPythonTypes() const override
{
return QStringList() << QObject::tr( "str: name of an existing map theme" )
<< QStringLiteral( "QgsProperty" );
}

QStringList acceptedStringValues() const override
{
return QStringList() << QObject::tr( "Name of an existing map theme" );
}

};
#endif // QGSPROCESSINGPARAMETERTYPEIMPL_H
@@ -55,6 +55,7 @@ QgsProcessingRegistry::QgsProcessingRegistry( QObject *parent SIP_TRANSFERTHIS )
#if PROJ_VERSION_MAJOR>=6
addParameterType( new QgsProcessingParameterTypeCoordinateOperation() );
#endif
addParameterType( new QgsProcessingParameterTypeMapTheme() );
}

QgsProcessingRegistry::~QgsProcessingRegistry()

0 comments on commit 54629b8

Please sign in to comment.
You can’t perform that action at this time.