@@ -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+ }
0 commit comments