Skip to content

Commit

Permalink
[processing] Add new parameter type for layout item objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 11, 2019
1 parent dd49720 commit ba82954
Show file tree
Hide file tree
Showing 6 changed files with 471 additions and 0 deletions.
Expand Up @@ -185,6 +185,8 @@ their acceptable ranges, defaults, etc.
sipType = sipType_QgsProcessingParameterBand;
else if ( sipCpp->type() == QgsProcessingParameterLayout::typeName() )
sipType = sipType_QgsProcessingParameterLayout;
else if ( sipCpp->type() == QgsProcessingParameterLayoutItem::typeName() )
sipType = sipType_QgsProcessingParameterLayoutItem;
else
sipType = nullptr;
%End
Expand Down Expand Up @@ -2792,6 +2794,90 @@ Creates a new parameter using the definition from a script code.

};

class QgsProcessingParameterLayoutItem : QgsProcessingParameterDefinition
{
%Docstring
A print layout item parameter, allowing users to select a particular item from a print layout.

QgsProcessingParameterLayoutItem should be evaluated by calling :py:func:`QgsProcessingAlgorithm.parameterAsLayoutItem()`
Internally, QgsProcessingParameterLayoutItems are string parameters, storing references to items either by
their UUID (QgsLayoutItem.uuid()) or ID (QgsLayoutItem.id()).

.. versionadded:: 3.8
%End

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

QgsProcessingParameterLayoutItem( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
const QString &parentLayoutParameterName = QString(),
int itemType = -1,
bool optional = false );
%Docstring
Constructor for QgsProcessingParameterLayoutItem.
%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;

virtual QVariantMap toVariantMap() const;

virtual bool fromVariantMap( const QVariantMap &map );

virtual QStringList dependsOnOtherParameters() const;


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

QString parentLayoutParameterName() const;
%Docstring
Returns the name of the parent layout parameter, or an empty string if this is not set.

.. seealso:: :py:func:`setParentLayoutParameterName`
%End

void setParentLayoutParameterName( const QString &name );
%Docstring
Sets the ``name`` of the parent layout parameter. Use an empty string if this is not required.

.. seealso:: :py:func:`parentLayoutParameterName`
%End

int itemType() const;
%Docstring
Returns the acceptable item type, or -1 if any item type is allowed.

These values correspond to the registered item types from :py:class:`QgsLayoutItemRegistry`.

.. seealso:: :py:func:`setItemType`
%End

void setItemType( int type );
%Docstring
Sets the acceptable item ``type``, or -1 if any item type is allowed.

These values correspond to the registered item types from :py:class:`QgsLayoutItemRegistry`.

.. seealso:: :py:func:`itemType`
%End

};




Expand Down
138 changes: 138 additions & 0 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -1521,6 +1521,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromVariantM
def.reset( new QgsProcessingParameterMeshLayer( name ) );
else if ( type == QgsProcessingParameterLayout::typeName() )
def.reset( new QgsProcessingParameterLayout( name ) );
else if ( type == QgsProcessingParameterLayoutItem::typeName() )
def.reset( new QgsProcessingParameterLayoutItem( name ) );
else
{
QgsProcessingParameterType *paramType = QgsApplication::instance()->processingRegistry()->parameterType( type );
Expand Down Expand Up @@ -1607,6 +1609,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromScriptCo
return QgsProcessingParameterMeshLayer::fromScriptCode( name, description, isOptional, definition );
else if ( type == QStringLiteral( "layout" ) )
return QgsProcessingParameterLayout::fromScriptCode( name, description, isOptional, definition );
else if ( type == QStringLiteral( "layoutitem" ) )
return QgsProcessingParameterLayoutItem::fromScriptCode( name, description, isOptional, definition );

return nullptr;
}
Expand Down Expand Up @@ -5272,3 +5276,137 @@ QgsProcessingParameterLayout *QgsProcessingParameterLayout::fromScriptCode( cons

return new QgsProcessingParameterLayout( name, description, defaultValue, isOptional );
}


//
// QString mParentLayerParameterName;
//

