Skip to content
Permalink
Browse files

[processing] Add new parameter type for print layout names

  • Loading branch information
nyalldawson committed Mar 11, 2019
1 parent 5ec43cf commit dd497208630526e67dc3218825fa4fd4c529e154
@@ -183,6 +183,8 @@ their acceptable ranges, defaults, etc.
sipType = sipType_QgsProcessingParameterFolderDestination;
else if ( sipCpp->type() == QgsProcessingParameterBand::typeName() )
sipType = sipType_QgsProcessingParameterBand;
else if ( sipCpp->type() == QgsProcessingParameterLayout::typeName() )
sipType = sipType_QgsProcessingParameterLayout;
else
sipType = nullptr;
%End
@@ -2747,6 +2749,49 @@ Sets whether multiple band selections are permitted.

};

class QgsProcessingParameterLayout : QgsProcessingParameterDefinition
{
%Docstring
A print layout parameter, allowing users to select a print layout.

QgsProcessingParameterLayout should be evaluated by calling :py:func:`QgsProcessingAlgorithm.parameterAsString()`
This will return the name of the target print layout.

.. versionadded:: 3.8
%End

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

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

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

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

virtual QString asScriptCode() const;

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


static QgsProcessingParameterLayout *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

};




@@ -1519,6 +1519,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromVariantM
def.reset( new QgsProcessingParameterBand( name ) );
else if ( type == QgsProcessingParameterMeshLayer::typeName() )
def.reset( new QgsProcessingParameterMeshLayer( name ) );
else if ( type == QgsProcessingParameterLayout::typeName() )
def.reset( new QgsProcessingParameterLayout( name ) );
else
{
QgsProcessingParameterType *paramType = QgsApplication::instance()->processingRegistry()->parameterType( type );
@@ -1603,6 +1605,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromScriptCo
return QgsProcessingParameterBand::fromScriptCode( name, description, isOptional, definition );
else if ( type == QStringLiteral( "mesh" ) )
return QgsProcessingParameterMeshLayer::fromScriptCode( name, description, isOptional, definition );
else if ( type == QStringLiteral( "layout" ) )
return QgsProcessingParameterLayout::fromScriptCode( name, description, isOptional, definition );

return nullptr;
}
@@ -5198,3 +5202,73 @@ bool QgsProcessingParameterDistance::fromVariantMap( const QVariantMap &map )
mDefaultUnit = static_cast< QgsUnitTypes::DistanceUnit>( map.value( QStringLiteral( "default_unit" ), QgsUnitTypes::DistanceUnknownUnit ).toInt() );
return true;
}


//
// QgsProcessingParameterLayout
//

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

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

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

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

QString s = value.toString();
return QgsProcessingUtils::stringToPythonLiteral( s );
}

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

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

QString QgsProcessingParameterLayout::asPythonString( const QgsProcessing::PythonOutputType outputType ) const
{
switch ( outputType )
{
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
{
QString code = QStringLiteral( "QgsProcessingParameterLayout('%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();
}

QgsProcessingParameterLayout *QgsProcessingParameterLayout::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
{
QString def = definition;

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

QVariant defaultValue = def;
if ( def == QStringLiteral( "None" ) )
defaultValue = QVariant();

return new QgsProcessingParameterLayout( name, description, defaultValue, isOptional );
}
@@ -258,6 +258,8 @@ class CORE_EXPORT QgsProcessingParameterDefinition
sipType = sipType_QgsProcessingParameterFolderDestination;
else if ( sipCpp->type() == QgsProcessingParameterBand::typeName() )
sipType = sipType_QgsProcessingParameterBand;
else if ( sipCpp->type() == QgsProcessingParameterLayout::typeName() )
sipType = sipType_QgsProcessingParameterLayout;
else
sipType = nullptr;
SIP_END
@@ -2582,6 +2584,43 @@ class CORE_EXPORT QgsProcessingParameterBand : public QgsProcessingParameterDefi
bool mAllowMultiple = false;
};

/**
* \class QgsProcessingParameterLayout
* \ingroup core
* A print layout parameter, allowing users to select a print layout.
*
* QgsProcessingParameterLayout should be evaluated by calling QgsProcessingAlgorithm::parameterAsString().
* This will return the name of the target print layout.
*
* \since QGIS 3.8
*/
class CORE_EXPORT QgsProcessingParameterLayout : public QgsProcessingParameterDefinition
{
public:

/**
* Constructor for QgsProcessingParameterLayout.
*/
QgsProcessingParameterLayout( 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( "layout" ); }
QgsProcessingParameterDefinition *clone() const override SIP_FACTORY;
QString type() const override { return typeName(); }
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
QString asScriptCode() const override;
QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const override;

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

};

// clazy:excludeall=qstring-allocations

#endif // QGSPROCESSINGPARAMETERS_H
@@ -1458,4 +1458,57 @@ class CORE_EXPORT QgsProcessingParameterTypeFeatureSink : public QgsProcessingPa

};


/**
* A print layout parameter for Processing algorithms.
*
* \ingroup core
* \note No Python bindings available. Get your copy from QgsApplication.processingRegistry().parameterType('layout')
* \since QGIS 3.8
*/
class CORE_EXPORT QgsProcessingParameterTypeLayout : public QgsProcessingParameterType
{
QgsProcessingParameterDefinition *create( const QString &name ) const override SIP_FACTORY
{
return new QgsProcessingParameterLayout( name );
}

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

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

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

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

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

QStringList acceptedPythonTypes() const override
{
return QStringList() << QObject::tr( "str: name of print layout in current project" )
<< QStringLiteral( "QgsProperty" );
}

QStringList acceptedStringValues() const override
{
return QStringList() << QObject::tr( "Name of print layout in current project" );
}


};
#endif // QGSPROCESSINGPARAMETERTYPEIMPL_H
@@ -48,6 +48,7 @@ QgsProcessingRegistry::QgsProcessingRegistry( QObject *parent SIP_TRANSFERTHIS )
addParameterType( new QgsProcessingParameterTypeDistance() );
addParameterType( new QgsProcessingParameterTypeBand() );
addParameterType( new QgsProcessingParameterTypeFeatureSink() );
addParameterType( new QgsProcessingParameterTypeLayout() );
}

QgsProcessingRegistry::~QgsProcessingRegistry()

0 comments on commit dd49720

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