QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgsprocessingparameters.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingparameters.cpp
3  ---------------------------
4  begin : April 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgsprocessingprovider.h"
20 #include "qgsprocessingcontext.h"
21 #include "qgsprocessingutils.h"
22 #include "qgsprocessingalgorithm.h"
24 #include "qgsprocessingoutputs.h"
25 #include "qgssettings.h"
26 #include "qgsvectorfilewriter.h"
27 #include "qgsreferencedgeometry.h"
28 #include "qgsprocessingregistry.h"
30 #include "qgsrasterfilewriter.h"
31 #include "qgsvectorlayer.h"
32 #include "qgsmeshlayer.h"
33 #include "qgsapplication.h"
34 #include "qgslayoutmanager.h"
35 #include "qgsprintlayout.h"
36 #include "qgssymbollayerutils.h"
37 #include "qgsfileutils.h"
38 #include "qgsproviderregistry.h"
39 #include <functional>
40 #include <QRegularExpression>
41 
42 
44 {
45  QVariantMap map;
46  map.insert( QStringLiteral( "source" ), source.toVariant() );
47  map.insert( QStringLiteral( "selected_only" ), selectedFeaturesOnly );
48  map.insert( QStringLiteral( "feature_limit" ), featureLimit );
49  map.insert( QStringLiteral( "flags" ), static_cast< int >( flags ) );
50  map.insert( QStringLiteral( "geometry_check" ), static_cast< int >( geometryCheck ) );
51  return map;
52 }
53 
55 {
56  source.loadVariant( map.value( QStringLiteral( "source" ) ) );
57  selectedFeaturesOnly = map.value( QStringLiteral( "selected_only" ), false ).toBool();
58  featureLimit = map.value( QStringLiteral( "feature_limit" ), -1 ).toLongLong();
59  flags = static_cast< Flags >( map.value( QStringLiteral( "flags" ), 0 ).toInt() );
60  geometryCheck = static_cast< QgsFeatureRequest::InvalidGeometryCheck >( map.value( QStringLiteral( "geometry_check" ), QgsFeatureRequest::GeometryAbortOnInvalid ).toInt() );
61  return true;
62 }
63 
64 
65 //
66 // QgsProcessingOutputLayerDefinition
67 //
68 
70 {
71  mUseRemapping = true;
72  mRemappingDefinition = definition;
73 }
74 
76 {
77  QVariantMap map;
78  map.insert( QStringLiteral( "sink" ), sink.toVariant() );
79  map.insert( QStringLiteral( "create_options" ), createOptions );
80  if ( mUseRemapping )
81  map.insert( QStringLiteral( "remapping" ), QVariant::fromValue( mRemappingDefinition ) );
82  return map;
83 }
84 
86 {
87  sink.loadVariant( map.value( QStringLiteral( "sink" ) ) );
88  createOptions = map.value( QStringLiteral( "create_options" ) ).toMap();
89  if ( map.contains( QStringLiteral( "remapping" ) ) )
90  {
91  mUseRemapping = true;
92  mRemappingDefinition = map.value( QStringLiteral( "remapping" ) ).value< QgsRemappingSinkDefinition >();
93  }
94  else
95  {
96  mUseRemapping = false;
97  }
98  return true;
99 }
100 
102 {
104  && mUseRemapping == other.mUseRemapping && mRemappingDefinition == other.mRemappingDefinition;
105 }
106 
108 {
109  return !( *this == other );
110 }
111 
112 bool QgsProcessingParameters::isDynamic( const QVariantMap &parameters, const QString &name )
113 {
114  QVariant val = parameters.value( name );
115  if ( val.canConvert<QgsProperty>() )
116  return val.value< QgsProperty >().propertyType() != QgsProperty::StaticProperty;
117  else
118  return false;
119 }
120 
121 QString QgsProcessingParameters::parameterAsString( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
122 {
123  if ( !definition )
124  return QString();
125 
126  return parameterAsString( definition, parameters.value( definition->name() ), context );
127 }
128 
129 QString QgsProcessingParameters::parameterAsString( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
130 {
131  if ( !definition )
132  return QString();
133 
134  QVariant val = value;
135  if ( val.canConvert<QgsProperty>() )
136  return val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
137 
138  if ( !val.isValid() )
139  {
140  // fall back to default
141  val = definition->defaultValue();
142  }
143 
144  if ( val == QgsProcessing::TEMPORARY_OUTPUT )
145  {
146  if ( const QgsProcessingDestinationParameter *destParam = dynamic_cast< const QgsProcessingDestinationParameter * >( definition ) )
147  return destParam->generateTemporaryDestination();
148  }
149 
150  return val.toString();
151 }
152 
153 QString QgsProcessingParameters::parameterAsExpression( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
154 {
155  if ( !definition )
156  return QString();
157 
158  return parameterAsExpression( definition, parameters.value( definition->name() ), context );
159 }
160 
161 QString QgsProcessingParameters::parameterAsExpression( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
162 {
163  if ( !definition )
164  return QString();
165 
166  QVariant val = value;
167  if ( val.canConvert<QgsProperty>() )
168  return val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
169 
170  if ( val.isValid() && !val.toString().isEmpty() )
171  {
172  QgsExpression e( val.toString() );
173  if ( e.isValid() )
174  return val.toString();
175  }
176 
177  // fall back to default
178  return definition->defaultValue().toString();
179 }
180 
181 double QgsProcessingParameters::parameterAsDouble( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
182 {
183  if ( !definition )
184  return 0;
185 
186  return parameterAsDouble( definition, parameters.value( definition->name() ), context );
187 }
188 
189 double QgsProcessingParameters::parameterAsDouble( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
190 {
191  if ( !definition )
192  return 0;
193 
194  QVariant val = value;
195  if ( val.canConvert<QgsProperty>() )
196  return val.value< QgsProperty >().valueAsDouble( context.expressionContext(), definition->defaultValue().toDouble() );
197 
198  bool ok = false;
199  double res = val.toDouble( &ok );
200  if ( ok )
201  return res;
202 
203  // fall back to default
204  val = definition->defaultValue();
205  return val.toDouble();
206 }
207 
208 int QgsProcessingParameters::parameterAsInt( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
209 {
210  if ( !definition )
211  return 0;
212 
213  return parameterAsInt( definition, parameters.value( definition->name() ), context );
214 }
215 
216 int QgsProcessingParameters::parameterAsInt( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
217 {
218  if ( !definition )
219  return 0;
220 
221  QVariant val = value;
222  if ( val.canConvert<QgsProperty>() )
223  return val.value< QgsProperty >().valueAsInt( context.expressionContext(), definition->defaultValue().toInt() );
224 
225  bool ok = false;
226  double dbl = val.toDouble( &ok );
227  if ( !ok )
228  {
229  // fall back to default
230  val = definition->defaultValue();
231  dbl = val.toDouble( &ok );
232  }
233 
234  //String representations of doubles in QVariant will not convert to int
235  //work around this by first converting to double, and then checking whether the double is convertible to int
236  if ( ok )
237  {
238  double round = std::round( dbl );
239  if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
240  {
241  //double too large to fit in int
242  return 0;
243  }
244  return static_cast< int >( std::round( dbl ) );
245  }
246 
247  return val.toInt();
248 }
249 
250 QList< int > QgsProcessingParameters::parameterAsInts( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
251 {
252  if ( !definition )
253  return QList< int >();
254 
255  return parameterAsInts( definition, parameters.value( definition->name() ), context );
256 }
257 
258 QList< int > QgsProcessingParameters::parameterAsInts( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
259 {
260  if ( !definition )
261  return QList< int >();
262 
263  QList< int > resultList;
264  QVariant val = value;
265  if ( val.isValid() )
266  {
267  if ( val.canConvert<QgsProperty>() )
268  resultList << val.value< QgsProperty >().valueAsInt( context.expressionContext(), definition->defaultValue().toInt() );
269  else if ( val.type() == QVariant::List )
270  {
271  QVariantList list = val.toList();
272  for ( auto it = list.constBegin(); it != list.constEnd(); ++it )
273  resultList << it->toInt();
274  }
275  else
276  {
277  QStringList parts = val.toString().split( ';' );
278  for ( auto it = parts.constBegin(); it != parts.constEnd(); ++it )
279  resultList << it->toInt();
280  }
281  }
282 
283  if ( ( resultList.isEmpty() || resultList.at( 0 ) == 0 ) )
284  {
285  resultList.clear();
286  // check default
287  if ( definition->defaultValue().isValid() )
288  {
289  if ( definition->defaultValue().type() == QVariant::List )
290  {
291  QVariantList list = definition->defaultValue().toList();
292  for ( auto it = list.constBegin(); it != list.constEnd(); ++it )
293  resultList << it->toInt();
294  }
295  else
296  {
297  QStringList parts = definition->defaultValue().toString().split( ';' );
298  for ( auto it = parts.constBegin(); it != parts.constEnd(); ++it )
299  resultList << it->toInt();
300  }
301  }
302  }
303 
304  return resultList;
305 }
306 
307 QDateTime QgsProcessingParameters::parameterAsDateTime( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
308 {
309  if ( !definition )
310  return QDateTime();
311 
312  return parameterAsDateTime( definition, parameters.value( definition->name() ), context );
313 }
314 
315 QDateTime QgsProcessingParameters::parameterAsDateTime( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
316 {
317  if ( !definition )
318  return QDateTime();
319 
320  QVariant val = value;
321  if ( val.canConvert<QgsProperty>() )
322  val = val.value< QgsProperty >().value( context.expressionContext(), definition->defaultValue() );
323 
324  QDateTime d = val.toDateTime();
325  if ( !d.isValid() && val.type() == QVariant::String )
326  {
327  d = QDateTime::fromString( val.toString() );
328  }
329 
330  if ( !d.isValid() )
331  {
332  // fall back to default
333  val = definition->defaultValue();
334  d = val.toDateTime();
335  }
336  if ( !d.isValid() && val.type() == QVariant::String )
337  {
338  d = QDateTime::fromString( val.toString() );
339  }
340 
341  return d;
342 }
343 
344 QDate QgsProcessingParameters::parameterAsDate( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
345 {
346  if ( !definition )
347  return QDate();
348 
349  return parameterAsDate( definition, parameters.value( definition->name() ), context );
350 }
351 
352 QDate QgsProcessingParameters::parameterAsDate( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
353 {
354  if ( !definition )
355  return QDate();
356 
357  QVariant val = value;
358  if ( val.canConvert<QgsProperty>() )
359  val = val.value< QgsProperty >().value( context.expressionContext(), definition->defaultValue() );
360 
361  QDate d = val.toDate();
362  if ( !d.isValid() && val.type() == QVariant::String )
363  {
364  d = QDate::fromString( val.toString() );
365  }
366 
367  if ( !d.isValid() )
368  {
369  // fall back to default
370  val = definition->defaultValue();
371  d = val.toDate();
372  }
373  if ( !d.isValid() && val.type() == QVariant::String )
374  {
375  d = QDate::fromString( val.toString() );
376  }
377 
378  return d;
379 }
380 
381 QTime QgsProcessingParameters::parameterAsTime( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
382 {
383  if ( !definition )
384  return QTime();
385 
386  return parameterAsTime( definition, parameters.value( definition->name() ), context );
387 }
388 
389 QTime QgsProcessingParameters::parameterAsTime( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
390 {
391  if ( !definition )
392  return QTime();
393 
394  QVariant val = value;
395  if ( val.canConvert<QgsProperty>() )
396  val = val.value< QgsProperty >().value( context.expressionContext(), definition->defaultValue() );
397 
398  QTime d;
399 
400  if ( val.type() == QVariant::DateTime )
401  d = val.toDateTime().time();
402  else
403  d = val.toTime();
404 
405  if ( !d.isValid() && val.type() == QVariant::String )
406  {
407  d = QTime::fromString( val.toString() );
408  }
409 
410  if ( !d.isValid() )
411  {
412  // fall back to default
413  val = definition->defaultValue();
414  d = val.toTime();
415  }
416  if ( !d.isValid() && val.type() == QVariant::String )
417  {
418  d = QTime::fromString( val.toString() );
419  }
420 
421  return d;
422 }
423 
424 int QgsProcessingParameters::parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
425 {
426  if ( !definition )
427  return 0;
428 
429  return parameterAsEnum( definition, parameters.value( definition->name() ), context );
430 }
431 
432 int QgsProcessingParameters::parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
433 {
434  if ( !definition )
435  return 0;
436 
437  int val = parameterAsInt( definition, value, context );
438  const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
439  if ( enumDef && val >= enumDef->options().size() )
440  {
441  return enumDef->defaultValue().toInt();
442  }
443  return val;
444 }
445 
446 QList<int> QgsProcessingParameters::parameterAsEnums( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
447 {
448  if ( !definition )
449  return QList<int>();
450 
451  return parameterAsEnums( definition, parameters.value( definition->name() ), context );
452 }
453 
454 QList<int> QgsProcessingParameters::parameterAsEnums( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
455 {
456  if ( !definition )
457  return QList<int>();
458 
459  QVariantList resultList;
460  QVariant val = value;
461  if ( val.canConvert<QgsProperty>() )
462  resultList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
463  else if ( val.type() == QVariant::List )
464  {
465  const auto constToList = val.toList();
466  for ( const QVariant &var : constToList )
467  resultList << var;
468  }
469  else if ( val.type() == QVariant::String )
470  {
471  const auto constSplit = val.toString().split( ',' );
472  for ( const QString &var : constSplit )
473  resultList << var;
474  }
475  else
476  resultList << val;
477 
478  if ( resultList.isEmpty() )
479  return QList< int >();
480 
481  if ( ( !val.isValid() || !resultList.at( 0 ).isValid() ) && definition )
482  {
483  resultList.clear();
484  // check default
485  if ( definition->defaultValue().type() == QVariant::List )
486  {
487  const auto constToList = definition->defaultValue().toList();
488  for ( const QVariant &var : constToList )
489  resultList << var;
490  }
491  else if ( definition->defaultValue().type() == QVariant::String )
492  {
493  const auto constSplit = definition->defaultValue().toString().split( ',' );
494  for ( const QString &var : constSplit )
495  resultList << var;
496  }
497  else
498  resultList << definition->defaultValue();
499  }
500 
501  QList< int > result;
502  const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
503  const auto constResultList = resultList;
504  for ( const QVariant &var : constResultList )
505  {
506  int resInt = var.toInt();
507  if ( !enumDef || resInt < enumDef->options().size() )
508  {
509  result << resInt;
510  }
511  }
512  return result;
513 }
514 
515 QString QgsProcessingParameters::parameterAsEnumString( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
516 {
517  if ( !definition )
518  return QString();
519 
520  return parameterAsEnumString( definition, parameters.value( definition->name() ), context );
521 }
522 
523 QString QgsProcessingParameters::parameterAsEnumString( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
524 {
525  if ( !definition )
526  return QString();
527 
528  QString enumText = parameterAsString( definition, value, context );
529  const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
530  if ( enumText.isEmpty() || !enumDef->options().contains( enumText ) )
531  enumText = definition->defaultValue().toString();
532 
533  return enumText;
534 }
535 
536 QStringList QgsProcessingParameters::parameterAsEnumStrings( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
537 {
538  if ( !definition )
539  return QStringList();
540 
541  return parameterAsEnumStrings( definition, parameters.value( definition->name() ), context );
542 }
543 
544 QStringList QgsProcessingParameters::parameterAsEnumStrings( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
545 {
546  if ( !definition )
547  return QStringList();
548 
549  QVariant val = value;
550 
551  QStringList enumValues;
552 
553  std::function< void( const QVariant &var ) > processVariant;
554  processVariant = [ &enumValues, &context, &definition, &processVariant ]( const QVariant & var )
555  {
556  if ( var.type() == QVariant::List )
557  {
558  const auto constToList = var.toList();
559  for ( const QVariant &listVar : constToList )
560  {
561  processVariant( listVar );
562  }
563  }
564  else if ( var.type() == QVariant::StringList )
565  {
566  const auto constToStringList = var.toStringList();
567  for ( const QString &s : constToStringList )
568  {
569  processVariant( s );
570  }
571  }
572  else if ( var.canConvert<QgsProperty>() )
573  processVariant( var.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() ) );
574  else
575  {
576  const QStringList parts = var.toString().split( ',' );
577  for ( const QString &s : parts )
578  {
579  enumValues << s;
580  }
581  }
582  };
583 
584  processVariant( val );
585 
586  const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
587  // check that values are valid enum values. The resulting set will be empty
588  // if all values are present in the enumDef->options(), otherwise it will contain
589  // values which are invalid
590 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
591  QSet<QString> subtraction = enumValues.toSet().subtract( enumDef->options().toSet() );
592 #else
593  const QStringList options = enumDef->options();
594  QSet<QString> subtraction = QSet<QString>( enumValues.begin(), enumValues.end() ).subtract( QSet<QString>( options.begin(), options.end() ) );
595 #endif
596 
597  if ( enumValues.isEmpty() || !subtraction.isEmpty() )
598  {
599  enumValues.clear();
600  processVariant( definition->defaultValue() );
601  }
602 
603  return enumValues;
604 }
605 
606 bool QgsProcessingParameters::parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
607 {
608  if ( !definition )
609  return false;
610 
611  return parameterAsBool( definition, parameters.value( definition->name() ), context );
612 }
613 
614 bool QgsProcessingParameters::parameterAsBoolean( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
615 {
616  if ( !definition )
617  return false;
618 
619  return parameterAsBoolean( definition, parameters.value( definition->name() ), context );
620 }
621 
622 bool QgsProcessingParameters::parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
623 {
624  if ( !definition )
625  return false;
626 
627  QVariant def = definition->defaultValue();
628 
629  QVariant val = value;
630  if ( val.canConvert<QgsProperty>() )
631  return val.value< QgsProperty >().valueAsBool( context.expressionContext(), def.toBool() );
632  else if ( val.isValid() )
633  return val.toBool();
634  else
635  return def.toBool();
636 }
637 
638 bool QgsProcessingParameters::parameterAsBoolean( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
639 {
640  if ( !definition )
641  return false;
642 
643  QVariant def = definition->defaultValue();
644 
645  QVariant val = value;
646  if ( val.canConvert<QgsProperty>() )
647  return val.value< QgsProperty >().valueAsBool( context.expressionContext(), def.toBool() );
648  else if ( val.isValid() )
649  return val.toBool();
650  else
651  return def.toBool();
652 }
653 
654 QgsFeatureSink *QgsProcessingParameters::parameterAsSink( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsFields &fields,
656  QgsProcessingContext &context, QString &destinationIdentifier, QgsFeatureSink::SinkFlags sinkFlags,
657  const QVariantMap &createOptions, const QStringList &datasourceOptions, const QStringList &layerOptions )
658 {
659  QVariant val;
660  if ( definition )
661  {
662  val = parameters.value( definition->name() );
663  }
664 
665  return parameterAsSink( definition, val, fields, geometryType, crs, context, destinationIdentifier, sinkFlags, createOptions, datasourceOptions, layerOptions );
666 }
667 
668 QgsFeatureSink *QgsProcessingParameters::parameterAsSink( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context, QString &destinationIdentifier, QgsFeatureSink::SinkFlags sinkFlags, const QVariantMap &createOptions, const QStringList &datasourceOptions, const QStringList &layerOptions )
669 {
670  QVariantMap options = createOptions;
671  QVariant val = value;
672 
673  QgsProject *destinationProject = nullptr;
674  QString destName;
675  QgsRemappingSinkDefinition remapDefinition;
676  bool useRemapDefinition = false;
677  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
678  {
679  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
680  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
681  destinationProject = fromVar.destinationProject;
682  options = fromVar.createOptions;
683 
684  val = fromVar.sink;
685  destName = fromVar.destinationName;
686  if ( fromVar.useRemapping() )
687  {
688  useRemapDefinition = true;
689  remapDefinition = fromVar.remappingDefinition();
690  }
691  }
692 
693  QString dest;
694  if ( definition && val.canConvert<QgsProperty>() )
695  {
696  dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
697  }
698  else if ( !val.isValid() || val.toString().isEmpty() )
699  {
700  if ( definition && definition->flags() & QgsProcessingParameterDefinition::FlagOptional && !definition->defaultValue().isValid() )
701  {
702  // unset, optional sink, no default => no sink
703  return nullptr;
704  }
705  // fall back to default
706  if ( !definition )
707  {
708  throw QgsProcessingException( QObject::tr( "No parameter definition for the sink" ) );
709  }
710  dest = definition->defaultValue().toString();
711  }
712  else
713  {
714  dest = val.toString();
715  }
716  if ( dest == QgsProcessing::TEMPORARY_OUTPUT )
717  {
718  if ( const QgsProcessingDestinationParameter *destParam = dynamic_cast< const QgsProcessingDestinationParameter * >( definition ) )
719  dest = destParam->generateTemporaryDestination();
720  }
721 
722  if ( dest.isEmpty() )
723  return nullptr;
724 
725  std::unique_ptr< QgsFeatureSink > sink( QgsProcessingUtils::createFeatureSink( dest, context, fields, geometryType, crs, options, datasourceOptions, layerOptions, sinkFlags, useRemapDefinition ? &remapDefinition : nullptr ) );
726  destinationIdentifier = dest;
727 
728  if ( destinationProject )
729  {
730  if ( destName.isEmpty() && definition )
731  {
732  destName = definition->description();
733  }
734  QString outputName;
735  if ( definition )
736  outputName = definition->name();
737  context.addLayerToLoadOnCompletion( destinationIdentifier, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName, QgsProcessingUtils::LayerHint::Vector ) );
738  }
739 
740  return sink.release();
741 }
742 
744 {
745  if ( !definition )
746  return nullptr;
747 
748  return parameterAsSource( definition, parameters.value( definition->name() ), context );
749 }
750 
752 {
753  if ( !definition )
754  return nullptr;
755 
756  return QgsProcessingUtils::variantToSource( value, context, definition->defaultValue() );
757 }
758 
759 QString parameterAsCompatibleSourceLayerPathInternal( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback, QString *layerName )
760 {
761  if ( !definition )
762  return QString();
763 
764  QVariant val = parameters.value( definition->name() );
765 
766  bool selectedFeaturesOnly = false;
767  long long featureLimit = -1;
768  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
769  {
770  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
771  QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
772  selectedFeaturesOnly = fromVar.selectedFeaturesOnly;
773  featureLimit = fromVar.featureLimit;
774  val = fromVar.source;
775  }
776  else if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
777  {
778  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
779  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
780  val = fromVar.sink;
781  }
782 
783  if ( val.canConvert<QgsProperty>() )
784  {
785  val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
786  }
787 
788  QgsVectorLayer *vl = nullptr;
789  vl = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) );
790 
791  if ( !vl )
792  {
793  QString layerRef;
794  if ( val.canConvert<QgsProperty>() )
795  {
796  layerRef = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
797  }
798  else if ( !val.isValid() || val.toString().isEmpty() )
799  {
800  // fall back to default
801  val = definition->defaultValue();
802 
803  // default value may be a vector layer
804  vl = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) );
805  if ( !vl )
806  layerRef = definition->defaultValue().toString();
807  }
808  else
809  {
810  layerRef = val.toString();
811  }
812 
813  if ( !vl )
814  {
815  if ( layerRef.isEmpty() )
816  return QString();
817 
818  vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context, true, QgsProcessingUtils::LayerHint::Vector ) );
819  }
820  }
821 
822  if ( !vl )
823  return QString();
824 
825  if ( layerName )
826  return QgsProcessingUtils::convertToCompatibleFormatAndLayerName( vl, selectedFeaturesOnly, definition->name(),
827  compatibleFormats, preferredFormat, context, feedback, *layerName, featureLimit );
828  else
829  return QgsProcessingUtils::convertToCompatibleFormat( vl, selectedFeaturesOnly, definition->name(),
830  compatibleFormats, preferredFormat, context, feedback, featureLimit );
831 }
832 
833 QString QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback )
834 {
835  return parameterAsCompatibleSourceLayerPathInternal( definition, parameters, context, compatibleFormats, preferredFormat, feedback, nullptr );
836 }
837 
838 QString QgsProcessingParameters::parameterAsCompatibleSourceLayerPathAndLayerName( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback, QString *layerName )
839 {
840  QString *destLayer = layerName;
841  QString tmp;
842  if ( destLayer )
843  destLayer->clear();
844  else
845  destLayer = &tmp;
846 
847  return parameterAsCompatibleSourceLayerPathInternal( definition, parameters, context, compatibleFormats, preferredFormat, feedback, destLayer );
848 }
849 
851 {
852  if ( !definition )
853  return nullptr;
854 
855  return parameterAsLayer( definition, parameters.value( definition->name() ), context, layerHint );
856 }
857 
859 {
860  if ( !definition )
861  return nullptr;
862 
863  QVariant val = value;
864  if ( val.canConvert<QgsProperty>() )
865  {
866  val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
867  }
868 
869  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
870  {
871  return layer;
872  }
873 
874  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
875  {
876  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
877  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
878  val = fromVar.sink;
879  }
880 
881  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
882  {
883  val = val.value< QgsProperty >().staticValue();
884  }
885 
886  if ( !val.isValid() || val.toString().isEmpty() )
887  {
888  // fall back to default
889  val = definition->defaultValue();
890  }
891 
892  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
893  {
894  return layer;
895  }
896 
897  QString layerRef = val.toString();
898  if ( layerRef.isEmpty() )
899  layerRef = definition->defaultValue().toString();
900 
901  if ( layerRef.isEmpty() )
902  return nullptr;
903 
904  return QgsProcessingUtils::mapLayerFromString( layerRef, context, true, layerHint );
905 }
906 
908 {
909  return qobject_cast< QgsRasterLayer *>( parameterAsLayer( definition, parameters, context, QgsProcessingUtils::LayerHint::Raster ) );
910 }
911 
913 {
914  return qobject_cast< QgsRasterLayer *>( parameterAsLayer( definition, value, context, QgsProcessingUtils::LayerHint::Raster ) );
915 }
916 
918 {
919  return qobject_cast< QgsMeshLayer *>( parameterAsLayer( definition, parameters, context, QgsProcessingUtils::LayerHint::Mesh ) );
920 }
921 
923 {
924  return qobject_cast< QgsMeshLayer *>( parameterAsLayer( definition, value, context, QgsProcessingUtils::LayerHint::Mesh ) );
925 }
926 
927 QString QgsProcessingParameters::parameterAsOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
928 {
929  QVariant val;
930  if ( definition )
931  {
932  val = parameters.value( definition->name() );
933  }
934  return parameterAsOutputLayer( definition, val, context );
935 }
936 
938 {
939  QVariant val = value;
940 
941  QgsProject *destinationProject = nullptr;
942  QString destName;
943  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
944  {
945  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
946  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
947  destinationProject = fromVar.destinationProject;
948  val = fromVar.sink;
949  destName = fromVar.destinationName;
950  }
951 
952  QString dest;
953  if ( definition && val.canConvert<QgsProperty>() )
954  {
955  dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
956  }
957  else if ( definition && ( !val.isValid() || val.toString().isEmpty() ) )
958  {
959  // fall back to default
960  dest = definition->defaultValue().toString();
961  }
962  else
963  {
964  dest = val.toString();
965  }
966  if ( dest == QgsProcessing::TEMPORARY_OUTPUT )
967  {
968  if ( const QgsProcessingDestinationParameter *destParam = dynamic_cast< const QgsProcessingDestinationParameter * >( definition ) )
969  dest = destParam->generateTemporaryDestination();
970  }
971 
972  if ( destinationProject )
973  {
974  QString outputName;
975  if ( destName.isEmpty() && definition )
976  {
977  destName = definition->description();
978  }
979  if ( definition )
980  outputName = definition->name();
981 
983  if ( definition && definition->type() == QgsProcessingParameterVectorDestination::typeName() )
985  else if ( definition && definition->type() == QgsProcessingParameterRasterDestination::typeName() )
987 
988  context.addLayerToLoadOnCompletion( dest, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName, layerTypeHint ) );
989  }
990 
991  return dest;
992 }
993 
994 QString QgsProcessingParameters::parameterAsFileOutput( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
995 {
996  QVariant val;
997  if ( definition )
998  {
999  val = parameters.value( definition->name() );
1000  }
1001  return parameterAsFileOutput( definition, val, context );
1002 }
1003 
1005 {
1006  QVariant val = value;
1007 
1008  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
1009  {
1010  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1011  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
1012  val = fromVar.sink;
1013  }
1014 
1015  QString dest;
1016  if ( definition && val.canConvert<QgsProperty>() )
1017  {
1018  dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1019  }
1020  else if ( definition && ( !val.isValid() || val.toString().isEmpty() ) )
1021  {
1022  // fall back to default
1023  dest = definition->defaultValue().toString();
1024  }
1025  else
1026  {
1027  dest = val.toString();
1028  }
1029  if ( dest == QgsProcessing::TEMPORARY_OUTPUT )
1030  {
1031  if ( const QgsProcessingDestinationParameter *destParam = dynamic_cast< const QgsProcessingDestinationParameter * >( definition ) )
1032  dest = destParam->generateTemporaryDestination();
1033  }
1034  return dest;
1035 }
1036 
1038 {
1039  return qobject_cast< QgsVectorLayer *>( parameterAsLayer( definition, parameters, context, QgsProcessingUtils::LayerHint::Vector ) );
1040 }
1041 
1043 {
1044  return qobject_cast< QgsVectorLayer *>( parameterAsLayer( definition, value, context, QgsProcessingUtils::LayerHint::Vector ) );
1045 }
1046 
1048 {
1049  if ( !definition )
1051 
1052  return parameterAsCrs( definition, parameters.value( definition->name() ), context );
1053 }
1054 
1056 {
1057  if ( !definition )
1059 
1060  return QgsProcessingUtils::variantToCrs( value, context, definition->defaultValue() );
1061 }
1062 
1065 {
1066  if ( !definition )
1067  return QgsRectangle();
1068 
1069  return parameterAsExtent( definition, parameters.value( definition->name() ), context, crs );
1070 }
1071 
1073 {
1074  if ( !definition )
1075  return QgsRectangle();
1076 
1077  QVariant val = value;
1078 
1079  if ( val.canConvert< QgsRectangle >() )
1080  {
1081  return val.value<QgsRectangle>();
1082  }
1083  if ( val.canConvert< QgsGeometry >() )
1084  {
1085  const QgsGeometry geom = val.value<QgsGeometry>();
1086  if ( !geom.isNull() )
1087  return geom.boundingBox();
1088  }
1089  if ( val.canConvert< QgsReferencedRectangle >() )
1090  {
1092  if ( crs.isValid() && rr.crs().isValid() && crs != rr.crs() )
1093  {
1094  QgsCoordinateTransform ct( rr.crs(), crs, context.project() );
1095  try
1096  {
1097  return ct.transformBoundingBox( rr );
1098  }
1099  catch ( QgsCsException & )
1100  {
1101  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
1102  }
1103  }
1104  return rr;
1105  }
1106 
1107  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
1108  {
1109  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
1110  QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
1111  val = fromVar.source;
1112  }
1113  else if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
1114  {
1115  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1116  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
1117  val = fromVar.sink;
1118  }
1119 
1120  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
1121  {
1122  val = val.value< QgsProperty >().staticValue();
1123  }
1124 
1125  // maybe parameter is a direct layer value?
1126  QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) );
1127 
1128  QString rectText;
1129  if ( val.canConvert<QgsProperty>() )
1130  rectText = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1131  else
1132  rectText = val.toString();
1133 
1134  if ( rectText.isEmpty() && !layer )
1135  return QgsRectangle();
1136 
1137  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
1138  QRegularExpressionMatch match = rx.match( rectText );
1139  if ( match.hasMatch() )
1140  {
1141  bool xMinOk = false;
1142  double xMin = match.captured( 1 ).toDouble( &xMinOk );
1143  bool xMaxOk = false;
1144  double xMax = match.captured( 2 ).toDouble( &xMaxOk );
1145  bool yMinOk = false;
1146  double yMin = match.captured( 3 ).toDouble( &yMinOk );
1147  bool yMaxOk = false;
1148  double yMax = match.captured( 4 ).toDouble( &yMaxOk );
1149  if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
1150  {
1151  QgsRectangle rect( xMin, yMin, xMax, yMax );
1152  QgsCoordinateReferenceSystem rectCrs( match.captured( 5 ) );
1153  if ( crs.isValid() && rectCrs.isValid() && crs != rectCrs )
1154  {
1155  QgsCoordinateTransform ct( rectCrs, crs, context.project() );
1156  try
1157  {
1158  return ct.transformBoundingBox( rect );
1159  }
1160  catch ( QgsCsException & )
1161  {
1162  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
1163  }
1164  }
1165  return rect;
1166  }
1167  }
1168 
1169  // try as layer extent
1170  if ( !layer )
1171  layer = QgsProcessingUtils::mapLayerFromString( rectText, context );
1172 
1173  if ( layer )
1174  {
1175  QgsRectangle rect = layer->extent();
1176  if ( crs.isValid() && layer->crs().isValid() && crs != layer->crs() )
1177  {
1178  QgsCoordinateTransform ct( layer->crs(), crs, context.project() );
1179  try
1180  {
1181  return ct.transformBoundingBox( rect );
1182  }
1183  catch ( QgsCsException & )
1184  {
1185  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
1186  }
1187  }
1188  return rect;
1189  }
1190  return QgsRectangle();
1191 }
1192 
1194 {
1195  if ( !definition )
1196  return QgsGeometry();
1197 
1198  QVariant val = parameters.value( definition->name() );
1199 
1200  if ( val.canConvert< QgsReferencedRectangle >() )
1201  {
1204  if ( crs.isValid() && rr.crs().isValid() && crs != rr.crs() )
1205  {
1206  g = g.densifyByCount( 20 );
1207  QgsCoordinateTransform ct( rr.crs(), crs, context.project() );
1208  try
1209  {
1210  g.transform( ct );
1211  }
1212  catch ( QgsCsException & )
1213  {
1214  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
1215  }
1216  return g;
1217  }
1218  }
1219 
1220  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
1221  {
1222  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
1223  QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
1224  val = fromVar.source;
1225  }
1226  else if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
1227  {
1228  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1229  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
1230  val = fromVar.sink;
1231  }
1232 
1233  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
1234  {
1235  val = val.value< QgsProperty >().staticValue();
1236  }
1237 
1238  QString rectText;
1239  if ( val.canConvert<QgsProperty>() )
1240  rectText = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1241  else
1242  rectText = val.toString();
1243 
1244  if ( !rectText.isEmpty() )
1245  {
1246  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
1247  QRegularExpressionMatch match = rx.match( rectText );
1248  if ( match.hasMatch() )
1249  {
1250  bool xMinOk = false;
1251  double xMin = match.captured( 1 ).toDouble( &xMinOk );
1252  bool xMaxOk = false;
1253  double xMax = match.captured( 2 ).toDouble( &xMaxOk );
1254  bool yMinOk = false;
1255  double yMin = match.captured( 3 ).toDouble( &yMinOk );
1256  bool yMaxOk = false;
1257  double yMax = match.captured( 4 ).toDouble( &yMaxOk );
1258  if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
1259  {
1260  QgsRectangle rect( xMin, yMin, xMax, yMax );
1261  QgsCoordinateReferenceSystem rectCrs( match.captured( 5 ) );
1262  QgsGeometry g = QgsGeometry::fromRect( rect );
1263  if ( crs.isValid() && rectCrs.isValid() && crs != rectCrs )
1264  {
1265  g = g.densifyByCount( 20 );
1266  QgsCoordinateTransform ct( rectCrs, crs, context.project() );
1267  try
1268  {
1269  g.transform( ct );
1270  }
1271  catch ( QgsCsException & )
1272  {
1273  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
1274  }
1275  return g;
1276  }
1277  }
1278  }
1279  }
1280 
1281  // try as layer extent
1282 
1283  // maybe parameter is a direct layer value?
1284  QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) );
1285  if ( !layer )
1286  layer = QgsProcessingUtils::mapLayerFromString( rectText, context );
1287 
1288  if ( layer )
1289  {
1290  QgsRectangle rect = layer->extent();
1291  QgsGeometry g = QgsGeometry::fromRect( rect );
1292  if ( crs.isValid() && layer->crs().isValid() && crs != layer->crs() )
1293  {
1294  g = g.densifyByCount( 20 );
1295  QgsCoordinateTransform ct( layer->crs(), crs, context.project() );
1296  try
1297  {
1298  g.transform( ct );
1299  }
1300  catch ( QgsCsException & )
1301  {
1302  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
1303  }
1304  }
1305  return g;
1306  }
1307 
1308  return QgsGeometry::fromRect( parameterAsExtent( definition, parameters, context, crs ) );
1309 }
1310 
1312 {
1313  QVariant val = parameters.value( definition->name() );
1314  return parameterAsExtentCrs( definition, val, context );
1315 }
1316 
1318 {
1319  QVariant val = value;
1320  if ( val.canConvert< QgsReferencedRectangle >() )
1321  {
1323  if ( rr.crs().isValid() )
1324  {
1325  return rr.crs();
1326  }
1327  }
1328 
1329  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
1330  {
1331  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
1332  QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
1333  val = fromVar.source;
1334  }
1335  else if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
1336  {
1337  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1338  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
1339  val = fromVar.sink;
1340  }
1341 
1342  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
1343  {
1344  val = val.value< QgsProperty >().staticValue();
1345  }
1346 
1347  QString valueAsString;
1348  if ( val.canConvert<QgsProperty>() )
1349  valueAsString = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1350  else
1351  valueAsString = val.toString();
1352 
1353  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
1354 
1355  QRegularExpressionMatch match = rx.match( valueAsString );
1356  if ( match.hasMatch() )
1357  {
1358  QgsCoordinateReferenceSystem crs( match.captured( 5 ) );
1359  if ( crs.isValid() )
1360  return crs;
1361  }
1362 
1363  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
1364  {
1365  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
1366  QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
1367  val = fromVar.source;
1368  }
1369  else if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
1370  {
1371  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1372  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
1373  val = fromVar.sink;
1374  }
1375 
1376  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
1377  {
1378  val = val.value< QgsProperty >().staticValue();
1379  }
1380 
1381  // try as layer crs
1382  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
1383  return layer->crs();
1384  else if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( valueAsString, context ) )
1385  return layer->crs();
1386 
1387  if ( auto *lProject = context.project() )
1388  return lProject->crs();
1389  else
1391 }
1392 
1394 {
1395  if ( !definition )
1396  return QgsPointXY();
1397 
1398  return parameterAsPoint( definition, parameters.value( definition->name() ), context, crs );
1399 }
1400 
1402 {
1403  if ( !definition )
1404  return QgsPointXY();
1405 
1406  QVariant val = value;
1407  if ( val.canConvert< QgsPointXY >() )
1408  {
1409  return val.value<QgsPointXY>();
1410  }
1411  if ( val.canConvert< QgsGeometry >() )
1412  {
1413  const QgsGeometry geom = val.value<QgsGeometry>();
1414  if ( !geom.isNull() )
1415  return geom.centroid().asPoint();
1416  }
1417  if ( val.canConvert< QgsReferencedPointXY >() )
1418  {
1419  QgsReferencedPointXY rp = val.value<QgsReferencedPointXY>();
1420  if ( crs.isValid() && rp.crs().isValid() && crs != rp.crs() )
1421  {
1422  QgsCoordinateTransform ct( rp.crs(), crs, context.project() );
1423  try
1424  {
1425  return ct.transform( rp );
1426  }
1427  catch ( QgsCsException & )
1428  {
1429  QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
1430  }
1431  }
1432  return rp;
1433  }
1434 
1435  QString pointText = parameterAsString( definition, value, context );
1436  if ( pointText.isEmpty() )
1437  pointText = definition->defaultValue().toString();
1438 
1439  if ( pointText.isEmpty() )
1440  return QgsPointXY();
1441 
1442  QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
1443 
1444  QString valueAsString = parameterAsString( definition, value, context );
1445  QRegularExpressionMatch match = rx.match( valueAsString );
1446  if ( match.hasMatch() )
1447  {
1448  bool xOk = false;
1449  double x = match.captured( 1 ).toDouble( &xOk );
1450  bool yOk = false;
1451  double y = match.captured( 2 ).toDouble( &yOk );
1452 
1453  if ( xOk && yOk )
1454  {
1455  QgsPointXY pt( x, y );
1456 
1457  QgsCoordinateReferenceSystem pointCrs( match.captured( 3 ) );
1458  if ( crs.isValid() && pointCrs.isValid() && crs != pointCrs )
1459  {
1460  QgsCoordinateTransform ct( pointCrs, crs, context.project() );
1461  try
1462  {
1463  return ct.transform( pt );
1464  }
1465  catch ( QgsCsException & )
1466  {
1467  QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
1468  }
1469  }
1470  return pt;
1471  }
1472  }
1473 
1474  return QgsPointXY();
1475 }
1476 
1478 {
1479  QVariant val = parameters.value( definition->name() );
1480  return parameterAsPointCrs( definition, val, context );
1481 }
1482 
1484 {
1485  if ( value.canConvert< QgsReferencedPointXY >() )
1486  {
1487  QgsReferencedPointXY rr = value.value<QgsReferencedPointXY>();
1488  if ( rr.crs().isValid() )
1489  {
1490  return rr.crs();
1491  }
1492  }
1493 
1494  QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
1495 
1496  QString valueAsString = parameterAsString( definition, value, context );
1497  QRegularExpressionMatch match = rx.match( valueAsString );
1498  if ( match.hasMatch() )
1499  {
1500  QgsCoordinateReferenceSystem crs( match.captured( 3 ) );
1501  if ( crs.isValid() )
1502  return crs;
1503  }
1504 
1505  if ( auto *lProject = context.project() )
1506  return lProject->crs();
1507  else
1509 }
1510 
1512 {
1513  if ( !definition )
1514  return QgsGeometry();
1515 
1516  return parameterAsGeometry( definition, parameters.value( definition->name() ), context, crs );
1517 }
1518 
1520 {
1521  if ( !definition )
1522  return QgsGeometry();
1523 
1524  QVariant val = value;
1525  if ( val.canConvert< QgsGeometry >() )
1526  {
1527  return val.value<QgsGeometry>();
1528  }
1529 
1530  if ( val.canConvert< QgsPointXY >() )
1531  {
1532  return QgsGeometry::fromPointXY( val.value<QgsPointXY>() );
1533  }
1534 
1535  if ( val.canConvert< QgsRectangle >() )
1536  {
1537  return QgsGeometry::fromRect( val.value<QgsRectangle>() );
1538  }
1539 
1540  if ( val.canConvert< QgsReferencedPointXY >() )
1541  {
1542  QgsReferencedPointXY rp = val.value<QgsReferencedPointXY>();
1543  if ( crs.isValid() && rp.crs().isValid() && crs != rp.crs() )
1544  {
1545  QgsCoordinateTransform ct( rp.crs(), crs, context.project() );
1546  try
1547  {
1548  return QgsGeometry::fromPointXY( ct.transform( rp ) );
1549  }
1550  catch ( QgsCsException & )
1551  {
1552  QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
1553  }
1554  }
1555  return QgsGeometry::fromPointXY( rp );
1556  }
1557 
1558  if ( val.canConvert< QgsReferencedRectangle >() )
1559  {
1562  if ( crs.isValid() && rr.crs().isValid() && crs != rr.crs() )
1563  {
1564  g = g.densifyByCount( 20 );
1565  QgsCoordinateTransform ct( rr.crs(), crs, context.project() );
1566  try
1567  {
1568  g.transform( ct );
1569  }
1570  catch ( QgsCsException & )
1571  {
1572  QgsMessageLog::logMessage( QObject::tr( "Error transforming rectangle geometry" ) );
1573  }
1574  }
1575  return g;
1576  }
1577 
1578  if ( val.canConvert< QgsReferencedGeometry >() )
1579  {
1580  QgsReferencedGeometry rg = val.value<QgsReferencedGeometry>();
1581  if ( crs.isValid() && rg.crs().isValid() && crs != rg.crs() )
1582  {
1583  QgsCoordinateTransform ct( rg.crs(), crs, context.project() );
1584  try
1585  {
1586  rg.transform( ct );
1587  }
1588  catch ( QgsCsException & )
1589  {
1590  QgsMessageLog::logMessage( QObject::tr( "Error transforming geometry" ) );
1591  }
1592  }
1593  return rg;
1594  }
1595 
1596  QString valueAsString = parameterAsString( definition, value, context );
1597  if ( valueAsString.isEmpty() )
1598  valueAsString = definition->defaultValue().toString();
1599 
1600  if ( valueAsString.isEmpty() )
1601  return QgsGeometry();
1602 
1603  QRegularExpression rx( QStringLiteral( "^\\s*(?:CRS=(.*);)?(.*?)$" ) );
1604 
1605  QRegularExpressionMatch match = rx.match( valueAsString );
1606  if ( match.hasMatch() )
1607  {
1608  QgsGeometry g = QgsGeometry::fromWkt( match.captured( 2 ) );
1609  if ( !g.isNull() )
1610  {
1611  QgsCoordinateReferenceSystem geomCrs( match.captured( 1 ) );
1612  if ( crs.isValid() && geomCrs.isValid() && crs != geomCrs )
1613  {
1614  QgsCoordinateTransform ct( geomCrs, crs, context.project() );
1615  try
1616  {
1617  g.transform( ct );
1618  }
1619  catch ( QgsCsException & )
1620  {
1621  QgsMessageLog::logMessage( QObject::tr( "Error transforming geometry" ) );
1622  }
1623  }
1624  return g;
1625  }
1626  }
1627 
1628  return QgsGeometry();
1629 }
1630 
1632 {
1633  QVariant val = parameters.value( definition->name() );
1634  return parameterAsGeometryCrs( definition, val, context );
1635 }
1636 
1638 {
1639  if ( value.canConvert< QgsReferencedGeometry >() )
1640  {
1641  QgsReferencedGeometry rg = value.value<QgsReferencedGeometry>();
1642  if ( rg.crs().isValid() )
1643  {
1644  return rg.crs();
1645  }
1646  }
1647 
1648  if ( value.canConvert< QgsReferencedPointXY >() )
1649  {
1650  QgsReferencedPointXY rp = value.value<QgsReferencedPointXY>();
1651  if ( rp.crs().isValid() )
1652  {
1653  return rp.crs();
1654  }
1655  }
1656 
1657  if ( value.canConvert< QgsReferencedRectangle >() )
1658  {
1659  QgsReferencedRectangle rr = value.value<QgsReferencedRectangle>();
1660  if ( rr.crs().isValid() )
1661  {
1662  return rr.crs();
1663  }
1664  }
1665 
1666  // Match against EWKT
1667  QRegularExpression rx( QStringLiteral( "^\\s*(?:CRS=(.*);)?(.*?)$" ) );
1668 
1669  QString valueAsString = parameterAsString( definition, value, context );
1670  QRegularExpressionMatch match = rx.match( valueAsString );
1671  if ( match.hasMatch() )
1672  {
1673  QgsCoordinateReferenceSystem crs( match.captured( 1 ) );
1674  if ( crs.isValid() )
1675  return crs;
1676  }
1677 
1678  if ( auto *lProject = context.project() )
1679  return lProject->crs();
1680  else
1682 }
1683 
1684 QString QgsProcessingParameters::parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1685 {
1686  if ( !definition )
1687  return QString();
1688 
1689  QString fileText = parameterAsString( definition, parameters, context );
1690  if ( fileText.isEmpty() )
1691  fileText = definition->defaultValue().toString();
1692  return fileText;
1693 }
1694 
1695 QString QgsProcessingParameters::parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1696 {
1697  if ( !definition )
1698  return QString();
1699 
1700  QString fileText = parameterAsString( definition, value, context );
1701  if ( fileText.isEmpty() )
1702  fileText = definition->defaultValue().toString();
1703  return fileText;
1704 }
1705 
1706 QVariantList QgsProcessingParameters::parameterAsMatrix( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1707 {
1708  if ( !definition )
1709  return QVariantList();
1710 
1711  return parameterAsMatrix( definition, parameters.value( definition->name() ), context );
1712 }
1713 
1714 QVariantList QgsProcessingParameters::parameterAsMatrix( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1715 {
1716  if ( !definition )
1717  return QVariantList();
1718 
1719  QString resultString;
1720  QVariant val = value;
1721  if ( val.canConvert<QgsProperty>() )
1722  resultString = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1723  else if ( val.type() == QVariant::List )
1724  return val.toList();
1725  else
1726  resultString = val.toString();
1727 
1728  if ( resultString.isEmpty() )
1729  {
1730  // check default
1731  if ( definition->defaultValue().type() == QVariant::List )
1732  return definition->defaultValue().toList();
1733  else
1734  resultString = definition->defaultValue().toString();
1735  }
1736 
1737  QVariantList result;
1738  const auto constSplit = resultString.split( ',' );
1739  for ( const QString &s : constSplit )
1740  result << s;
1741 
1742  return result;
1743 }
1744 
1745 QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1746 {
1747  if ( !definition )
1748  return QList<QgsMapLayer *>();
1749 
1750  return parameterAsLayerList( definition, parameters.value( definition->name() ), context );
1751 }
1752 
1753 QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1754 {
1755  if ( !definition )
1756  return QList<QgsMapLayer *>();
1757 
1758  QVariant val = value;
1759  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
1760  {
1761  return QList<QgsMapLayer *>() << layer;
1762  }
1763 
1764  QList<QgsMapLayer *> layers;
1765 
1766  std::function< void( const QVariant &var ) > processVariant;
1767  processVariant = [ &layers, &context, &definition, &processVariant ]( const QVariant & var )
1768  {
1769  if ( var.type() == QVariant::List )
1770  {
1771  const auto constToList = var.toList();
1772  for ( const QVariant &listVar : constToList )
1773  {
1774  processVariant( listVar );
1775  }
1776  }
1777  else if ( var.type() == QVariant::StringList )
1778  {
1779  const auto constToStringList = var.toStringList();
1780  for ( const QString &s : constToStringList )
1781  {
1782  processVariant( s );
1783  }
1784  }
1785  else if ( var.canConvert<QgsProperty>() )
1786  processVariant( var.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() ) );
1787  else if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
1788  {
1789  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1790  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
1791  QVariant sink = fromVar.sink;
1792  if ( sink.canConvert<QgsProperty>() )
1793  {
1794  processVariant( sink.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() ) );
1795  }
1796  }
1797  else if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
1798  {
1799  layers << layer;
1800  }
1801  else
1802  {
1803  QgsMapLayer *alayer = QgsProcessingUtils::mapLayerFromString( var.toString(), context );
1804  if ( alayer )
1805  layers << alayer;
1806  }
1807  };
1808 
1809  processVariant( val );
1810 
1811  if ( layers.isEmpty() )
1812  {
1813  // check default
1814  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( definition->defaultValue() ) ) )
1815  {
1816  layers << layer;
1817  }
1818  else if ( definition->defaultValue().type() == QVariant::List )
1819  {
1820  const auto constToList = definition->defaultValue().toList();
1821  for ( const QVariant &var : constToList )
1822  {
1823  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
1824  {
1825  layers << layer;
1826  }
1827  else
1828  {
1829  processVariant( var );
1830  }
1831  }
1832  }
1833  else
1834  processVariant( definition->defaultValue() );
1835  }
1836 
1837  return layers;
1838 }
1839 
1840 QStringList QgsProcessingParameters::parameterAsFileList( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1841 {
1842  if ( !definition )
1843  return QStringList();
1844 
1845  QVariant val = value;
1846 
1847  QStringList files;
1848 
1849  std::function< void( const QVariant &var ) > processVariant;
1850  processVariant = [ &files, &context, &definition, &processVariant ]( const QVariant & var )
1851  {
1852  if ( var.type() == QVariant::List )
1853  {
1854  const auto constToList = var.toList();
1855  for ( const QVariant &listVar : constToList )
1856  {
1857  processVariant( listVar );
1858  }
1859  }
1860  else if ( var.type() == QVariant::StringList )
1861  {
1862  const auto constToStringList = var.toStringList();
1863  for ( const QString &s : constToStringList )
1864  {
1865  processVariant( s );
1866  }
1867  }
1868  else if ( var.canConvert<QgsProperty>() )
1869  processVariant( var.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() ) );
1870  else
1871  {
1872  files << var.toString();
1873  }
1874  };
1875 
1876  processVariant( val );
1877 
1878  if ( files.isEmpty() )
1879  {
1880  processVariant( definition->defaultValue() );
1881  }
1882 
1883  return files;
1884 }
1885 
1886 QStringList QgsProcessingParameters::parameterAsFileList( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1887 {
1888  if ( !definition )
1889  return QStringList();
1890 
1891  return parameterAsFileList( definition, parameters.value( definition->name() ), context );
1892 }
1893 
1894 QList<double> QgsProcessingParameters::parameterAsRange( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1895 {
1896  if ( !definition )
1897  return QList<double>();
1898 
1899  return parameterAsRange( definition, parameters.value( definition->name() ), context );
1900 }
1901 
1902 QList<double> QgsProcessingParameters::parameterAsRange( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1903 {
1904  if ( !definition )
1905  return QList<double>();
1906 
1907  QStringList resultStringList;
1908  QVariant val = value;
1909 
1910  if ( val.canConvert<QgsProperty>() )
1911  resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1912  else if ( val.type() == QVariant::List )
1913  {
1914  const auto constToList = val.toList();
1915  for ( const QVariant &var : constToList )
1916  resultStringList << var.toString();
1917  }
1918  else
1919  resultStringList << val.toString();
1920 
1921  if ( ( resultStringList.isEmpty() || ( resultStringList.size() == 1 && resultStringList.at( 0 ).isEmpty() ) ) )
1922  {
1923  resultStringList.clear();
1924  // check default
1925  if ( definition->defaultValue().type() == QVariant::List )
1926  {
1927  const auto constToList = definition->defaultValue().toList();
1928  for ( const QVariant &var : constToList )
1929  resultStringList << var.toString();
1930  }
1931  else
1932  resultStringList << definition->defaultValue().toString();
1933  }
1934 
1935  if ( resultStringList.size() == 1 )
1936  {
1937  resultStringList = resultStringList.at( 0 ).split( ',' );
1938  }
1939 
1940  if ( resultStringList.size() < 2 )
1941  return QList< double >() << std::numeric_limits<double>::quiet_NaN() << std::numeric_limits<double>::quiet_NaN() ;
1942 
1943  QList< double > result;
1944  bool ok = false;
1945  double n = resultStringList.at( 0 ).toDouble( &ok );
1946  if ( ok )
1947  result << n;
1948  else
1949  result << std::numeric_limits<double>::quiet_NaN() ;
1950  ok = false;
1951  n = resultStringList.at( 1 ).toDouble( &ok );
1952  if ( ok )
1953  result << n;
1954  else
1955  result << std::numeric_limits<double>::quiet_NaN() ;
1956 
1957  return result;
1958 }
1959 
1960 QStringList QgsProcessingParameters::parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1961 {
1962  if ( !definition )
1963  return QStringList();
1964 
1965  QStringList resultStringList;
1966  return parameterAsFields( definition, parameters.value( definition->name() ), context );
1967 }
1968 
1969 QStringList QgsProcessingParameters::parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1970 {
1971  if ( !definition )
1972  return QStringList();
1973 
1974  QStringList resultStringList;
1975  QVariant val = value;
1976  if ( val.isValid() )
1977  {
1978  if ( val.canConvert<QgsProperty>() )
1979  resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1980  else if ( val.type() == QVariant::List )
1981  {
1982  const auto constToList = val.toList();
1983  for ( const QVariant &var : constToList )
1984  resultStringList << var.toString();
1985  }
1986  else if ( val.type() == QVariant::StringList )
1987  {
1988  resultStringList = val.toStringList();
1989  }
1990  else
1991  resultStringList.append( val.toString().split( ';' ) );
1992  }
1993 
1994  if ( ( resultStringList.isEmpty() || resultStringList.at( 0 ).isEmpty() ) )
1995  {
1996  resultStringList.clear();
1997  // check default
1998  if ( definition->defaultValue().isValid() )
1999  {
2000  if ( definition->defaultValue().type() == QVariant::List )
2001  {
2002  const auto constToList = definition->defaultValue().toList();
2003  for ( const QVariant &var : constToList )
2004  resultStringList << var.toString();
2005  }
2006  else if ( definition->defaultValue().type() == QVariant::StringList )
2007  {
2008  resultStringList = definition->defaultValue().toStringList();
2009  }
2010  else
2011  resultStringList.append( definition->defaultValue().toString().split( ';' ) );
2012  }
2013  }
2014 
2015  return resultStringList;
2016 }
2017 
2019 {
2020  if ( !definition )
2021  return nullptr;
2022 
2023  return parameterAsLayout( definition, parameters.value( definition->name() ), context );
2024 }
2025 
2027 {
2028  const QString layoutName = parameterAsString( definition, value, context );
2029  if ( layoutName.isEmpty() )
2030  return nullptr;
2031 
2032  if ( !context.project() )
2033  return nullptr;
2034 
2035  QgsMasterLayoutInterface *l = context.project()->layoutManager()->layoutByName( layoutName );
2037  return static_cast< QgsPrintLayout * >( l );
2038  else
2039  return nullptr;
2040 }
2041 
2043 {
2044  if ( !definition )
2045  return nullptr;
2046 
2047  return parameterAsLayoutItem( definition, parameters.value( definition->name() ), context, layout );
2048 }
2049 
2051 {
2052  if ( !layout )
2053  return nullptr;
2054 
2055  const QString id = parameterAsString( definition, value, context );
2056  if ( id.isEmpty() )
2057  return nullptr;
2058 
2059  // prefer matching by uuid, since it's guaranteed to be unique.
2060  if ( QgsLayoutItem *item = layout->itemByUuid( id ) )
2061  return item;
2062  else if ( QgsLayoutItem *item = layout->itemById( id ) )
2063  return item;
2064  else
2065  return nullptr;
2066 }
2067 
2068 QColor QgsProcessingParameters::parameterAsColor( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
2069 {
2070  if ( !definition )
2071  return QColor();
2072 
2073  return parameterAsColor( definition, parameters.value( definition->name() ), context );
2074 }
2075 
2077 {
2078  if ( !definition )
2079  return QColor();
2080 
2081  QVariant val = value;
2082  if ( val.canConvert<QgsProperty>() )
2083  {
2084  val = val.value< QgsProperty >().value( context.expressionContext(), definition->defaultValue() );
2085  }
2086  if ( val.type() == QVariant::Color )
2087  {
2088  QColor c = val.value< QColor >();
2089  if ( const QgsProcessingParameterColor *colorParam = dynamic_cast< const QgsProcessingParameterColor * >( definition ) )
2090  if ( !colorParam->opacityEnabled() )
2091  c.setAlpha( 255 );
2092  return c;
2093  }
2094 
2095  QString colorText = parameterAsString( definition, value, context );
2096  if ( colorText.isEmpty() && !( definition->flags() & QgsProcessingParameterDefinition::FlagOptional ) )
2097  {
2098  if ( definition->defaultValue().type() == QVariant::Color )
2099  return definition->defaultValue().value< QColor >();
2100  else
2101  colorText = definition->defaultValue().toString();
2102  }
2103 
2104  if ( colorText.isEmpty() )
2105  return QColor();
2106 
2107  bool containsAlpha = false;
2108  QColor c = QgsSymbolLayerUtils::parseColorWithAlpha( colorText, containsAlpha );
2109  if ( const QgsProcessingParameterColor *colorParam = dynamic_cast< const QgsProcessingParameterColor * >( definition ) )
2110  if ( c.isValid() && !colorParam->opacityEnabled() )
2111  c.setAlpha( 255 );
2112  return c;
2113 }
2114 
2115 QString QgsProcessingParameters::parameterAsConnectionName( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
2116 {
2117  if ( !definition )
2118  return QString();
2119 
2120  return parameterAsConnectionName( definition, parameters.value( definition->name() ), context );
2121 }
2122 
2123 QString QgsProcessingParameters::parameterAsConnectionName( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
2124 {
2125  // for now it's just treated identical to strings, but in future we may want flexibility to amend this
2126  // (hence the new method)
2127  return parameterAsString( definition, value, context );
2128 }
2129 
2130 QString QgsProcessingParameters::parameterAsSchema( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
2131 {
2132  if ( !definition )
2133  return QString();
2134 
2135  return parameterAsSchema( definition, parameters.value( definition->name() ), context );
2136 }
2137 
2138 QString QgsProcessingParameters::parameterAsSchema( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
2139 {
2140  // for now it's just treated identical to strings, but in future we may want flexibility to amend this (e.g. if we want to embed connection details into the schema
2141  // parameter values, such as via a delimiter separated string)
2142  return parameterAsString( definition, value, context );
2143 }
2144 
2145 QString QgsProcessingParameters::parameterAsDatabaseTableName( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
2146 {
2147  if ( !definition )
2148  return QString();
2149 
2150  return parameterAsDatabaseTableName( definition, parameters.value( definition->name() ), context );
2151 }
2152 
2154 {
2155  // for now it's just treated identical to strings, but in future we may want flexibility to amend this (e.g. if we want to embed connection details into the table name
2156  // parameter values, such as via a delimiter separated string)
2157  return parameterAsString( definition, value, context );
2158 }
2159 
2161 {
2162  QString type = map.value( QStringLiteral( "parameter_type" ) ).toString();
2163  QString name = map.value( QStringLiteral( "name" ) ).toString();
2164  std::unique_ptr< QgsProcessingParameterDefinition > def;
2165 
2166  // probably all these hardcoded values aren't required anymore, and we could
2167  // always resort to the registry lookup...
2168  // TODO: confirm
2170  def.reset( new QgsProcessingParameterBoolean( name ) );
2171  else if ( type == QgsProcessingParameterCrs::typeName() )
2172  def.reset( new QgsProcessingParameterCrs( name ) );
2173  else if ( type == QgsProcessingParameterMapLayer::typeName() )
2174  def.reset( new QgsProcessingParameterMapLayer( name ) );
2175  else if ( type == QgsProcessingParameterExtent::typeName() )
2176  def.reset( new QgsProcessingParameterExtent( name ) );
2177  else if ( type == QgsProcessingParameterPoint::typeName() )
2178  def.reset( new QgsProcessingParameterPoint( name ) );
2179  else if ( type == QgsProcessingParameterFile::typeName() )
2180  def.reset( new QgsProcessingParameterFile( name ) );
2181  else if ( type == QgsProcessingParameterMatrix::typeName() )
2182  def.reset( new QgsProcessingParameterMatrix( name ) );
2184  def.reset( new QgsProcessingParameterMultipleLayers( name ) );
2185  else if ( type == QgsProcessingParameterNumber::typeName() )
2186  def.reset( new QgsProcessingParameterNumber( name ) );
2187  else if ( type == QgsProcessingParameterRange::typeName() )
2188  def.reset( new QgsProcessingParameterRange( name ) );
2189  else if ( type == QgsProcessingParameterRasterLayer::typeName() )
2190  def.reset( new QgsProcessingParameterRasterLayer( name ) );
2191  else if ( type == QgsProcessingParameterEnum::typeName() )
2192  def.reset( new QgsProcessingParameterEnum( name ) );
2193  else if ( type == QgsProcessingParameterString::typeName() )
2194  def.reset( new QgsProcessingParameterString( name ) );
2195  else if ( type == QgsProcessingParameterAuthConfig::typeName() )
2196  def.reset( new QgsProcessingParameterAuthConfig( name ) );
2197  else if ( type == QgsProcessingParameterExpression::typeName() )
2198  def.reset( new QgsProcessingParameterExpression( name ) );
2199  else if ( type == QgsProcessingParameterVectorLayer::typeName() )
2200  def.reset( new QgsProcessingParameterVectorLayer( name ) );
2201  else if ( type == QgsProcessingParameterField::typeName() )
2202  def.reset( new QgsProcessingParameterField( name ) );
2203  else if ( type == QgsProcessingParameterFeatureSource::typeName() )
2204  def.reset( new QgsProcessingParameterFeatureSource( name ) );
2205  else if ( type == QgsProcessingParameterFeatureSink::typeName() )
2206  def.reset( new QgsProcessingParameterFeatureSink( name ) );
2208  def.reset( new QgsProcessingParameterVectorDestination( name ) );
2210  def.reset( new QgsProcessingParameterRasterDestination( name ) );
2212  def.reset( new QgsProcessingParameterFileDestination( name ) );
2214  def.reset( new QgsProcessingParameterFolderDestination( name ) );
2215  else if ( type == QgsProcessingParameterBand::typeName() )
2216  def.reset( new QgsProcessingParameterBand( name ) );
2217  else if ( type == QgsProcessingParameterMeshLayer::typeName() )
2218  def.reset( new QgsProcessingParameterMeshLayer( name ) );
2219  else if ( type == QgsProcessingParameterLayout::typeName() )
2220  def.reset( new QgsProcessingParameterLayout( name ) );
2221  else if ( type == QgsProcessingParameterLayoutItem::typeName() )
2222  def.reset( new QgsProcessingParameterLayoutItem( name ) );
2223  else if ( type == QgsProcessingParameterColor::typeName() )
2224  def.reset( new QgsProcessingParameterColor( name ) );
2226  def.reset( new QgsProcessingParameterCoordinateOperation( name ) );
2227  else
2228  {
2230  if ( paramType )
2231  def.reset( paramType->create( name ) );
2232  }
2233 
2234  if ( !def )
2235  return nullptr;
2236 
2237  def->fromVariantMap( map );
2238  return def.release();
2239 }
2240 
2241 QString QgsProcessingParameters::descriptionFromName( const QString &name )
2242 {
2243  QString desc = name;
2244  desc.replace( '_', ' ' );
2245  return desc;
2246 }
2247 
2249 {
2250  bool isOptional = false;
2251  QString name;
2252  QString definition;
2253  QString type;
2254  if ( !parseScriptCodeParameterOptions( code, isOptional, name, type, definition ) )
2255  return nullptr;
2256 
2257  QString description = descriptionFromName( name );
2258 
2259  if ( type == QLatin1String( "boolean" ) )
2260  return QgsProcessingParameterBoolean::fromScriptCode( name, description, isOptional, definition );
2261  else if ( type == QLatin1String( "crs" ) )
2262  return QgsProcessingParameterCrs::fromScriptCode( name, description, isOptional, definition );
2263  else if ( type == QLatin1String( "layer" ) )
2264  return QgsProcessingParameterMapLayer::fromScriptCode( name, description, isOptional, definition );
2265  else if ( type == QLatin1String( "extent" ) )
2266  return QgsProcessingParameterExtent::fromScriptCode( name, description, isOptional, definition );
2267  else if ( type == QLatin1String( "point" ) )
2268  return QgsProcessingParameterPoint::fromScriptCode( name, description, isOptional, definition );
2269  else if ( type == QLatin1String( "geometry" ) )
2270  return QgsProcessingParameterGeometry::fromScriptCode( name, description, isOptional, definition );
2271  else if ( type == QLatin1String( "file" ) )
2272  return QgsProcessingParameterFile::fromScriptCode( name, description, isOptional, definition, QgsProcessingParameterFile::File );
2273  else if ( type == QLatin1String( "folder" ) )
2274  return QgsProcessingParameterFile::fromScriptCode( name, description, isOptional, definition, QgsProcessingParameterFile::Folder );
2275  else if ( type == QLatin1String( "matrix" ) )
2276  return QgsProcessingParameterMatrix::fromScriptCode( name, description, isOptional, definition );
2277  else if ( type == QLatin1String( "multiple" ) )
2278  return QgsProcessingParameterMultipleLayers::fromScriptCode( name, description, isOptional, definition );
2279  else if ( type == QLatin1String( "number" ) )
2280  return QgsProcessingParameterNumber::fromScriptCode( name, description, isOptional, definition );
2281  else if ( type == QLatin1String( "distance" ) )
2282  return QgsProcessingParameterDistance::fromScriptCode( name, description, isOptional, definition );
2283  else if ( type == QLatin1String( "scale" ) )
2284  return QgsProcessingParameterScale::fromScriptCode( name, description, isOptional, definition );
2285  else if ( type == QLatin1String( "range" ) )
2286  return QgsProcessingParameterRange::fromScriptCode( name, description, isOptional, definition );
2287  else if ( type == QLatin1String( "raster" ) )
2288  return QgsProcessingParameterRasterLayer::fromScriptCode( name, description, isOptional, definition );
2289  else if ( type == QLatin1String( "enum" ) )
2290  return QgsProcessingParameterEnum::fromScriptCode( name, description, isOptional, definition );
2291  else if ( type == QLatin1String( "string" ) )
2292  return QgsProcessingParameterString::fromScriptCode( name, description, isOptional, definition );
2293  else if ( type == QLatin1String( "authcfg" ) )
2294  return QgsProcessingParameterAuthConfig::fromScriptCode( name, description, isOptional, definition );
2295  else if ( type == QLatin1String( "expression" ) )
2296  return QgsProcessingParameterExpression::fromScriptCode( name, description, isOptional, definition );
2297  else if ( type == QLatin1String( "field" ) )
2298  return QgsProcessingParameterField::fromScriptCode( name, description, isOptional, definition );
2299  else if ( type == QLatin1String( "vector" ) )
2300  return QgsProcessingParameterVectorLayer::fromScriptCode( name, description, isOptional, definition );
2301  else if ( type == QLatin1String( "source" ) )
2302  return QgsProcessingParameterFeatureSource::fromScriptCode( name, description, isOptional, definition );
2303  else if ( type == QLatin1String( "sink" ) )
2304  return QgsProcessingParameterFeatureSink::fromScriptCode( name, description, isOptional, definition );
2305  else if ( type == QLatin1String( "vectordestination" ) )
2306  return QgsProcessingParameterVectorDestination::fromScriptCode( name, description, isOptional, definition );
2307  else if ( type == QLatin1String( "rasterdestination" ) )
2308  return QgsProcessingParameterRasterDestination::fromScriptCode( name, description, isOptional, definition );
2309  else if ( type == QLatin1String( "filedestination" ) )
2310  return QgsProcessingParameterFileDestination::fromScriptCode( name, description, isOptional, definition );
2311  else if ( type == QLatin1String( "folderdestination" ) )
2312  return QgsProcessingParameterFolderDestination::fromScriptCode( name, description, isOptional, definition );
2313  else if ( type == QLatin1String( "band" ) )
2314  return QgsProcessingParameterBand::fromScriptCode( name, description, isOptional, definition );
2315  else if ( type == QLatin1String( "mesh" ) )
2316  return QgsProcessingParameterMeshLayer::fromScriptCode( name, description, isOptional, definition );
2317  else if ( type == QLatin1String( "layout" ) )
2318  return QgsProcessingParameterLayout::fromScriptCode( name, description, isOptional, definition );
2319  else if ( type == QLatin1String( "layoutitem" ) )
2320  return QgsProcessingParameterLayoutItem::fromScriptCode( name, description, isOptional, definition );
2321  else if ( type == QLatin1String( "color" ) )
2322  return QgsProcessingParameterColor::fromScriptCode( name, description, isOptional, definition );
2323  else if ( type == QLatin1String( "coordinateoperation" ) )
2324  return QgsProcessingParameterCoordinateOperation::fromScriptCode( name, description, isOptional, definition );
2325  else if ( type == QLatin1String( "maptheme" ) )
2326  return QgsProcessingParameterMapTheme::fromScriptCode( name, description, isOptional, definition );
2327  else if ( type == QLatin1String( "datetime" ) )
2328  return QgsProcessingParameterDateTime::fromScriptCode( name, description, isOptional, definition );
2329  else if ( type == QLatin1String( "providerconnection" ) )
2330  return QgsProcessingParameterProviderConnection::fromScriptCode( name, description, isOptional, definition );
2331  else if ( type == QLatin1String( "databaseschema" ) )
2332  return QgsProcessingParameterDatabaseSchema::fromScriptCode( name, description, isOptional, definition );
2333  else if ( type == QLatin1String( "databasetable" ) )
2334  return QgsProcessingParameterDatabaseTable::fromScriptCode( name, description, isOptional, definition );
2335 
2336  return nullptr;
2337 }
2338 
2339 bool QgsProcessingParameters::parseScriptCodeParameterOptions( const QString &code, bool &isOptional, QString &name, QString &type, QString &definition )
2340 {
2341  QRegularExpression re( QStringLiteral( "(?:#*)(.*?)=\\s*(.*)" ) );
2342  QRegularExpressionMatch m = re.match( code );
2343  if ( !m.hasMatch() )
2344  return false;
2345 
2346  name = m.captured( 1 );
2347  QString tokens = m.captured( 2 );
2348  if ( tokens.startsWith( QLatin1String( "optional" ), Qt::CaseInsensitive ) )
2349  {
2350  isOptional = true;
2351  tokens.remove( 0, 8 ); // length "optional" = 8
2352  }
2353  else
2354  {
2355  isOptional = false;
2356  }
2357 
2358  tokens = tokens.trimmed();
2359 
2360  QRegularExpression re2( QStringLiteral( "(.*?)\\s+(.*)" ) );
2361  m = re2.match( tokens );
2362  if ( !m.hasMatch() )
2363  {
2364  type = tokens.toLower().trimmed();
2365  definition.clear();
2366  }
2367  else
2368  {
2369  type = m.captured( 1 ).toLower().trimmed();
2370  definition = m.captured( 2 );
2371  }
2372  return true;
2373 }
2374 
2375 //
2376 // QgsProcessingParameterDefinition
2377 //
2378 
2379 QgsProcessingParameterDefinition::QgsProcessingParameterDefinition( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, const QString &help )
2380  : mName( name )
2381  , mDescription( description )
2382  , mHelp( help )
2383  , mDefault( defaultValue )
2384  , mFlags( optional ? FlagOptional : 0 )
2385 {}
2386 
2388 {
2389  if ( !input.isValid() && !mDefault.isValid() )
2390  return mFlags & FlagOptional;
2391 
2392  if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
2393  || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
2394  return mFlags & FlagOptional;
2395 
2396  return true;
2397 }
2398 
2400 {
2401  if ( !value.isValid() )
2402  return QStringLiteral( "None" );
2403 
2404  if ( value.canConvert<QgsProperty>() )
2405  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2406 
2407  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
2408 }
2409 
2411 {
2412  QString code = QStringLiteral( "##%1=" ).arg( mName );
2413  if ( mFlags & FlagOptional )
2414  code += QLatin1String( "optional " );
2415  code += type() + ' ';
2416  code += mDefault.toString();
2417  return code.trimmed();
2418 }
2419 
2421 {
2422  // base class method is probably not much use
2423  if ( QgsProcessingParameterType *t = QgsApplication::processingRegistry()->parameterType( type() ) )
2424  {
2425  switch ( outputType )
2426  {
2428  {
2429  QString code = t->className() + QStringLiteral( "('%1', '%2'" ).arg( name(), description() );
2430  if ( mFlags & FlagOptional )
2431  code += QLatin1String( ", optional=True" );
2432 
2434  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
2435  return code;
2436  }
2437  }
2438  }
2439 
2440  // oh well, we tried
2441  return QString();
2442 }
2443 
2445 {
2446  QVariantMap map;
2447  map.insert( QStringLiteral( "parameter_type" ), type() );
2448  map.insert( QStringLiteral( "name" ), mName );
2449  map.insert( QStringLiteral( "description" ), mDescription );
2450  map.insert( QStringLiteral( "help" ), mHelp );
2451  map.insert( QStringLiteral( "default" ), mDefault );
2452  map.insert( QStringLiteral( "defaultGui" ), mGuiDefault );
2453  map.insert( QStringLiteral( "flags" ), static_cast< int >( mFlags ) );
2454  map.insert( QStringLiteral( "metadata" ), mMetadata );
2455  return map;
2456 }
2457 
2459 {
2460  mName = map.value( QStringLiteral( "name" ) ).toString();
2461  mDescription = map.value( QStringLiteral( "description" ) ).toString();
2462  mHelp = map.value( QStringLiteral( "help" ) ).toString();
2463  mDefault = map.value( QStringLiteral( "default" ) );
2464  mGuiDefault = map.value( QStringLiteral( "defaultGui" ) );
2465  mFlags = static_cast< Flags >( map.value( QStringLiteral( "flags" ) ).toInt() );
2466  mMetadata = map.value( QStringLiteral( "metadata" ) ).toMap();
2467  return true;
2468 }
2469 
2471 {
2472  return mAlgorithm;
2473 }
2474 
2476 {
2477  return mAlgorithm ? mAlgorithm->provider() : nullptr;
2478 }
2479 
2481 {
2482  QString text = QStringLiteral( "<p><b>%1</b></p>" ).arg( description() );
2483  if ( !help().isEmpty() )
2484  {
2485  text += QStringLiteral( "<p>%1</p>" ).arg( help() );
2486  }
2487  text += QStringLiteral( "<p>%1</p>" ).arg( QObject::tr( "Python identifier: ‘%1’" ).arg( QStringLiteral( "<i>%1</i>" ).arg( name() ) ) );
2488  return text;
2489 }
2490 
2491 QgsProcessingParameterBoolean::QgsProcessingParameterBoolean( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
2492  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2493 {}
2494 
2496 {
2497  return new QgsProcessingParameterBoolean( *this );
2498 }
2499 
2501 {
2502  if ( !val.isValid() )
2503  return QStringLiteral( "None" );
2504 
2505  if ( val.canConvert<QgsProperty>() )
2506  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
2507  return val.toBool() ? QStringLiteral( "True" ) : QStringLiteral( "False" );
2508 }
2509 
2511 {
2512  QString code = QStringLiteral( "##%1=" ).arg( mName );
2513  if ( mFlags & FlagOptional )
2514  code += QLatin1String( "optional " );
2515  code += type() + ' ';
2516  code += mDefault.toBool() ? QStringLiteral( "true" ) : QStringLiteral( "false" );
2517  return code.trimmed();
2518 }
2519 
2520 QgsProcessingParameterBoolean *QgsProcessingParameterBoolean::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2521 {
2522  return new QgsProcessingParameterBoolean( name, description, definition.toLower().trimmed() != QStringLiteral( "false" ), isOptional );
2523 }
2524 
2525 QgsProcessingParameterCrs::QgsProcessingParameterCrs( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
2526  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2527 {
2528 
2529 }
2530 
2532 {
2533  return new QgsProcessingParameterCrs( *this );
2534 }
2535 
2537 {
2538  if ( !input.isValid() )
2539  return mFlags & FlagOptional;
2540 
2541  if ( input.canConvert<QgsCoordinateReferenceSystem>() )
2542  {
2543  return true;
2544  }
2545  else if ( input.canConvert<QgsProcessingFeatureSourceDefinition>() )
2546  {
2547  return true;
2548  }
2549  else if ( input.canConvert<QgsProcessingOutputLayerDefinition>() )
2550  {
2551  return true;
2552  }
2553 
2554  if ( input.canConvert<QgsProperty>() )
2555  {
2556  return true;
2557  }
2558 
2559  // direct map layer value
2560  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
2561  return true;
2562 
2563  if ( input.type() != QVariant::String || input.toString().isEmpty() )
2564  return mFlags & FlagOptional;
2565 
2566  return true;
2567 }
2568 
2569 QString QgsProcessingParameterCrs::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
2570 {
2571  if ( !value.isValid() )
2572  return QStringLiteral( "None" );
2573 
2574  if ( value.canConvert<QgsCoordinateReferenceSystem>() )
2575  {
2576  if ( !value.value< QgsCoordinateReferenceSystem >().isValid() )
2577  return QStringLiteral( "QgsCoordinateReferenceSystem()" );
2578  else
2579  return QStringLiteral( "QgsCoordinateReferenceSystem('%1')" ).arg( value.value< QgsCoordinateReferenceSystem >().authid() );
2580  }
2581 
2582  if ( value.canConvert<QgsProperty>() )
2583  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2584 
2585  QVariantMap p;
2586  p.insert( name(), value );
2587  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
2588  if ( layer )
2590 
2592 }
2593 
2594 QgsProcessingParameterCrs *QgsProcessingParameterCrs::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2595 {
2596  return new QgsProcessingParameterCrs( name, description, definition.compare( QLatin1String( "none" ), Qt::CaseInsensitive ) == 0 ? QVariant() : definition, isOptional );
2597 }
2598 
2599 QgsProcessingParameterMapLayer::QgsProcessingParameterMapLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, const QList<int> &types )
2600  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2602 {
2603 
2604 }
2605 
2607 {
2608  return new QgsProcessingParameterMapLayer( *this );
2609 }
2610 
2612 {
2613  if ( !input.isValid() )
2614  return mFlags & FlagOptional;
2615 
2616  if ( input.canConvert<QgsProperty>() )
2617  {
2618  return true;
2619  }
2620 
2621  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
2622  {
2623  return true;
2624  }
2625 
2626  if ( input.type() != QVariant::String || input.toString().isEmpty() )
2627  return mFlags & FlagOptional;
2628 
2629  if ( !context )
2630  {
2631  // that's as far as we can get without a context
2632  return true;
2633  }
2634 
2635  // try to load as layer
2636  if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context ) )
2637  return true;
2638 
2639  return false;
2640 }
2641 
2643 {
2644  if ( !val.isValid() )
2645  return QStringLiteral( "None" );
2646 
2647  if ( val.canConvert<QgsProperty>() )
2648  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
2649 
2650  QVariantMap p;
2651  p.insert( name(), val );
2652  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
2654  : QgsProcessingUtils::stringToPythonLiteral( val.toString() );
2655 }
2656 
2658 {
2659  QStringList vectors = QgsProviderRegistry::instance()->fileVectorFilters().split( QStringLiteral( ";;" ) );
2660  QStringList rasters = QgsProviderRegistry::instance()->fileRasterFilters().split( QStringLiteral( ";;" ) );
2661  for ( const QString &raster : rasters )
2662  {
2663  if ( !vectors.contains( raster ) )
2664  vectors << raster;
2665  }
2666  QStringList meshFilters = QgsProviderRegistry::instance()->fileMeshFilters().split( QStringLiteral( ";;" ) );
2667  for ( const QString &mesh : meshFilters )
2668  {
2669  if ( !vectors.contains( mesh ) )
2670  vectors << mesh;
2671  }
2672  vectors.removeAll( QObject::tr( "All files (*.*)" ) );
2673  std::sort( vectors.begin(), vectors.end() );
2674 
2675  return QObject::tr( "All files (*.*)" ) + QStringLiteral( ";;" ) + vectors.join( QLatin1String( ";;" ) );
2676 }
2677 
2679 {
2680  return createAllMapLayerFileFilter();
2681 }
2682 
2684 {
2685  QString code = QStringLiteral( "##%1=" ).arg( mName );
2686  if ( mFlags & FlagOptional )
2687  code += QLatin1String( "optional " );
2688  code += QLatin1String( "layer " );
2689 
2690  for ( int type : mDataTypes )
2691  {
2692  switch ( type )
2693  {
2695  code += QLatin1String( "hasgeometry " );
2696  break;
2697 
2699  code += QLatin1String( "point " );
2700  break;
2701 
2703  code += QLatin1String( "line " );
2704  break;
2705 
2707  code += QLatin1String( "polygon " );
2708  break;
2709 
2711  code += QLatin1String( "raster " );
2712  break;
2713 
2715  code += QLatin1String( "mesh " );
2716  break;
2717  }
2718  }
2719 
2720  code += mDefault.toString();
2721  return code.trimmed();
2722 }
2723 
2724 QgsProcessingParameterMapLayer *QgsProcessingParameterMapLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2725 {
2726  QList< int > types;
2727  QString def = definition;
2728  while ( true )
2729  {
2730  if ( def.startsWith( QLatin1String( "hasgeometry" ), Qt::CaseInsensitive ) )
2731  {
2733  def = def.mid( 12 );
2734  continue;
2735  }
2736  else if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
2737  {
2739  def = def.mid( 6 );
2740  continue;
2741  }
2742  else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
2743  {
2745  def = def.mid( 5 );
2746  continue;
2747  }
2748  else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
2749  {
2751  def = def.mid( 8 );
2752  continue;
2753  }
2754  else if ( def.startsWith( QLatin1String( "raster" ), Qt::CaseInsensitive ) )
2755  {
2756  types << QgsProcessing::TypeRaster;
2757  def = def.mid( 7 );
2758  continue;
2759  }
2760  else if ( def.startsWith( QLatin1String( "mesh" ), Qt::CaseInsensitive ) )
2761  {
2762  types << QgsProcessing::TypeMesh;
2763  def = def.mid( 5 );
2764  continue;
2765  }
2766  break;
2767  }
2768 
2769  return new QgsProcessingParameterMapLayer( name, description, def, isOptional, types );
2770 }
2771 
2773 {
2774  switch ( outputType )
2775  {
2777  {
2778  QString code = QStringLiteral( "QgsProcessingParameterMapLayer('%1', '%2'" ).arg( name(), description() );
2779  if ( mFlags & FlagOptional )
2780  code += QLatin1String( ", optional=True" );
2781 
2783  code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );
2784 
2785  if ( !mDataTypes.empty() )
2786  {
2787  QStringList options;
2788  options.reserve( mDataTypes.size() );
2789  for ( int t : mDataTypes )
2790  options << QStringLiteral( "QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( static_cast< QgsProcessing::SourceType >( t ) ) );
2791  code += QStringLiteral( ", types=[%1])" ).arg( options.join( ',' ) );
2792  }
2793  else
2794  {
2795  code += QLatin1Char( ')' );
2796  }
2797 
2798  return code;
2799  }
2800  }
2801  return QString();
2802 }
2803 
2805 {
2807  QVariantList types;
2808  for ( int type : mDataTypes )
2809  {
2810  types << type;
2811  }
2812  map.insert( QStringLiteral( "data_types" ), types );
2813  return map;
2814 }
2815 
2817 {
2819  mDataTypes.clear();
2820  const QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
2821  for ( const QVariant &val : values )
2822  {
2823  mDataTypes << val.toInt();
2824  }
2825  return true;
2826 }
2827 
2828 QgsProcessingParameterExtent::QgsProcessingParameterExtent( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
2829  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2830 {
2831 
2832 }
2833 
2835 {
2836  return new QgsProcessingParameterExtent( *this );
2837 }
2838 
2840 {
2841  if ( !input.isValid() )
2842  return mFlags & FlagOptional;
2843 
2844  if ( input.canConvert<QgsProcessingFeatureSourceDefinition>() )
2845  {
2846  return true;
2847  }
2848  else if ( input.canConvert<QgsProcessingOutputLayerDefinition>() )
2849  {
2850  return true;
2851  }
2852 
2853  if ( input.canConvert<QgsProperty>() )
2854  {
2855  return true;
2856  }
2857 
2858  if ( input.canConvert< QgsRectangle >() )
2859  {
2860  QgsRectangle r = input.value<QgsRectangle>();
2861  return !r.isNull();
2862  }
2863  if ( input.canConvert< QgsGeometry >() )
2864  {
2865  return true;
2866  }
2867  if ( input.canConvert< QgsReferencedRectangle >() )
2868  {
2870  return !r.isNull();
2871  }
2872 
2873  // direct map layer value
2874  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
2875  return true;
2876 
2877  if ( input.type() != QVariant::String || input.toString().isEmpty() )
2878  return mFlags & FlagOptional;
2879 
2880  if ( !context )
2881  {
2882  // that's as far as we can get without a context
2883  return true;
2884  }
2885 
2886  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
2887  QRegularExpressionMatch match = rx.match( input.toString() );
2888  if ( match.hasMatch() )
2889  {
2890  bool xMinOk = false;
2891  ( void )match.captured( 1 ).toDouble( &xMinOk );
2892  bool xMaxOk = false;
2893  ( void )match.captured( 2 ).toDouble( &xMaxOk );
2894  bool yMinOk = false;
2895  ( void )match.captured( 3 ).toDouble( &yMinOk );
2896  bool yMaxOk = false;
2897  ( void )match.captured( 4 ).toDouble( &yMaxOk );
2898  if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
2899  return true;
2900  }
2901 
2902  // try as layer extent
2903  return QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
2904 }
2905 
2906 QString QgsProcessingParameterExtent::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
2907 {
2908  if ( !value.isValid() )
2909  return QStringLiteral( "None" );
2910 
2911  if ( value.canConvert<QgsProperty>() )
2912  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2913 
2914  if ( value.canConvert< QgsRectangle >() )
2915  {
2916  QgsRectangle r = value.value<QgsRectangle>();
2917  return QStringLiteral( "'%1, %3, %2, %4'" ).arg( qgsDoubleToString( r.xMinimum() ),
2918  qgsDoubleToString( r.yMinimum() ),
2919  qgsDoubleToString( r.xMaximum() ),
2920  qgsDoubleToString( r.yMaximum() ) );
2921  }
2922  else if ( value.canConvert< QgsReferencedRectangle >() )
2923  {
2925  return QStringLiteral( "'%1, %3, %2, %4 [%5]'" ).arg( qgsDoubleToString( r.xMinimum() ),
2926  qgsDoubleToString( r.yMinimum() ),
2927  qgsDoubleToString( r.xMaximum() ),
2928  qgsDoubleToString( r.yMaximum() ), r.crs().authid() );
2929  }
2930  else if ( value.canConvert< QgsGeometry >() )
2931  {
2932  const QgsGeometry g = value.value<QgsGeometry>();
2933  if ( !g.isNull() )
2934  {
2935  const QString wkt = g.asWkt();
2936  return QStringLiteral( "QgsGeometry.fromWkt('%1')" ).arg( wkt );
2937  }
2938  }
2939 
2940  QVariantMap p;
2941  p.insert( name(), value );
2942  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
2943  if ( layer )
2945 
2947 }
2948 
2949 QgsProcessingParameterExtent *QgsProcessingParameterExtent::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2950 {
2951  return new QgsProcessingParameterExtent( name, description, definition, isOptional );
2952 }
2953 
2954 QgsProcessingParameterPoint::QgsProcessingParameterPoint( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
2955  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2956 {
2957 
2958 }
2959 
2961 {
2962  return new QgsProcessingParameterPoint( *this );
2963 }
2964 
2966 {
2967  if ( !input.isValid() )
2968  return mFlags & FlagOptional;
2969 
2970  if ( input.canConvert<QgsProperty>() )
2971  {
2972  return true;
2973  }
2974 
2975  if ( input.canConvert< QgsPointXY >() )
2976  {
2977  return true;
2978  }
2979  if ( input.canConvert< QgsReferencedPointXY >() )
2980  {
2981  return true;
2982  }
2983  if ( input.canConvert< QgsGeometry >() )
2984  {
2985  return true;
2986  }
2987 
2988  if ( input.type() == QVariant::String )
2989  {
2990  if ( input.toString().isEmpty() )
2991  return mFlags & FlagOptional;
2992  }
2993 
2994  QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
2995 
2996  QRegularExpressionMatch match = rx.match( input.toString() );
2997  if ( match.hasMatch() )
2998  {
2999  bool xOk = false;
3000  ( void )match.captured( 1 ).toDouble( &xOk );
3001  bool yOk = false;
3002  ( void )match.captured( 2 ).toDouble( &yOk );
3003  return xOk && yOk;
3004  }
3005  else
3006  return false;
3007 }
3008 
3009 QString QgsProcessingParameterPoint::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
3010 {
3011  if ( !value.isValid() )
3012  return QStringLiteral( "None" );
3013 
3014  if ( value.canConvert<QgsProperty>() )
3015  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3016 
3017  if ( value.canConvert< QgsPointXY >() )
3018  {
3019  QgsPointXY r = value.value<QgsPointXY>();
3020  return QStringLiteral( "'%1,%2'" ).arg( qgsDoubleToString( r.x() ),
3021  qgsDoubleToString( r.y() ) );
3022  }
3023  else if ( value.canConvert< QgsReferencedPointXY >() )
3024  {
3025  QgsReferencedPointXY r = value.value<QgsReferencedPointXY>();
3026  return QStringLiteral( "'%1,%2 [%3]'" ).arg( qgsDoubleToString( r.x() ),
3027  qgsDoubleToString( r.y() ),
3028  r.crs().authid() );
3029  }
3030  else if ( value.canConvert< QgsGeometry >() )
3031  {
3032  const QgsGeometry g = value.value<QgsGeometry>();
3033  if ( !g.isNull() )
3034  {
3035  const QString wkt = g.asWkt();
3036  return QStringLiteral( "QgsGeometry.fromWkt('%1')" ).arg( wkt );
3037  }
3038  }
3039 
3041 }
3042 
3043 QgsProcessingParameterPoint *QgsProcessingParameterPoint::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3044 {
3045  return new QgsProcessingParameterPoint( name, description, definition, isOptional );
3046 }
3047 
3048 QgsProcessingParameterGeometry::QgsProcessingParameterGeometry( const QString &name, const QString &description,
3049  const QVariant &defaultValue, bool optional, const QList<int> &geometryTypes )
3050  : QgsProcessingParameterDefinition( name, description, defaultValue, optional ),
3051  mGeomTypes( geometryTypes )
3052 {
3053 
3054 }
3055 
3057 {
3058  return new QgsProcessingParameterGeometry( *this );
3059 }
3060 
3062 {
3063  if ( !input.isValid() )
3064  return mFlags & FlagOptional;
3065 
3066  if ( input.canConvert<QgsProperty>() )
3067  {
3068  return true;
3069  }
3070 
3071  bool anyTypeAllowed = mGeomTypes.isEmpty() || mGeomTypes.contains( QgsWkbTypes::UnknownGeometry );
3072 
3073  if ( input.canConvert< QgsGeometry >() )
3074  {
3075  return anyTypeAllowed || mGeomTypes.contains( input.value<QgsGeometry>().type() );
3076  }
3077 
3078  if ( input.canConvert< QgsReferencedGeometry >() )
3079  {
3080  return anyTypeAllowed || mGeomTypes.contains( input.value<QgsReferencedGeometry>().type() );
3081  }
3082 
3083  if ( input.canConvert< QgsPointXY >() )
3084  {
3085  return anyTypeAllowed || mGeomTypes.contains( QgsWkbTypes::PointGeometry );
3086  }
3087 
3088  if ( input.canConvert< QgsRectangle >() )
3089  {
3090  return anyTypeAllowed || mGeomTypes.contains( QgsWkbTypes::PolygonGeometry );
3091  }
3092 
3093  if ( input.canConvert< QgsReferencedPointXY >() )
3094  {
3095  return anyTypeAllowed || mGeomTypes.contains( QgsWkbTypes::PointGeometry );
3096  }
3097 
3098  if ( input.canConvert< QgsReferencedRectangle >() )
3099  {
3100  return anyTypeAllowed || mGeomTypes.contains( QgsWkbTypes::PolygonGeometry );
3101  }
3102 
3103  if ( input.type() == QVariant::String )
3104  {
3105  if ( input.toString().isEmpty() )
3106  return mFlags & FlagOptional;
3107  }
3108 
3109  // Match against EWKT
3110  QRegularExpression rx( QStringLiteral( "^\\s*(?:CRS=(.*);)?(.*?)$" ) );
3111 
3112  QRegularExpressionMatch match = rx.match( input.toString() );
3113  if ( match.hasMatch() )
3114  {
3115  QgsGeometry g = QgsGeometry::fromWkt( match.captured( 2 ) );
3116  if ( ! g.isNull() )
3117  {
3118  return anyTypeAllowed || mGeomTypes.contains( g.type() );
3119  }
3120  else
3121  {
3122  QgsMessageLog::logMessage( QObject::tr( "Error creating geometry: \"%1\"" ).arg( g.lastError() ), QObject::tr( "Processing" ) );
3123  }
3124  }
3125  return false;
3126 }
3127 
3128 QString QgsProcessingParameterGeometry::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
3129 {
3131  {
3132  if ( !crs.isValid() )
3134  else
3135  return QgsProcessingUtils::stringToPythonLiteral( QStringLiteral( "CRS=%1;%2" ).arg( crs.authid().isEmpty() ? crs.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) : crs.authid(), g.asWkt() ) );
3136  };
3137 
3138  if ( !value.isValid() )
3139  return QStringLiteral( "None" );
3140 
3141  if ( value.canConvert<QgsProperty>() )
3142  return QStringLiteral( "QgsProperty.fromExpression(%1)" ).arg( QgsProcessingUtils::stringToPythonLiteral( value.value< QgsProperty >().asExpression() ) );
3143 
3144  if ( value.canConvert< QgsGeometry >() )
3145  {
3146  const QgsGeometry g = value.value<QgsGeometry>();
3147  if ( !g.isNull() )
3148  return asPythonString( g );
3149  }
3150 
3151  if ( value.canConvert< QgsReferencedGeometry >() )
3152  {
3153  const QgsReferencedGeometry g = value.value<QgsReferencedGeometry>();
3154  if ( !g.isNull() )
3155  return asPythonString( g, g.crs() );
3156  }
3157 
3158  if ( value.canConvert< QgsPointXY >() )
3159  {
3160  const QgsGeometry g = QgsGeometry::fromPointXY( value.value<QgsPointXY>() );
3161  if ( !g.isNull() )
3162  return asPythonString( g );
3163  }
3164 
3165  if ( value.canConvert< QgsReferencedPointXY >() )
3166  {
3168  if ( !g.isNull() )
3169  return asPythonString( g, g.crs() );
3170  }
3171 
3172  if ( value.canConvert< QgsRectangle >() )
3173  {
3174  const QgsGeometry g = QgsGeometry::fromRect( value.value<QgsRectangle>() );
3175  if ( !g.isNull() )
3176  return asPythonString( g );
3177  }
3178 
3179  if ( value.canConvert< QgsReferencedRectangle >() )
3180  {
3182  if ( !g.isNull() )
3183  return asPythonString( g, g.crs() );
3184  }
3185 
3187 }
3188 
3190 {
3191  QString code = QStringLiteral( "##%1=" ).arg( mName );
3192  if ( mFlags & FlagOptional )
3193  code += QLatin1String( "optional " );
3194  code += type() + ' ';
3195 
3196  for ( int type : mGeomTypes )
3197  {
3198  switch ( static_cast<QgsWkbTypes::GeometryType>( type ) )
3199  {
3201  code += QLatin1String( "point " );
3202  break;
3203 
3205  code += QLatin1String( "line " );
3206  break;
3207 
3209  code += QLatin1String( "polygon " );
3210  break;
3211 
3212  default:
3213  code += QLatin1String( "unknown" );
3214  break;
3215  }
3216  }
3217 
3218  code += mDefault.toString();
3219  return code.trimmed();
3220 }
3221 
3223 {
3224  switch ( outputType )
3225  {
3227  {
3228  QString code = QStringLiteral( "QgsProcessingParameterGeometry('%1', '%2'" ).arg( name(), description() );
3229  if ( mFlags & FlagOptional )
3230  code += QLatin1String( ", optional=True" );
3231 
3232  if ( !mGeomTypes.empty() )
3233  {
3234  auto geomTypeToString = []( QgsWkbTypes::GeometryType t ) -> QString
3235  {
3236  switch ( t )
3237  {
3239  return QStringLiteral( "PointGeometry" );
3240 
3242  return QStringLiteral( "LineGeometry" );
3243 
3245  return QStringLiteral( "PolygonGeometry" );
3246 
3248  return QStringLiteral( "UnknownGeometry" );
3249 
3251  return QStringLiteral( "NullGeometry" );
3252  }
3253  return QString();
3254  };
3255 
3256  QStringList options;
3257  options.reserve( mGeomTypes.size() );
3258  for ( int type : mGeomTypes )
3259  {
3260  options << QStringLiteral( " QgsWkbTypes.%1" ).arg( geomTypeToString( static_cast<QgsWkbTypes::GeometryType>( type ) ) );
3261  }
3262  code += QStringLiteral( ", geometryTypes=[%1 ]" ).arg( options.join( ',' ) );
3263  }
3264 
3266  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
3267  return code;
3268  }
3269  }
3270  return QString();
3271 }
3272 
3274 {
3276  QVariantList types;
3277  for ( int type : mGeomTypes )
3278  {
3279  types << type;
3280  }
3281  map.insert( QStringLiteral( "geometrytypes" ), types );
3282  return map;
3283 }
3284 
3286 {
3288  mGeomTypes.clear();
3289  const QVariantList values = map.value( QStringLiteral( "geometrytypes" ) ).toList();
3290  for ( const QVariant &val : values )
3291  {
3292  mGeomTypes << val.toInt();
3293  }
3294  return true;
3295 }
3296 
3297 QgsProcessingParameterGeometry *QgsProcessingParameterGeometry::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3298 {
3299  return new QgsProcessingParameterGeometry( name, description, definition, isOptional );
3300 }
3301 
3302 QgsProcessingParameterFile::QgsProcessingParameterFile( const QString &name, const QString &description, Behavior behavior, const QString &extension, const QVariant &defaultValue, bool optional, const QString &fileFilter )
3303  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3304  , mBehavior( behavior )
3305  , mExtension( fileFilter.isEmpty() ? extension : QString() )
3306  , mFileFilter( fileFilter.isEmpty() && extension.isEmpty() ? QObject::tr( "All files (*.*)" ) : fileFilter )
3307 {
3308 
3309 }
3310 
3312 {
3313  return new QgsProcessingParameterFile( *this );
3314 }
3315 
3317 {
3318  if ( !input.isValid() )
3319  return mFlags & FlagOptional;
3320 
3321  if ( input.canConvert<QgsProperty>() )
3322  {
3323  return true;
3324  }
3325 
3326  QString string = input.toString().trimmed();
3327 
3328  if ( input.type() != QVariant::String || string.isEmpty() )
3329  return mFlags & FlagOptional;
3330 
3331  switch ( mBehavior )
3332  {
3333  case File:
3334  {
3335  if ( !mExtension.isEmpty() )
3336  {
3337  return string.endsWith( mExtension, Qt::CaseInsensitive );
3338  }
3339  else if ( !mFileFilter.isEmpty() )
3340  {
3341  const QString test = QgsFileUtils::addExtensionFromFilter( string, mFileFilter );
3342  return test == string;
3343  }
3344  else
3345  {
3346  return true;
3347  }
3348  }
3349 
3350  case Folder:
3351  return true;
3352  }
3353  return true;
3354 }
3355 
3357 {
3358  QString code = QStringLiteral( "##%1=" ).arg( mName );
3359  if ( mFlags & FlagOptional )
3360  code += QLatin1String( "optional " );
3361  code += ( mBehavior == File ? QStringLiteral( "file" ) : QStringLiteral( "folder" ) ) + ' ';
3362  code += mDefault.toString();
3363  return code.trimmed();
3364 }
3365 
3367 {
3368  switch ( outputType )
3369  {
3371  {
3372 
3373  QString code = QStringLiteral( "QgsProcessingParameterFile('%1', '%2'" ).arg( name(), description() );
3374  if ( mFlags & FlagOptional )
3375  code += QLatin1String( ", optional=True" );
3376  code += QStringLiteral( ", behavior=%1" ).arg( mBehavior == File ? QStringLiteral( "QgsProcessingParameterFile.File" ) : QStringLiteral( "QgsProcessingParameterFile.Folder" ) );
3377  if ( !mExtension.isEmpty() )
3378  code += QStringLiteral( ", extension='%1'" ).arg( mExtension );
3379  if ( !mFileFilter.isEmpty() )
3380  code += QStringLiteral( ", fileFilter='%1'" ).arg( mFileFilter );
3382  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
3383  return code;
3384  }
3385  }
3386  return QString();
3387 }
3388 
3390 {
3391  switch ( mBehavior )
3392  {
3393  case File:
3394  {
3395  if ( !mFileFilter.isEmpty() )
3396  return mFileFilter != QObject::tr( "All files (*.*)" ) ? mFileFilter + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" ) : mFileFilter;
3397  else if ( !mExtension.isEmpty() )
3398  return QObject::tr( "%1 files" ).arg( mExtension.toUpper() ) + QStringLiteral( " (*." ) + mExtension.toLower() + QStringLiteral( ");;" ) + QObject::tr( "All files (*.*)" );
3399  else
3400  return QObject::tr( "All files (*.*)" );
3401  }
3402 
3403  case Folder:
3404  return QString();
3405  }
3406  return QString();
3407 }
3408 
3409 void QgsProcessingParameterFile::setExtension( const QString &extension )
3410 {
3411  mExtension = extension;
3412  mFileFilter.clear();
3413 }
3414 
3416 {
3417  return mFileFilter;
3418 }
3419 
3420 void QgsProcessingParameterFile::setFileFilter( const QString &filter )
3421 {
3422  mFileFilter = filter;
3423  mExtension.clear();
3424 }
3425 
3427 {
3429  map.insert( QStringLiteral( "behavior" ), mBehavior );
3430  map.insert( QStringLiteral( "extension" ), mExtension );
3431  map.insert( QStringLiteral( "filefilter" ), mFileFilter );
3432  return map;
3433 }
3434 
3435 bool QgsProcessingParameterFile::fromVariantMap( const QVariantMap &map )
3436 {
3438  mBehavior = static_cast< Behavior >( map.value( QStringLiteral( "behavior" ) ).toInt() );
3439  mExtension = map.value( QStringLiteral( "extension" ) ).toString();
3440  mFileFilter = map.value( QStringLiteral( "filefilter" ) ).toString();
3441  return true;
3442 }
3443 
3444 QgsProcessingParameterFile *QgsProcessingParameterFile::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition, QgsProcessingParameterFile::Behavior behavior )
3445 {
3446  return new QgsProcessingParameterFile( name, description, behavior, QString(), definition, isOptional );
3447 }
3448 
3449 QgsProcessingParameterMatrix::QgsProcessingParameterMatrix( const QString &name, const QString &description, int numberRows, bool fixedNumberRows, const QStringList &headers, const QVariant &defaultValue, bool optional )
3450  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3451  , mHeaders( headers )
3452  , mNumberRows( numberRows )
3453  , mFixedNumberRows( fixedNumberRows )
3454 {
3455 
3456 }
3457 
3459 {
3460  return new QgsProcessingParameterMatrix( *this );
3461 }
3462 
3464 {
3465  if ( !input.isValid() )
3466  return mFlags & FlagOptional;
3467 
3468  if ( input.type() == QVariant::String )
3469  {
3470  if ( input.toString().isEmpty() )
3471  return mFlags & FlagOptional;
3472  return true;
3473  }
3474  else if ( input.type() == QVariant::List )
3475  {
3476  if ( input.toList().isEmpty() )
3477  return mFlags & FlagOptional;
3478  return true;
3479  }
3480  else if ( input.type() == QVariant::Double || input.type() == QVariant::Int )
3481  {
3482  return true;
3483  }
3484 
3485  return false;
3486 }
3487 
3488 QString QgsProcessingParameterMatrix::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
3489 {
3490  if ( !value.isValid() )
3491  return QStringLiteral( "None" );
3492 
3493  if ( value.canConvert<QgsProperty>() )
3494  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3495 
3496  QVariantMap p;
3497  p.insert( name(), value );
3498  QVariantList list = QgsProcessingParameters::parameterAsMatrix( this, p, context );
3499 
3500  QStringList parts;
3501  const auto constList = list;
3502  for ( const QVariant &v : constList )
3503  {
3504  if ( v.type() == QVariant::List )
3505  {
3506  QStringList parts2;
3507  const auto constToList = v.toList();
3508  for ( const QVariant &v2 : constToList )
3509  {
3510  if ( v2.isNull() || !v2.isValid() )
3511  parts2 << QStringLiteral( "None" );
3512  else if ( v2.toString().isEmpty() )
3513  parts2 << QStringLiteral( "''" );
3514  else
3515  parts2 << v2.toString();
3516  }
3517  parts << parts2.join( ',' ).prepend( '[' ).append( ']' );
3518  }
3519  else
3520  {
3521  if ( v.isNull() || !v.isValid() )
3522  parts << QStringLiteral( "None" );
3523  else if ( v.toString().isEmpty() )
3524  parts << QStringLiteral( "''" );
3525  else
3526  parts << v.toString();
3527  }
3528  }
3529 
3530  return parts.join( ',' ).prepend( '[' ).append( ']' );
3531 }
3532 
3534 {
3535  switch ( outputType )
3536  {
3538  {
3539  QString code = QStringLiteral( "QgsProcessingParameterMatrix('%1', '%2'" ).arg( name(), description() );
3540  if ( mFlags & FlagOptional )
3541  code += QLatin1String( ", optional=True" );
3542  code += QStringLiteral( ", numberRows=" ).arg( mNumberRows );
3543  code += QStringLiteral( ", hasFixedNumberRows=" ).arg( mFixedNumberRows ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
3544 
3545  QStringList headers;
3546  headers.reserve( mHeaders.size() );
3547  for ( const QString &h : mHeaders )
3549  code += QStringLiteral( ", headers=[%1]" ).arg( headers.join( ',' ) );
3550 
3552  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
3553  return code;
3554  }
3555  }
3556  return QString();
3557 }
3558 
3560 {
3561  return mHeaders;
3562 }
3563 
3564 void QgsProcessingParameterMatrix::setHeaders( const QStringList &headers )
3565 {
3566  mHeaders = headers;
3567 }
3568 
3570 {
3571  return mNumberRows;
3572 }
3573 
3575 {
3576  mNumberRows = numberRows;
3577 }
3578 
3580 {
3581  return mFixedNumberRows;
3582 }
3583 
3585 {
3586  mFixedNumberRows = fixedNumberRows;
3587 }
3588 
3590 {
3592  map.insert( QStringLiteral( "headers" ), mHeaders );
3593  map.insert( QStringLiteral( "rows" ), mNumberRows );
3594  map.insert( QStringLiteral( "fixed_number_rows" ), mFixedNumberRows );
3595  return map;
3596 }
3597 
3598 bool QgsProcessingParameterMatrix::fromVariantMap( const QVariantMap &map )
3599 {
3601  mHeaders = map.value( QStringLiteral( "headers" ) ).toStringList();
3602  mNumberRows = map.value( QStringLiteral( "rows" ) ).toInt();
3603  mFixedNumberRows = map.value( QStringLiteral( "fixed_number_rows" ) ).toBool();
3604  return true;
3605 }
3606 
3607 QgsProcessingParameterMatrix *QgsProcessingParameterMatrix::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3608 {
3609  return new QgsProcessingParameterMatrix( name, description, 0, false, QStringList(), definition.isEmpty() ? QVariant() : definition, isOptional );
3610 }
3611 
3612 QgsProcessingParameterMultipleLayers::QgsProcessingParameterMultipleLayers( const QString &name, const QString &description, QgsProcessing::SourceType layerType, const QVariant &defaultValue, bool optional )
3613  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3614  , mLayerType( layerType )
3615 {
3616 
3617 }
3618 
3620 {
3621  return new QgsProcessingParameterMultipleLayers( *this );
3622 }
3623 
3625 {
3626  if ( !input.isValid() )
3627  return mFlags & FlagOptional;
3628 
3629  if ( mLayerType != QgsProcessing::TypeFile )
3630  {
3631  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
3632  {
3633  return true;
3634  }
3635  }
3636 
3637  if ( input.type() == QVariant::String )
3638  {
3639  if ( input.toString().isEmpty() )
3640  return mFlags & FlagOptional;
3641 
3642  if ( mMinimumNumberInputs > 1 )
3643  return false;
3644 
3645  if ( !context )
3646  return true;
3647 
3648  if ( mLayerType != QgsProcessing::TypeFile )
3649  return QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
3650  else
3651  return true;
3652  }
3653  else if ( input.type() == QVariant::List )
3654  {
3655  if ( input.toList().count() < mMinimumNumberInputs )
3656  return mFlags & FlagOptional;
3657 
3658  if ( mMinimumNumberInputs > input.toList().count() )
3659  return false;
3660 
3661  if ( !context )
3662  return true;
3663 
3664  if ( mLayerType != QgsProcessing::TypeFile )
3665  {
3666  const auto constToList = input.toList();
3667  for ( const QVariant &v : constToList )
3668  {
3669  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( v ) ) )
3670  continue;
3671 
3672  if ( !QgsProcessingUtils::mapLayerFromString( v.toString(), *context ) )
3673  return false;
3674  }
3675  }
3676  return true;
3677  }
3678  else if ( input.type() == QVariant::StringList )
3679  {
3680  if ( input.toStringList().count() < mMinimumNumberInputs )
3681  return mFlags & FlagOptional;
3682 
3683  if ( mMinimumNumberInputs > input.toStringList().count() )
3684  return false;
3685 
3686  if ( !context )
3687  return true;
3688 
3689  if ( mLayerType != QgsProcessing::TypeFile )
3690  {
3691  const auto constToStringList = input.toStringList();
3692  for ( const QString &v : constToStringList )
3693  {
3694  if ( !QgsProcessingUtils::mapLayerFromString( v, *context ) )
3695  return false;
3696  }
3697  }
3698  return true;
3699  }
3700  return false;
3701 }
3702 
3704 {
3705  if ( !value.isValid() )
3706  return QStringLiteral( "None" );
3707 
3708  if ( value.canConvert<QgsProperty>() )
3709  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3710 
3711  if ( mLayerType == QgsProcessing::TypeFile )
3712  {
3713  QStringList parts;
3714  if ( value.type() == QVariant::StringList )
3715  {
3716  const QStringList list = value.toStringList();
3717  parts.reserve( list.count() );
3718  for ( const QString &v : list )
3720  }
3721  else if ( value.type() == QVariant::List )
3722  {
3723  const QVariantList list = value.toList();
3724  parts.reserve( list.count() );
3725  for ( const QVariant &v : list )
3726  parts << QgsProcessingUtils::stringToPythonLiteral( v.toString() );
3727  }
3728  if ( !parts.isEmpty() )
3729  return parts.join( ',' ).prepend( '[' ).append( ']' );
3730  }
3731  else
3732  {
3733  QVariantMap p;
3734  p.insert( name(), value );
3735  const QList<QgsMapLayer *> list = QgsProcessingParameters::parameterAsLayerList( this, p, context );
3736  if ( !list.isEmpty() )
3737  {
3738  QStringList parts;
3739  parts.reserve( list.count() );
3740  for ( const QgsMapLayer *layer : list )
3741  {
3743  }
3744  return parts.join( ',' ).prepend( '[' ).append( ']' );
3745  }
3746  }
3747 
3749 }
3750 
3752 {
3753  QString code = QStringLiteral( "##%1=" ).arg( mName );
3754  if ( mFlags & FlagOptional )
3755  code += QLatin1String( "optional " );
3756  switch ( mLayerType )
3757  {
3759  code += QLatin1String( "multiple raster" );
3760  break;
3761 
3763  code += QLatin1String( "multiple file" );
3764  break;
3765 
3766  default:
3767  code += QLatin1String( "multiple vector" );
3768  break;
3769  }
3770  code += ' ';
3771  if ( mDefault.type() == QVariant::List )
3772  {
3773  QStringList parts;
3774  const auto constToList = mDefault.toList();
3775  for ( const QVariant &var : constToList )
3776  {
3777  parts << var.toString();
3778  }
3779  code += parts.join( ',' );
3780  }
3781  else if ( mDefault.type() == QVariant::StringList )
3782  {
3783  code += mDefault.toStringList().join( ',' );
3784  }
3785  else
3786  {
3787  code += mDefault.toString();
3788  }
3789  return code.trimmed();
3790 }
3791 
3793 {
3794  switch ( outputType )
3795  {
3797  {
3798  QString code = QStringLiteral( "QgsProcessingParameterMultipleLayers('%1', '%2'" ).arg( name(), description() );
3799  if ( mFlags & FlagOptional )
3800  code += QLatin1String( ", optional=True" );
3801 
3802  QString layerType = QStringLiteral( "QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( mLayerType ) );
3803 
3804  code += QStringLiteral( ", layerType=%1" ).arg( layerType );
3806  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
3807  return code;
3808  }
3809  }
3810  return QString();
3811 }
3812 
3814 {
3815  QStringList exts;
3816  switch ( mLayerType )
3817  {
3819  return QObject::tr( "All files (*.*)" );
3820 
3822  return QgsProviderRegistry::instance()->fileRasterFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
3823 
3829  return QgsProviderRegistry::instance()->fileVectorFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
3830 
3832  return QgsProviderRegistry::instance()->fileMeshFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
3833 
3835  return createAllMapLayerFileFilter();
3836  }
3837  return QString();
3838 }
3839 
3841 {
3842  return mLayerType;
3843 }
3844 
3846 {
3847  mLayerType = type;
3848 }
3849 
3851 {
3852  return mMinimumNumberInputs;
3853 }
3854 
3856 {
3857  if ( mMinimumNumberInputs >= 1 || !( flags() & QgsProcessingParameterDefinition::FlagOptional ) )
3858  mMinimumNumberInputs = minimumNumberInputs;
3859 }
3860 
3862 {
3864  map.insert( QStringLiteral( "layer_type" ), mLayerType );
3865  map.insert( QStringLiteral( "min_inputs" ), mMinimumNumberInputs );
3866  return map;
3867 }
3868 
3870 {
3872  mLayerType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "layer_type" ) ).toInt() );
3873  mMinimumNumberInputs = map.value( QStringLiteral( "min_inputs" ) ).toInt();
3874  return true;
3875 }
3876 
3877 QgsProcessingParameterMultipleLayers *QgsProcessingParameterMultipleLayers::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3878 {
3879  QString type = definition;
3880  QString defaultVal;
3881  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)" ) );
3882  QRegularExpressionMatch m = re.match( definition );
3883  if ( m.hasMatch() )
3884  {
3885  type = m.captured( 1 ).toLower().trimmed();
3886  defaultVal = m.captured( 2 );
3887  }
3889  if ( type == QLatin1String( "vector" ) )
3891  else if ( type == QLatin1String( "raster" ) )
3893  else if ( type == QLatin1String( "file" ) )
3895  return new QgsProcessingParameterMultipleLayers( name, description, layerType, defaultVal.isEmpty() ? QVariant() : defaultVal, isOptional );
3896 }
3897 
3898 QgsProcessingParameterNumber::QgsProcessingParameterNumber( const QString &name, const QString &description, Type type, const QVariant &defaultValue, bool optional, double minValue, double maxValue )
3899  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3900  , mMin( minValue )
3901  , mMax( maxValue )
3902  , mDataType( type )
3903 {
3904  if ( mMin >= mMax )
3905  {
3906  QgsMessageLog::logMessage( QObject::tr( "Invalid number parameter \"%1\": min value %2 is >= max value %3!" ).arg( name ).arg( mMin ).arg( mMax ), QObject::tr( "Processing" ) );
3907  }
3908 }
3909 
3911 {
3912  return new QgsProcessingParameterNumber( *this );
3913 }
3914 
3916 {
3917  QVariant input = value;
3918  if ( !input.isValid() )
3919  {
3920  if ( !defaultValue().isValid() )
3921  return mFlags & FlagOptional;
3922 
3923  input = defaultValue();
3924  }
3925 
3926  if ( input.canConvert<QgsProperty>() )
3927  {
3928  return true;
3929  }
3930 
3931  bool ok = false;
3932  double res = input.toDouble( &ok );
3933  if ( !ok )
3934  return mFlags & FlagOptional;
3935 
3936  return !( res < mMin || res > mMax );
3937 }
3938 
3940 {
3941  if ( !value.isValid() )
3942  return QStringLiteral( "None" );
3943 
3944  if ( value.canConvert<QgsProperty>() )
3945  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3946 
3947  return value.toString();
3948 }
3949 
3951 {
3953  QStringList parts;
3954  if ( mMin > std::numeric_limits<double>::lowest() + 1 )
3955  parts << QObject::tr( "Minimum value: %1" ).arg( mMin );
3956  if ( mMax < std::numeric_limits<double>::max() )
3957  parts << QObject::tr( "Maximum value: %1" ).arg( mMax );
3958  if ( mDefault.isValid() )
3959  parts << QObject::tr( "Default value: %1" ).arg( mDataType == Integer ? mDefault.toInt() : mDefault.toDouble() );
3960  QString extra = parts.join( QLatin1String( "<br />" ) );
3961  if ( !extra.isEmpty() )
3962  text += QStringLiteral( "<p>%1</p>" ).arg( extra );
3963  return text;
3964 }
3965 
3967 {
3968  switch ( outputType )
3969  {
3971  {
3972  QString code = QStringLiteral( "QgsProcessingParameterNumber('%1', '%2'" ).arg( name(), description() );
3973  if ( mFlags & FlagOptional )
3974  code += QLatin1String( ", optional=True" );
3975 
3976  code += QStringLiteral( ", type=%1" ).arg( mDataType == Integer ? QStringLiteral( "QgsProcessingParameterNumber.Integer" ) : QStringLiteral( "QgsProcessingParameterNumber.Double" ) );
3977 
3978  if ( mMin != std::numeric_limits<double>::lowest() + 1 )
3979  code += QStringLiteral( ", minValue=%1" ).arg( mMin );
3980  if ( mMax != std::numeric_limits<double>::max() )
3981  code += QStringLiteral( ", maxValue=%1" ).arg( mMax );
3983  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
3984  return code;
3985  }
3986  }
3987  return QString();
3988 }
3989 
3991 {
3992  return mMin;
3993 }
3994 
3996 {
3997  mMin = min;
3998 }
3999 
4001 {
4002  return mMax;
4003 }
4004 
4006 {
4007  mMax = max;
4008 }
4009 
4011 {
4012  return mDataType;
4013 }
4014 
4016 {
4017  mDataType = dataType;
4018 }
4019 
4021 {
4023  map.insert( QStringLiteral( "min" ), mMin );
4024  map.insert( QStringLiteral( "max" ), mMax );
4025  map.insert( QStringLiteral( "data_type" ), mDataType );
4026  return map;
4027 }
4028 
4029 bool QgsProcessingParameterNumber::fromVariantMap( const QVariantMap &map )
4030 {
4032  mMin = map.value( QStringLiteral( "min" ) ).toDouble();
4033  mMax = map.value( QStringLiteral( "max" ) ).toDouble();
4034  mDataType = static_cast< Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
4035  return true;
4036 }
4037 
4038 QgsProcessingParameterNumber *QgsProcessingParameterNumber::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4039 {
4040  return new QgsProcessingParameterNumber( name, description, Double, definition.isEmpty() ? QVariant()
4041  : ( definition.toLower().trimmed() == QLatin1String( "none" ) ? QVariant() : definition ), isOptional );
4042 }
4043 
4044 QgsProcessingParameterRange::QgsProcessingParameterRange( const QString &name, const QString &description, QgsProcessingParameterNumber::Type type, const QVariant &defaultValue, bool optional )
4045  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4046  , mDataType( type )
4047 {
4048 
4049 }
4050 
4052 {
4053  return new QgsProcessingParameterRange( *this );
4054 }
4055 
4057 {
4058  if ( !input.isValid() )
4059  return mFlags & FlagOptional;
4060 
4061  if ( input.canConvert<QgsProperty>() )
4062  {
4063  return true;
4064  }
4065 
4066  if ( input.type() == QVariant::String )
4067  {
4068  QStringList list = input.toString().split( ',' );
4069  if ( list.count() != 2 )
4070  return mFlags & FlagOptional;
4071  bool ok = false;
4072  list.at( 0 ).toDouble( &ok );
4073  bool ok2 = false;
4074  list.at( 1 ).toDouble( &ok2 );
4075  if ( !ok || !ok2 )
4076  return mFlags & FlagOptional;
4077  return true;
4078  }
4079  else if ( input.type() == QVariant::List )
4080  {
4081  if ( input.toList().count() != 2 )
4082  return mFlags & FlagOptional;
4083 
4084  bool ok = false;
4085  input.toList().at( 0 ).toDouble( &ok );
4086  bool ok2 = false;
4087  input.toList().at( 1 ).toDouble( &ok2 );
4088  if ( !ok || !ok2 )
4089  return mFlags & FlagOptional;
4090  return true;
4091  }
4092 
4093  return false;
4094 }
4095 
4096 QString QgsProcessingParameterRange::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
4097 {
4098  if ( !value.isValid() )
4099  return QStringLiteral( "None" );
4100 
4101  if ( value.canConvert<QgsProperty>() )
4102  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4103 
4104  QVariantMap p;
4105  p.insert( name(), value );
4106  QList< double > parts = QgsProcessingParameters::parameterAsRange( this, p, context );
4107 
4108  QStringList stringParts;
4109  const auto constParts = parts;
4110  for ( double v : constParts )
4111  {
4112  stringParts << QString::number( v );
4113  }
4114  return stringParts.join( ',' ).prepend( '[' ).append( ']' );
4115 }
4116 
4118 {
4119  switch ( outputType )
4120  {
4122  {
4123  QString code = QStringLiteral( "QgsProcessingParameterRange('%1', '%2'" ).arg( name(), description() );
4124  if ( mFlags & FlagOptional )
4125  code += QLatin1String( ", optional=True" );
4126 
4127  code += QStringLiteral( ", type=%1" ).arg( mDataType == QgsProcessingParameterNumber::Integer ? QStringLiteral( "QgsProcessingParameterNumber.Integer" ) : QStringLiteral( "QgsProcessingParameterNumber.Double" ) );
4128 
4130  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
4131  return code;
4132  }
4133  }
4134  return QString();
4135 }
4136 
4138 {
4139  return mDataType;
4140 }
4141 
4143 {
4144  mDataType = dataType;
4145 }
4146 
4148 {
4150  map.insert( QStringLiteral( "data_type" ), mDataType );
4151  return map;
4152 }
4153 
4154 bool QgsProcessingParameterRange::fromVariantMap( const QVariantMap &map )
4155 {
4157  mDataType = static_cast< QgsProcessingParameterNumber::Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
4158  return true;
4159 }
4160 
4161 QgsProcessingParameterRange *QgsProcessingParameterRange::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4162 {
4163  return new QgsProcessingParameterRange( name, description, QgsProcessingParameterNumber::Double, definition.isEmpty() ? QVariant()
4164  : ( definition.toLower().trimmed() == QLatin1String( "none" ) ? QVariant() : definition ), isOptional );
4165 }
4166 
4167 QgsProcessingParameterRasterLayer::QgsProcessingParameterRasterLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
4168  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4169 {
4170 
4171 }
4172 
4174 {
4175  return new QgsProcessingParameterRasterLayer( *this );
4176 }
4177 
4179 {
4180  if ( !input.isValid() )
4181  return mFlags & FlagOptional;
4182 
4183  if ( input.canConvert<QgsProperty>() )
4184  {
4185  return true;
4186  }
4187 
4188  if ( qobject_cast< QgsRasterLayer * >( qvariant_cast<QObject *>( input ) ) )
4189  return true;
4190 
4191  if ( input.type() != QVariant::String || input.toString().isEmpty() )
4192  return mFlags & FlagOptional;
4193 
4194  if ( !context )
4195  {
4196  // that's as far as we can get without a context
4197  return true;
4198  }
4199 
4200  // try to load as layer
4201  if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context, true, QgsProcessingUtils::LayerHint::Raster ) )
4202  return true;
4203 
4204  return false;
4205 }
4206 
4208 {
4209  if ( !val.isValid() )
4210  return QStringLiteral( "None" );
4211 
4212  if ( val.canConvert<QgsProperty>() )
4213  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
4214 
4215  QVariantMap p;
4216  p.insert( name(), val );
4219  : QgsProcessingUtils::stringToPythonLiteral( val.toString() );
4220 }
4221 
4223 {
4224  return QgsProviderRegistry::instance()->fileRasterFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
4225 }
4226 
4227 QgsProcessingParameterRasterLayer *QgsProcessingParameterRasterLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4228 {
4229  return new QgsProcessingParameterRasterLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
4230 }
4231 
4232 QgsProcessingParameterEnum::QgsProcessingParameterEnum( const QString &name, const QString &description, const QStringList &options, bool allowMultiple, const QVariant &defaultValue, bool optional, bool usesStaticStrings )
4233  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4234  , mOptions( options )
4235  , mAllowMultiple( allowMultiple )
4236  , mUsesStaticStrings( usesStaticStrings )
4237 {
4238 
4239 }
4240 
4242 {
4243  return new QgsProcessingParameterEnum( *this );
4244 }
4245 
4247 {
4248  QVariant input = value;
4249  if ( !input.isValid() )
4250  {
4251  if ( !defaultValue().isValid() )
4252  return mFlags & FlagOptional;
4253 
4254  input = defaultValue();
4255  }
4256 
4257  if ( input.canConvert<QgsProperty>() )
4258  {
4259  return true;
4260  }
4261 
4262  if ( mUsesStaticStrings )
4263  {
4264  if ( input.type() == QVariant::List )
4265  {
4266  if ( !mAllowMultiple )
4267  return false;
4268 
4269  const QVariantList values = input.toList();
4270  if ( values.empty() && !( mFlags & FlagOptional ) )
4271  return false;
4272 
4273  for ( const QVariant &val : values )
4274  {
4275  if ( !mOptions.contains( val.toString() ) )
4276  return false;
4277  }
4278 
4279  return true;
4280  }
4281  else if ( input.type() == QVariant::StringList )
4282  {
4283  if ( !mAllowMultiple )
4284  return false;
4285 
4286  const QStringList values = input.toStringList();
4287 
4288  if ( values.empty() && !( mFlags & FlagOptional ) )
4289  return false;
4290 
4291  if ( values.count() > 1 && !mAllowMultiple )
4292  return false;
4293 
4294  for ( const QString &val : values )
4295  {
4296  if ( !mOptions.contains( val ) )
4297  return false;
4298  }
4299  return true;
4300  }
4301  else if ( input.type() == QVariant::String )
4302  {
4303  QStringList parts = input.toString().split( ',' );
4304  if ( parts.count() > 1 && !mAllowMultiple )
4305  return false;
4306 
4307  const auto constParts = parts;
4308  for ( const QString &part : constParts )
4309  {
4310  if ( !mOptions.contains( part ) )
4311  return false;
4312  }
4313  return true;
4314  }
4315  }
4316  else
4317  {
4318  if ( input.type() == QVariant::List )
4319  {
4320  if ( !mAllowMultiple )
4321  return false;
4322 
4323  const QVariantList values = input.toList();
4324  if ( values.empty() && !( mFlags & FlagOptional ) )
4325  return false;
4326 
4327  for ( const QVariant &val : values )
4328  {
4329  bool ok = false;
4330  int res = val.toInt( &ok );
4331  if ( !ok )
4332  return false;
4333  else if ( res < 0 || res >= mOptions.count() )
4334  return false;
4335  }
4336 
4337  return true;
4338  }
4339  else if ( input.type() == QVariant::String )
4340  {
4341  QStringList parts = input.toString().split( ',' );
4342  if ( parts.count() > 1 && !mAllowMultiple )
4343  return false;
4344 
4345  const auto constParts = parts;
4346  for ( const QString &part : constParts )
4347  {
4348  bool ok = false;
4349  int res = part.toInt( &ok );
4350  if ( !ok )
4351  return false;
4352  else if ( res < 0 || res >= mOptions.count() )
4353  return false;
4354  }
4355  return true;
4356  }
4357  else if ( input.type() == QVariant::Int || input.type() == QVariant::Double )
4358  {
4359  bool ok = false;
4360  int res = input.toInt( &ok );
4361  if ( !ok )
4362  return false;
4363  else if ( res >= 0 && res < mOptions.count() )
4364  return true;
4365  }
4366  }
4367 
4368  return false;
4369 }
4370 
4372 {
4373  if ( !value.isValid() )
4374  return QStringLiteral( "None" );
4375 
4376  if ( value.canConvert<QgsProperty>() )
4377  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4378 
4379  if ( mUsesStaticStrings )
4380  {
4381  if ( value.type() == QVariant::StringList )
4382  {
4383  QStringList parts;
4384  const QStringList constList = value.toStringList();
4385  for ( const QString &val : constList )
4386  {
4388  }
4389  return parts.join( ',' ).prepend( '[' ).append( ']' );
4390  }
4391  else if ( value.type() == QVariant::String )
4392  {
4393  QStringList parts;
4394  const QStringList constList = value.toString().split( ',' );
4395  if ( constList.count() > 1 )
4396  {
4397  for ( const QString &val : constList )
4398  {
4400  }
4401  return parts.join( ',' ).prepend( '[' ).append( ']' );
4402  }
4403  }
4404 
4405  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
4406  }
4407  else
4408  {
4409  if ( value.type() == QVariant::List )
4410  {
4411  QStringList parts;
4412  const auto constToList = value.toList();
4413  for ( const QVariant &val : constToList )
4414  {
4415  parts << QString::number( static_cast< int >( val.toDouble() ) );
4416  }
4417  return parts.join( ',' ).prepend( '[' ).append( ']' );
4418  }
4419  else if ( value.type() == QVariant::String )
4420  {
4421  QStringList parts = value.toString().split( ',' );
4422  if ( parts.count() > 1 )
4423  {
4424  return parts.join( ',' ).prepend( '[' ).append( ']' );
4425  }
4426  }
4427 
4428  return QString::number( static_cast< int >( value.toDouble() ) );
4429  }
4430 }
4431 
4433 {
4434  QString code = QStringLiteral( "##%1=" ).arg( mName );
4435  if ( mFlags & FlagOptional )
4436  code += QLatin1String( "optional " );
4437  code += QLatin1String( "enum " );
4438 
4439  if ( mAllowMultiple )
4440  code += QLatin1String( "multiple " );
4441 
4442  if ( mUsesStaticStrings )
4443  code += QLatin1String( "static " );
4444 
4445  code += mOptions.join( ';' ) + ' ';
4446 
4447  code += mDefault.toString();
4448  return code.trimmed();
4449 }
4450 
4452 {
4453  switch ( outputType )
4454  {
4456  {
4457  QString code = QStringLiteral( "QgsProcessingParameterEnum('%1', '%2'" ).arg( name(), description() );
4458  if ( mFlags & FlagOptional )
4459  code += QLatin1String( ", optional=True" );
4460 
4461  QStringList options;
4462  options.reserve( mOptions.size() );
4463  for ( const QString &o : mOptions )
4465  code += QStringLiteral( ", options=[%1]" ).arg( options.join( ',' ) );
4466 
4467  code += QStringLiteral( ", allowMultiple=%1" ).arg( mAllowMultiple ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
4468 
4469  code += QStringLiteral( ", usesStaticStrings=%1" ).arg( mUsesStaticStrings ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
4470 
4472  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
4473 
4474  return code;
4475  }
4476  }
4477  return QString();
4478 }
4479 
4481 {
4482  return mOptions;
4483 }
4484 
4485 void QgsProcessingParameterEnum::setOptions( const QStringList &options )
4486 {
4487  mOptions = options;
4488 }
4489 
4491 {
4492  return mAllowMultiple;
4493 }
4494 
4496 {
4497  mAllowMultiple = allowMultiple;
4498 }
4499 
4501 {
4502  return mUsesStaticStrings;
4503 }
4504 
4506 {
4507  mUsesStaticStrings = usesStaticStrings;
4508 }
4509 
4511 {
4513  map.insert( QStringLiteral( "options" ), mOptions );
4514  map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
4515  map.insert( QStringLiteral( "uses_static_strings" ), mUsesStaticStrings );
4516  return map;
4517 }
4518 
4519 bool QgsProcessingParameterEnum::fromVariantMap( const QVariantMap &map )
4520 {
4522  mOptions = map.value( QStringLiteral( "options" ) ).toStringList();
4523  mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
4524  mUsesStaticStrings = map.value( QStringLiteral( "uses_static_strings" ) ).toBool();
4525  return true;
4526 }
4527 
4528 QgsProcessingParameterEnum *QgsProcessingParameterEnum::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4529 {
4530  QString defaultVal;
4531  QString def = definition;
4532 
4533  bool multiple = false;
4534  if ( def.startsWith( QLatin1String( "multiple" ), Qt::CaseInsensitive ) )
4535  {
4536  multiple = true;
4537  def = def.mid( 9 );
4538  }
4539 
4540  bool staticStrings = false;
4541  if ( def.startsWith( QLatin1String( "static" ), Qt::CaseInsensitive ) )
4542  {
4543  staticStrings = true;
4544  def = def.mid( 7 );
4545  }
4546 
4547  QRegularExpression re( QStringLiteral( "(.*)\\s+(.*?)$" ) );
4548  QRegularExpressionMatch m = re.match( def );
4549  QString values = def;
4550  if ( m.hasMatch() )
4551  {
4552  values = m.captured( 1 ).trimmed();
4553  defaultVal = m.captured( 2 );
4554  }
4555 
4556  return new QgsProcessingParameterEnum( name, description, values.split( ';' ), multiple, defaultVal.isEmpty() ? QVariant() : defaultVal, isOptional, staticStrings );
4557 }
4558 
4559 QgsProcessingParameterString::QgsProcessingParameterString( const QString &name, const QString &description, const QVariant &defaultValue, bool multiLine, bool optional )
4560  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4561  , mMultiLine( multiLine )
4562 {
4563 
4564 }
4565 
4567 {
4568  return new QgsProcessingParameterString( *this );
4569 }
4570 
4572 {
4573  if ( !value.isValid() || value.isNull() )
4574  return QStringLiteral( "None" );
4575 
4576  if ( value.canConvert<QgsProperty>() )
4577  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4578 
4579  QString s = value.toString();
4581 }
4582 
4584 {
4585  QString code = QStringLiteral( "##%1=" ).arg( mName );
4586  if ( mFlags & FlagOptional )
4587  code += QLatin1String( "optional " );
4588  code += QLatin1String( "string " );
4589 
4590  if ( mMultiLine )
4591  code += QLatin1String( "long " );
4592 
4593  code += mDefault.toString();
4594  return code.trimmed();
4595 }
4596 
4598 {
4599  switch ( outputType )
4600  {
4602  {
4603  QString code = QStringLiteral( "QgsProcessingParameterString('%1', '%2'" ).arg( name(), description() );
4604  if ( mFlags & FlagOptional )
4605  code += QLatin1String( ", optional=True" );
4606  code += QStringLiteral( ", multiLine=%1" ).arg( mMultiLine ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
4607 
4609  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
4610  return code;
4611  }
4612  }
4613  return QString();
4614 }
4615 
4617 {
4618  return mMultiLine;
4619 }
4620 
4622 {
4623  mMultiLine = multiLine;
4624 }
4625 
4627 {
4629  map.insert( QStringLiteral( "multiline" ), mMultiLine );
4630  return map;
4631 }
4632 
4633 bool QgsProcessingParameterString::fromVariantMap( const QVariantMap &map )
4634 {
4636  mMultiLine = map.value( QStringLiteral( "multiline" ) ).toBool();
4637  return true;
4638 }
4639 
4640 QgsProcessingParameterString *QgsProcessingParameterString::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4641 {
4642  QString def = definition;
4643  bool multiLine = false;
4644  if ( def.startsWith( QLatin1String( "long" ), Qt::CaseInsensitive ) )
4645  {
4646  multiLine = true;
4647  def = def.mid( 5 );
4648  }
4649 
4650  if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
4651  def = def.mid( 1 );
4652  if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
4653  def.chop( 1 );
4654 
4655  QVariant defaultValue = def;
4656  if ( def == QLatin1String( "None" ) )
4657  defaultValue = QVariant();
4658 
4660 }
4661 
4662 //
4663 // QgsProcessingParameterAuthConfig
4664 //
4665 
4666 QgsProcessingParameterAuthConfig::QgsProcessingParameterAuthConfig( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
4667  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4668 {
4669 
4670 }
4671 
4673 {
4674  return new QgsProcessingParameterAuthConfig( *this );
4675 }
4676 
4678 {
4679  if ( !value.isValid() )
4680  return QStringLiteral( "None" );
4681 
4682  QString s = value.toString();
4684 }
4685 
4687 {
4688  QString code = QStringLiteral( "##%1=" ).arg( mName );
4689  if ( mFlags & FlagOptional )
4690  code += QLatin1String( "optional " );
4691  code += QLatin1String( "authcfg " );
4692 
4693  code += mDefault.toString();
4694  return code.trimmed();
4695 }
4696 
4697 QgsProcessingParameterAuthConfig *QgsProcessingParameterAuthConfig::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4698 {
4699  QString def = definition;
4700 
4701  if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
4702  def = def.mid( 1 );
4703  if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
4704  def.chop( 1 );
4705 
4706  QVariant defaultValue = def;
4707  if ( def == QLatin1String( "None" ) )
4708  defaultValue = QVariant();
4709 
4710  return new QgsProcessingParameterAuthConfig( name, description, defaultValue, isOptional );
4711 }
4712 
4713 
4714 //
4715 // QgsProcessingParameterExpression
4716 //
4717 
4718 QgsProcessingParameterExpression::QgsProcessingParameterExpression( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional )
4719  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4720  , mParentLayerParameterName( parentLayerParameterName )
4721 {
4722 
4723 }
4724 
4726 {
4727  return new QgsProcessingParameterExpression( *this );
4728 }
4729 
4731 {
4732  if ( !value.isValid() )
4733  return QStringLiteral( "None" );
4734 
4735  if ( value.canConvert<QgsProperty>() )
4736  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4737 
4738  QString s = value.toString();
4740 }
4741 
4743 {
4744  QStringList depends;
4745  if ( !mParentLayerParameterName.isEmpty() )
4746  depends << mParentLayerParameterName;
4747  return depends;
4748 }
4749 
4751 {
4752  switch ( outputType )
4753  {
4755  {
4756  QString code = QStringLiteral( "QgsProcessingParameterExpression('%1', '%2'" ).arg( name(), description() );
4757  if ( mFlags & FlagOptional )
4758  code += QLatin1String( ", optional=True" );
4759 
4760  code += QStringLiteral( ", parentLayerParameterName='%1'" ).arg( mParentLayerParameterName );
4761 
4763  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
4764  return code;
4765  }
4766  }
4767  return QString();
4768 }
4769 
4771 {
4772  return mParentLayerParameterName;
4773 }
4774 
4775 void QgsProcessingParameterExpression::setParentLayerParameterName( const QString &parentLayerParameterName )
4776 {
4777  mParentLayerParameterName = parentLayerParameterName;
4778 }
4779 
4781 {
4783  map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
4784  return map;
4785 }
4786 
4788 {
4790  mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
4791  return true;
4792 }
4793 
4794 QgsProcessingParameterExpression *QgsProcessingParameterExpression::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4795 {
4796  return new QgsProcessingParameterExpression( name, description, definition, QString(), isOptional );
4797 }
4798 
4799 QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
4800  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4802 {
4803 
4804 }
4805 
4807 {
4808  return new QgsProcessingParameterVectorLayer( *this );
4809 }
4810 
4812 {
4813  if ( !v.isValid() )
4814  return mFlags & FlagOptional;
4815 
4816  QVariant var = v;
4817 
4818  if ( var.canConvert<QgsProperty>() )
4819  {
4820  QgsProperty p = var.value< QgsProperty >();
4822  {
4823  var = p.staticValue();
4824  }
4825  else
4826  {
4827  return true;
4828  }
4829  }
4830 
4831  if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( var ) ) )
4832  return true;
4833 
4834  if ( var.type() != QVariant::String || var.toString().isEmpty() )
4835  return mFlags & FlagOptional;
4836 
4837  if ( !context )
4838  {
4839  // that's as far as we can get without a context
4840  return true;
4841  }
4842 
4843  // try to load as layer
4844  if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::LayerHint::Vector ) )
4845  return true;
4846 
4847  return false;
4848 }
4849 
4851 {
4852  if ( !val.isValid() )
4853  return QStringLiteral( "None" );
4854 
4855  if ( val.canConvert<QgsProperty>() )
4856  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
4857 
4858  QVariantMap p;
4859  p.insert( name(), val );
4862  : QgsProcessingUtils::stringToPythonLiteral( val.toString() );
4863 }
4864 
4866 {
4867  switch ( outputType )
4868  {
4870  {
4871  QString code = QStringLiteral( "QgsProcessingParameterVectorLayer('%1', '%2'" ).arg( name(), description() );
4872  if ( mFlags & FlagOptional )
4873  code += QLatin1String( ", optional=True" );
4874 
4875  if ( !mDataTypes.empty() )
4876  {
4877  QStringList options;
4878  for ( int t : mDataTypes )
4879  options << QStringLiteral( "QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( static_cast< QgsProcessing::SourceType >( t ) ) );
4880  code += QStringLiteral( ", types=[%1]" ).arg( options.join( ',' ) );
4881  }
4882 
4884  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
4885  return code;
4886  }
4887  }
4888  return QString();
4889 }
4890 
4892 {
4893  return QgsProviderRegistry::instance()->fileVectorFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
4894 }
4895 
4897 {
4898  return mDataTypes;
4899 }
4900 
4902 {
4903  mDataTypes = types;
4904 }
4905 
4907 {
4909  QVariantList types;
4910  for ( int type : mDataTypes )
4911  {
4912  types << type;
4913  }
4914  map.insert( QStringLiteral( "data_types" ), types );
4915  return map;
4916 }
4917 
4919 {
4921  mDataTypes.clear();
4922  const QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
4923  for ( const QVariant &val : values )
4924  {
4925  mDataTypes << val.toInt();
4926  }
4927  return true;
4928 }
4929 
4930 QgsProcessingParameterVectorLayer *QgsProcessingParameterVectorLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4931 {
4932  return new QgsProcessingParameterVectorLayer( name, description, QList< int>(), definition.isEmpty() ? QVariant() : definition, isOptional );
4933 }
4934 
4936  const QString &description,
4937  const QVariant &defaultValue,
4938  bool optional )
4939  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4940 {
4941 
4942 }
4943 
4945 {
4946  return new QgsProcessingParameterMeshLayer( *this );
4947 }
4948 
4950 {
4951  if ( !v.isValid() )
4952  return mFlags & FlagOptional;
4953 
4954  QVariant var = v;
4955 
4956  if ( var.canConvert<QgsProperty>() )
4957  {
4958  QgsProperty p = var.value< QgsProperty >();
4960  {
4961  var = p.staticValue();
4962  }
4963  else
4964  {
4965  return true;
4966  }
4967  }
4968 
4969  if ( qobject_cast< QgsMeshLayer * >( qvariant_cast<QObject *>( var ) ) )
4970  return true;
4971 
4972  if ( var.type() != QVariant::String || var.toString().isEmpty() )
4973  return mFlags & FlagOptional;
4974 
4975  if ( !context )
4976  {
4977  // that's as far as we can get without a context
4978  return true;
4979  }
4980 
4981  // try to load as layer
4982  if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::LayerHint::Mesh ) )
4983  return true;
4984 
4985  return false;
4986 }
4987 
4989 {
4990  if ( !val.isValid() )
4991  return QStringLiteral( "None" );
4992 
4993  if ( val.canConvert<QgsProperty>() )
4994  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
4995 
4996  QVariantMap p;
4997  p.insert( name(), val );
4998  QgsMeshLayer *layer = QgsProcessingParameters::parameterAsMeshLayer( this, p, context );
5000  : QgsProcessingUtils::stringToPythonLiteral( val.toString() );
5001 }
5002 
5004 {
5005  return QgsProviderRegistry::instance()->fileMeshFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
5006 }
5007 
5008 QgsProcessingParameterMeshLayer *QgsProcessingParameterMeshLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5009 {
5010  return new QgsProcessingParameterMeshLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
5011 }
5012 
5013 QgsProcessingParameterField::QgsProcessingParameterField( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, DataType type, bool allowMultiple, bool optional, bool defaultToAllFields )
5014  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5015  , mParentLayerParameterName( parentLayerParameterName )
5016  , mDataType( type )
5017  , mAllowMultiple( allowMultiple )
5018  , mDefaultToAllFields( defaultToAllFields )
5019 {
5020 
5021 }
5022 
5023 
5025 {
5026  return new QgsProcessingParameterField( *this );
5027 }
5028 
5030 {
5031  if ( !input.isValid() )
5032  return mFlags & FlagOptional;
5033 
5034  if ( input.canConvert<QgsProperty>() )
5035  {
5036  return true;
5037  }
5038 
5039  if ( input.type() == QVariant::List || input.type() == QVariant::StringList )
5040  {
5041  if ( !mAllowMultiple )
5042  return false;
5043 
5044  if ( input.toList().isEmpty() && !( mFlags & FlagOptional ) )
5045  return false;
5046  }
5047  else if ( input.type() == QVariant::String )
5048  {
5049  if ( input.toString().isEmpty() )
5050  return mFlags & FlagOptional;
5051 
5052  QStringList parts = input.toString().split( ';' );
5053  if ( parts.count() > 1 && !mAllowMultiple )
5054  return false;
5055  }
5056  else
5057  {
5058  if ( input.toString().isEmpty() )
5059  return mFlags & FlagOptional;
5060  }
5061  return true;
5062 }
5063 
5065 {
5066  if ( !value.isValid() )
5067  return QStringLiteral( "None" );
5068 
5069  if ( value.canConvert<QgsProperty>() )
5070  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
5071 
5072  if ( value.type() == QVariant::List )
5073  {
5074  QStringList parts;
5075  const auto constToList = value.toList();
5076  for ( const QVariant &val : constToList )
5077  {
5078  parts << QgsProcessingUtils::stringToPythonLiteral( val.toString() );
5079  }
5080  return parts.join( ',' ).prepend( '[' ).append( ']' );
5081  }
5082  else if ( value.type() == QVariant::StringList )
5083  {
5084  QStringList parts;
5085  const auto constToStringList = value.toStringList();
5086  for ( QString s : constToStringList )
5087  {
5089  }
5090  return parts.join( ',' ).prepend( '[' ).append( ']' );
5091  }
5092 
5093  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
5094 }
5095 
5097 {
5098  QString code = QStringLiteral( "##%1=" ).arg( mName );
5099  if ( mFlags & FlagOptional )
5100  code += QLatin1String( "optional " );
5101  code += QLatin1String( "field " );
5102 
5103  switch ( mDataType )
5104  {
5105  case Numeric:
5106  code += QLatin1String( "numeric " );
5107  break;
5108 
5109  case String:
5110  code += QLatin1String( "string " );
5111  break;
5112 
5113  case DateTime:
5114  code += QLatin1String( "datetime " );
5115  break;
5116 
5117  case Any:
5118  break;
5119  }
5120 
5121  if ( mAllowMultiple )
5122  code += QLatin1String( "multiple " );
5123 
5124  if ( mDefaultToAllFields )
5125  code += QLatin1String( "default_to_all_fields " );
5126 
5127  code += mParentLayerParameterName + ' ';
5128 
5129  code += mDefault.toString();
5130  return code.trimmed();
5131 }
5132 
5134 {
5135  switch ( outputType )
5136  {
5138  {
5139  QString code = QStringLiteral( "QgsProcessingParameterField('%1', '%2'" ).arg( name(), description() );
5140  if ( mFlags & FlagOptional )
5141  code += QLatin1String( ", optional=True" );
5142 
5143  QString dataType;
5144  switch ( mDataType )
5145  {
5146  case Any:
5147  dataType = QStringLiteral( "QgsProcessingParameterField.Any" );
5148  break;
5149 
5150  case Numeric:
5151  dataType = QStringLiteral( "QgsProcessingParameterField.Numeric" );
5152  break;
5153 
5154  case String:
5155  dataType = QStringLiteral( "QgsProcessingParameterField.String" );
5156  break;
5157 
5158  case DateTime:
5159  dataType = QStringLiteral( "QgsProcessingParameterField.DateTime" );
5160  break;
5161  }
5162  code += QStringLiteral( ", type=%1" ).arg( dataType );
5163 
5164  code += QStringLiteral( ", parentLayerParameterName='%1'" ).arg( mParentLayerParameterName );
5165  code += QStringLiteral( ", allowMultiple=%1" ).arg( mAllowMultiple ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
5167  code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );
5168 
5169  if ( mDefaultToAllFields )
5170  code += QLatin1String( ", defaultToAllFields=True" );
5171 
5172  code += ')';
5173 
5174  return code;
5175  }
5176  }
5177  return QString();
5178 }
5179 
5181 {
5182  QStringList depends;
5183  if ( !mParentLayerParameterName.isEmpty() )
5184  depends << mParentLayerParameterName;
5185  return depends;
5186 }
5187 
5189 {
5190  return mParentLayerParameterName;
5191 }
5192 
5193 void QgsProcessingParameterField::setParentLayerParameterName( const QString &parentLayerParameterName )
5194 {
5195  mParentLayerParameterName = parentLayerParameterName;
5196 }
5197 
5199 {
5200  return mDataType;
5201 }
5202 
5204 {
5205  mDataType = dataType;
5206 }
5207 
5209 {
5210  return mAllowMultiple;
5211 }
5212 
5214 {
5215  mAllowMultiple = allowMultiple;
5216 }
5217 
5219 {
5220  return mDefaultToAllFields;
5221 }
5222 
5224 {
5225  mDefaultToAllFields = enabled;
5226 }
5227 
5229 {
5231  map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
5232  map.insert( QStringLiteral( "data_type" ), mDataType );
5233  map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
5234  map.insert( QStringLiteral( "default_to_all_fields" ), mDefaultToAllFields );
5235  return map;
5236 }
5237 
5238 bool QgsProcessingParameterField::fromVariantMap( const QVariantMap &map )
5239 {
5241  mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
5242  mDataType = static_cast< DataType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
5243  mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
5244  mDefaultToAllFields = map.value( QStringLiteral( "default_to_all_fields" ) ).toBool();
5245  return true;
5246 }
5247 
5248 QgsProcessingParameterField *QgsProcessingParameterField::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5249 {
5250  QString parent;
5251  DataType type = Any;
5252  bool allowMultiple = false;
5253  bool defaultToAllFields = false;
5254  QString def = definition;
5255 
5256  if ( def.startsWith( QLatin1String( "numeric " ), Qt::CaseInsensitive ) )
5257  {
5258  type = Numeric;
5259  def = def.mid( 8 );
5260  }
5261  else if ( def.startsWith( QLatin1String( "string " ), Qt::CaseInsensitive ) )
5262  {
5263  type = String;
5264  def = def.mid( 7 );
5265  }
5266  else if ( def.startsWith( QLatin1String( "datetime " ), Qt::CaseInsensitive ) )
5267  {
5268  type = DateTime;
5269  def = def.mid( 9 );
5270  }
5271 
5272  if ( def.startsWith( QLatin1String( "multiple" ), Qt::CaseInsensitive ) )
5273  {
5274  allowMultiple = true;
5275  def = def.mid( 8 ).trimmed();
5276  }
5277 
5278  if ( def.startsWith( QLatin1String( "default_to_all_fields" ), Qt::CaseInsensitive ) )
5279  {
5280  defaultToAllFields = true;
5281  def = def.mid( 21 ).trimmed();
5282  }
5283 
5284  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
5285  QRegularExpressionMatch m = re.match( def );
5286  if ( m.hasMatch() )
5287  {
5288  parent = m.captured( 1 ).trimmed();
5289  def = m.captured( 2 );
5290  }
5291  else
5292  {
5293  parent = def;
5294  def.clear();
5295  }
5296 
5297  return new QgsProcessingParameterField( name, description, def.isEmpty() ? QVariant() : def, parent, type, allowMultiple, isOptional, defaultToAllFields );
5298 }
5299 
5300 QgsProcessingParameterFeatureSource::QgsProcessingParameterFeatureSource( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
5301  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5303 {
5304 
5305 }
5306 
5308 {
5309  return new QgsProcessingParameterFeatureSource( *this );
5310 }
5311 
5313 {
5314  QVariant var = input;
5315  if ( !var.isValid() )
5316  return mFlags & FlagOptional;
5317 
5318  if ( var.canConvert<QgsProcessingFeatureSourceDefinition>() )
5319  {
5320  QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( var );
5321  var = fromVar.source;
5322  }
5323  else if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
5324  {
5325  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
5326  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
5327  var = fromVar.sink;
5328  }
5329 
5330  if ( var.canConvert<QgsProperty>() )
5331  {
5332  QgsProperty p = var.value< QgsProperty >();
5334  {
5335  var = p.staticValue();
5336  }
5337  else
5338  {
5339  return true;
5340  }
5341  }
5342  if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( input ) ) )
5343  {
5344  return true;
5345  }
5346 
5347  if ( var.type() != QVariant::String || var.toString().isEmpty() )
5348  return mFlags & FlagOptional;
5349 
5350  if ( !context )
5351  {
5352  // that's as far as we can get without a context
5353  return true;
5354  }
5355 
5356  // try to load as layer
5357  if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::LayerHint::Vector ) )
5358  return true;
5359 
5360  return false;
5361 }
5362 
5364 {
5365  if ( !value.isValid() )
5366  return QStringLiteral( "None" );
5367 
5368  if ( value.canConvert<QgsProperty>() )
5369  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
5370 
5371  if ( value.canConvert<QgsProcessingFeatureSourceDefinition>() )
5372  {
5373  QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( value );
5374  QString geometryCheckString;
5375  switch ( fromVar.geometryCheck )
5376  {
5378  geometryCheckString = QStringLiteral( "QgsFeatureRequest.GeometryNoCheck" );
5379  break;
5380 
5382  geometryCheckString = QStringLiteral( "QgsFeatureRequest.GeometrySkipInvalid" );
5383  break;
5384 
5386  geometryCheckString = QStringLiteral( "QgsFeatureRequest.GeometryAbortOnInvalid" );
5387  break;
5388  }
5389 
5390  QStringList flags;
5391  QString flagString;
5392  if ( fromVar.flags & QgsProcessingFeatureSourceDefinition::Flag::FlagOverrideDefaultGeometryCheck )
5393  flags << QStringLiteral( "QgsProcessingFeatureSourceDefinition.FlagOverrideDefaultGeometryCheck" );
5394  if ( fromVar.flags & QgsProcessingFeatureSourceDefinition::Flag::FlagCreateIndividualOutputPerInputFeature )
5395  flags << QStringLiteral( "QgsProcessingFeatureSourceDefinition.FlagCreateIndividualOutputPerInputFeature" );
5396  if ( !flags.empty() )
5397  flagString = flags.join( QLatin1String( " | " ) );
5398 
5399  if ( fromVar.source.propertyType() == QgsProperty::StaticProperty )
5400  {
5401  QString layerString = fromVar.source.staticValue().toString();
5402  // prefer to use layer source instead of id if possible (since it's persistent)
5403  if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context, true, QgsProcessingUtils::LayerHint::Vector ) ) )
5404  layerString = layer->source();
5405 
5406  if ( fromVar.selectedFeaturesOnly || fromVar.featureLimit != -1 || fromVar.flags )
5407  {
5408  return QStringLiteral( "QgsProcessingFeatureSourceDefinition('%1', selectedFeaturesOnly=%2, featureLimit=%3%4, geometryCheck=%5)" ).arg( layerString,
5409  fromVar.selectedFeaturesOnly ? QStringLiteral( "True" ) : QStringLiteral( "False" ),
5410  QString::number( fromVar.featureLimit ),
5411  flagString.isEmpty() ? QString() : ( QStringLiteral( ", flags=%1" ).arg( flagString ) ),
5412  geometryCheckString );
5413  }
5414  else
5415  {
5416  return QgsProcessingUtils::stringToPythonLiteral( layerString );
5417  }
5418  }
5419  else
5420  {
5421  if ( fromVar.selectedFeaturesOnly || fromVar.featureLimit != -1 || fromVar.flags )
5422  {
5423  return QStringLiteral( "QgsProcessingFeatureSourceDefinition(QgsProperty.fromExpression('%1'), selectedFeaturesOnly=%2, featureLimit=%3%4, geometryCheck=%5)" )
5424  .arg( fromVar.source.asExpression(),
5425  fromVar.selectedFeaturesOnly ? QStringLiteral( "True" ) : QStringLiteral( "False" ),
5426  QString::number( fromVar.featureLimit ),
5427  flagString.isEmpty() ? QString() : ( QStringLiteral( ", flags=%1" ).arg( flagString ) ),
5428  geometryCheckString );
5429  }
5430  else
5431  {
5432  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.source.asExpression() );
5433  }
5434  }
5435  }
5436  else if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( value ) ) )
5437  {
5438  return QgsProcessingUtils::stringToPythonLiteral( layer->source() );
5439  }
5440 
5441  QString layerString = value.toString();
5442 
5443  // prefer to use layer source if possible (since it's persistent)
5444  if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context, true, QgsProcessingUtils::LayerHint::Vector ) ) )
5445  layerString = layer->providerType() != QLatin1String( "ogr" ) && layer->providerType() != QLatin1String( "gdal" ) && layer->providerType() != QLatin1String( "mdal" ) ? QgsProcessingUtils::encodeProviderKeyAndUri( layer->providerType(), layer->source() ) : layer->source();
5446 
5447  return QgsProcessingUtils::stringToPythonLiteral( layerString );
5448 }
5449 
5451 {
5452  QString code = QStringLiteral( "##%1=" ).arg( mName );
5453  if ( mFlags & FlagOptional )
5454  code += QLatin1String( "optional " );
5455  code += QLatin1String( "source " );
5456 
5457  for ( int type : mDataTypes )
5458  {
5459  switch ( type )
5460  {
5462  code += QLatin1String( "point " );
5463  break;
5464 
5466  code += QLatin1String( "line " );
5467  break;
5468 
5470  code += QLatin1String( "polygon " );
5471  break;
5472 
5473  }
5474  }
5475 
5476  code += mDefault.toString();
5477  return code.trimmed();
5478 }
5479 
5481 {
5482  switch ( outputType )
5483  {
5485  {
5486  QString code = QStringLiteral( "QgsProcessingParameterFeatureSource('%1', '%2'" ).arg( name(), description() );
5487  if ( mFlags & FlagOptional )
5488  code += QLatin1String( ", optional=True" );
5489 
5490  if ( !mDataTypes.empty() )
5491  {
5492  QStringList options;
5493  options.reserve( mDataTypes.size() );
5494  for ( int t : mDataTypes )
5495  options << QStringLiteral( "QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( static_cast< QgsProcessing::SourceType >( t ) ) );
5496  code += QStringLiteral( ", types=[%1]" ).arg( options.join( ',' ) );
5497  }
5498 
5500  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
5501  return code;
5502  }
5503  }
5504  return QString();
5505 }
5506 
5508 {
5509  return QgsProviderRegistry::instance()->fileVectorFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
5510 }
5511 
5513  : mDataTypes( types )
5514 {
5515 
5516 }
5517 
5519 {
5521  QVariantList types;
5522  for ( int type : mDataTypes )
5523  {
5524  types << type;
5525  }
5526  map.insert( QStringLiteral( "data_types" ), types );
5527  return map;
5528 }
5529 
5531 {
5533  mDataTypes.clear();
5534  const QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
5535  for ( const QVariant &val : values )
5536  {
5537  mDataTypes << val.toInt();
5538  }
5539  return true;
5540 }
5541 
5542 QgsProcessingParameterFeatureSource *QgsProcessingParameterFeatureSource::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5543 {
5544  QList< int > types;
5545  QString def = definition;
5546  while ( true )
5547  {
5548  if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
5549  {
5551  def = def.mid( 6 );
5552  continue;
5553  }
5554  else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
5555  {
5557  def = def.mid( 5 );
5558  continue;
5559  }
5560  else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
5561  {
5563  def = def.mid( 8 );
5564  continue;
5565  }
5566  break;
5567  }
5568 
5569  return new QgsProcessingParameterFeatureSource( name, description, types, def, isOptional );
5570 }
5571 
5572 QgsProcessingParameterFeatureSink::QgsProcessingParameterFeatureSink( const QString &name, const QString &description, QgsProcessing::SourceType type, const QVariant &defaultValue, bool optional, bool createByDefault, bool supportsAppend )
5573  : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
5574  , mDataType( type )
5575  , mSupportsAppend( supportsAppend )
5576 {
5577 }
5578 
5580 {
5581  return new QgsProcessingParameterFeatureSink( *this );
5582 }
5583 
5585 {
5586  QVariant var = input;
5587  if ( !var.isValid() )
5588  return mFlags & FlagOptional;
5589 
5590  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
5591  {
5592  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
5593  var = fromVar.sink;
5594  }
5595 
5596  if ( var.canConvert<QgsProperty>() )
5597  {
5598  QgsProperty p = var.value< QgsProperty >();
5600  {
5601  var = p.staticValue();
5602  }
5603  else
5604  {
5605  return true;
5606  }
5607  }
5608 
5609  if ( var.type() != QVariant::String )
5610  return false;
5611 
5612  if ( var.toString().isEmpty() )
5613  return mFlags & FlagOptional;
5614 
5615  return true;
5616 }
5617 
5619 {
5620  if ( !value.isValid() )
5621  return QStringLiteral( "None" );
5622 
5623  if ( value.canConvert<QgsProperty>() )
5624  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
5625 
5626  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
5627  {
5628  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
5629  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
5630  {
5631  return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
5632  }
5633  else
5634  {
5635  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
5636  }
5637  }
5638 
5639  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
5640 }
5641 
5643 {
5644  QString code = QStringLiteral( "##%1=" ).arg( mName );
5645  if ( mFlags & FlagOptional )
5646  code += QLatin1String( "optional " );
5647  code += QLatin1String( "sink " );
5648 
5649  switch ( mDataType )
5650  {
5652  code += QLatin1String( "point " );
5653  break;
5654 
5656  code += QLatin1String( "line " );
5657  break;
5658 
5660  code += QLatin1String( "polygon " );
5661  break;
5662 
5664  code += QLatin1String( "table " );
5665  break;
5666 
5667  default:
5668  break;
5669  }
5670 
5671  code += mDefault.toString();
5672  return code.trimmed();
5673 }
5674 
5676 {
5677  return new QgsProcessingOutputVectorLayer( name(), description(), mDataType );
5678 }
5679 
5681 {
5682  if ( auto *lOriginalProvider = originalProvider() )
5683  {
5684  return lOriginalProvider->defaultVectorFileExtension( hasGeometry() );
5685  }
5686  else if ( QgsProcessingProvider *p = provider() )
5687  {
5688  return p->defaultVectorFileExtension( hasGeometry() );
5689  }
5690  else
5691  {
5692  if ( hasGeometry() )
5693  {
5695  }
5696  else
5697  {
5698  return QStringLiteral( "dbf" );
5699  }
5700  }
5701 }
5702 
5704 {
5705  switch ( outputType )
5706  {
5708  {
5709  QString code = QStringLiteral( "QgsProcessingParameterFeatureSink('%1', '%2'" ).arg( name(), description() );
5710  if ( mFlags & FlagOptional )
5711  code += QLatin1String( ", optional=True" );
5712 
5713  code += QStringLiteral( ", type=QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( mDataType ) );
5714 
5715  code += QStringLiteral( ", createByDefault=%1" ).arg( createByDefault() ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
5716  if ( mSupportsAppend )
5717  code += QLatin1String( ", supportsAppend=True" );
5718 
5720  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
5721  return code;
5722  }
5723  }
5724  return QString();
5725 }
5726 
5728 {
5729  const QStringList exts = supportedOutputVectorLayerExtensions();
5730  QStringList filters;
5731  for ( const QString &ext : exts )
5732  {
5733  filters << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
5734  }
5735  return filters.join( QLatin1String( ";;" ) ) + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
5736 
5737 }
5738 
5740 {
5741  if ( auto *lOriginalProvider = originalProvider() )
5742  {
5743  if ( hasGeometry() )
5744  return lOriginalProvider->supportedOutputVectorLayerExtensions();
5745  else
5746  return lOriginalProvider->supportedOutputTableExtensions();
5747  }
5748  else if ( QgsProcessingProvider *p = provider() )
5749  {
5750  if ( hasGeometry() )
5751  return p->supportedOutputVectorLayerExtensions();
5752  else
5753  return p->supportedOutputTableExtensions();
5754  }
5755  else
5756  {
5758  }
5759 }
5760 
5762 {
5763  return mDataType;
5764 }
5765 
5767 {
5768  switch ( mDataType )
5769  {
5775  return true;
5776 
5781  return false;
5782  }
5783  return true;
5784 }
5785 
5787 {
5788  mDataType = type;
5789 }
5790 
5792 {
5794  map.insert( QStringLiteral( "data_type" ), mDataType );
5795  map.insert( QStringLiteral( "supports_append" ), mSupportsAppend );
5796  return map;
5797 }
5798 
5800 {
5802  mDataType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
5803  mSupportsAppend = map.value( QStringLiteral( "supports_append" ), false ).toBool();
5804  return true;
5805 }
5806 
5808 {
5810  return QStringLiteral( "memory:%1" ).arg( description() );
5811  else
5813 }
5814 
5815 QgsProcessingParameterFeatureSink *QgsProcessingParameterFeatureSink::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5816 {
5818  QString def = definition;
5819  if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
5820  {
5822  def = def.mid( 6 );
5823  }
5824  else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
5825  {
5827  def = def.mid( 5 );
5828  }
5829  else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
5830  {
5832  def = def.mid( 8 );
5833  }
5834  else if ( def.startsWith( QLatin1String( "table" ), Qt::CaseInsensitive ) )
5835  {
5837  def = def.mid( 6 );
5838  }
5839 
5840  return new QgsProcessingParameterFeatureSink( name, description, type, definition, isOptional );
5841 }
5842 
5844 {
5845  return mSupportsAppend;
5846 }
5847 
5849 {
5850  mSupportsAppend = supportsAppend;
5851 }
5852 
5853 QgsProcessingParameterRasterDestination::QgsProcessingParameterRasterDestination( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, bool createByDefault )
5854  : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
5855 {
5856 }
5857 
5859 {
5860  return new QgsProcessingParameterRasterDestination( *this );
5861 }
5862 
5864 {
5865  QVariant var = input;
5866  if ( !var.isValid() )
5867  return mFlags & FlagOptional;
5868 
5869  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
5870  {
5871  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
5872  var = fromVar.sink;
5873  }
5874 
5875  if ( var.canConvert<QgsProperty>() )
5876  {
5877  QgsProperty p = var.value< QgsProperty >();
5879  {
5880  var = p.staticValue();
5881  }
5882  else
5883  {
5884  return true;
5885  }
5886  }
5887 
5888  if ( var.type() != QVariant::String )
5889  return false;
5890 
5891  if ( var.toString().isEmpty() )
5892  return mFlags & FlagOptional;
5893 
5894  return true;
5895 }
5896 
5898 {
5899  if ( !value.isValid() )
5900  return QStringLiteral( "None" );
5901 
5902  if ( value.canConvert<QgsProperty>() )
5903  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
5904 
5905  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
5906  {
5907  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
5908  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
5909  {
5910  return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
5911  }
5912  else
5913  {
5914  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
5915  }
5916  }
5917 
5918  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
5919 }
5920 
5922 {
5923  return new QgsProcessingOutputRasterLayer( name(), description() );
5924 }
5925 
5927 {
5928  if ( auto *lOriginalProvider = originalProvider() )
5929  {
5930  return lOriginalProvider->defaultRasterFileExtension();
5931  }
5932  else if ( QgsProcessingProvider *p = provider() )
5933  {
5934  return p->defaultRasterFileExtension();
5935  }
5936  else
5937  {
5939  }
5940 }
5941 
5943 {
5944  const QStringList exts = supportedOutputRasterLayerExtensions();
5945  QStringList filters;
5946  for ( const QString &ext : exts )
5947  {
5948  filters << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
5949  }
5950  return filters.join( QLatin1String( ";;" ) ) + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
5951 }
5952 
5954 {
5955  if ( auto *lOriginalProvider = originalProvider() )
5956  {
5957  return lOriginalProvider->supportedOutputRasterLayerExtensions();
5958  }
5959  else if ( QgsProcessingProvider *p = provider() )
5960  {
5961  return p->supportedOutputRasterLayerExtensions();
5962  }
5963  else
5964  {
5966  }
5967 }
5968 
5969 QgsProcessingParameterRasterDestination *QgsProcessingParameterRasterDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5970 {
5971  return new QgsProcessingParameterRasterDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
5972 }
5973 
5974 
5975 QgsProcessingParameterFileDestination::QgsProcessingParameterFileDestination( const QString &name, const QString &description, const QString &fileFilter, const QVariant &defaultValue, bool optional, bool createByDefault )
5976  : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
5977  , mFileFilter( fileFilter.isEmpty() ? QObject::tr( "All files (*.*)" ) : fileFilter )
5978 {
5979 
5980 }
5981 
5983 {
5984  return new QgsProcessingParameterFileDestination( *this );
5985 }
5986 
5988 {
5989  QVariant var = input;
5990  if ( !var.isValid() )
5991  return mFlags & FlagOptional;
5992 
5993  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
5994  {
5995  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
5996  var = fromVar.sink;
5997  }
5998 
5999  if ( var.canConvert<QgsProperty>() )
6000  {
6001  QgsProperty p = var.value< QgsProperty >();
6003  {
6004  var = p.staticValue();
6005  }
6006  else
6007  {
6008  return true;
6009  }
6010  }
6011 
6012  if ( var.type() != QVariant::String )
6013  return false;
6014 
6015  if ( var.toString().isEmpty() )
6016  return mFlags & FlagOptional;
6017 
6018  // possible enhancement - check that value is compatible with file filter?
6019 
6020  return true;
6021 }
6022 
6024 {
6025  if ( !value.isValid() )
6026  return QStringLiteral( "None" );
6027 
6028  if ( value.canConvert<QgsProperty>() )
6029  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
6030 
6031  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
6032  {
6033  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
6034  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
6035  {
6036  return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
6037  }
6038  else
6039  {
6040  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
6041  }
6042  }
6043 
6044  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
6045 }
6046 
6048 {
6049  if ( !mFileFilter.isEmpty() && mFileFilter.contains( QStringLiteral( "htm" ), Qt::CaseInsensitive ) )
6050  {
6051  return new QgsProcessingOutputHtml( name(), description() );
6052  }
6053  else
6054  {
6055  return new QgsProcessingOutputFile( name(), description() );
6056  }
6057 }
6058 
6060 {
6061  if ( mFileFilter.isEmpty() || mFileFilter == QObject::tr( "All files (*.*)" ) )
6062  return QStringLiteral( "file" );
6063 
6064  // get first extension from filter
6065  QRegularExpression rx( QStringLiteral( ".*?\\(\\*\\.([a-zA-Z0-9._]+).*" ) );
6066  QRegularExpressionMatch match = rx.match( mFileFilter );
6067  if ( !match.hasMatch() )
6068  return QStringLiteral( "file" );
6069 
6070  return match.captured( 1 );
6071 }
6072 
6074 {
6075  switch ( outputType )
6076  {
6078  {
6079  QString code = QStringLiteral( "QgsProcessingParameterFileDestination('%1', '%2'" ).arg( name(), description() );
6080  if ( mFlags & FlagOptional )
6081  code += QLatin1String( ", optional=True" );
6082 
6083  code += QStringLiteral( ", fileFilter=%1" ).arg( QgsProcessingUtils::stringToPythonLiteral( mFileFilter ) );
6084 
6085  code += QStringLiteral( ", createByDefault=%1" ).arg( createByDefault() ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
6086 
6088  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6089  return code;
6090  }
6091  }
6092  return QString();
6093 }
6094 
6096 {
6097  return ( fileFilter().isEmpty() ? QString() : fileFilter() + QStringLiteral( ";;" ) ) + QObject::tr( "All files (*.*)" );
6098 }
6099 
6101 {
6102  return mFileFilter;
6103 }
6104 
6106 {
6107  mFileFilter = fileFilter;
6108 }
6109 
6111 {
6113  map.insert( QStringLiteral( "file_filter" ), mFileFilter );
6114  return map;
6115 }
6116 
6118 {
6120  mFileFilter = map.value( QStringLiteral( "file_filter" ) ).toString();
6121  return true;
6122 
6123 }
6124 
6125 QgsProcessingParameterFileDestination *QgsProcessingParameterFileDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6126 {
6127  return new QgsProcessingParameterFileDestination( name, description, QString(), definition.isEmpty() ? QVariant() : definition, isOptional );
6128 }
6129 
6130 QgsProcessingParameterFolderDestination::QgsProcessingParameterFolderDestination( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, bool createByDefault )
6131  : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
6132 {}
6133 
6135 {
6136  return new QgsProcessingParameterFolderDestination( *this );
6137 }
6138 
6140 {
6141  QVariant var = input;
6142  if ( !var.isValid() )
6143  return mFlags & FlagOptional;
6144 
6145  if ( var.canConvert<QgsProperty>() )
6146  {
6147  QgsProperty p = var.value< QgsProperty >();
6149  {
6150  var = p.staticValue();
6151  }
6152  else
6153  {
6154  return true;
6155  }
6156  }
6157 
6158  if ( var.type() != QVariant::String )
6159  return false;
6160 
6161  if ( var.toString().isEmpty() )
6162  return mFlags & FlagOptional;
6163 
6164  return true;
6165 }
6166 
6168 {
6169  return new QgsProcessingOutputFolder( name(), description() );
6170 }
6171 
6173 {
6174  return QString();
6175 }
6176 
6177 QgsProcessingParameterFolderDestination *QgsProcessingParameterFolderDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6178 {
6179  return new QgsProcessingParameterFolderDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
6180 }
6181 
6182 QgsProcessingDestinationParameter::QgsProcessingDestinationParameter( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, bool createByDefault )
6183  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
6184  , mCreateByDefault( createByDefault )
6185 {
6186 
6187 }
6188 
6190 {
6192  map.insert( QStringLiteral( "supports_non_file_outputs" ), mSupportsNonFileBasedOutputs );
6193  map.insert( QStringLiteral( "create_by_default" ), mCreateByDefault );
6194  return map;
6195 }
6196 
6198 {
6200  mSupportsNonFileBasedOutputs = map.value( QStringLiteral( "supports_non_file_outputs" ) ).toBool();
6201  mCreateByDefault = map.value( QStringLiteral( "create_by_default" ), QStringLiteral( "1" ) ).toBool();
6202  return true;
6203 }
6204 
6206 {
6207  switch ( outputType )
6208  {
6210  {
6211  // base class method is probably not much use
6212  if ( QgsProcessingParameterType *t = QgsApplication::processingRegistry()->parameterType( type() ) )
6213  {
6214  QString code = t->className() + QStringLiteral( "('%1', '%2'" ).arg( name(), description() );
6215  if ( mFlags & FlagOptional )
6216  code += QLatin1String( ", optional=True" );
6217 
6218  code += QStringLiteral( ", createByDefault=%1" ).arg( mCreateByDefault ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
6219 
6221  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6222  return code;
6223  }
6224  break;
6225  }
6226  }
6227  // oh well, we tried
6228  return QString();
6229 }
6230 
6232 {
6233  return QObject::tr( "Default extension" ) + QStringLiteral( " (*." ) + defaultFileExtension() + ')';
6234 }
6235 
6237 {
6238  // sanitize name to avoid multiple . in the filename. E.g. when name() contain
6239  // backend command name having a "." inside as in case of grass commands
6240  QRegularExpression rx( QStringLiteral( "[.]" ) );
6241  QString sanitizedName = name();
6242  sanitizedName.replace( rx, QStringLiteral( "_" ) );
6243 
6244  if ( defaultFileExtension().isEmpty() )
6245  {
6246  return QgsProcessingUtils::generateTempFilename( sanitizedName );
6247  }
6248  else
6249  {
6250  return QgsProcessingUtils::generateTempFilename( sanitizedName + '.' + defaultFileExtension() );
6251  }
6252 }
6253 
6254 bool QgsProcessingDestinationParameter::isSupportedOutputValue( const QVariant &value, QgsProcessingContext &context, QString &error ) const
6255 {
6256  if ( auto *lOriginalProvider = originalProvider() )
6257  return lOriginalProvider->isSupportedOutputValue( value, this, context, error );
6258  else if ( provider() )
6259  return provider()->isSupportedOutputValue( value, this, context, error );
6260 
6261  return true;
6262 }
6263 
6265 {
6266  return mCreateByDefault;
6267 }
6268 
6270 {
6271  mCreateByDefault = createByDefault;
6272 }
6273 
6274 QgsProcessingParameterVectorDestination::QgsProcessingParameterVectorDestination( const QString &name, const QString &description, QgsProcessing::SourceType type, const QVariant &defaultValue, bool optional, bool createByDefault )
6275  : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
6276  , mDataType( type )
6277 {
6278 
6279 }
6280 
6282 {
6283  return new QgsProcessingParameterVectorDestination( *this );
6284 }
6285 
6287 {
6288  QVariant var = input;
6289  if ( !var.isValid() )
6290  return mFlags & FlagOptional;
6291 
6292  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
6293  {
6294  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
6295  var = fromVar.sink;
6296  }
6297 
6298  if ( var.canConvert<QgsProperty>() )
6299  {
6300  QgsProperty p = var.value< QgsProperty >();
6302  {
6303  var = p.staticValue();
6304  }
6305  else
6306  {
6307  return true;
6308  }
6309  }
6310 
6311  if ( var.type() != QVariant::String )
6312  return false;
6313 
6314  if ( var.toString().isEmpty() )
6315  return mFlags & FlagOptional;
6316 
6317  return true;
6318 }
6319 
6321 {
6322  if ( !value.isValid() )
6323  return QStringLiteral( "None" );
6324 
6325  if ( value.canConvert<QgsProperty>() )
6326  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
6327 
6328  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
6329  {
6330  QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
6331  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
6332  {
6333  return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
6334  }
6335  else
6336  {
6337  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
6338  }
6339  }
6340 
6341  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
6342 }
6343 
6345 {
6346  QString code = QStringLiteral( "##%1=" ).arg( mName );
6347  if ( mFlags & FlagOptional )
6348  code += QLatin1String( "optional " );
6349  code += QLatin1String( "vectorDestination " );
6350 
6351  switch ( mDataType )
6352  {
6354  code += QLatin1String( "point " );
6355  break;
6356 
6358  code += QLatin1String( "line " );
6359  break;
6360 
6362  code += QLatin1String( "polygon " );
6363  break;
6364 
6365  default:
6366  break;
6367  }
6368 
6369  code += mDefault.toString();
6370  return code.trimmed();
6371 }
6372 
6374 {
6375  return new QgsProcessingOutputVectorLayer( name(), description(), mDataType );
6376 }
6377 
6379 {
6380  if ( auto *lOriginalProvider = originalProvider() )
6381  {
6382  return lOriginalProvider->defaultVectorFileExtension( hasGeometry() );
6383  }
6384  else if ( QgsProcessingProvider *p = provider() )
6385  {
6386  return p->defaultVectorFileExtension( hasGeometry() );
6387  }
6388  else
6389  {
6390  if ( hasGeometry() )
6391  {
6393  }
6394  else
6395  {
6396  return QStringLiteral( "dbf" );
6397  }
6398  }
6399 }
6400 
6402 {
6403  switch ( outputType )
6404  {
6406  {
6407  QString code = QStringLiteral( "QgsProcessingParameterVectorDestination('%1', '%2'" ).arg( name(), description() );
6408  if ( mFlags & FlagOptional )
6409  code += QLatin1String( ", optional=True" );
6410 
6411  code += QStringLiteral( ", type=QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( mDataType ) );
6412 
6413  code += QStringLiteral( ", createByDefault=%1" ).arg( createByDefault() ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
6414 
6416  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6417  return code;
6418  }
6419  }
6420  return QString();
6421 }
6422 
6424 {
6425  const QStringList exts = supportedOutputVectorLayerExtensions();
6426  QStringList filters;
6427  for ( const QString &ext : exts )
6428  {
6429  filters << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
6430  }
6431  return filters.join( QLatin1String( ";;" ) ) + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
6432 }
6433 
6435 {
6436  if ( auto *lOriginalProvider = originalProvider() )
6437  {
6438  if ( hasGeometry() )
6439  return lOriginalProvider->supportedOutputVectorLayerExtensions();
6440  else
6441  return lOriginalProvider->supportedOutputTableExtensions();
6442  }
6443  else if ( QgsProcessingProvider *p = provider() )
6444  {
6445  if ( hasGeometry() )
6446  return p->supportedOutputVectorLayerExtensions();
6447  else
6448  return p->supportedOutputTableExtensions();
6449  }
6450  else
6451  {
6453  }
6454 }
6455 
6457 {
6458  return mDataType;
6459 }
6460 
6462 {
6463  switch ( mDataType )
6464  {
6470  return true;
6471 
6476  return false;
6477  }
6478  return true;
6479 }
6480 
6482 {
6483  mDataType = type;
6484 }
6485 
6487 {
6489  map.insert( QStringLiteral( "data_type" ), mDataType );
6490  return map;
6491 }
6492 
6494 {
6496  mDataType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
6497  return true;
6498 }
6499 
6500 QgsProcessingParameterVectorDestination *QgsProcessingParameterVectorDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6501 {
6503  QString def = definition;
6504  if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
6505  {
6507  def = def.mid( 6 );
6508  }
6509  else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
6510  {
6512  def = def.mid( 5 );
6513  }
6514  else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
6515  {
6517  def = def.mid( 8 );
6518  }
6519 
6520  return new QgsProcessingParameterVectorDestination( name, description, type, definition, isOptional );
6521 }
6522 
6523 QgsProcessingParameterBand::QgsProcessingParameterBand( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional, bool allowMultiple )
6524  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
6525  , mParentLayerParameterName( parentLayerParameterName )
6526  , mAllowMultiple( allowMultiple )
6527 {
6528 
6529 }
6530 
6532 {
6533  return new QgsProcessingParameterBand( *this );
6534 }
6535 
6537 {
6538  if ( !input.isValid() )
6539  return mFlags & FlagOptional;
6540 
6541  if ( input.canConvert<QgsProperty>() )
6542  {
6543  return true;
6544  }
6545 
6546  if ( input.type() == QVariant::List || input.type() == QVariant::StringList )
6547  {
6548  if ( !mAllowMultiple )
6549  return false;
6550 
6551  if ( input.toList().isEmpty() && !( mFlags & FlagOptional ) )
6552  return false;
6553  }
6554  else
6555  {
6556  bool ok = false;
6557  double res = input.toInt( &ok );
6558  Q_UNUSED( res )
6559  if ( !ok )
6560  return mFlags & FlagOptional;
6561  }
6562  return true;
6563 }
6564 
6566 {
6567  return mAllowMultiple;
6568 }
6569 
6571 {
6572  mAllowMultiple = allowMultiple;
6573 }
6574 
6576 {
6577  if ( !value.isValid() )
6578  return QStringLiteral( "None" );
6579 
6580  if ( value.canConvert<QgsProperty>() )
6581  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
6582 
6583  if ( value.type() == QVariant::List )
6584  {
6585  QStringList parts;
6586  QVariantList values = value.toList();
6587  for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
6588  {
6589  parts << QString::number( static_cast< int >( it->toDouble() ) );
6590  }
6591  return parts.join( ',' ).prepend( '[' ).append( ']' );
6592  }
6593  else if ( value.type() == QVariant::StringList )
6594  {
6595  QStringList parts;
6596  QStringList values = value.toStringList();
6597  for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
6598  {
6599  parts << QString::number( static_cast< int >( it->toDouble() ) );
6600  }
6601  return parts.join( ',' ).prepend( '[' ).append( ']' );
6602  }
6603 
6604  return value.toString();
6605 }
6606 
6608 {
6609  QString code = QStringLiteral( "##%1=" ).arg( mName );
6610  if ( mFlags & FlagOptional )
6611  code += QLatin1String( "optional " );
6612  code += QLatin1String( "band " );
6613 
6614  if ( mAllowMultiple )
6615  code += QLatin1String( "multiple " );
6616 
6617  code += mParentLayerParameterName + ' ';
6618 
6619  code += mDefault.toString();
6620  return code.trimmed();
6621 }
6622 
6624 {
6625  QStringList depends;
6626  if ( !mParentLayerParameterName.isEmpty() )
6627  depends << mParentLayerParameterName;
6628  return depends;
6629 }
6630 
6632 {
6633  switch ( outputType )
6634  {
6636  {
6637  QString code = QStringLiteral( "QgsProcessingParameterBand('%1', '%2'" ).arg( name(), description() );
6638  if ( mFlags & FlagOptional )
6639  code += QLatin1String( ", optional=True" );
6640 
6641  code += QStringLiteral( ", parentLayerParameterName='%1'" ).arg( mParentLayerParameterName );
6642  code += QStringLiteral( ", allowMultiple=%1" ).arg( mAllowMultiple ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
6643 
6645  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6646  return code;
6647  }
6648  }
6649  return QString();
6650 }
6651 
6653 {
6654  return mParentLayerParameterName;
6655 }
6656 
6657 void QgsProcessingParameterBand::setParentLayerParameterName( const QString &parentLayerParameterName )
6658 {
6659  mParentLayerParameterName = parentLayerParameterName;
6660 }
6661 
6663 {
6665  map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
6666  map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
6667  return map;
6668 }
6669 
6670 bool QgsProcessingParameterBand::fromVariantMap( const QVariantMap &map )
6671 {
6673  mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
6674  mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
6675  return true;
6676 }
6677 
6678 QgsProcessingParameterBand *QgsProcessingParameterBand::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6679 {
6680  QString parent;
6681  QString def = definition;
6682  bool allowMultiple = false;
6683 
6684  if ( def.startsWith( QLatin1String( "multiple" ), Qt::CaseInsensitive ) )
6685  {
6686  allowMultiple = true;
6687  def = def.mid( 8 ).trimmed();
6688  }
6689 
6690  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
6691  QRegularExpressionMatch m = re.match( def );
6692  if ( m.hasMatch() )
6693  {
6694  parent = m.captured( 1 ).trimmed();
6695  def = m.captured( 2 );
6696  }
6697  else
6698  {
6699  parent = def;
6700  def.clear();
6701  }
6702 
6703  return new QgsProcessingParameterBand( name, description, def.isEmpty() ? QVariant() : def, parent, isOptional, allowMultiple );
6704 }
6705 
6706 //
6707 // QgsProcessingParameterDistance
6708 //
6709 
6710 QgsProcessingParameterDistance::QgsProcessingParameterDistance( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentParameterName, bool optional, double minValue, double maxValue )
6711  : QgsProcessingParameterNumber( name, description, Double, defaultValue, optional, minValue, maxValue )
6712  , mParentParameterName( parentParameterName )
6713 {
6714 
6715 }
6716 
6718 {
6719  return new QgsProcessingParameterDistance( *this );
6720 }
6721 
6723 {
6724  return typeName();
6725 }
6726 
6728 {
6729  QStringList depends;
6730  if ( !mParentParameterName.isEmpty() )
6731  depends << mParentParameterName;
6732  return depends;
6733 }
6734 
6736 {
6737  switch ( outputType )
6738  {
6740  {
6741  QString code = QStringLiteral( "QgsProcessingParameterDistance('%1', '%2'" ).arg( name(), description() );
6742  if ( mFlags & FlagOptional )
6743  code += QLatin1String( ", optional=True" );
6744 
6745  code += QStringLiteral( ", parentParameterName='%1'" ).arg( mParentParameterName );
6746 
6747  if ( minimum() != std::numeric_limits<double>::lowest() + 1 )
6748  code += QStringLiteral( ", minValue=%1" ).arg( minimum() );
6749  if ( maximum() != std::numeric_limits<double>::max() )
6750  code += QStringLiteral( ", maxValue=%1" ).arg( maximum() );
6752  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6753  return code;
6754  }
6755  }
6756  return QString();
6757 }
6758 
6760 {
6761  return mParentParameterName;
6762 }
6763 
6764 void QgsProcessingParameterDistance::setParentParameterName( const QString &parentParameterName )
6765 {
6766  mParentParameterName = parentParameterName;
6767 }
6768 
6770 {
6771  QVariantMap map = QgsProcessingParameterNumber::toVariantMap();
6772  map.insert( QStringLiteral( "parent" ), mParentParameterName );
6773  map.insert( QStringLiteral( "default_unit" ), static_cast< int >( mDefaultUnit ) );
6774  return map;
6775 }
6776 
6778 {
6780  mParentParameterName = map.value( QStringLiteral( "parent" ) ).toString();
6781  mDefaultUnit = static_cast< QgsUnitTypes::DistanceUnit>( map.value( QStringLiteral( "default_unit" ), QgsUnitTypes::DistanceUnknownUnit ).toInt() );
6782  return true;
6783 }
6784 
6785 
6786 //
6787 // QgsProcessingParameterScale
6788 //
6789 
6790 QgsProcessingParameterScale::QgsProcessingParameterScale( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
6791  : QgsProcessingParameterNumber( name, description, Double, defaultValue, optional )
6792 {
6793 
6794 }
6795 
6797 {
6798  return new QgsProcessingParameterScale( *this );
6799 }
6800 
6802 {
6803  return typeName();
6804 }
6805 
6807 {
6808  switch ( outputType )
6809  {
6811  {
6812  QString code = QStringLiteral( "QgsProcessingParameterScale('%1', '%2'" ).arg( name(), description() );
6813  if ( mFlags & FlagOptional )
6814  code += QLatin1String( ", optional=True" );
6816  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6817  return code;
6818  }
6819  }
6820  return QString();
6821 }
6822 
6823 QgsProcessingParameterScale *QgsProcessingParameterScale::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6824 {
6825  return new QgsProcessingParameterScale( name, description, definition.isEmpty() ? QVariant()
6826  : ( definition.toLower().trimmed() == QLatin1String( "none" ) ? QVariant() : definition ), isOptional );
6827 }
6828 
6829 
6830 //
6831 // QgsProcessingParameterLayout
6832 //
6833 
6834 QgsProcessingParameterLayout::QgsProcessingParameterLayout( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
6835  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
6836 {}
6837 
6839 {
6840  return new QgsProcessingParameterLayout( *this );
6841 }
6842 
6844 {
6845  if ( !value.isValid() || value.isNull() )
6846  return QStringLiteral( "None" );
6847 
6848  if ( value.canConvert<QgsProperty>() )
6849  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
6850 
6851  QString s = value.toString();
6853 }
6854 
6856 {
6857  QString code = QStringLiteral( "##%1=" ).arg( mName );
6858  if ( mFlags & FlagOptional )
6859  code += QLatin1String( "optional " );
6860  code += QLatin1String( "layout " );
6861 
6862  code += mDefault.toString();
6863  return code.trimmed();
6864 }
6865 
6867 {
6868  switch ( outputType )
6869  {
6871  {
6872  QString code = QStringLiteral( "QgsProcessingParameterLayout('%1', '%2'" ).arg( name(), description() );
6873  if ( mFlags & FlagOptional )
6874  code += QLatin1String( ", optional=True" );
6876  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6877  return code;
6878  }
6879  }
6880  return QString();
6881 }
6882 
6883 QgsProcessingParameterLayout *QgsProcessingParameterLayout::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6884 {
6885  QString def = definition;
6886 
6887  if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
6888  def = def.mid( 1 );
6889  if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
6890  def.chop( 1 );
6891 
6892  QVariant defaultValue = def;
6893  if ( def == QLatin1String( "None" ) )
6894  defaultValue = QVariant();
6895 
6896  return new QgsProcessingParameterLayout( name, description, defaultValue, isOptional );
6897 }
6898 
6899 
6900 //
6901 // QString mParentLayerParameterName;
6902 //
6903 
6904 QgsProcessingParameterLayoutItem::QgsProcessingParameterLayoutItem( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayoutParameterName, int itemType, bool optional )
6905  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
6906  , mParentLayoutParameterName( parentLayoutParameterName )
6907  , mItemType( itemType )
6908 {
6909 
6910 }
6911 
6913 {
6914  return new QgsProcessingParameterLayoutItem( *this );
6915 }
6916 
6918 {
6919  if ( !value.isValid() || value.isNull() )
6920  return QStringLiteral( "None" );
6921 
6922  if ( value.canConvert<QgsProperty>() )
6923  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
6924 
6925  QString s = value.toString();
6927 }
6928 
6930 {
6931  QString code = QStringLiteral( "##%1=" ).arg( mName );
6932  if ( mFlags & FlagOptional )
6933  code += QLatin1String( "optional " );
6934  code += QLatin1String( "layoutitem " );
6935  if ( mItemType >= 0 )
6936  code += QString::number( mItemType ) + ' ';
6937 
6938  code += mParentLayoutParameterName + ' ';
6939 
6940  code += mDefault.toString();
6941  return code.trimmed();
6942 }
6943 
6945 {
6946  switch ( outputType )
6947  {
6949  {
6950  QString code = QStringLiteral( "QgsProcessingParameterLayoutItem('%1', '%2'" ).arg( name(), description() );
6951  if ( mFlags & FlagOptional )
6952  code += QLatin1String( ", optional=True" );
6953 
6954  if ( mItemType >= 0 )
6955  code += QStringLiteral( ", itemType=%1" ).arg( mItemType );
6956 
6957  code += QStringLiteral( ", parentLayoutParameterName='%1'" ).arg( mParentLayoutParameterName );
6958 
6960  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6961  return code;
6962  }
6963  }
6964  return QString();
6965 }
6966 
6968 {
6970  map.insert( QStringLiteral( "parent_layout" ), mParentLayoutParameterName );
6971  map.insert( QStringLiteral( "item_type" ), mItemType );
6972  return map;
6973 }
6974 
6976 {
6978  mParentLayoutParameterName = map.value( QStringLiteral( "parent_layout" ) ).toString();
6979  mItemType = map.value( QStringLiteral( "item_type" ) ).toInt();
6980  return true;
6981 }
6982 
6984 {
6985  QStringList depends;
6986  if ( !mParentLayoutParameterName.isEmpty() )
6987  depends << mParentLayoutParameterName;
6988  return depends;
6989 }
6990 
6991 QgsProcessingParameterLayoutItem *QgsProcessingParameterLayoutItem::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6992 {
6993  QString parent;
6994  QString def = definition;
6995  int itemType = -1;
6996  QRegularExpression re( QStringLiteral( "(\\d+)?\\s*(.*?)\\s+(.*)$" ) );
6997  QRegularExpressionMatch m = re.match( def );
6998  if ( m.hasMatch() )
6999  {
7000  itemType = m.captured( 1 ).trimmed().isEmpty() ? -1 : m.captured( 1 ).trimmed().toInt();
7001  parent = m.captured( 2 ).trimmed().isEmpty() ? m.captured( 3 ).trimmed() : m.captured( 2 ).trimmed();
7002  def = !m.captured( 2 ).trimmed().isEmpty() ? m.captured( 3 ) : QString();
7003  }
7004  else
7005  {
7006  parent = def;
7007  def.clear();
7008  }
7009 
7010  return new QgsProcessingParameterLayoutItem( name, description, def.isEmpty() ? QVariant() : def, parent, itemType, isOptional );
7011 }
7012 
7014 {
7015  return mParentLayoutParameterName;
7016 }
7017 
7019 {
7020  mParentLayoutParameterName = name;
7021 }
7022 
7024 {
7025  return mItemType;
7026 }
7027 
7029 {
7030  mItemType = type;
7031 }
7032 
7033 //
7034 // QgsProcessingParameterColor
7035 //
7036 
7037 QgsProcessingParameterColor::QgsProcessingParameterColor( const QString &name, const QString &description, const QVariant &defaultValue, bool opacityEnabled, bool optional )
7038  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7039  , mAllowOpacity( opacityEnabled )
7040 {
7041 
7042 }
7043 
7045 {
7046  return new QgsProcessingParameterColor( *this );
7047 }
7048 
7050 {
7051  if ( !value.isValid() || value.isNull() )
7052  return QStringLiteral( "None" );
7053 
7054  if ( value.canConvert<QgsProperty>() )
7055  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7056 
7057  if ( value.canConvert< QColor >() && !value.value< QColor >().isValid() )
7058  return QStringLiteral( "QColor()" );
7059 
7060  if ( value.canConvert< QColor >() )
7061  {
7062  QColor c = value.value< QColor >();
7063  if ( !mAllowOpacity || c.alpha() == 255 )
7064  return QStringLiteral( "QColor(%1, %2, %3)" ).arg( c.red() ).arg( c.green() ).arg( c.blue() );
7065  else
7066  return QStringLiteral( "QColor(%1, %2, %3, %4)" ).arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
7067  }
7068 
7069  QString s = value.toString();
7071 }
7072 
7074 {
7075  QString code = QStringLiteral( "##%1=" ).arg( mName );
7076  if ( mFlags & FlagOptional )
7077  code += QLatin1String( "optional " );
7078  code += QLatin1String( "color " );
7079 
7080  if ( mAllowOpacity )
7081  code += QLatin1String( "withopacity " );
7082 
7083  code += mDefault.toString();
7084  return code.trimmed();
7085 }
7086 
7088 {
7089  switch ( outputType )
7090  {
7092  {
7093  QString code = QStringLiteral( "QgsProcessingParameterColor('%1', '%2'" ).arg( name(), description() );
7094  if ( mFlags & FlagOptional )
7095  code += QLatin1String( ", optional=True" );
7096 
7097  code += QStringLiteral( ", opacityEnabled=%1" ).arg( mAllowOpacity ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
7098 
7100  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7101  return code;
7102  }
7103  }
7104  return QString();
7105 }
7106 
7108 {
7109  if ( !input.isValid() && ( mDefault.isValid() && ( !mDefault.toString().isEmpty() || mDefault.value< QColor >().isValid() ) ) )
7110  return true;
7111 
7112  if ( !input.isValid() )
7113  return mFlags & FlagOptional;
7114 
7115  if ( input.type() == QVariant::Color )
7116  {
7117  return true;
7118  }
7119  else if ( input.canConvert<QgsProperty>() )
7120  {
7121  return true;
7122  }
7123 
7124  if ( input.type() != QVariant::String || input.toString().isEmpty() )
7125  return mFlags & FlagOptional;
7126 
7127  bool containsAlpha = false;
7128  return QgsSymbolLayerUtils::parseColorWithAlpha( input.toString(), containsAlpha ).isValid();
7129 }
7130 
7132 {
7134  map.insert( QStringLiteral( "opacityEnabled" ), mAllowOpacity );
7135  return map;
7136 }
7137 
7138 bool QgsProcessingParameterColor::fromVariantMap( const QVariantMap &map )
7139 {
7141  mAllowOpacity = map.value( QStringLiteral( "opacityEnabled" ) ).toBool();
7142  return true;
7143 }
7144 
7146 {
7147  return mAllowOpacity;
7148 }
7149 
7151 {
7152  mAllowOpacity = enabled;
7153 }
7154 
7155 QgsProcessingParameterColor *QgsProcessingParameterColor::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7156 {
7157  QString def = definition;
7158 
7159  bool allowOpacity = false;
7160  if ( def.startsWith( QLatin1String( "withopacity" ), Qt::CaseInsensitive ) )
7161  {
7162  allowOpacity = true;
7163  def = def.mid( 12 );
7164  }
7165 
7166  if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
7167  def = def.mid( 1 );
7168  if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
7169  def.chop( 1 );
7170 
7171  QVariant defaultValue = def;
7172  if ( def == QLatin1String( "None" ) )
7173  defaultValue = QVariant();
7174 
7175  return new QgsProcessingParameterColor( name, description, defaultValue, allowOpacity, isOptional );
7176 }
7177 
7178 //
7179 // QgsProcessingParameterCoordinateOperation
7180 //
7181 QgsProcessingParameterCoordinateOperation::QgsProcessingParameterCoordinateOperation( const QString &name, const QString &description, const QVariant &defaultValue, const QString &sourceCrsParameterName, const QString &destinationCrsParameterName, const QVariant &staticSourceCrs, const QVariant &staticDestinationCrs, bool optional )
7182  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7183  , mSourceParameterName( sourceCrsParameterName )
7184  , mDestParameterName( destinationCrsParameterName )
7185  , mSourceCrs( staticSourceCrs )
7186  , mDestCrs( staticDestinationCrs )
7187 {
7188 
7189 }
7190 
7192 {
7193  return new QgsProcessingParameterCoordinateOperation( * this );
7194 }
7195 
7197 {
7198  if ( !value.isValid() || value.isNull() )
7199  return QStringLiteral( "None" );
7200 
7201  if ( value.canConvert<QgsCoordinateReferenceSystem>() )
7202  {
7203  if ( !value.value< QgsCoordinateReferenceSystem >().isValid() )
7204  return QStringLiteral( "QgsCoordinateReferenceSystem()" );
7205  else
7206  return QStringLiteral( "QgsCoordinateReferenceSystem('%1')" ).arg( value.value< QgsCoordinateReferenceSystem >().authid() );
7207  }
7208 
7209  if ( value.canConvert<QgsProperty>() )
7210  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7211 
7212  QVariantMap p;
7213  p.insert( name(), value );
7214  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
7215  if ( layer )
7217 
7218  QString s = value.toString();
7220 }
7221 
7223 {
7224  QString code = QStringLiteral( "##%1=" ).arg( mName );
7225  if ( mFlags & FlagOptional )
7226  code += QLatin1String( "optional " );
7227  code += QLatin1String( "coordinateoperation " );
7228 
7229  code += mDefault.toString();
7230  return code.trimmed();
7231 }
7232 
7234 {
7235  switch ( outputType )
7236  {
7238  {
7240  QString code = QStringLiteral( "QgsProcessingParameterCoordinateOperation('%1', '%2'" ).arg( name(), description() );
7241  if ( mFlags & FlagOptional )
7242  code += QLatin1String( ", optional=True" );
7243  if ( !mSourceParameterName.isEmpty() )
7244  code += QStringLiteral( ", sourceCrsParameterName=%1" ).arg( valueAsPythonString( mSourceParameterName, c ) );
7245  if ( !mDestParameterName.isEmpty() )
7246  code += QStringLiteral( ", destinationCrsParameterName=%1" ).arg( valueAsPythonString( mDestParameterName, c ) );
7247 
7248  if ( mSourceCrs.isValid() )
7249  code += QStringLiteral( ", staticSourceCrs=%1" ).arg( valueAsPythonString( mSourceCrs, c ) );
7250  if ( mDestCrs.isValid() )
7251  code += QStringLiteral( ", staticDestinationCrs=%1" ).arg( valueAsPythonString( mDestCrs, c ) );
7252 
7253  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7254  return code;
7255  }
7256  }
7257  return QString();
7258 }
7259 
7261 {
7262  QStringList res;
7263  if ( !mSourceParameterName.isEmpty() )
7264  res << mSourceParameterName;
7265  if ( !mDestParameterName.isEmpty() )
7266  res << mDestParameterName;
7267  return res;
7268 }
7269 
7271 {
7273  map.insert( QStringLiteral( "source_crs_parameter_name" ), mSourceParameterName );
7274  map.insert( QStringLiteral( "dest_crs_parameter_name" ), mDestParameterName );
7275  map.insert( QStringLiteral( "static_source_crs" ), mSourceCrs );
7276  map.insert( QStringLiteral( "static_dest_crs" ), mDestCrs );
7277  return map;
7278 }
7279 
7281 {
7283  mSourceParameterName = map.value( QStringLiteral( "source_crs_parameter_name" ) ).toString();
7284  mDestParameterName = map.value( QStringLiteral( "dest_crs_parameter_name" ) ).toString();
7285  mSourceCrs = map.value( QStringLiteral( "static_source_crs" ) );
7286  mDestCrs = map.value( QStringLiteral( "static_dest_crs" ) );
7287  return true;
7288 }
7289 
7290 QgsProcessingParameterCoordinateOperation *QgsProcessingParameterCoordinateOperation::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7291 {
7292  QString def = definition;
7293 
7294  if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
7295  def = def.mid( 1 );
7296  if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
7297  def.chop( 1 );
7298 
7299  QVariant defaultValue = def;
7300  if ( def == QLatin1String( "None" ) )
7301  defaultValue = QVariant();
7302 
7303  return new QgsProcessingParameterCoordinateOperation( name, description, defaultValue, QString(), QString(), QVariant(), QVariant(), isOptional );
7304 }
7305 
7306 
7307 //
7308 // QgsProcessingParameterMapTheme
7309 //
7310 
7311 QgsProcessingParameterMapTheme::QgsProcessingParameterMapTheme( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
7312  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7313 {
7314 
7315 }
7316 
7317 
7319 {
7320  return new QgsProcessingParameterMapTheme( *this );
7321 }
7322 
7324 {
7325  if ( !input.isValid() && !mDefault.isValid() )
7326  return mFlags & FlagOptional;
7327 
7328  if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
7329  || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
7330  return mFlags & FlagOptional;
7331 
7332  return true;
7333 }
7334 
7336 {
7337  if ( !value.isValid() )
7338  return QStringLiteral( "None" );
7339 
7340  if ( value.canConvert<QgsProperty>() )
7341  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7342 
7343  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
7344 }
7345 
7347 {
7348  QString code = QStringLiteral( "##%1=" ).arg( mName );
7349  if ( mFlags & FlagOptional )
7350  code += QLatin1String( "optional " );
7351  code += QLatin1String( "maptheme " );
7352 
7353  code += mDefault.toString();
7354  return code.trimmed();
7355 }
7356 
7358 {
7359  switch ( outputType )
7360  {
7362  {
7363  QString code = QStringLiteral( "QgsProcessingParameterMapTheme('%1', '%2'" ).arg( name(), description() );
7364  if ( mFlags & FlagOptional )
7365  code += QLatin1String( ", optional=True" );
7366 
7368  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7369 
7370  return code;
7371  }
7372  }
7373  return QString();
7374 }
7375 
7377 {
7379  return map;
7380 }
7381 
7383 {
7385  return true;
7386 }
7387 
7388 QgsProcessingParameterMapTheme *QgsProcessingParameterMapTheme::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7389 {
7390  QString parent;
7391 
7392  QString def = definition;
7393  if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
7394  def = def.mid( 1 );
7395  if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
7396  def.chop( 1 );
7397 
7398  QVariant defaultValue = def;
7399 
7400  if ( defaultValue == QLatin1String( "None" ) || defaultValue.toString().isEmpty() )
7401  defaultValue = QVariant();
7402 
7403  return new QgsProcessingParameterMapTheme( name, description, defaultValue, isOptional );
7404 }
7405 
7406 
7407 //
7408 // QgsProcessingParameterDateTime
7409 //
7410 
7411 QgsProcessingParameterDateTime::QgsProcessingParameterDateTime( const QString &name, const QString &description, Type type, const QVariant &defaultValue, bool optional, const QDateTime &minValue, const QDateTime &maxValue )
7412  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7413  , mMin( minValue )
7414  , mMax( maxValue )
7415  , mDataType( type )
7416 {
7417  if ( mMin.isValid() && mMax.isValid() && mMin >= mMax )
7418  {
7419  QgsMessageLog::logMessage( QObject::tr( "Invalid datetime parameter \"%1\": min value %2 is >= max value %3!" ).arg( name, mMin.toString(), mMax.toString() ), QObject::tr( "Processing" ) );
7420  }
7421 }
7422 
7424 {
7425  return new QgsProcessingParameterDateTime( *this );
7426 }
7427 
7429 {
7430  QVariant input = value;
7431  if ( !input.isValid() )
7432  {
7433  if ( !defaultValue().isValid() )
7434  return mFlags & FlagOptional;
7435 
7436  input = defaultValue();
7437  }
7438 
7439  if ( input.canConvert<QgsProperty>() )
7440  {
7441  return true;
7442  }
7443 
7444  if ( input.type() != QVariant::DateTime && input.type() != QVariant::Date && input.type() != QVariant::Time && input.type() != QVariant::String )
7445  return false;
7446 
7447  if ( ( input.type() == QVariant::DateTime || input.type() == QVariant::Date ) && mDataType == Time )
7448  return false;
7449 
7450  if ( input.type() == QVariant::String )
7451  {
7452  QString s = input.toString();
7453  if ( s.isEmpty() )
7454  return mFlags & FlagOptional;
7455 
7456  input = QDateTime::fromString( s, Qt::ISODate );
7457  if ( mDataType == Time )
7458  {
7459  if ( !input.toDateTime().isValid() )
7460  input = QTime::fromString( s );
7461  else
7462  input = input.toDateTime().time();
7463  }
7464  }
7465 
7466  if ( mDataType != Time )
7467  {
7468  QDateTime res = input.toDateTime();
7469  return res.isValid() && ( res >= mMin || !mMin.isValid() ) && ( res <= mMax || !mMax.isValid() );
7470  }
7471  else
7472  {
7473  QTime res = input.toTime();
7474  return res.isValid() && ( res >= mMin.time() || !mMin.isValid() ) && ( res <= mMax.time() || !mMax.isValid() );
7475  }
7476 }
7477 
7479 {
7480  if ( !value.isValid() )
7481  return QStringLiteral( "None" );
7482 
7483  if ( value.canConvert<QgsProperty>() )
7484  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7485 
7486  if ( value.type() == QVariant::DateTime )
7487  {
7488  const QDateTime dt = value.toDateTime();
7489  if ( !dt.isValid() )
7490  return QStringLiteral( "QDateTime()" );
7491  else
7492  return QStringLiteral( "QDateTime(QDate(%1, %2, %3), QTime(%4, %5, %6))" ).arg( dt.date().year() )
7493  .arg( dt.date().month() )
7494  .arg( dt.date().day() )
7495  .arg( dt.time().hour() )
7496  .arg( dt.time().minute() )
7497  .arg( dt.time().second() );
7498  }
7499  else if ( value.type() == QVariant::Date )
7500  {
7501  const QDate dt = value.toDate();
7502  if ( !dt.isValid() )
7503  return QStringLiteral( "QDate()" );
7504  else
7505  return QStringLiteral( "QDate(%1, %2, %3)" ).arg( dt.year() )
7506  .arg( dt.month() )
7507  .arg( dt.day() );
7508  }
7509  else if ( value.type() == QVariant::Time )
7510  {
7511  const QTime dt = value.toTime();
7512  if ( !dt.isValid() )
7513  return QStringLiteral( "QTime()" );
7514  else
7515  return QStringLiteral( "QTime(%4, %5, %6)" )
7516  .arg( dt.hour() )
7517  .arg( dt.minute() )
7518  .arg( dt.second() );
7519  }
7520  return value.toString();
7521 }
7522 
7524 {
7526  QStringList parts;
7527  if ( mMin.isValid() )
7528  parts << QObject::tr( "Minimum value: %1" ).arg( mMin.toString( Qt::ISODate ) );
7529  if ( mMax.isValid() )
7530  parts << QObject::tr( "Maximum value: %1" ).arg( mMax.toString( Qt::ISODate ) );
7531  if ( mDefault.isValid() )
7532  parts << QObject::tr( "Default value: %1" ).arg( mDataType == DateTime ? mDefault.toDateTime().toString( Qt::ISODate ) :
7533  ( mDataType == Date ? mDefault.toDate().toString( Qt::ISODate ) : mDefault.toTime( ).toString() ) );
7534  QString extra = parts.join( QLatin1String( "<br />" ) );
7535  if ( !extra.isEmpty() )
7536  text += QStringLiteral( "<p>%1</p>" ).arg( extra );
7537  return text;
7538 }
7539 
7541 {
7542  switch ( outputType )
7543  {
7545  {
7546  QString code = QStringLiteral( "QgsProcessingParameterDateTime('%1', '%2'" ).arg( name(), description() );
7547  if ( mFlags & FlagOptional )
7548  code += QLatin1String( ", optional=True" );
7549 
7550  code += QStringLiteral( ", type=%1" ).arg( mDataType == DateTime ? QStringLiteral( "QgsProcessingParameterDateTime.DateTime" )
7551  : mDataType == Date ? QStringLiteral( "QgsProcessingParameterDateTime.Date" )
7552  : QStringLiteral( "QgsProcessingParameterDateTime.Time" ) );
7553 
7555  if ( mMin.isValid() )
7556  code += QStringLiteral( ", minValue=%1" ).arg( valueAsPythonString( mMin, c ) );
7557  if ( mMax.isValid() )
7558  code += QStringLiteral( ", maxValue=%1" ).arg( valueAsPythonString( mMax, c ) );
7559  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7560  return code;
7561  }
7562  }
7563  return QString();
7564 }
7565 
7567 {
7568  return mMin;
7569 }
7570 
7572 {
7573  mMin = min;
7574 }
7575 
7577 {
7578  return mMax;
7579 }
7580 
7582 {
7583  mMax = max;
7584 }
7585 
7587 {
7588  return mDataType;
7589 }
7590 
7592 {
7593  mDataType = dataType;
7594 }
7595 
7597 {
7599  map.insert( QStringLiteral( "min" ), mMin );
7600  map.insert( QStringLiteral( "max" ), mMax );
7601  map.insert( QStringLiteral( "data_type" ), mDataType );
7602  return map;
7603 }
7604 
7606 {
7608  mMin = map.value( QStringLiteral( "min" ) ).toDateTime();
7609  mMax = map.value( QStringLiteral( "max" ) ).toDateTime();
7610  mDataType = static_cast< Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
7611  return true;
7612 }
7613 
7614 QgsProcessingParameterDateTime *QgsProcessingParameterDateTime::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7615 {
7616  return new QgsProcessingParameterDateTime( name, description, DateTime, definition.isEmpty() ? QVariant()
7617  : ( definition.toLower().trimmed() == QLatin1String( "none" ) ? QVariant() : definition ), isOptional );
7618 }
7619 
7620 
7621 
7622 //
7623 // QgsProcessingParameterProviderConnection
7624 //
7625 
7626 QgsProcessingParameterProviderConnection::QgsProcessingParameterProviderConnection( const QString &name, const QString &description, const QString &provider, const QVariant &defaultValue, bool optional )
7627  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7628  , mProviderId( provider )
7629 {
7630 
7631 }
7632 
7633 
7635 {
7636  return new QgsProcessingParameterProviderConnection( *this );
7637 }
7638 
7640 {
7641  if ( !input.isValid() && !mDefault.isValid() )
7642  return mFlags & FlagOptional;
7643 
7644  if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
7645  || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
7646  return mFlags & FlagOptional;
7647 
7648  return true;
7649 }
7650 
7652 {
7653  if ( !value.isValid() )
7654  return QStringLiteral( "None" );
7655 
7656  if ( value.canConvert<QgsProperty>() )
7657  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7658 
7659  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
7660 }
7661 
7663 {
7664  QString code = QStringLiteral( "##%1=" ).arg( mName );
7665  if ( mFlags & FlagOptional )
7666  code += QLatin1String( "optional " );
7667  code += QLatin1String( "providerconnection " );
7668  code += mProviderId + ' ';
7669 
7670  code += mDefault.toString();
7671  return code.trimmed();
7672 }
7673 
7675 {
7676  switch ( outputType )
7677  {
7679  {
7680  QString code = QStringLiteral( "QgsProcessingParameterProviderConnection('%1', '%2', '%3'" ).arg( name(), description(), mProviderId );
7681  if ( mFlags & FlagOptional )
7682  code += QLatin1String( ", optional=True" );
7683 
7685  code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7686 
7687  return code;
7688  }
7689  }
7690  return QString();
7691 }
7692 
7694 {
7696  map.insert( QStringLiteral( "provider" ), mProviderId );
7697  return map;
7698 }
7699 
7701 {
7703  mProviderId = map.value( QStringLiteral( "provider" ) ).toString();
7704  return true;
7705 }
7706 
7707 QgsProcessingParameterProviderConnection *QgsProcessingParameterProviderConnection::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7708 {
7709  QString parent;
7710 
7711  QString def = definition;
7712  QString provider;
7713  if ( def.contains( ' ' ) )
7714  {
7715  provider = def.left( def.indexOf( ' ' ) );
7716  def = def.mid( def.indexOf( ' ' ) + 1 );
7717  }
7718  else
7719  {
7720  provider = def;
7721  def.clear();
7722  }
7723 
7724  if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
7725  def = def.mid( 1 );
7726  if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
7727  def.chop( 1 );
7728 
7729  QVariant defaultValue = def;
7730 
7731  if ( defaultValue == QLatin1String( "None" ) || defaultValue.toString().isEmpty() )
7732  defaultValue = QVariant();
7733 
7735 }
7736 
7737 
7738 //
7739 // QgsProcessingParameterDatabaseSchema
7740 //
7741 
7742 QgsProcessingParameterDatabaseSchema::QgsProcessingParameterDatabaseSchema( const QString &name, const QString &description, const QString &parentLayerParameterName, const QVariant &defaultValue, bool optional )
7743  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7744  , mParentConnectionParameterName( parentLayerParameterName )
7745 {
7746 
7747 }
7748 
7749 
7751 {
7752  return new QgsProcessingParameterDatabaseSchema( *this );
7753 }
7754 
7756 {
7757  if ( !input.isValid() && !mDefault.isValid() )
7758  return mFlags & FlagOptional;
7759 
7760  if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
7761  || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
7762  return mFlags & FlagOptional;
7763 
7764  return true;
7765 }
7766 
7768 {
7769  if ( !value.isValid() )
7770  return QStringLiteral( "None" );
7771 
7772  if ( value.canConvert<QgsProperty>() )
7773  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7774 
7775  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
7776 }
7777 
7779 {
7780  QString code = QStringLiteral( "##%1=" ).arg( mName );
7781  if ( mFlags & FlagOptional )
7782  code += QLatin1String( "optional " );
7783  code += QLatin1String( "databaseschema " );
7784 
7785  code += mParentConnectionParameterName + ' ';
7786 
7787  code += mDefault.toString();
7788  return code.trimmed();
7789 }
7790 
7792 {
7793  switch ( outputType )
7794  {
7796  {
7797  QString code = QStringLiteral( "QgsProcessingParameterDatabaseSchema('%1', '%2'" ).arg( name(), description() );
7798  if ( mFlags & FlagOptional )
7799  code += QLatin1String( ", optional=True" );
7800 
7801  code += QStringLiteral( ", connectionParameterName='%1'" ).arg( mParentConnectionParameterName );
7803  code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );
7804 
7805  code += ')';
7806 
7807  return code;
7808  }
7809  }
7810  return QString();
7811 }
7812 
7814 {
7815  QStringList depends;
7816  if ( !mParentConnectionParameterName.isEmpty() )
7817  depends << mParentConnectionParameterName;
7818  return depends;
7819 }
7820 
7822 {
7823  return mParentConnectionParameterName;
7824 }
7825 
7827 {
7828  mParentConnectionParameterName = name;
7829 }
7830 
7832 {
7834  map.insert( QStringLiteral( "mParentConnectionParameterName" ), mParentConnectionParameterName );
7835  return map;
7836 }
7837 
7839 {
7841  mParentConnectionParameterName = map.value( QStringLiteral( "mParentConnectionParameterName" ) ).toString();
7842  return true;
7843 }
7844 
7845 QgsProcessingParameterDatabaseSchema *QgsProcessingParameterDatabaseSchema::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7846 {
7847  QString parent;
7848  QString def = definition;
7849 
7850  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
7851  QRegularExpressionMatch m = re.match( def );
7852  if ( m.hasMatch() )
7853  {
7854  parent = m.captured( 1 ).trimmed();
7855  def = m.captured( 2 );
7856  }
7857  else
7858  {
7859  parent = def;
7860  def.clear();
7861  }
7862 
7863  return new QgsProcessingParameterDatabaseSchema( name, description, parent, def.isEmpty() ? QVariant() : def, isOptional );
7864 }
7865 
7866 //
7867 // QgsProcessingParameterDatabaseTable
7868 //
7869 
7870 QgsProcessingParameterDatabaseTable::QgsProcessingParameterDatabaseTable( const QString &name, const QString &description,
7871  const QString &connectionParameterName,
7872  const QString &schemaParameterName,
7873  const QVariant &defaultValue, bool optional, bool allowNewTableNames )
7874  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7875  , mParentConnectionParameterName( connectionParameterName )
7876  , mParentSchemaParameterName( schemaParameterName )
7877  , mAllowNewTableNames( allowNewTableNames )
7878 {
7879 
7880 }
7881 
7882 
7884 {
7885  return new QgsProcessingParameterDatabaseTable( *this );
7886 }
7887 
7889 {
7890  if ( !input.isValid() && !mDefault.isValid() )
7891  return mFlags & FlagOptional;
7892 
7893  if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
7894  || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
7895  return mFlags & FlagOptional;
7896 
7897  return true;
7898 }
7899 
7901 {
7902  if ( !value.isValid() )
7903  return QStringLiteral( "None" );
7904 
7905  if ( value.canConvert<QgsProperty>() )
7906  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7907 
7908  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
7909 }
7910 
7912 {
7913  QString code = QStringLiteral( "##%1=" ).arg( mName );
7914  if ( mFlags & FlagOptional )
7915  code += QLatin1String( "optional " );
7916  code += QLatin1String( "databasetable " );
7917 
7918  code += ( mParentConnectionParameterName.isEmpty() ? QStringLiteral( "none" ) : mParentConnectionParameterName ) + ' ';
7919  code += ( mParentSchemaParameterName.isEmpty() ? QStringLiteral( "none" ) : mParentSchemaParameterName ) + ' ';
7920 
7921  code += mDefault.toString();
7922  return code.trimmed();
7923 }
7924 
7926 {
7927  switch ( outputType )
7928  {
7930  {
7931  QString code = QStringLiteral( "QgsProcessingParameterDatabaseTable('%1', '%2'" ).arg( name(), description() );
7932  if ( mFlags & FlagOptional )
7933  code += QLatin1String( ", optional=True" );
7934 
7935  if ( mAllowNewTableNames )
7936  code += QLatin1String( ", allowNewTableNames=True" );
7937 
7938  code += QStringLiteral( ", connectionParameterName='%1'" ).arg( mParentConnectionParameterName );
7939  code += QStringLiteral( ", schemaParameterName='%1'" ).arg( mParentSchemaParameterName );
7941  code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );
7942 
7943  code += ')';
7944 
7945  return code;
7946  }
7947  }
7948  return QString();
7949 }
7950 
7952 {
7953  QStringList depends;
7954  if ( !mParentConnectionParameterName.isEmpty() )
7955  depends << mParentConnectionParameterName;
7956  if ( !mParentSchemaParameterName.isEmpty() )
7957  depends << mParentSchemaParameterName;
7958  return depends;
7959 }
7960 
7962 {
7963  return mParentConnectionParameterName;
7964 }
7965 
7967 {
7968  mParentConnectionParameterName = name;
7969 }
7970 
7972 {
7973  return mParentSchemaParameterName;
7974 }
7975 
7977 {
7978  mParentSchemaParameterName = name;
7979 }
7980 
7982 {
7984  map.insert( QStringLiteral( "mParentConnectionParameterName" ), mParentConnectionParameterName );
7985  map.insert( QStringLiteral( "mParentSchemaParameterName" ), mParentSchemaParameterName );
7986  map.insert( QStringLiteral( "mAllowNewTableNames" ), mAllowNewTableNames );
7987  return map;
7988 }
7989 
7991 {
7993  mParentConnectionParameterName = map.value( QStringLiteral( "mParentConnectionParameterName" ) ).toString();
7994  mParentSchemaParameterName = map.value( QStringLiteral( "mParentSchemaParameterName" ) ).toString();
7995  mAllowNewTableNames = map.value( QStringLiteral( "mAllowNewTableNames" ), false ).toBool();
7996  return true;
7997 }
7998 
7999 QgsProcessingParameterDatabaseTable *QgsProcessingParameterDatabaseTable::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8000 {
8001  QString connection;
8002  QString schema;
8003  QString def = definition;
8004 
8005  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*+)\\b\\s*(.*)$" ) );
8006  QRegularExpressionMatch m = re.match( def );
8007  if ( m.hasMatch() )
8008  {
8009  connection = m.captured( 1 ).trimmed();
8010  if ( connection == QLatin1String( "none" ) )
8011  connection.clear();
8012  schema = m.captured( 2 ).trimmed();
8013  if ( schema == QLatin1String( "none" ) )
8014  schema.clear();
8015  def = m.captured( 3 );
8016  }
8017 
8018  return new QgsProcessingParameterDatabaseTable( name, description, connection, schema, def.isEmpty() ? QVariant() : def, isOptional );
8019 }
8020 
8022 {
8023  return mAllowNewTableNames;
8024 }
8025 
8027 {
8028  mAllowNewTableNames = allowNewTableNames;
8029 }
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QString authid() const
Returns the authority identifier for the CRS.
@ WKT_PREFERRED
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
QString toWkt(WktVariant variant=WKT1_GDAL, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
Class for doing transforms between two map coordinate systems.
QgsPointXY transform(const QgsPointXY &point, TransformDirection direction=ForwardTransform) const SIP_THROW(QgsCsException)
Transform the point from the source CRS to the destination CRS.
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, TransformDirection direction=ForwardTransform, bool handle180Crossover=false) const SIP_THROW(QgsCsException)
Transforms a rectangle from the source CRS to the destination CRS.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
Class for parsing and evaluation of expressions (formerly called "search strings").
bool isValid() const
Checks if this expression is valid.
InvalidGeometryCheck
Handling of features with invalid geometries.
@ GeometryNoCheck
No invalid geometry checking.
@ GeometryAbortOnInvalid
Close iterator on encountering any features with invalid geometry. This requires a slow geometry vali...
@ GeometrySkipInvalid
Skip any features with invalid geometry. This requires a slow geometry validity check for every featu...
An interface for objects which accept features via addFeature(s) methods.
Container of fields for a vector layer.
Definition: qgsfields.h:45
static QString addExtensionFromFilter(const QString &fileName, const QString &filter)
Ensures that a fileName ends with an extension from the specified filter string.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsGeometry densifyByCount(int extraNodesPerSegment) const
Returns a copy of the geometry which has been densified by adding the specified number of extra nodes...
Q_GADGET bool isNull
Definition: qgsgeometry.h:126
static QgsGeometry fromRect(const QgsRectangle &rect) SIP_HOLDGIL
Creates a new geometry from a QgsRectangle.
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
static QgsGeometry fromPointXY(const QgsPointXY &point) SIP_HOLDGIL
Creates a new geometry from a QgsPointXY object.
QgsWkbTypes::GeometryType type
Definition: qgsgeometry.h:127
QgsGeometry centroid() const
Returns the center of mass of a geometry.
QString lastError() const SIP_HOLDGIL
Returns an error string referring to the last error encountered either when this geometry was created...
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
QString asWkt(int precision=17) const
Exports the geometry to WKT.
Base class for graphical items within a QgsLayout.
QgsMasterLayoutInterface * layoutByName(const QString &name) const
Returns the layout with a matching name, or nullptr if no matching layouts were found.
QgsLayoutItem * itemById(const QString &id) const
Returns a layout item given its id.
Definition: qgslayout.cpp:266
QgsLayoutItem * itemByUuid(const QString &uuid, bool includeTemplateUuids=false) const
Returns the layout item with matching uuid unique identifier, or nullptr if a matching item could not...
Definition: qgslayout.cpp:238
Base class for all map layer types.
Definition: qgsmaplayer.h:85
virtual QgsRectangle extent() const
Returns the extent of the layer.
QString source() const
Returns the source for the layer.
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:91
Interface for master layout type objects, such as print layouts and reports.
virtual QgsMasterLayoutInterface::Type layoutType() const =0
Returns the master layout type.
@ PrintLayout
Individual print layout (QgsPrintLayout)
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:95
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
A class to represent a 2D point.
Definition: qgspointxy.h:44
double y
Definition: qgspointxy.h:48
Q_GADGET double x
Definition: qgspointxy.h:47
Print layout, a QgsLayout subclass for static or atlas-based layouts.
Abstract base class for processing algorithms.
QgsProcessingProvider * provider() const
Returns the provider to which this algorithm belongs.
Details for layers to load into projects.
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
void addLayerToLoadOnCompletion(const QString &layer, const QgsProcessingContext::LayerDetails &details)
Adds a layer to load (by ID or datasource) into the canvas upon completion of the algorithm or model.
Base class for all parameter definitions which represent file or layer destinations,...
virtual QString defaultFileExtension() const =0
Returns the default file extension for destination file paths associated with this parameter.
void setCreateByDefault(bool createByDefault)
Sets whether the destination should be created by default.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
virtual QString generateTemporaryDestination() const
Generates a temporary destination value for this parameter.
bool supportsNonFileBasedOutput() const
Returns true if the destination parameter supports non filed-based outputs, such as memory layers or ...
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
bool createByDefault() const
Returns true if the destination should be created by default.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
virtual bool isSupportedOutputValue(const QVariant &value, QgsProcessingContext &context, QString &error) const
Tests whether a value is a supported value for this parameter.
QgsProcessingProvider * originalProvider() const
Original (source) provider which this parameter has been derived from.
QgsProcessingDestinationParameter(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingDestinationParameter.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
Encapsulates settings relating to a feature source input to a processing algorithm.
bool loadVariant(const QVariantMap &map)
Loads this source definition from a QVariantMap, wrapped in a QVariant.
Flags flags
Flags which dictate source behavior.
bool selectedFeaturesOnly
true if only selected features in the source should be used by algorithms.
QgsFeatureRequest::InvalidGeometryCheck geometryCheck
Geometry check method to apply to this source.
long long featureLimit
If set to a value > 0, places a limit on the maximum number of features which will be read from the s...
QVariant toVariant() const
Saves this source definition to a QVariantMap, wrapped in a QVariant.
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource,...
Base class for providing feedback from a processing algorithm.
Base class for the definition of processing outputs.
A file output for processing algorithms.
A folder output for processing algorithms.
A HTML file output for processing algorithms.
Encapsulates settings relating to a feature sink or output raster layer for a processing algorithm.
bool loadVariant(const QVariantMap &map)
Loads this output layer definition from a QVariantMap, wrapped in a QVariant.
bool operator!=(const QgsProcessingOutputLayerDefinition &other) const
QgsProject * destinationProject
Destination project.
bool operator==(const QgsProcessingOutputLayerDefinition &other) const
QgsProperty sink
Sink/layer definition.
bool useRemapping() const
Returns true if the output uses a remapping definition.
QgsRemappingSinkDefinition remappingDefinition() const
Returns the output remapping definition, if useRemapping() is true.
QVariant toVariant() const
Saves this output layer definition to a QVariantMap, wrapped in a QVariant.
QString destinationName
Name to use for sink if it's to be loaded into a destination project.
QVariantMap createOptions
Map of optional sink/layer creation options, which are passed to the underlying provider when creatin...
void setRemappingDefinition(const QgsRemappingSinkDefinition &definition)
Sets the remapping definition to use when adding features to the output layer.
A raster layer output for processing algorithms.
A vector layer output for processing algorithms.
A string parameter for authentication configuration ID values.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterAuthConfig(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterAuthConfig.
static QString typeName()
Returns the type name for the parameter class.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QgsProcessingParameterAuthConfig * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
A raster band parameter for Processing algorithms.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
void setAllowMultiple(bool allowMultiple)
Sets whether multiple band selections are permitted.
void setParentLayerParameterName(const QString &parentLayerParameterName)
Sets the name of the parent layer parameter.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString parentLayerParameterName() const
Returns the name of the parent layer parameter, or an empty string if this is not set.
QgsProcessingParameterBand(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentLayerParameterName=QString(), bool optional=false, bool allowMultiple=false)
Constructor for QgsProcessingParameterBand.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QgsProcessingParameterBand * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
static QString typeName()
Returns the type name for the parameter class.
bool allowMultiple() const
Returns whether multiple band selections are permitted.
A boolean parameter for processing algorithms.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString type() const override
Unique parameter type name.
static QString typeName()
Returns the type name for the parameter class.
static QgsProcessingParameterBoolean * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterBoolean(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterBoolean.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
A color parameter for processing algorithms.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QgsProcessingParameterColor * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
bool opacityEnabled() const
Returns true if the parameter allows opacity control.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
void setOpacityEnabled(bool enabled)
Sets whether the parameter allows opacity control.
QgsProcessingParameterColor(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool opacityEnabled=true, bool optional=false)
Constructor for QgsProcessingParameterColor.
static QString typeName()
Returns the type name for the parameter class.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
A coordinate operation parameter for processing algorithms, for selection between available coordinat...
static QString typeName()
Returns the type name for the parameter class.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
static QgsProcessingParameterCoordinateOperation * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterCoordinateOperation(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &sourceCrsParameterName=QString(), const QString &destinationCrsParameterName=QString(), const QVariant &staticSourceCrs=QVariant(), const QVariant &staticDestinationCrs=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterCoordinateOperation.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
A coordinate reference system parameter for processing algorithms.
QgsProcessingParameterCrs(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterCrs.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QString typeName()
Returns the type name for the parameter class.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QgsProcessingParameterCrs * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
A database schema parameter for processing algorithms, allowing users to select from existing schemas...
void setParentConnectionParameterName(const QString &name)
Sets the name of the parent connection parameter.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterDatabaseSchema(const QString &name, const QString &description, const QString &connectionParameterName=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterDatabaseSchema.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QgsProcessingParameterDatabaseSchema * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString parentConnectionParameterName() const
Returns the name of the parent connection parameter, or an empty string if this is not set.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
A database table name parameter for processing algorithms, allowing users to select from existing dat...
QgsProcessingParameterDatabaseTable(const QString &name, const QString &description, const QString &connectionParameterName=QString(), const QString &schemaParameterName=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool allowNewTableNames=false)
Constructor for QgsProcessingParameterDatabaseTable.
void setParentSchemaParameterName(const QString &name)
Sets the name of the parent schema parameter.
QString parentConnectionParameterName() const
Returns the name of the parent connection parameter, or an empty string if this is not set.
QString parentSchemaParameterName() const
Returns the name of the parent schema parameter, or an empty string if this is not set.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
static QgsProcessingParameterDatabaseTable * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
bool allowNewTableNames() const
Returns true if the parameter allows users to enter names for a new (non-existing) tables.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
void setAllowNewTableNames(bool allowed)
Sets whether the parameter allows users to enter names for a new (non-existing) tables.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
void setParentConnectionParameterName(const QString &name)
Sets the name of the parent connection parameter.
A datetime (or pure date or time) parameter for processing algorithms.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
void setMaximum(const QDateTime &maximum)
Sets the maximum value acceptable by the parameter.
static QgsProcessingParameterDateTime * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QDateTime minimum() const
Returns the minimum value acceptable by the parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString toolTip() const override
Returns a formatted tooltip for use with the parameter, which gives helpful information like paramete...
void setMinimum(const QDateTime &minimum)
Sets the minimum value acceptable by the parameter.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QDateTime maximum() const
Returns the maximum value acceptable by the parameter.
QgsProcessingParameterDateTime(const QString &name, const QString &description=QString(), Type type=DateTime, const QVariant &defaultValue=QVariant(), bool optional=false, const QDateTime &minValue=QDateTime(), const QDateTime &maxValue=QDateTime())
Constructor for QgsProcessingParameterDateTime.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
Type dataType() const
Returns the acceptable data type for the parameter.
void setDataType(Type type)
Sets the acceptable data type for the parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
Base class for the definition of processing parameters.
QgsProcessingAlgorithm * mAlgorithm
Pointer to algorithm which owns this parameter.
QVariant defaultValue() const
Returns the default value for the parameter.
QString help() const
Returns the help for the parameter.
virtual QString asScriptCode() const
Returns the parameter definition encoded in a string which can be used within a Processing script.
virtual QString toolTip() const
Returns a formatted tooltip for use with the parameter, which gives helpful information like paramete...
QgsProcessingAlgorithm * algorithm() const
Returns a pointer to the algorithm which owns this parameter.
QgsProcessingProvider * provider() const
Returns a pointer to the provider for the algorithm which owns this parameter.
virtual QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString description() const
Returns the description for the parameter.
QgsProcessingParameterDefinition(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, const QString &help=QString())
Constructor for QgsProcessingParameterDefinition.
QVariantMap mMetadata
Freeform metadata for parameter. Mostly used by widget wrappers to customize their appearance and beh...
QString mDescription
Parameter description.
virtual QString type() const =0
Unique parameter type name.
Flags flags() const
Returns any flags associated with the parameter.
virtual QVariantMap toVariantMap() const
Saves this parameter to a QVariantMap.
QString name() const
Returns the name of the parameter.
QVariant mDefault
Default value for parameter.
QVariant mGuiDefault
Default value for parameter in GUI.
virtual QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
virtual bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const
Checks whether the specified input value is acceptable for the parameter.
virtual bool fromVariantMap(const QVariantMap &map)
Restores this parameter to a QVariantMap.
A double numeric parameter for distance values.
void setParentParameterName(const QString &parentParameterName)
Sets the name of the parent layer parameter.
static QString typeName()
Returns the type name for the parameter class.
QString parentParameterName() const
Returns the name of the parent parameter, or an empty string if this is not set.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsProcessingParameterDistance * clone() const override
Creates a clone of the parameter definition.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString type() const override
Unique parameter type name.
QgsProcessingParameterDistance(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentParameterName=QString(), bool optional=false, double minValue=std::numeric_limits< double >::lowest()+1, double maxValue=std::numeric_limits< double >::max())
Constructor for QgsProcessingParameterDistance.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
void setUsesStaticStrings(bool usesStaticStrings)
Sets whether the parameter uses static (non-translated) string values for its enumeration choice list...
static QgsProcessingParameterEnum * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
bool allowMultiple() const
Returns true if the parameter allows multiple selected values.
QStringList options() const
Returns the list of acceptable options for the parameter.
QgsProcessingParameterEnum(const QString &name, const QString &description=QString(), const QStringList &options=QStringList(), bool allowMultiple=false, const QVariant &defaultValue=QVariant(), bool optional=false, bool usesStaticStrings=false)
Constructor for QgsProcessingParameterEnum.
void setOptions(const QStringList &options)
Sets the list of acceptable options for the parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool usesStaticStrings() const
Returns true if the parameter uses static (non-translated) string values for its enumeration choice l...
void setAllowMultiple(bool allowMultiple)
Sets whether the parameter allows multiple selected values.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
static QString typeName()
Returns the type name for the parameter class.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
An expression parameter for processing algorithms.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QgsProcessingParameterExpression * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
QgsProcessingParameterExpression(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentLayerParameterName=QString(), bool optional=false)
Constructor for QgsProcessingParameterExpression.
void setParentLayerParameterName(const QString &parentLayerParameterName)
Sets the name of the parent layer parameter.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString parentLayerParameterName() const
Returns the name of the parent layer parameter, or an empty string if this is not set.
static QString typeName()
Returns the type name for the parameter class.
A rectangular map extent parameter for processing algorithms.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterExtent(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterExtent.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QString typeName()
Returns the type name for the parameter class.
static QgsProcessingParameterExtent * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
A feature sink output for processing algorithms.
QString generateTemporaryDestination() const override
Generates a temporary destination value for this parameter.
static QgsProcessingParameterFeatureSink * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
bool hasGeometry() const
Returns true if sink is likely to include geometries.
QString type() const override
Unique parameter type name.
QgsProcessing::SourceType dataType() const
Returns the layer type for sinks associated with the parameter.
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
void setSupportsAppend(bool supportsAppend)
Sets whether the sink supports appending features to an existing table.
QgsProcessingParameterFeatureSink(const QString &name, const QString &description=QString(), QgsProcessing::SourceType type=QgsProcessing::TypeVectorAnyGeometry, const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true, bool supportsAppend=false)
Constructor for QgsProcessingParameterFeatureSink.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter.
void setDataType(QgsProcessing::SourceType type)
Sets the layer type for the sinks associated with the parameter.
bool supportsAppend() const
Returns true if the sink supports appending features to an existing table.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
static QString typeName()
Returns the type name for the parameter class.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
An input feature source (such as vector layers) parameter for processing algorithms.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterFeatureSource(const QString &name, const QString &description=QString(), const QList< int > &types=QList< int >(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterFeatureSource.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
static QString typeName()
Returns the type name for the parameter class.
QString type() const override
Unique parameter type name.
static QgsProcessingParameterFeatureSource * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
A vector layer or feature source field parameter for processing algorithms.
QgsProcessingParameterField(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentLayerParameterName=QString(), DataType type=Any, bool allowMultiple=false, bool optional=false, bool defaultToAllFields=false)
Constructor for QgsProcessingParameterField.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
QString parentLayerParameterName() const
Returns the name of the parent layer parameter, or an empty string if this is not set.
void setDataType(DataType type)
Sets the acceptable data type for the field.
void setParentLayerParameterName(const QString &parentLayerParameterName)
Sets the name of the parent layer parameter.
bool allowMultiple() const
Returns whether multiple field selections are permitted.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString type() const override
Unique parameter type name.
bool defaultToAllFields() const
Returns whether a parameter which allows multiple selections (see allowMultiple()) should automatical...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
static QString typeName()
Returns the type name for the parameter class.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
void setAllowMultiple(bool allowMultiple)
Sets whether multiple field selections are permitted.
@ DateTime
Accepts datetime fields.
@ Numeric
Accepts numeric fields.
DataType dataType() const
Returns the acceptable data type for the field.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
void setDefaultToAllFields(bool enabled)
Sets whether a parameter which allows multiple selections (see allowMultiple()) should automatically ...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QgsProcessingParameterField * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
static QgsProcessingParameterFileDestination * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterFileDestination(const QString &name, const QString &description=QString(), const QString &fileFilter=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterFileDestination.
static QString typeName()
Returns the type name for the parameter class.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString fileFilter() const
Returns the file filter string for file destinations compatible with this parameter.
void setFileFilter(const QString &filter)
Sets the file filter string for file destinations compatible with this parameter.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
An input file or folder parameter for processing algorithms.
QString extension() const
Returns any specified file extension for the parameter.
void setExtension(const QString &extension)
Sets a file extension for the parameter.
static QString typeName()
Returns the type name for the parameter class.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
void setFileFilter(const QString &filter)
Sets the file filter string for file destinations compatible with this parameter.
static QgsProcessingParameterFile * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition, Behavior behavior=File)
Creates a new parameter using the definition from a script code.
@ File
Parameter is a single file.
Behavior behavior() const
Returns the parameter behavior (e.g.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString fileFilter() const
Returns the file filter string for file destinations compatible with this parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingParameterFile(const QString &name, const QString &description=QString(), Behavior behavior=File, const QString &extension=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, const QString &fileFilter=QString())
Constructor for QgsProcessingParameterFile.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
A folder destination parameter, for specifying the destination path for a folder created by the algor...
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
static QgsProcessingParameterFolderDestination * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QString typeName()
Returns the type name for the parameter class.
QgsProcessingParameterFolderDestination(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterFolderDestination.
A geometry parameter for processing algorithms.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString type() const override
Unique parameter type name.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QgsProcessingParameterGeometry * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterGeometry(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, const QList< int > &geometryTypes=QList< int >())
Constructor for QgsProcessingParameterGeometry.
A print layout item parameter, allowing users to select a particular item from a print layout.
QString type() const override
Unique parameter type name.
static QgsProcessingParameterLayoutItem * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterLayoutItem(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentLayoutParameterName=QString(), int itemType=-1, bool optional=false)
Constructor for QgsProcessingParameterLayoutItem.
void setParentLayoutParameterName(const QString &name)
Sets the name of the parent layout parameter.
QString parentLayoutParameterName() const
Returns the name of the parent layout parameter, or an empty string if this is not set.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
static QString typeName()
Returns the type name for the parameter class.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
int itemType() const
Returns the acceptable item type, or -1 if any item type is allowed.
void setItemType(int type)
Sets the acceptable item type, or -1 if any item type is allowed.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
A print layout parameter, allowing users to select a print layout.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
static QgsProcessingParameterLayout * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QgsProcessingParameterLayout(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterLayout.
static QString typeName()
Returns the type name for the parameter class.
Can be inherited by parameters which require limits to their acceptable data types.
void setDataTypes(const QList< int > &types)
Sets the geometry types for sources acceptable by the parameter.
QgsProcessingParameterLimitedDataTypes(const QList< int > &types=QList< int >())
Constructor for QgsProcessingParameterLimitedDataTypes, with a list of acceptable data types.
QList< int > mDataTypes
List of acceptable data types for the parameter.
QList< int > dataTypes() const
Returns the geometry types for sources acceptable by the parameter.
A map layer parameter for processing algorithms.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
static QgsProcessingParameterMapLayer * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString type() const override
Unique parameter type name.
QgsProcessingParameterMapLayer(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, const QList< int > &types=QList< int >())
Constructor for QgsProcessingParameterMapLayer.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QString typeName()
Returns the type name for the parameter class.
A map theme parameter for processing algorithms, allowing users to select an existing map theme from ...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QgsProcessingParameterMapTheme * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterMapTheme(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterMapTheme.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
A table (matrix) parameter for processing algorithms.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QStringList headers() const
Returns a list of column headers (if set).
void setHeaders(const QStringList &headers)
Sets the list of column headers.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
void setHasFixedNumberRows(bool hasFixedNumberRows)
Sets whether the table has a fixed number of rows.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
void setNumberRows(int rows)
Sets the fixed number of rows in the table.
int numberRows() const
Returns the fixed number of rows in the table.
static QgsProcessingParameterMatrix * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QString typeName()
Returns the type name for the parameter class.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterMatrix(const QString &name, const QString &description=QString(), int numberRows=3, bool hasFixedNumberRows=false, const QStringList &headers=QStringList(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterMatrix.
bool hasFixedNumberRows() const
Returns whether the table has a fixed number of rows.
A mesh layer parameter for processing algorithms.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingParameterMeshLayer(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterMeshLayer.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QgsProcessingParameterMeshLayer * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
static QString typeName()
Returns the type name for the parameter class.
A parameter for processing algorithms which accepts multiple map layers.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
void setMinimumNumberInputs(int minimum)
Sets the minimum number of layers required for the parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QgsProcessingParameterMultipleLayers(const QString &name, const QString &description=QString(), QgsProcessing::SourceType layerType=QgsProcessing::TypeVectorAnyGeometry, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterMultipleLayers.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QString typeName()
Returns the type name for the parameter class.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
void setLayerType(QgsProcessing::SourceType type)
Sets the layer type for layers acceptable by the parameter.
static QgsProcessingParameterMultipleLayers * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessing::SourceType layerType() const
Returns the layer type for layers acceptable by the parameter.
QString type() const override
Unique parameter type name.
int minimumNumberInputs() const
Returns the minimum number of layers required for the parameter.
A numeric parameter for processing algorithms.
double minimum() const
Returns the minimum value acceptable by the parameter.
static QgsProcessingParameterNumber * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
void setMinimum(double minimum)
Sets the minimum value acceptable by the parameter.
void setMaximum(double maximum)
Sets the maximum value acceptable by the parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
double maximum() const
Returns the maximum value acceptable by the parameter.
QgsProcessingParameterNumber(const QString &name, const QString &description=QString(), Type type=Integer, const QVariant &defaultValue=QVariant(), bool optional=false, double minValue=std::numeric_limits< double >::lowest()+1, double maxValue=std::numeric_limits< double >::max())
Constructor for QgsProcessingParameterNumber.
void setDataType(Type type)
Sets the acceptable data type for the parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
Type dataType() const
Returns the acceptable data type for the parameter.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString toolTip() const override
Returns a formatted tooltip for use with the parameter, which gives helpful information like paramete...
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QString typeName()
Returns the type name for the parameter class.
A point parameter for processing algorithms.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QgsProcessingParameterPoint * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterPoint(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterPoint.
static QString typeName()
Returns the type name for the parameter class.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
A data provider connection parameter for processing algorithms, allowing users to select from availab...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterProviderConnection(const QString &name, const QString &description, const QString &provider, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterProviderConnection, for the specified provider type.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
static QgsProcessingParameterProviderConnection * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
A numeric range parameter for processing algorithms.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterNumber::Type dataType() const
Returns the acceptable data type for the range.
static QString typeName()
Returns the type name for the parameter class.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
void setDataType(QgsProcessingParameterNumber::Type dataType)
Sets the acceptable data type for the range.
QgsProcessingParameterRange(const QString &name, const QString &description=QString(), QgsProcessingParameterNumber::Type type=QgsProcessingParameterNumber::Integer, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterRange.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QgsProcessingParameterRange * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QgsProcessingParameterRasterDestination(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterRasterDestination.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QgsProcessingParameterRasterDestination * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter.
virtual QStringList supportedOutputRasterLayerExtensions() const
Returns a list of the raster format file extensions supported for this parameter.
static QString typeName()
Returns the type name for the parameter class.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
A raster layer parameter for processing algorithms.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QgsProcessingParameterRasterLayer * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QString typeName()
Returns the type name for the parameter class.
QgsProcessingParameterRasterLayer(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterRasterLayer.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
A double numeric parameter for map scale values.
QString type() const override
Unique parameter type name.
static QString typeName()
Returns the type name for the parameter class.
static QgsProcessingParameterScale * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterScale * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterScale(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterScale.
A string parameter for processing algorithms.
static QString typeName()
Returns the type name for the parameter class.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
void setMultiLine(bool multiLine)
Sets whether the parameter allows multiline strings.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
static QgsProcessingParameterString * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool multiLine() const
Returns true if the parameter allows multiline strings.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterString(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool multiLine=false, bool optional=false)
Constructor for QgsProcessingParameterString.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
Makes metadata of processing parameters available.
virtual QgsProcessingParameterDefinition * create(const QString &name) const =0
Creates a new parameter of this type.
A vector layer destination parameter, for specifying the destination path for a vector layer created ...
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QgsProcessing::SourceType dataType() const
Returns the layer type for this created vector layer.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString type() const override
Unique parameter type name.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
static QString typeName()
Returns the type name for the parameter class.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
void setDataType(QgsProcessing::SourceType type)
Sets the layer type for the created vector layer.
QgsProcessingParameterVectorDestination(const QString &name, const QString &description=QString(), QgsProcessing::SourceType type=QgsProcessing::TypeVectorAnyGeometry, const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterVectorDestination.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this parameter.
bool hasGeometry() const
Returns true if the created layer is likely to include geometries.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QgsProcessingParameterVectorDestination * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
A vector layer (with or without geometry) parameter for processing algorithms.
static QgsProcessingParameterVectorLayer * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString type() const override
Unique parameter type name.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterVectorLayer(const QString &name, const QString &description=QString(), const QList< int > &types=QList< int >(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterVectorLayer.
static QString typeName()
Returns the type name for the parameter class.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QList< QgsMapLayer * > parameterAsLayerList(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of map layers.
static QString descriptionFromName(const QString &name)
Creates an autogenerated parameter description from a parameter name.
static int parameterAsEnum(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a enum value.
static double parameterAsDouble(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static double value.
static QgsPointXY parameterAsPoint(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching definition to a point.
static QString parameterAsOutputLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a output layer destination.
static QgsPrintLayout * parameterAsLayout(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a print layout.
static QTime parameterAsTime(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static time value.
static QgsProcessingParameterDefinition * parameterFromVariantMap(const QVariantMap &map)
Creates a new QgsProcessingParameterDefinition using the configuration from a supplied variant map.
static QgsRectangle parameterAsExtent(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching definition to a rectangular extent.
static QgsCoordinateReferenceSystem parameterAsGeometryCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the coordinate reference system associated with a geometry parameter value.
static QString parameterAsEnumString(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static enum string.
static QList< double > parameterAsRange(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a range of values.
static QgsMapLayer * parameterAsLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint=QgsProcessingUtils::LayerHint::UnknownType)
Evaluates the parameter with matching definition to a map layer.
static QList< int > parameterAsInts(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of integer values.
static QString parameterAsConnectionName(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a connection name string.
static QgsProcessingFeatureSource * parameterAsSource(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a feature source.
static QString parameterAsFileOutput(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a file based output destination.
static QgsFeatureSink * parameterAsSink(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context, QString &destinationIdentifier, QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), const QVariantMap &createOptions=QVariantMap(), const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList())
Evaluates the parameter with matching definition to a feature sink.
static bool parameterAsBoolean(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static boolean value.
static QgsCoordinateReferenceSystem parameterAsPointCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the coordinate reference system associated with an point parameter value.
static QgsLayoutItem * parameterAsLayoutItem(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsPrintLayout *layout)
Evaluates the parameter with matching definition to a print layout item, taken from the specified lay...
static bool parameterAsBool(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static boolean value.
static QString parameterAsCompatibleSourceLayerPathAndLayerName(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat=QString("shp"), QgsProcessingFeedback *feedback=nullptr, QString *layerName=nullptr)
Evaluates the parameter with matching definition to a source vector layer file path and layer name of...
static QgsMeshLayer * parameterAsMeshLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition and value to a mesh layer.
static QString parameterAsCompatibleSourceLayerPath(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat=QString("shp"), QgsProcessingFeedback *feedback=nullptr)
Evaluates the parameter with matching definition to a source vector layer file path of compatible for...
static QgsProcessingParameterDefinition * parameterFromScriptCode(const QString &code)
Creates a new QgsProcessingParameterDefinition using the configuration from a supplied script code st...
static QColor parameterAsColor(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the color associated with an point parameter value, or an invalid color if the parameter was ...
static QgsVectorLayer * parameterAsVectorLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a vector layer.
static int parameterAsInt(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static integer value.
static QString parameterAsDatabaseTableName(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a database table name.
static QString parameterAsSchema(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a database schema name.
static QgsGeometry parameterAsGeometry(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching definition to a geometry.
static QString parameterAsExpression(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to an expression.
static QString parameterAsString(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static string value.
static QgsRasterLayer * parameterAsRasterLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a raster layer.
static QList< int > parameterAsEnums(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to list of enum values.
static QStringList parameterAsEnumStrings(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to list of static enum strings.
static QgsGeometry parameterAsExtentGeometry(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching definition to a rectangular extent, and returns a geometry cove...
static QStringList parameterAsFileList(const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of files (for QgsProcessingParameterMultip...
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
static QStringList parameterAsFields(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of fields.
static QgsCoordinateReferenceSystem parameterAsExtentCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the coordinate reference system associated with an extent parameter value.
static QDateTime parameterAsDateTime(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static datetime value.
static QString parameterAsFile(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a file/folder name.
static QDate parameterAsDate(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static date value.
static QVariantList parameterAsMatrix(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a matrix/table of values.
static QgsCoordinateReferenceSystem parameterAsCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a coordinate reference system.
Abstract base class for processing providers.
virtual bool isSupportedOutputValue(const QVariant &outputValue, const QgsProcessingDestinationParameter *parameter, QgsProcessingContext &context, QString &error) const
Returns true if the specified outputValue is of a supported file format for the given destination par...
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
static QString convertToCompatibleFormat(const QgsVectorLayer *layer, bool selectedFeaturesOnly, const QString &baseName, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingContext &context, QgsProcessingFeedback *feedback, long long featureLimit=-1)
Converts a source vector layer to a file path of a vector layer of compatible format.
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
static QString defaultVectorExtension()
Returns the default vector extension to use, in the absence of all other constraints (e....
static QString normalizeLayerSource(const QString &source)
Normalizes a layer source string for safe comparison across different operating system environments.
static QgsFeatureSink * createFeatureSink(QString &destination, QgsProcessingContext &context, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &createOptions=QVariantMap(), const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), QgsRemappingSinkDefinition *remappingDefinition=nullptr)
Creates a feature sink ready for adding features.
static QString encodeProviderKeyAndUri(const QString &providerKey, const QString &uri)
Encodes a provider key and layer uri to a single string, for use with decodeProviderKeyAndUri()
LayerHint
Layer type hints.
@ Vector
Vector layer type.
@ Mesh
Mesh layer type, since QGIS 3.6.
@ Raster
Raster layer type.
@ UnknownType
Unknown layer type.
static QString generateTempFilename(const QString &basename)
Returns a temporary filename for a given file, putting it into a temporary folder (creating that fold...
static QgsProcessingFeatureSource * variantToSource(const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue=QVariant())
Converts a variant value to a new feature source.
static QString convertToCompatibleFormatAndLayerName(const QgsVectorLayer *layer, bool selectedFeaturesOnly, const QString &baseName, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingContext &context, QgsProcessingFeedback *feedback, QString &layerName, long long featureLimit=-1)
Converts a source vector layer to a file path and layer name of a vector layer of compatible format.
static QgsCoordinateReferenceSystem variantToCrs(const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue=QVariant())
Converts a variant value to a coordinate reference system.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType)
Interprets a string as a map layer within the supplied context.
static QString defaultRasterExtension()
Returns the default raster extension to use, in the absence of all other constraints (e....
static const QString TEMPORARY_OUTPUT
Constant used to indicate that a Processing algorithm output should be a temporary layer/file.
Definition: qgsprocessing.h:99
PythonOutputType
Available Python output types.
Definition: qgsprocessing.h:59
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
Definition: qgsprocessing.h:60
SourceType
Data source types enum.
Definition: qgsprocessing.h:45
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:49
@ TypeMapLayer
Any map layer type (raster or vector or mesh)
Definition: qgsprocessing.h:46
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:50
@ TypeFile
Files (i.e. non map layer sources, such as text files)
Definition: qgsprocessing.h:52
@ TypeMesh
Mesh layers.
Definition: qgsprocessing.h:54
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:53
@ TypeRaster
Raster layers.
Definition: qgsprocessing.h:51
@ TypeVectorPoint
Vector point layers.
Definition: qgsprocessing.h:48
@ TypeVectorAnyGeometry
Any vector layer with geometry.
Definition: qgsprocessing.h:47
static QString sourceTypeToString(SourceType type)
Converts a source type to a string representation.
Definition: qgsprocessing.h:68
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:99
const QgsLayoutManager * layoutManager() const
Returns the project's layout manager, which manages print layouts, atlases and reports within the pro...
A store for object properties.
Definition: qgsproperty.h:232
@ StaticProperty
Static property (QgsStaticProperty)
Definition: qgsproperty.h:239
QString asExpression() const
Returns an expression string representing the state of the property, or an empty string if the proper...
QString valueAsString(const QgsExpressionContext &context, const QString &defaultString=QString(), bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a string.
QVariant value(const QgsExpressionContext &context, const QVariant &defaultValue=QVariant(), bool *ok=nullptr) const
Calculates the current value of the property, including any transforms which are set for the property...
QVariant toVariant() const
Saves this property to a QVariantMap, wrapped in a QVariant.
bool loadVariant(const QVariant &property)
Loads this property from a QVariantMap, wrapped in a QVariant.
QVariant staticValue() const
Returns the current static value for the property.
Type propertyType() const
Returns the property type.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QString fileVectorFilters() const
Returns a file filter string for supported vector files.
QString fileRasterFilters() const
Returns a file filter string for supported raster files.
QString fileMeshFilters() const
Returns a file filter string for supported mesh files.
static QStringList supportedFormatExtensions(RasterFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats.
Represents a raster layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:172
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:162
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:177
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:447
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
A QgsGeometry with associated coordinate reference system.
static QgsReferencedGeometry fromReferencedPointXY(const QgsReferencedPointXY &point)
Construct a new QgsReferencedGeometry from referenced point.
static QgsReferencedGeometry fromReferencedRect(const QgsReferencedRectangle &rectangle)
Construct a new QgsReferencedGeometry from referenced rectangle.
A QgsPointXY with associated coordinate reference system.
A QgsRectangle with associated coordinate reference system.
Defines the parameters used to remap features when creating a QgsRemappingProxyFeatureSink.
static QColor parseColorWithAlpha(const QString &colorStr, bool &containsAlpha, bool strictEval=false)
Attempts to parse a string as a color using a variety of common formats, including hex codes,...
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:68
@ DistanceUnknownUnit
Unknown distance unit.
Definition: qgsunittypes.h:78
static QStringList supportedFormatExtensions(VectorFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats, e.g "shp", "gpkg".
Represents a vector layer which manages a vector based data sets.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:276
QString parameterAsCompatibleSourceLayerPathInternal(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback, QString *layerName)
QString createAllMapLayerFileFilter()
const QgsCoordinateReferenceSystem & crs