QgsProcessingParameterLayoutItem::QgsProcessingParameterLayoutItem( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayoutParameterName, int itemType, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
, mParentLayoutParameterName( parentLayoutParameterName )
, mItemType( itemType )
{

}

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

QString QgsProcessingParameterLayoutItem::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 QgsProcessingParameterLayoutItem::asScriptCode() const
{
QString code = QStringLiteral( "##%1=" ).arg( mName );
if ( mFlags & FlagOptional )
code += QStringLiteral( "optional " );
code += QStringLiteral( "layoutitem " );
if ( mItemType >= 0 )
code += QString::number( mItemType ) + ' ';

code += mParentLayoutParameterName + ' ';

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

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

if ( mItemType >= 0 )
code += QStringLiteral( ", itemType=%1" ).arg( mItemType );

code += QStringLiteral( ", parentLayoutParameterName='%1'" ).arg( mParentLayoutParameterName );

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

QVariantMap QgsProcessingParameterLayoutItem::toVariantMap() const
{
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
map.insert( QStringLiteral( "parent_layout" ), mParentLayoutParameterName );
map.insert( QStringLiteral( "item_type" ), mItemType );
return map;
}

bool QgsProcessingParameterLayoutItem::fromVariantMap( const QVariantMap &map )
{
QgsProcessingParameterDefinition::fromVariantMap( map );
mParentLayoutParameterName = map.value( QStringLiteral( "parent_layout" ) ).toString();
mItemType = map.value( QStringLiteral( "item_type" ) ).toInt();
return true;
}

QStringList QgsProcessingParameterLayoutItem::dependsOnOtherParameters() const
{
QStringList depends;
if ( !mParentLayoutParameterName.isEmpty() )
depends << mParentLayoutParameterName;
return depends;
}

QgsProcessingParameterLayoutItem *QgsProcessingParameterLayoutItem::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
{
QString parent;
QString def = definition;
int itemType = -1;
QRegularExpression re( QStringLiteral( "(\\d+)?\\s*(.*?)\\s+(.*)$" ) );
QRegularExpressionMatch m = re.match( def );
if ( m.hasMatch() )
{
itemType = m.captured( 1 ).trimmed().isEmpty() ? -1 : m.captured( 1 ).trimmed().toInt();
parent = m.captured( 2 ).trimmed().isEmpty() ? m.captured( 3 ).trimmed() : m.captured( 2 ).trimmed();
def = !m.captured( 2 ).trimmed().isEmpty() ? m.captured( 3 ) : QString();
}
else
{
parent = def;
def.clear();
}

return new QgsProcessingParameterLayoutItem( name, description, def.isEmpty() ? QVariant() : def, parent, itemType, isOptional );
}

QString QgsProcessingParameterLayoutItem::parentLayoutParameterName() const
{
return mParentLayoutParameterName;
}

void QgsProcessingParameterLayoutItem::setParentLayoutParameterName( const QString &name )
{
mParentLayoutParameterName = name;
}

int QgsProcessingParameterLayoutItem::itemType() const
{
return mItemType;
}

void QgsProcessingParameterLayoutItem::setItemType( int type )
{
mItemType = type;
}
78 changes: 78 additions & 0 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -260,6 +260,8 @@ class CORE_EXPORT QgsProcessingParameterDefinition
sipType = sipType_QgsProcessingParameterBand;
else if ( sipCpp->type() == QgsProcessingParameterLayout::typeName() )
sipType = sipType_QgsProcessingParameterLayout;
else if ( sipCpp->type() == QgsProcessingParameterLayoutItem::typeName() )
sipType = sipType_QgsProcessingParameterLayoutItem;
else
sipType = nullptr;
SIP_END
Expand Down Expand Up @@ -2621,6 +2623,82 @@ class CORE_EXPORT QgsProcessingParameterLayout : public QgsProcessingParameterDe

};

/**
* \class QgsProcessingParameterLayoutItem
* \ingroup core
* A print layout item parameter, allowing users to select a particular item from a print layout.
*
* QgsProcessingParameterLayoutItem should be evaluated by calling QgsProcessingAlgorithm::parameterAsLayoutItem().
* Internally, QgsProcessingParameterLayoutItems are string parameters, storing references to items either by
* their UUID (QgsLayoutItem::uuid()) or ID (QgsLayoutItem::id()).
*
* \since QGIS 3.8
*/
class CORE_EXPORT QgsProcessingParameterLayoutItem : public QgsProcessingParameterDefinition
{
public:

/**
* Constructor for QgsProcessingParameterLayoutItem.
*/
QgsProcessingParameterLayoutItem( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
const QString &parentLayoutParameterName = QString(),
int itemType = -1,
bool optional = false );

/**
* Returns the type name for the parameter class.
*/
static QString typeName() { return QStringLiteral( "layoutitem" ); }
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;
QVariantMap toVariantMap() const override;
bool fromVariantMap( const QVariantMap &map ) override;
QStringList dependsOnOtherParameters() const override;

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

/**
* Returns the name of the parent layout parameter, or an empty string if this is not set.
* \see setParentLayoutParameterName()
*/
QString parentLayoutParameterName() const;

/**
* Sets the \a name of the parent layout parameter. Use an empty string if this is not required.
* \see parentLayoutParameterName()
*/
void setParentLayoutParameterName( const QString &name );

/**
* Returns the acceptable item type, or -1 if any item type is allowed.
*
* These values correspond to the registered item types from QgsLayoutItemRegistry.
*
* \see setItemType()
*/
int itemType() const;

/**
* Sets the acceptable item \a type, or -1 if any item type is allowed.
*
* These values correspond to the registered item types from QgsLayoutItemRegistry.
*
* \see itemType()
*/
void setItemType( int type );

private:
QString mParentLayoutParameterName;
int mItemType = -1;
};

// clazy:excludeall=qstring-allocations

#endif // QGSPROCESSINGPARAMETERS_H
Expand Down

0 comments on commit ba82954

Please sign in to comment.