Skip to content

Commit ba82954

Browse files
committed
[processing] Add new parameter type for layout item objects
1 parent dd49720 commit ba82954

File tree

6 files changed

+471
-0
lines changed

6 files changed

+471
-0
lines changed

python/core/auto_generated/processing/qgsprocessingparameters.sip.in

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ their acceptable ranges, defaults, etc.
185185
sipType = sipType_QgsProcessingParameterBand;
186186
else if ( sipCpp->type() == QgsProcessingParameterLayout::typeName() )
187187
sipType = sipType_QgsProcessingParameterLayout;
188+
else if ( sipCpp->type() == QgsProcessingParameterLayoutItem::typeName() )
189+
sipType = sipType_QgsProcessingParameterLayoutItem;
188190
else
189191
sipType = nullptr;
190192
%End
@@ -2792,6 +2794,90 @@ Creates a new parameter using the definition from a script code.
27922794

27932795
};
27942796

2797+
class QgsProcessingParameterLayoutItem : QgsProcessingParameterDefinition
2798+
{
2799+
%Docstring
2800+
A print layout item parameter, allowing users to select a particular item from a print layout.
2801+
2802+
QgsProcessingParameterLayoutItem should be evaluated by calling :py:func:`QgsProcessingAlgorithm.parameterAsLayoutItem()`
2803+
Internally, QgsProcessingParameterLayoutItems are string parameters, storing references to items either by
2804+
their UUID (QgsLayoutItem.uuid()) or ID (QgsLayoutItem.id()).
2805+
2806+
.. versionadded:: 3.8
2807+
%End
2808+
2809+
%TypeHeaderCode
2810+
#include "qgsprocessingparameters.h"
2811+
%End
2812+
public:
2813+
2814+
QgsProcessingParameterLayoutItem( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
2815+
const QString &parentLayoutParameterName = QString(),
2816+
int itemType = -1,
2817+
bool optional = false );
2818+
%Docstring
2819+
Constructor for QgsProcessingParameterLayoutItem.
2820+
%End
2821+
2822+
static QString typeName();
2823+
%Docstring
2824+
Returns the type name for the parameter class.
2825+
%End
2826+
virtual QgsProcessingParameterDefinition *clone() const /Factory/;
2827+
2828+
virtual QString type() const;
2829+
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
2830+
2831+
virtual QString asScriptCode() const;
2832+
2833+
virtual QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const;
2834+
2835+
virtual QVariantMap toVariantMap() const;
2836+
2837+
virtual bool fromVariantMap( const QVariantMap &map );
2838+
2839+
virtual QStringList dependsOnOtherParameters() const;
2840+
2841+
2842+
static QgsProcessingParameterLayoutItem *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/;
2843+
%Docstring
2844+
Creates a new parameter using the definition from a script code.
2845+
%End
2846+
2847+
QString parentLayoutParameterName() const;
2848+
%Docstring
2849+
Returns the name of the parent layout parameter, or an empty string if this is not set.
2850+
2851+
.. seealso:: :py:func:`setParentLayoutParameterName`
2852+
%End
2853+
2854+
void setParentLayoutParameterName( const QString &name );
2855+
%Docstring
2856+
Sets the ``name`` of the parent layout parameter. Use an empty string if this is not required.
2857+
2858+
.. seealso:: :py:func:`parentLayoutParameterName`
2859+
%End
2860+
2861+
int itemType() const;
2862+
%Docstring
2863+
Returns the acceptable item type, or -1 if any item type is allowed.
2864+
2865+
These values correspond to the registered item types from :py:class:`QgsLayoutItemRegistry`.
2866+
2867+
.. seealso:: :py:func:`setItemType`
2868+
%End
2869+
2870+
void setItemType( int type );
2871+
%Docstring
2872+
Sets the acceptable item ``type``, or -1 if any item type is allowed.
2873+
2874+
These values correspond to the registered item types from :py:class:`QgsLayoutItemRegistry`.
2875+
2876+
.. seealso:: :py:func:`itemType`
2877+
%End
2878+
2879+
};
2880+
27952881

27962882

27972883

src/core/processing/qgsprocessingparameters.cpp

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromVariantM
15211521
def.reset( new QgsProcessingParameterMeshLayer( name ) );
15221522
else if ( type == QgsProcessingParameterLayout::typeName() )
15231523
def.reset( new QgsProcessingParameterLayout( name ) );
1524+
else if ( type == QgsProcessingParameterLayoutItem::typeName() )
1525+
def.reset( new QgsProcessingParameterLayoutItem( name ) );
15241526
else
15251527
{
15261528
QgsProcessingParameterType *paramType = QgsApplication::instance()->processingRegistry()->parameterType( type );
@@ -1607,6 +1609,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromScriptCo
16071609
return QgsProcessingParameterMeshLayer::fromScriptCode( name, description, isOptional, definition );
16081610
else if ( type == QStringLiteral( "layout" ) )
16091611
return QgsProcessingParameterLayout::fromScriptCode( name, description, isOptional, definition );
1612+
else if ( type == QStringLiteral( "layoutitem" ) )
1613+
return QgsProcessingParameterLayoutItem::fromScriptCode( name, description, isOptional, definition );
16101614

16111615
return nullptr;
16121616
}
@@ -5272,3 +5276,137 @@ QgsProcessingParameterLayout *QgsProcessingParameterLayout::fromScriptCode( cons
52725276

52735277
return new QgsProcessingParameterLayout( name, description, defaultValue, isOptional );
52745278
}
5279+
5280+
5281+
//
5282+
// QString mParentLayerParameterName;
5283+
//
5284+
5285+
QgsProcessingParameterLayoutItem::QgsProcessingParameterLayoutItem( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayoutParameterName, int itemType, bool optional )
5286+
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5287+
, mParentLayoutParameterName( parentLayoutParameterName )
5288+
, mItemType( itemType )
5289+
{
5290+
5291+
}
5292+
5293+
QgsProcessingParameterDefinition *QgsProcessingParameterLayoutItem::clone() const
5294+
{
5295+
return new QgsProcessingParameterLayoutItem( *this );
5296+
}
5297+
5298+
QString QgsProcessingParameterLayoutItem::valueAsPythonString( const QVariant &value, QgsProcessingContext & ) const
5299+
{
5300+
if ( !value.isValid() || value.isNull() )
5301+
return QStringLiteral( "None" );
5302+
5303+
if ( value.canConvert<QgsProperty>() )
5304+
return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
5305+
5306+
QString s = value.toString();
5307+
return QgsProcessingUtils::stringToPythonLiteral( s );
5308+
}
5309+
5310+
QString QgsProcessingParameterLayoutItem::asScriptCode() const
5311+
{
5312+
QString code = QStringLiteral( "##%1=" ).arg( mName );
5313+
if ( mFlags & FlagOptional )
5314+
code += QStringLiteral( "optional " );
5315+
code += QStringLiteral( "layoutitem " );
5316+
if ( mItemType >= 0 )
5317+
code += QString::number( mItemType ) + ' ';
5318+
5319+
code += mParentLayoutParameterName + ' ';
5320+
5321+
code += mDefault.toString();
5322+
return code.trimmed();
5323+
}
5324+
5325+
QString QgsProcessingParameterLayoutItem::asPythonString( QgsProcessing::PythonOutputType outputType ) const
5326+
{
5327+
switch ( outputType )
5328+
{
5329+
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
5330+
{
5331+
QString code = QStringLiteral( "QgsProcessingParameterLayoutItem('%1', '%2'" ).arg( name(), description() );
5332+
if ( mFlags & FlagOptional )
5333+
code += QStringLiteral( ", optional=True" );
5334+
5335+
if ( mItemType >= 0 )
5336+
code += QStringLiteral( ", itemType=%1" ).arg( mItemType );
5337+
5338+
code += QStringLiteral( ", parentLayoutParameterName='%1'" ).arg( mParentLayoutParameterName );
5339+
5340+
QgsProcessingContext c;
5341+
code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
5342+
return code;
5343+
}
5344+
}
5345+
return QString();
5346+
}
5347+
5348+
QVariantMap QgsProcessingParameterLayoutItem::toVariantMap() const
5349+
{
5350+
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
5351+
map.insert( QStringLiteral( "parent_layout" ), mParentLayoutParameterName );
5352+
map.insert( QStringLiteral( "item_type" ), mItemType );
5353+
return map;
5354+
}
5355+
5356+
bool QgsProcessingParameterLayoutItem::fromVariantMap( const QVariantMap &map )
5357+
{
5358+
QgsProcessingParameterDefinition::fromVariantMap( map );
5359+
mParentLayoutParameterName = map.value( QStringLiteral( "parent_layout" ) ).toString();
5360+
mItemType = map.value( QStringLiteral( "item_type" ) ).toInt();
5361+
return true;
5362+
}
5363+
5364+
QStringList QgsProcessingParameterLayoutItem::dependsOnOtherParameters() const
5365+
{
5366+
QStringList depends;
5367+
if ( !mParentLayoutParameterName.isEmpty() )
5368+
depends << mParentLayoutParameterName;
5369+
return depends;
5370+
}
5371+
5372+
QgsProcessingParameterLayoutItem *QgsProcessingParameterLayoutItem::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5373+
{
5374+
QString parent;
5375+
QString def = definition;
5376+
int itemType = -1;
5377+
QRegularExpression re( QStringLiteral( "(\\d+)?\\s*(.*?)\\s+(.*)$" ) );
5378+
QRegularExpressionMatch m = re.match( def );
5379+
if ( m.hasMatch() )
5380+
{
5381+
itemType = m.captured( 1 ).trimmed().isEmpty() ? -1 : m.captured( 1 ).trimmed().toInt();
5382+
parent = m.captured( 2 ).trimmed().isEmpty() ? m.captured( 3 ).trimmed() : m.captured( 2 ).trimmed();
5383+
def = !m.captured( 2 ).trimmed().isEmpty() ? m.captured( 3 ) : QString();
5384+
}
5385+
else
5386+
{
5387+
parent = def;
5388+
def.clear();
5389+
}
5390+
5391+
return new QgsProcessingParameterLayoutItem( name, description, def.isEmpty() ? QVariant() : def, parent, itemType, isOptional );
5392+
}
5393+
5394+
QString QgsProcessingParameterLayoutItem::parentLayoutParameterName() const
5395+
{
5396+
return mParentLayoutParameterName;
5397+
}
5398+
5399+
void QgsProcessingParameterLayoutItem::setParentLayoutParameterName( const QString &name )
5400+
{
5401+
mParentLayoutParameterName = name;
5402+
}
5403+
5404+
int QgsProcessingParameterLayoutItem::itemType() const
5405+
{
5406+
return mItemType;
5407+
}
5408+
5409+
void QgsProcessingParameterLayoutItem::setItemType( int type )
5410+
{
5411+
mItemType = type;
5412+
}

src/core/processing/qgsprocessingparameters.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ class CORE_EXPORT QgsProcessingParameterDefinition
260260
sipType = sipType_QgsProcessingParameterBand;
261261
else if ( sipCpp->type() == QgsProcessingParameterLayout::typeName() )
262262
sipType = sipType_QgsProcessingParameterLayout;
263+
else if ( sipCpp->type() == QgsProcessingParameterLayoutItem::typeName() )
264+
sipType = sipType_QgsProcessingParameterLayoutItem;
263265
else
264266
sipType = nullptr;
265267
SIP_END
@@ -2621,6 +2623,82 @@ class CORE_EXPORT QgsProcessingParameterLayout : public QgsProcessingParameterDe
26212623

26222624
};
26232625

2626+
/**
2627+
* \class QgsProcessingParameterLayoutItem
2628+
* \ingroup core
2629+
* A print layout item parameter, allowing users to select a particular item from a print layout.
2630+
*
2631+
* QgsProcessingParameterLayoutItem should be evaluated by calling QgsProcessingAlgorithm::parameterAsLayoutItem().
2632+
* Internally, QgsProcessingParameterLayoutItems are string parameters, storing references to items either by
2633+
* their UUID (QgsLayoutItem::uuid()) or ID (QgsLayoutItem::id()).
2634+
*
2635+
* \since QGIS 3.8
2636+
*/
2637+
class CORE_EXPORT QgsProcessingParameterLayoutItem : public QgsProcessingParameterDefinition
2638+
{
2639+
public:
2640+
2641+
/**
2642+
* Constructor for QgsProcessingParameterLayoutItem.
2643+
*/
2644+
QgsProcessingParameterLayoutItem( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
2645+
const QString &parentLayoutParameterName = QString(),
2646+
int itemType = -1,
2647+
bool optional = false );
2648+
2649+
/**
2650+
* Returns the type name for the parameter class.
2651+
*/
2652+
static QString typeName() { return QStringLiteral( "layoutitem" ); }
2653+
QgsProcessingParameterDefinition *clone() const override SIP_FACTORY;
2654+
QString type() const override { return typeName(); }
2655+
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
2656+
QString asScriptCode() const override;
2657+
QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const override;
2658+
QVariantMap toVariantMap() const override;
2659+
bool fromVariantMap( const QVariantMap &map ) override;
2660+
QStringList dependsOnOtherParameters() const override;
2661+
2662+
/**
2663+
* Creates a new parameter using the definition from a script code.
2664+
*/
2665+
static QgsProcessingParameterLayoutItem *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) SIP_FACTORY;
2666+
2667+
/**
2668+
* Returns the name of the parent layout parameter, or an empty string if this is not set.
2669+
* \see setParentLayoutParameterName()
2670+
*/
2671+
QString parentLayoutParameterName() const;
2672+
2673+
/**
2674+
* Sets the \a name of the parent layout parameter. Use an empty string if this is not required.
2675+
* \see parentLayoutParameterName()
2676+
*/
2677+
void setParentLayoutParameterName( const QString &name );
2678+
2679+
/**
2680+
* Returns the acceptable item type, or -1 if any item type is allowed.
2681+
*
2682+
* These values correspond to the registered item types from QgsLayoutItemRegistry.
2683+
*
2684+
* \see setItemType()
2685+
*/
2686+
int itemType() const;
2687+
2688+
/**
2689+
* Sets the acceptable item \a type, or -1 if any item type is allowed.
2690+
*
2691+
* These values correspond to the registered item types from QgsLayoutItemRegistry.
2692+
*
2693+
* \see itemType()
2694+
*/
2695+
void setItemType( int type );
2696+
2697+
private:
2698+
QString mParentLayoutParameterName;
2699+
int mItemType = -1;
2700+
};
2701+
26242702
// clazy:excludeall=qstring-allocations
26252703

26262704
#endif // QGSPROCESSINGPARAMETERS_H

0 commit comments

Comments
 (0)