QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
33 #include <functional>
34 
35 
37 {
38  QVariantMap map;
39  map.insert( QStringLiteral( "sink" ), sink.toVariant() );
40  map.insert( QStringLiteral( "create_options" ), createOptions );
41  return map;
42 }
43 
45 {
46  sink.loadVariant( map.value( QStringLiteral( "sink" ) ) );
47  createOptions = map.value( QStringLiteral( "create_options" ) ).toMap();
48  return true;
49 }
50 
51 bool QgsProcessingParameters::isDynamic( const QVariantMap &parameters, const QString &name )
52 {
53  QVariant val = parameters.value( name );
54  if ( val.canConvert<QgsProperty>() )
55  return val.value< QgsProperty >().propertyType() != QgsProperty::StaticProperty;
56  else
57  return false;
58 }
59 
60 QString QgsProcessingParameters::parameterAsString( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
61 {
62  if ( !definition )
63  return QString();
64 
65  return parameterAsString( definition, parameters.value( definition->name() ), context );
66 }
67 
68 QString QgsProcessingParameters::parameterAsString( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
69 {
70  if ( !definition )
71  return QString();
72 
73  QVariant val = value;
74  if ( val.canConvert<QgsProperty>() )
75  return val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
76 
77  if ( !val.isValid() )
78  {
79  // fall back to default
80  val = definition->defaultValue();
81  }
82 
83  return val.toString();
84 }
85 
86 QString QgsProcessingParameters::parameterAsExpression( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
87 {
88  if ( !definition )
89  return QString();
90 
91  return parameterAsExpression( definition, parameters.value( definition->name() ), context );
92 }
93 
94 QString QgsProcessingParameters::parameterAsExpression( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
95 {
96  if ( !definition )
97  return QString();
98 
99  QVariant val = value;
100  if ( val.canConvert<QgsProperty>() )
101  return val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
102 
103  if ( val.isValid() && !val.toString().isEmpty() )
104  {
105  QgsExpression e( val.toString() );
106  if ( e.isValid() )
107  return val.toString();
108  }
109 
110  // fall back to default
111  return definition->defaultValue().toString();
112 }
113 
114 double QgsProcessingParameters::parameterAsDouble( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
115 {
116  if ( !definition )
117  return 0;
118 
119  return parameterAsDouble( definition, parameters.value( definition->name() ), context );
120 }
121 
122 double QgsProcessingParameters::parameterAsDouble( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
123 {
124  if ( !definition )
125  return 0;
126 
127  QVariant val = value;
128  if ( val.canConvert<QgsProperty>() )
129  return val.value< QgsProperty >().valueAsDouble( context.expressionContext(), definition->defaultValue().toDouble() );
130 
131  bool ok = false;
132  double res = val.toDouble( &ok );
133  if ( ok )
134  return res;
135 
136  // fall back to default
137  val = definition->defaultValue();
138  return val.toDouble();
139 }
140 
141 int QgsProcessingParameters::parameterAsInt( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
142 {
143  if ( !definition )
144  return 0;
145 
146  return parameterAsInt( definition, parameters.value( definition->name() ), context );
147 }
148 
149 int QgsProcessingParameters::parameterAsInt( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
150 {
151  if ( !definition )
152  return 0;
153 
154  QVariant val = value;
155  if ( val.canConvert<QgsProperty>() )
156  return val.value< QgsProperty >().valueAsInt( context.expressionContext(), definition->defaultValue().toInt() );
157 
158  bool ok = false;
159  double dbl = val.toDouble( &ok );
160  if ( !ok )
161  {
162  // fall back to default
163  val = definition->defaultValue();
164  dbl = val.toDouble( &ok );
165  }
166 
167  //String representations of doubles in QVariant will not convert to int
168  //work around this by first converting to double, and then checking whether the double is convertible to int
169  if ( ok )
170  {
171  double round = std::round( dbl );
172  if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
173  {
174  //double too large to fit in int
175  return 0;
176  }
177  return static_cast< int >( std::round( dbl ) );
178  }
179 
180  return val.toInt();
181 }
182 
183 QList< int > QgsProcessingParameters::parameterAsInts( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
184 {
185  if ( !definition )
186  return QList< int >();
187 
188  return parameterAsInts( definition, parameters.value( definition->name() ), context );
189 }
190 
191 QList< int > QgsProcessingParameters::parameterAsInts( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
192 {
193  if ( !definition )
194  return QList< int >();
195 
196  QList< int > resultList;
197  QVariant val = value;
198  if ( val.isValid() )
199  {
200  if ( val.canConvert<QgsProperty>() )
201  resultList << val.value< QgsProperty >().valueAsInt( context.expressionContext(), definition->defaultValue().toInt() );
202  else if ( val.type() == QVariant::List )
203  {
204  QVariantList list = val.toList();
205  for ( auto it = list.constBegin(); it != list.constEnd(); ++it )
206  resultList << it->toInt();
207  }
208  else
209  {
210  QStringList parts = val.toString().split( ';' );
211  for ( auto it = parts.constBegin(); it != parts.constEnd(); ++it )
212  resultList << it->toInt();
213  }
214  }
215 
216  if ( ( resultList.isEmpty() || resultList.at( 0 ) == 0 ) )
217  {
218  resultList.clear();
219  // check default
220  if ( definition->defaultValue().isValid() )
221  {
222  if ( definition->defaultValue().type() == QVariant::List )
223  {
224  QVariantList list = definition->defaultValue().toList();
225  for ( auto it = list.constBegin(); it != list.constEnd(); ++it )
226  resultList << it->toInt();
227  }
228  else
229  {
230  QStringList parts = definition->defaultValue().toString().split( ';' );
231  for ( auto it = parts.constBegin(); it != parts.constEnd(); ++it )
232  resultList << it->toInt();
233  }
234  }
235  }
236 
237  return resultList;
238 }
239 
240 int QgsProcessingParameters::parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
241 {
242  if ( !definition )
243  return 0;
244 
245  return parameterAsEnum( definition, parameters.value( definition->name() ), context );
246 }
247 
248 int QgsProcessingParameters::parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
249 {
250  if ( !definition )
251  return 0;
252 
253  int val = parameterAsInt( definition, value, context );
254  const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
255  if ( enumDef && val >= enumDef->options().size() )
256  {
257  return enumDef->defaultValue().toInt();
258  }
259  return val;
260 }
261 
262 QList<int> QgsProcessingParameters::parameterAsEnums( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
263 {
264  if ( !definition )
265  return QList<int>();
266 
267  QVariantList resultList;
268  return parameterAsEnums( definition, parameters.value( definition->name() ), context );
269 }
270 
271 QList<int> QgsProcessingParameters::parameterAsEnums( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
272 {
273  if ( !definition )
274  return QList<int>();
275 
276  QVariantList resultList;
277  QVariant val = value;
278  if ( val.canConvert<QgsProperty>() )
279  resultList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
280  else if ( val.type() == QVariant::List )
281  {
282  Q_FOREACH ( const QVariant &var, val.toList() )
283  resultList << var;
284  }
285  else if ( val.type() == QVariant::String )
286  {
287  Q_FOREACH ( const QString &var, val.toString().split( ',' ) )
288  resultList << var;
289  }
290  else
291  resultList << val;
292 
293  if ( resultList.isEmpty() )
294  return QList< int >();
295 
296  if ( ( !val.isValid() || !resultList.at( 0 ).isValid() ) && definition )
297  {
298  resultList.clear();
299  // check default
300  if ( definition->defaultValue().type() == QVariant::List )
301  {
302  Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
303  resultList << var;
304  }
305  else if ( definition->defaultValue().type() == QVariant::String )
306  {
307  Q_FOREACH ( const QString &var, definition->defaultValue().toString().split( ',' ) )
308  resultList << var;
309  }
310  else
311  resultList << definition->defaultValue();
312  }
313 
314  QList< int > result;
315  const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
316  Q_FOREACH ( const QVariant &var, resultList )
317  {
318  int resInt = var.toInt();
319  if ( !enumDef || resInt < enumDef->options().size() )
320  {
321  result << resInt;
322  }
323  }
324  return result;
325 }
326 
327 bool QgsProcessingParameters::parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
328 {
329  if ( !definition )
330  return false;
331 
332  return parameterAsBool( definition, parameters.value( definition->name() ), context );
333 }
334 
335 bool QgsProcessingParameters::parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
336 {
337  if ( !definition )
338  return false;
339 
340  QVariant def = definition->defaultValue();
341 
342  QVariant val = value;
343  if ( val.canConvert<QgsProperty>() )
344  return val.value< QgsProperty >().valueAsBool( context.expressionContext(), def.toBool() );
345  else if ( val.isValid() )
346  return val.toBool();
347  else
348  return def.toBool();
349 }
350 
351 QgsFeatureSink *QgsProcessingParameters::parameterAsSink( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsFields &fields,
353  QgsProcessingContext &context, QString &destinationIdentifier, QgsFeatureSink::SinkFlags sinkFlags )
354 {
355  QVariant val;
356  if ( definition )
357  {
358  val = parameters.value( definition->name() );
359  }
360 
361  return parameterAsSink( definition, val, fields, geometryType, crs, context, destinationIdentifier, sinkFlags );
362 }
363 
364 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 )
365 {
366  QVariant val = value;
367 
368  QgsProject *destinationProject = nullptr;
369  QString destName;
370  QVariantMap createOptions;
371  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
372  {
373  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
375  destinationProject = fromVar.destinationProject;
376  createOptions = fromVar.createOptions;
377 
378  val = fromVar.sink;
379  destName = fromVar.destinationName;
380  }
381 
382  QString dest;
383  if ( val.canConvert<QgsProperty>() )
384  {
385  dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
386  }
387  else if ( !val.isValid() || val.toString().isEmpty() )
388  {
389  if ( definition && definition->flags() & QgsProcessingParameterDefinition::FlagOptional && !definition->defaultValue().isValid() )
390  {
391  // unset, optional sink, no default => no sink
392  return nullptr;
393  }
394  // fall back to default
395  dest = definition->defaultValue().toString();
396  }
397  else
398  {
399  dest = val.toString();
400  }
401 
402  if ( dest.isEmpty() )
403  return nullptr;
404 
405  std::unique_ptr< QgsFeatureSink > sink( QgsProcessingUtils::createFeatureSink( dest, context, fields, geometryType, crs, createOptions, sinkFlags ) );
406  destinationIdentifier = dest;
407 
408  if ( destinationProject )
409  {
410  if ( destName.isEmpty() && definition )
411  {
412  destName = definition->description();
413  }
414  QString outputName;
415  if ( definition )
416  outputName = definition->name();
417  context.addLayerToLoadOnCompletion( destinationIdentifier, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName, QgsProcessingUtils::Vector ) );
418  }
419 
420  return sink.release();
421 }
422 
424 {
425  if ( !definition )
426  return nullptr;
427 
428  return parameterAsSource( definition, parameters.value( definition->name() ), context );
429 }
430 
432 {
433  if ( !definition )
434  return nullptr;
435 
436  return QgsProcessingUtils::variantToSource( value, context, definition->defaultValue() );
437 }
438 
439 QString QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback )
440 {
441  if ( !definition )
442  return QString();
443 
444  QVariant val = parameters.value( definition->name() );
445 
446  bool selectedFeaturesOnly = false;
447  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
448  {
449  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
451  selectedFeaturesOnly = fromVar.selectedFeaturesOnly;
452  val = fromVar.source;
453  }
454  else if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
455  {
456  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
458  val = fromVar.sink;
459  }
460 
461  if ( val.canConvert<QgsProperty>() )
462  {
463  val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
464  }
465 
466  QgsVectorLayer *vl = nullptr;
467  vl = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) );
468 
469  if ( !vl )
470  {
471  QString layerRef;
472  if ( val.canConvert<QgsProperty>() )
473  {
474  layerRef = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
475  }
476  else if ( !val.isValid() || val.toString().isEmpty() )
477  {
478  // fall back to default
479  val = definition->defaultValue();
480 
481  // default value may be a vector layer
482  vl = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) );
483  if ( !vl )
484  layerRef = definition->defaultValue().toString();
485  }
486  else
487  {
488  layerRef = val.toString();
489  }
490 
491  if ( !vl )
492  {
493  if ( layerRef.isEmpty() )
494  return QString();
495 
496  vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context, true, QgsProcessingUtils::Vector ) );
497  }
498  }
499 
500  if ( !vl )
501  return QString();
502 
503  return QgsProcessingUtils::convertToCompatibleFormat( vl, selectedFeaturesOnly, definition->name(),
504  compatibleFormats, preferredFormat, context, feedback );
505 }
506 
507 
509 {
510  if ( !definition )
511  return nullptr;
512 
513  return parameterAsLayer( definition, parameters.value( definition->name() ), context );
514 }
515 
517 {
518  if ( !definition )
519  return nullptr;
520 
521  QVariant val = value;
522  if ( val.canConvert<QgsProperty>() )
523  {
524  val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
525  }
526 
527  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
528  {
529  return layer;
530  }
531 
532  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
533  {
534  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
536  val = fromVar.sink;
537  }
538 
539  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
540  {
541  val = val.value< QgsProperty >().staticValue();
542  }
543 
544  if ( !val.isValid() || val.toString().isEmpty() )
545  {
546  // fall back to default
547  val = definition->defaultValue();
548  }
549 
550  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
551  {
552  return layer;
553  }
554 
555  QString layerRef = val.toString();
556  if ( layerRef.isEmpty() )
557  layerRef = definition->defaultValue().toString();
558 
559  if ( layerRef.isEmpty() )
560  return nullptr;
561 
562  return QgsProcessingUtils::mapLayerFromString( layerRef, context );
563 }
564 
566 {
567  return qobject_cast< QgsRasterLayer *>( parameterAsLayer( definition, parameters, context ) );
568 }
569 
571 {
572  return qobject_cast< QgsRasterLayer *>( parameterAsLayer( definition, value, context ) );
573 }
574 
575 QString QgsProcessingParameters::parameterAsOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
576 {
577  QVariant val;
578  if ( definition )
579  {
580  val = parameters.value( definition->name() );
581  }
582  return parameterAsOutputLayer( definition, val, context );
583 }
584 
586 {
587  QVariant val = value;
588 
589  QgsProject *destinationProject = nullptr;
590  QVariantMap createOptions;
591  QString destName;
592  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
593  {
594  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
596  destinationProject = fromVar.destinationProject;
597  createOptions = fromVar.createOptions;
598  val = fromVar.sink;
599  destName = fromVar.destinationName;
600  }
601 
602  QString dest;
603  if ( val.canConvert<QgsProperty>() )
604  {
605  dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
606  }
607  else if ( definition && ( !val.isValid() || val.toString().isEmpty() ) )
608  {
609  // fall back to default
610  dest = definition->defaultValue().toString();
611  }
612  else
613  {
614  dest = val.toString();
615  }
616 
617  if ( destinationProject )
618  {
619  QString outputName;
620  if ( destName.isEmpty() && definition )
621  {
622  destName = definition->description();
623  }
624  if ( definition )
625  outputName = definition->name();
626 
629  layerTypeHint = QgsProcessingUtils::Vector;
630  else if ( definition->type() == QgsProcessingParameterRasterDestination::typeName() )
631  layerTypeHint = QgsProcessingUtils::Raster;
632 
633  context.addLayerToLoadOnCompletion( dest, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName, layerTypeHint ) );
634  }
635 
636  return dest;
637 }
638 
639 QString QgsProcessingParameters::parameterAsFileOutput( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
640 {
641  QVariant val;
642  if ( definition )
643  {
644  val = parameters.value( definition->name() );
645  }
646  return parameterAsFileOutput( definition, val, context );
647 }
648 
650 {
651  QVariant val = value;
652 
653  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
654  {
655  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
657  val = fromVar.sink;
658  }
659 
660  QString dest;
661  if ( val.canConvert<QgsProperty>() )
662  {
663  dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
664  }
665  else if ( !val.isValid() || val.toString().isEmpty() )
666  {
667  // fall back to default
668  dest = definition->defaultValue().toString();
669  }
670  else
671  {
672  dest = val.toString();
673  }
674 
675  return dest;
676 }
677 
679 {
680  return qobject_cast< QgsVectorLayer *>( parameterAsLayer( definition, parameters, context ) );
681 }
682 
684 {
685  return qobject_cast< QgsVectorLayer *>( parameterAsLayer( definition, value, context ) );
686 }
687 
689 {
690  if ( !definition )
692 
693  return parameterAsCrs( definition, parameters.value( definition->name() ), context );
694 }
695 
697 {
698  if ( !definition )
700 
701  QVariant val = value;
702 
703  if ( val.canConvert<QgsCoordinateReferenceSystem>() )
704  {
705  // input is a QgsCoordinateReferenceSystem - done!
706  return val.value< QgsCoordinateReferenceSystem >();
707  }
708  else if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
709  {
710  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
712  val = fromVar.source;
713  }
714  else if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
715  {
716  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
718  val = fromVar.sink;
719  }
720 
721  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
722  {
723  val = val.value< QgsProperty >().staticValue();
724  }
725 
726  // maybe a map layer
727  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
728  return layer->crs();
729 
730  if ( val.canConvert<QgsProperty>() )
731  val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
732 
733  if ( !val.isValid() )
734  {
735  // fall back to default
736  val = definition->defaultValue();
737  }
738 
739  QString crsText = val.toString();
740  if ( crsText.isEmpty() )
741  crsText = definition->defaultValue().toString();
742 
743  if ( crsText.isEmpty() )
745 
746  // maybe special string
747  if ( context.project() && crsText.compare( QLatin1String( "ProjectCrs" ), Qt::CaseInsensitive ) == 0 )
748  return context.project()->crs();
749 
750  // maybe a map layer reference
751  if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( crsText, context ) )
752  return layer->crs();
753 
754  // else CRS from string
756  crs.createFromString( crsText );
757  return crs;
758 }
759 
762 {
763  if ( !definition )
764  return QgsRectangle();
765 
766  return parameterAsExtent( definition, parameters.value( definition->name() ), context, crs );
767 }
768 
770 {
771  if ( !definition )
772  return QgsRectangle();
773 
774  QVariant val = value;
775 
776  if ( val.canConvert< QgsRectangle >() )
777  {
778  return val.value<QgsRectangle>();
779  }
780  if ( val.canConvert< QgsReferencedRectangle >() )
781  {
783  if ( crs.isValid() && rr.crs().isValid() && crs != rr.crs() )
784  {
785  QgsCoordinateTransform ct( rr.crs(), crs, context.project() );
786  try
787  {
788  return ct.transformBoundingBox( rr );
789  }
790  catch ( QgsCsException & )
791  {
792  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
793  }
794  }
795  return rr;
796  }
797 
798  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
799  {
800  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
802  val = fromVar.source;
803  }
804  else if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
805  {
806  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
808  val = fromVar.sink;
809  }
810 
811  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
812  {
813  val = val.value< QgsProperty >().staticValue();
814  }
815 
816  // maybe parameter is a direct layer value?
817  QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) );
818 
819  QString rectText;
820  if ( val.canConvert<QgsProperty>() )
821  rectText = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
822  else
823  rectText = val.toString();
824 
825  if ( rectText.isEmpty() && !layer )
826  return QgsRectangle();
827 
828  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
829  QRegularExpressionMatch match = rx.match( rectText );
830  if ( match.hasMatch() )
831  {
832  bool xMinOk = false;
833  double xMin = match.captured( 1 ).toDouble( &xMinOk );
834  bool xMaxOk = false;
835  double xMax = match.captured( 2 ).toDouble( &xMaxOk );
836  bool yMinOk = false;
837  double yMin = match.captured( 3 ).toDouble( &yMinOk );
838  bool yMaxOk = false;
839  double yMax = match.captured( 4 ).toDouble( &yMaxOk );
840  if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
841  {
842  QgsRectangle rect( xMin, yMin, xMax, yMax );
843  QgsCoordinateReferenceSystem rectCrs( match.captured( 5 ) );
844  if ( crs.isValid() && rectCrs.isValid() && crs != rectCrs )
845  {
846  QgsCoordinateTransform ct( rectCrs, crs, context.project() );
847  try
848  {
849  return ct.transformBoundingBox( rect );
850  }
851  catch ( QgsCsException & )
852  {
853  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
854  }
855  }
856  return rect;
857  }
858  }
859 
860  // try as layer extent
861  if ( !layer )
862  layer = QgsProcessingUtils::mapLayerFromString( rectText, context );
863 
864  if ( layer )
865  {
866  QgsRectangle rect = layer->extent();
867  if ( crs.isValid() && layer->crs().isValid() && crs != layer->crs() )
868  {
869  QgsCoordinateTransform ct( layer->crs(), crs, context.project() );
870  try
871  {
872  return ct.transformBoundingBox( rect );
873  }
874  catch ( QgsCsException & )
875  {
876  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
877  }
878  }
879  return rect;
880  }
881  return QgsRectangle();
882 }
883 
885 {
886  if ( !definition )
887  return QgsGeometry();
888 
889  QVariant val = parameters.value( definition->name() );
890 
891  if ( val.canConvert< QgsReferencedRectangle >() )
892  {
895  if ( crs.isValid() && rr.crs().isValid() && crs != rr.crs() )
896  {
897  g = g.densifyByCount( 20 );
898  QgsCoordinateTransform ct( rr.crs(), crs, context.project() );
899  try
900  {
901  g.transform( ct );
902  }
903  catch ( QgsCsException & )
904  {
905  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
906  }
907  return g;
908  }
909  }
910 
911  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
912  {
913  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
915  val = fromVar.source;
916  }
917  else if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
918  {
919  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
921  val = fromVar.sink;
922  }
923 
924  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
925  {
926  val = val.value< QgsProperty >().staticValue();
927  }
928 
929  QString rectText;
930  if ( val.canConvert<QgsProperty>() )
931  rectText = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
932  else
933  rectText = val.toString();
934 
935  if ( !rectText.isEmpty() )
936  {
937  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
938  QRegularExpressionMatch match = rx.match( rectText );
939  if ( match.hasMatch() )
940  {
941  bool xMinOk = false;
942  double xMin = match.captured( 1 ).toDouble( &xMinOk );
943  bool xMaxOk = false;
944  double xMax = match.captured( 2 ).toDouble( &xMaxOk );
945  bool yMinOk = false;
946  double yMin = match.captured( 3 ).toDouble( &yMinOk );
947  bool yMaxOk = false;
948  double yMax = match.captured( 4 ).toDouble( &yMaxOk );
949  if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
950  {
951  QgsRectangle rect( xMin, yMin, xMax, yMax );
952  QgsCoordinateReferenceSystem rectCrs( match.captured( 5 ) );
954  if ( crs.isValid() && rectCrs.isValid() && crs != rectCrs )
955  {
956  g = g.densifyByCount( 20 );
957  QgsCoordinateTransform ct( rectCrs, crs, context.project() );
958  try
959  {
960  g.transform( ct );
961  }
962  catch ( QgsCsException & )
963  {
964  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
965  }
966  return g;
967  }
968  }
969  }
970  }
971 
972  // try as layer extent
973 
974  // maybe parameter is a direct layer value?
975  QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) );
976  if ( !layer )
977  layer = QgsProcessingUtils::mapLayerFromString( rectText, context );
978 
979  if ( layer )
980  {
981  QgsRectangle rect = layer->extent();
983  if ( crs.isValid() && layer->crs().isValid() && crs != layer->crs() )
984  {
985  g = g.densifyByCount( 20 );
986  QgsCoordinateTransform ct( layer->crs(), crs, context.project() );
987  try
988  {
989  g.transform( ct );
990  }
991  catch ( QgsCsException & )
992  {
993  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
994  }
995  }
996  return g;
997  }
998 
999  return QgsGeometry::fromRect( parameterAsExtent( definition, parameters, context, crs ) );
1000 }
1001 
1003 {
1004  QVariant val = parameters.value( definition->name() );
1005 
1006  if ( val.canConvert< QgsReferencedRectangle >() )
1007  {
1009  if ( rr.crs().isValid() )
1010  {
1011  return rr.crs();
1012  }
1013  }
1014 
1015  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
1016  {
1017  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
1019  val = fromVar.source;
1020  }
1021  else if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
1022  {
1023  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1025  val = fromVar.sink;
1026  }
1027 
1028  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
1029  {
1030  val = val.value< QgsProperty >().staticValue();
1031  }
1032 
1033  QString valueAsString;
1034  if ( val.canConvert<QgsProperty>() )
1035  valueAsString = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1036  else
1037  valueAsString = val.toString();
1038 
1039  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
1040 
1041  QRegularExpressionMatch match = rx.match( valueAsString );
1042  if ( match.hasMatch() )
1043  {
1044  QgsCoordinateReferenceSystem crs( match.captured( 5 ) );
1045  if ( crs.isValid() )
1046  return crs;
1047  }
1048 
1049  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
1050  {
1051  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
1053  val = fromVar.source;
1054  }
1055  else if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
1056  {
1057  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1059  val = fromVar.sink;
1060  }
1061 
1062  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
1063  {
1064  val = val.value< QgsProperty >().staticValue();
1065  }
1066 
1067  // try as layer crs
1068  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
1069  return layer->crs();
1070  else if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( valueAsString, context ) )
1071  return layer->crs();
1072 
1073  if ( context.project() )
1074  return context.project()->crs();
1075  else
1077 }
1078 
1080 {
1081  if ( !definition )
1082  return QgsPointXY();
1083 
1084  return parameterAsPoint( definition, parameters.value( definition->name() ), context, crs );
1085 }
1086 
1088 {
1089  if ( !definition )
1090  return QgsPointXY();
1091 
1092  QVariant val = value;
1093  if ( val.canConvert< QgsPointXY >() )
1094  {
1095  return val.value<QgsPointXY>();
1096  }
1097  if ( val.canConvert< QgsReferencedPointXY >() )
1098  {
1099  QgsReferencedPointXY rp = val.value<QgsReferencedPointXY>();
1100  if ( crs.isValid() && rp.crs().isValid() && crs != rp.crs() )
1101  {
1102  QgsCoordinateTransform ct( rp.crs(), crs, context.project() );
1103  try
1104  {
1105  return ct.transform( rp );
1106  }
1107  catch ( QgsCsException & )
1108  {
1109  QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
1110  }
1111  }
1112  return rp;
1113  }
1114 
1115  QString pointText = parameterAsString( definition, value, context );
1116  if ( pointText.isEmpty() )
1117  pointText = definition->defaultValue().toString();
1118 
1119  if ( pointText.isEmpty() )
1120  return QgsPointXY();
1121 
1122  QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
1123 
1124  QString valueAsString = parameterAsString( definition, value, context );
1125  QRegularExpressionMatch match = rx.match( valueAsString );
1126  if ( match.hasMatch() )
1127  {
1128  bool xOk = false;
1129  double x = match.captured( 1 ).toDouble( &xOk );
1130  bool yOk = false;
1131  double y = match.captured( 2 ).toDouble( &yOk );
1132 
1133  if ( xOk && yOk )
1134  {
1135  QgsPointXY pt( x, y );
1136 
1137  QgsCoordinateReferenceSystem pointCrs( match.captured( 3 ) );
1138  if ( crs.isValid() && pointCrs.isValid() && crs != pointCrs )
1139  {
1140  QgsCoordinateTransform ct( pointCrs, crs, context.project() );
1141  try
1142  {
1143  return ct.transform( pt );
1144  }
1145  catch ( QgsCsException & )
1146  {
1147  QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
1148  }
1149  }
1150  return pt;
1151  }
1152  }
1153 
1154  return QgsPointXY();
1155 }
1156 
1158 {
1159  QVariant val = parameters.value( definition->name() );
1160 
1161  if ( val.canConvert< QgsReferencedPointXY >() )
1162  {
1163  QgsReferencedPointXY rr = val.value<QgsReferencedPointXY>();
1164  if ( rr.crs().isValid() )
1165  {
1166  return rr.crs();
1167  }
1168  }
1169 
1170  QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
1171 
1172  QString valueAsString = parameterAsString( definition, parameters, context );
1173  QRegularExpressionMatch match = rx.match( valueAsString );
1174  if ( match.hasMatch() )
1175  {
1176  QgsCoordinateReferenceSystem crs( match.captured( 3 ) );
1177  if ( crs.isValid() )
1178  return crs;
1179  }
1180 
1181  if ( context.project() )
1182  return context.project()->crs();
1183  else
1185 }
1186 
1187 QString QgsProcessingParameters::parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1188 {
1189  if ( !definition )
1190  return QString();
1191 
1192  QString fileText = parameterAsString( definition, parameters, context );
1193  if ( fileText.isEmpty() )
1194  fileText = definition->defaultValue().toString();
1195  return fileText;
1196 }
1197 
1198 QString QgsProcessingParameters::parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1199 {
1200  if ( !definition )
1201  return QString();
1202 
1203  QString fileText = parameterAsString( definition, value, context );
1204  if ( fileText.isEmpty() )
1205  fileText = definition->defaultValue().toString();
1206  return fileText;
1207 }
1208 
1209 QVariantList QgsProcessingParameters::parameterAsMatrix( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1210 {
1211  if ( !definition )
1212  return QVariantList();
1213 
1214  return parameterAsMatrix( definition, parameters.value( definition->name() ), context );
1215 }
1216 
1217 QVariantList QgsProcessingParameters::parameterAsMatrix( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1218 {
1219  if ( !definition )
1220  return QVariantList();
1221 
1222  QString resultString;
1223  QVariant val = value;
1224  if ( val.canConvert<QgsProperty>() )
1225  resultString = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1226  else if ( val.type() == QVariant::List )
1227  return val.toList();
1228  else
1229  resultString = val.toString();
1230 
1231  if ( resultString.isEmpty() )
1232  {
1233  // check default
1234  if ( definition->defaultValue().type() == QVariant::List )
1235  return definition->defaultValue().toList();
1236  else
1237  resultString = definition->defaultValue().toString();
1238  }
1239 
1240  QVariantList result;
1241  Q_FOREACH ( const QString &s, resultString.split( ',' ) )
1242  result << s;
1243 
1244  return result;
1245 }
1246 
1247 QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1248 {
1249  if ( !definition )
1250  return QList<QgsMapLayer *>();
1251 
1252  return parameterAsLayerList( definition, parameters.value( definition->name() ), context );
1253 }
1254 
1255 QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1256 {
1257  if ( !definition )
1258  return QList<QgsMapLayer *>();
1259 
1260  QVariant val = value;
1261  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
1262  {
1263  return QList<QgsMapLayer *>() << layer;
1264  }
1265 
1266  QList<QgsMapLayer *> layers;
1267 
1268  std::function< void( const QVariant &var ) > processVariant;
1269  processVariant = [ &layers, &context, &definition, &processVariant ]( const QVariant & var )
1270  {
1271  if ( var.type() == QVariant::List )
1272  {
1273  Q_FOREACH ( const QVariant &listVar, var.toList() )
1274  {
1275  processVariant( listVar );
1276  }
1277  }
1278  else if ( var.type() == QVariant::StringList )
1279  {
1280  Q_FOREACH ( const QString &s, var.toStringList() )
1281  {
1282  processVariant( s );
1283  }
1284  }
1285  else if ( var.canConvert<QgsProperty>() )
1286  processVariant( var.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() ) );
1287  else if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
1288  {
1289  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1291  QVariant sink = fromVar.sink;
1292  if ( sink.canConvert<QgsProperty>() )
1293  {
1294  processVariant( sink.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() ) );
1295  }
1296  }
1297  else if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
1298  {
1299  layers << layer;
1300  }
1301  else
1302  {
1303  QgsMapLayer *alayer = QgsProcessingUtils::mapLayerFromString( var.toString(), context );
1304  if ( alayer )
1305  layers << alayer;
1306  }
1307  };
1308 
1309  processVariant( val );
1310 
1311  if ( layers.isEmpty() )
1312  {
1313  // check default
1314  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( definition->defaultValue() ) ) )
1315  {
1316  layers << layer;
1317  }
1318  else if ( definition->defaultValue().type() == QVariant::List )
1319  {
1320  Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
1321  {
1322  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
1323  {
1324  layers << layer;
1325  }
1326  else
1327  {
1328  processVariant( var );
1329  }
1330  }
1331  }
1332  else
1333  processVariant( definition->defaultValue() );
1334  }
1335 
1336  return layers;
1337 }
1338 
1339 QList<double> QgsProcessingParameters::parameterAsRange( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1340 {
1341  if ( !definition )
1342  return QList<double>();
1343 
1344  return parameterAsRange( definition, parameters.value( definition->name() ), context );
1345 }
1346 
1347 QList<double> QgsProcessingParameters::parameterAsRange( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1348 {
1349  if ( !definition )
1350  return QList<double>();
1351 
1352  QStringList resultStringList;
1353  QVariant val = value;
1354  if ( val.canConvert<QgsProperty>() )
1355  resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1356  else if ( val.type() == QVariant::List )
1357  {
1358  Q_FOREACH ( const QVariant &var, val.toList() )
1359  resultStringList << var.toString();
1360  }
1361  else
1362  resultStringList << val.toString();
1363 
1364  if ( ( resultStringList.isEmpty() || ( resultStringList.size() == 1 && resultStringList.at( 0 ).isEmpty() ) ) )
1365  {
1366  resultStringList.clear();
1367  // check default
1368  if ( definition->defaultValue().type() == QVariant::List )
1369  {
1370  Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
1371  resultStringList << var.toString();
1372  }
1373  else
1374  resultStringList << definition->defaultValue().toString();
1375  }
1376 
1377  if ( resultStringList.size() == 1 )
1378  {
1379  resultStringList = resultStringList.at( 0 ).split( ',' );
1380  }
1381 
1382  if ( resultStringList.size() < 2 )
1383  return QList< double >() << 0.0 << 0.0;
1384 
1385  return QList< double >() << resultStringList.at( 0 ).toDouble() << resultStringList.at( 1 ).toDouble();
1386 }
1387 
1388 QStringList QgsProcessingParameters::parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1389 {
1390  if ( !definition )
1391  return QStringList();
1392 
1393  QStringList resultStringList;
1394  return parameterAsFields( definition, parameters.value( definition->name() ), context );
1395 }
1396 
1397 QStringList QgsProcessingParameters::parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1398 {
1399  if ( !definition )
1400  return QStringList();
1401 
1402  QStringList resultStringList;
1403  QVariant val = value;
1404  if ( val.isValid() )
1405  {
1406  if ( val.canConvert<QgsProperty>() )
1407  resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1408  else if ( val.type() == QVariant::List )
1409  {
1410  Q_FOREACH ( const QVariant &var, val.toList() )
1411  resultStringList << var.toString();
1412  }
1413  else
1414  resultStringList.append( val.toString().split( ';' ) );
1415  }
1416 
1417  if ( ( resultStringList.isEmpty() || resultStringList.at( 0 ).isEmpty() ) )
1418  {
1419  resultStringList.clear();
1420  // check default
1421  if ( definition->defaultValue().isValid() )
1422  {
1423  if ( definition->defaultValue().type() == QVariant::List )
1424  {
1425  Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
1426  resultStringList << var.toString();
1427  }
1428  else
1429  resultStringList.append( definition->defaultValue().toString().split( ';' ) );
1430  }
1431  }
1432 
1433  return resultStringList;
1434 }
1435 
1437 {
1438  QString type = map.value( QStringLiteral( "parameter_type" ) ).toString();
1439  QString name = map.value( QStringLiteral( "name" ) ).toString();
1440  std::unique_ptr< QgsProcessingParameterDefinition > def;
1442  def.reset( new QgsProcessingParameterBoolean( name ) );
1443  else if ( type == QgsProcessingParameterCrs::typeName() )
1444  def.reset( new QgsProcessingParameterCrs( name ) );
1445  else if ( type == QgsProcessingParameterMapLayer::typeName() )
1446  def.reset( new QgsProcessingParameterMapLayer( name ) );
1447  else if ( type == QgsProcessingParameterExtent::typeName() )
1448  def.reset( new QgsProcessingParameterExtent( name ) );
1449  else if ( type == QgsProcessingParameterPoint::typeName() )
1450  def.reset( new QgsProcessingParameterPoint( name ) );
1451  else if ( type == QgsProcessingParameterFile::typeName() )
1452  def.reset( new QgsProcessingParameterFile( name ) );
1453  else if ( type == QgsProcessingParameterMatrix::typeName() )
1454  def.reset( new QgsProcessingParameterMatrix( name ) );
1456  def.reset( new QgsProcessingParameterMultipleLayers( name ) );
1457  else if ( type == QgsProcessingParameterNumber::typeName() )
1458  def.reset( new QgsProcessingParameterNumber( name ) );
1459  else if ( type == QgsProcessingParameterRange::typeName() )
1460  def.reset( new QgsProcessingParameterRange( name ) );
1461  else if ( type == QgsProcessingParameterRasterLayer::typeName() )
1462  def.reset( new QgsProcessingParameterRasterLayer( name ) );
1463  else if ( type == QgsProcessingParameterEnum::typeName() )
1464  def.reset( new QgsProcessingParameterEnum( name ) );
1465  else if ( type == QgsProcessingParameterString::typeName() )
1466  def.reset( new QgsProcessingParameterString( name ) );
1467  else if ( type == QgsProcessingParameterExpression::typeName() )
1468  def.reset( new QgsProcessingParameterExpression( name ) );
1469  else if ( type == QgsProcessingParameterVectorLayer::typeName() )
1470  def.reset( new QgsProcessingParameterVectorLayer( name ) );
1471  else if ( type == QgsProcessingParameterField::typeName() )
1472  def.reset( new QgsProcessingParameterField( name ) );
1473  else if ( type == QgsProcessingParameterFeatureSource::typeName() )
1474  def.reset( new QgsProcessingParameterFeatureSource( name ) );
1475  else if ( type == QgsProcessingParameterFeatureSink::typeName() )
1476  def.reset( new QgsProcessingParameterFeatureSink( name ) );
1478  def.reset( new QgsProcessingParameterVectorDestination( name ) );
1480  def.reset( new QgsProcessingParameterRasterDestination( name ) );
1482  def.reset( new QgsProcessingParameterFileDestination( name ) );
1484  def.reset( new QgsProcessingParameterFolderDestination( name ) );
1485  else if ( type == QgsProcessingParameterBand::typeName() )
1486  def.reset( new QgsProcessingParameterBand( name ) );
1487  else
1488  {
1490  if ( paramType )
1491  def.reset( paramType->create( name ) );
1492  }
1493 
1494  if ( !def )
1495  return nullptr;
1496 
1497  def->fromVariantMap( map );
1498  return def.release();
1499 }
1500 
1501 QString QgsProcessingParameters::descriptionFromName( const QString &name )
1502 {
1503  QString desc = name;
1504  desc.replace( '_', ' ' );
1505  return desc;
1506 }
1507 
1509 {
1510  bool isOptional = false;
1511  QString name;
1512  QString definition;
1513  QString type;
1514  if ( !parseScriptCodeParameterOptions( code, isOptional, name, type, definition ) )
1515  return nullptr;
1516 
1517  QString description = descriptionFromName( name );
1518 
1519  if ( type == QStringLiteral( "boolean" ) )
1520  return QgsProcessingParameterBoolean::fromScriptCode( name, description, isOptional, definition );
1521  else if ( type == QStringLiteral( "crs" ) )
1522  return QgsProcessingParameterCrs::fromScriptCode( name, description, isOptional, definition );
1523  else if ( type == QStringLiteral( "layer" ) )
1524  return QgsProcessingParameterMapLayer::fromScriptCode( name, description, isOptional, definition );
1525  else if ( type == QStringLiteral( "extent" ) )
1526  return QgsProcessingParameterExtent::fromScriptCode( name, description, isOptional, definition );
1527  else if ( type == QStringLiteral( "point" ) )
1528  return QgsProcessingParameterPoint::fromScriptCode( name, description, isOptional, definition );
1529  else if ( type == QStringLiteral( "file" ) )
1530  return QgsProcessingParameterFile::fromScriptCode( name, description, isOptional, definition, QgsProcessingParameterFile::File );
1531  else if ( type == QStringLiteral( "folder" ) )
1532  return QgsProcessingParameterFile::fromScriptCode( name, description, isOptional, definition, QgsProcessingParameterFile::Folder );
1533  else if ( type == QStringLiteral( "matrix" ) )
1534  return QgsProcessingParameterMatrix::fromScriptCode( name, description, isOptional, definition );
1535  else if ( type == QStringLiteral( "multiple" ) )
1536  return QgsProcessingParameterMultipleLayers::fromScriptCode( name, description, isOptional, definition );
1537  else if ( type == QStringLiteral( "number" ) )
1538  return QgsProcessingParameterNumber::fromScriptCode( name, description, isOptional, definition );
1539  else if ( type == QStringLiteral( "range" ) )
1540  return QgsProcessingParameterRange::fromScriptCode( name, description, isOptional, definition );
1541  else if ( type == QStringLiteral( "raster" ) )
1542  return QgsProcessingParameterRasterLayer::fromScriptCode( name, description, isOptional, definition );
1543  else if ( type == QStringLiteral( "enum" ) )
1544  return QgsProcessingParameterEnum::fromScriptCode( name, description, isOptional, definition );
1545  else if ( type == QStringLiteral( "string" ) )
1546  return QgsProcessingParameterString::fromScriptCode( name, description, isOptional, definition );
1547  else if ( type == QStringLiteral( "expression" ) )
1548  return QgsProcessingParameterExpression::fromScriptCode( name, description, isOptional, definition );
1549  else if ( type == QStringLiteral( "field" ) )
1550  return QgsProcessingParameterField::fromScriptCode( name, description, isOptional, definition );
1551  else if ( type == QStringLiteral( "vector" ) )
1552  return QgsProcessingParameterVectorLayer::fromScriptCode( name, description, isOptional, definition );
1553  else if ( type == QStringLiteral( "source" ) )
1554  return QgsProcessingParameterFeatureSource::fromScriptCode( name, description, isOptional, definition );
1555  else if ( type == QStringLiteral( "sink" ) )
1556  return QgsProcessingParameterFeatureSink::fromScriptCode( name, description, isOptional, definition );
1557  else if ( type == QStringLiteral( "vectordestination" ) )
1558  return QgsProcessingParameterVectorDestination::fromScriptCode( name, description, isOptional, definition );
1559  else if ( type == QStringLiteral( "rasterdestination" ) )
1560  return QgsProcessingParameterRasterDestination::fromScriptCode( name, description, isOptional, definition );
1561  else if ( type == QStringLiteral( "filedestination" ) )
1562  return QgsProcessingParameterFileDestination::fromScriptCode( name, description, isOptional, definition );
1563  else if ( type == QStringLiteral( "folderdestination" ) )
1564  return QgsProcessingParameterFolderDestination::fromScriptCode( name, description, isOptional, definition );
1565  else if ( type == QStringLiteral( "band" ) )
1566  return QgsProcessingParameterBand::fromScriptCode( name, description, isOptional, definition );
1567 
1568  return nullptr;
1569 }
1570 
1571 bool QgsProcessingParameters::parseScriptCodeParameterOptions( const QString &code, bool &isOptional, QString &name, QString &type, QString &definition )
1572 {
1573  QRegularExpression re( QStringLiteral( "(?:#*)(.*?)=\\s*(.*)" ) );
1574  QRegularExpressionMatch m = re.match( code );
1575  if ( !m.hasMatch() )
1576  return false;
1577 
1578  name = m.captured( 1 );
1579  QString tokens = m.captured( 2 );
1580  if ( tokens.startsWith( QLatin1String( "optional" ), Qt::CaseInsensitive ) )
1581  {
1582  isOptional = true;
1583  tokens.remove( 0, 8 ); // length "optional" = 8
1584  }
1585  else
1586  {
1587  isOptional = false;
1588  }
1589 
1590  tokens = tokens.trimmed();
1591 
1592  QRegularExpression re2( QStringLiteral( "(.*?)\\s+(.*)" ) );
1593  m = re2.match( tokens );
1594  if ( !m.hasMatch() )
1595  {
1596  type = tokens.toLower().trimmed();
1597  definition.clear();
1598  }
1599  else
1600  {
1601  type = m.captured( 1 ).toLower().trimmed();
1602  definition = m.captured( 2 );
1603  }
1604  return true;
1605 }
1606 
1607 //
1608 // QgsProcessingParameterDefinition
1609 //
1610 
1611 QgsProcessingParameterDefinition::QgsProcessingParameterDefinition( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1612  : mName( name )
1613  , mDescription( description )
1614  , mDefault( defaultValue )
1615  , mFlags( optional ? FlagOptional : 0 )
1616 {}
1617 
1619 {
1620  if ( !input.isValid() && !mDefault.isValid() )
1621  return mFlags & FlagOptional;
1622 
1623  if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
1624  || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
1625  return mFlags & FlagOptional;
1626 
1627  return true;
1628 }
1629 
1631 {
1632  if ( !value.isValid() )
1633  return QStringLiteral( "None" );
1634 
1635  if ( value.canConvert<QgsProperty>() )
1636  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1637 
1638  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
1639 }
1640 
1642 {
1643  QString code = QStringLiteral( "##%1=" ).arg( mName );
1644  if ( mFlags & FlagOptional )
1645  code += QStringLiteral( "optional " );
1646  code += type() + ' ';
1647  code += mDefault.toString();
1648  return code.trimmed();
1649 }
1650 
1652 {
1653  QVariantMap map;
1654  map.insert( QStringLiteral( "parameter_type" ), type() );
1655  map.insert( QStringLiteral( "name" ), mName );
1656  map.insert( QStringLiteral( "description" ), mDescription );
1657  map.insert( QStringLiteral( "default" ), mDefault );
1658  map.insert( QStringLiteral( "flags" ), static_cast< int >( mFlags ) );
1659  map.insert( QStringLiteral( "metadata" ), mMetadata );
1660  return map;
1661 }
1662 
1664 {
1665  mName = map.value( QStringLiteral( "name" ) ).toString();
1666  mDescription = map.value( QStringLiteral( "description" ) ).toString();
1667  mDefault = map.value( QStringLiteral( "default" ) );
1668  mFlags = static_cast< Flags >( map.value( QStringLiteral( "flags" ) ).toInt() );
1669  mMetadata = map.value( QStringLiteral( "metadata" ) ).toMap();
1670  return true;
1671 }
1672 
1674 {
1675  return mAlgorithm;
1676 }
1677 
1679 {
1680  return mAlgorithm ? mAlgorithm->provider() : nullptr;
1681 }
1682 
1684 {
1685  return QStringLiteral( "<p><b>%1</b></p><p>%2</p>" ).arg(
1686  description(),
1687  QObject::tr( "Python identifier: ‘%1’" ).arg( QStringLiteral( "<i>%1</i>" ).arg( name() ) ) );
1688 }
1689 
1690 QgsProcessingParameterBoolean::QgsProcessingParameterBoolean( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1691  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1692 {}
1693 
1695 {
1696  return new QgsProcessingParameterBoolean( *this );
1697 }
1698 
1700 {
1701  if ( !val.isValid() )
1702  return QStringLiteral( "None" );
1703 
1704  if ( val.canConvert<QgsProperty>() )
1705  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
1706  return val.toBool() ? QStringLiteral( "True" ) : QStringLiteral( "False" );
1707 }
1708 
1710 {
1711  QString code = QStringLiteral( "##%1=" ).arg( mName );
1712  if ( mFlags & FlagOptional )
1713  code += QStringLiteral( "optional " );
1714  code += type() + ' ';
1715  code += mDefault.toBool() ? QStringLiteral( "true" ) : QStringLiteral( "false" );
1716  return code.trimmed();
1717 }
1718 
1719 QgsProcessingParameterBoolean *QgsProcessingParameterBoolean::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1720 {
1721  return new QgsProcessingParameterBoolean( name, description, definition.toLower().trimmed() != QStringLiteral( "false" ), isOptional );
1722 }
1723 
1724 QgsProcessingParameterCrs::QgsProcessingParameterCrs( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1725  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1726 {
1727 
1728 }
1729 
1731 {
1732  return new QgsProcessingParameterCrs( *this );
1733 }
1734 
1736 {
1737  if ( !input.isValid() )
1738  return mFlags & FlagOptional;
1739 
1740  if ( input.canConvert<QgsCoordinateReferenceSystem>() )
1741  {
1742  return true;
1743  }
1744  else if ( input.canConvert<QgsProcessingFeatureSourceDefinition>() )
1745  {
1746  return true;
1747  }
1748  else if ( input.canConvert<QgsProcessingOutputLayerDefinition>() )
1749  {
1750  return true;
1751  }
1752 
1753  if ( input.canConvert<QgsProperty>() )
1754  {
1755  return true;
1756  }
1757 
1758  // direct map layer value
1759  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
1760  return true;
1761 
1762  if ( input.type() != QVariant::String || input.toString().isEmpty() )
1763  return mFlags & FlagOptional;
1764 
1765  return true;
1766 }
1767 
1768 QString QgsProcessingParameterCrs::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
1769 {
1770  if ( !value.isValid() )
1771  return QStringLiteral( "None" );
1772 
1773  if ( value.canConvert<QgsCoordinateReferenceSystem>() )
1774  {
1775  if ( !value.value< QgsCoordinateReferenceSystem >().isValid() )
1776  return QStringLiteral( "QgsCoordinateReferenceSystem()" );
1777  else
1778  return QStringLiteral( "QgsCoordinateReferenceSystem('%1')" ).arg( value.value< QgsCoordinateReferenceSystem >().authid() );
1779  }
1780 
1781  if ( value.canConvert<QgsProperty>() )
1782  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1783 
1784  QVariantMap p;
1785  p.insert( name(), value );
1786  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
1787  if ( layer )
1789 
1791 }
1792 
1793 QgsProcessingParameterCrs *QgsProcessingParameterCrs::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1794 {
1795  return new QgsProcessingParameterCrs( name, description, definition.compare( QLatin1String( "none" ), Qt::CaseInsensitive ) == 0 ? QVariant() : definition, isOptional );
1796 }
1797 
1798 QgsProcessingParameterMapLayer::QgsProcessingParameterMapLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1799  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1800 {
1801 
1802 }
1803 
1805 {
1806  return new QgsProcessingParameterMapLayer( *this );
1807 }
1808 
1810 {
1811  if ( !input.isValid() )
1812  return mFlags & FlagOptional;
1813 
1814  if ( input.canConvert<QgsProperty>() )
1815  {
1816  return true;
1817  }
1818 
1819  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
1820  {
1821  return true;
1822  }
1823 
1824  if ( input.type() != QVariant::String || input.toString().isEmpty() )
1825  return mFlags & FlagOptional;
1826 
1827  if ( !context )
1828  {
1829  // that's as far as we can get without a context
1830  return true;
1831  }
1832 
1833  // try to load as layer
1834  if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context ) )
1835  return true;
1836 
1837  return false;
1838 }
1839 
1841 {
1842  if ( !val.isValid() )
1843  return QStringLiteral( "None" );
1844 
1845  if ( val.canConvert<QgsProperty>() )
1846  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
1847 
1848  QVariantMap p;
1849  p.insert( name(), val );
1850  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
1852  : QgsProcessingUtils::stringToPythonLiteral( val.toString() );
1853 }
1854 
1855 QgsProcessingParameterMapLayer *QgsProcessingParameterMapLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1856 {
1857  return new QgsProcessingParameterMapLayer( name, description, definition, isOptional );
1858 }
1859 
1860 QgsProcessingParameterExtent::QgsProcessingParameterExtent( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1861  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1862 {
1863 
1864 }
1865 
1867 {
1868  return new QgsProcessingParameterExtent( *this );
1869 }
1870 
1872 {
1873  if ( !input.isValid() )
1874  return mFlags & FlagOptional;
1875 
1876  if ( input.canConvert<QgsProcessingFeatureSourceDefinition>() )
1877  {
1878  return true;
1879  }
1880  else if ( input.canConvert<QgsProcessingOutputLayerDefinition>() )
1881  {
1882  return true;
1883  }
1884 
1885  if ( input.canConvert<QgsProperty>() )
1886  {
1887  return true;
1888  }
1889 
1890  if ( input.canConvert< QgsRectangle >() )
1891  {
1892  QgsRectangle r = input.value<QgsRectangle>();
1893  return !r.isNull();
1894  }
1895  if ( input.canConvert< QgsReferencedRectangle >() )
1896  {
1898  return !r.isNull();
1899  }
1900 
1901  // direct map layer value
1902  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
1903  return true;
1904 
1905  if ( input.type() != QVariant::String || input.toString().isEmpty() )
1906  return mFlags & FlagOptional;
1907 
1908  if ( !context )
1909  {
1910  // that's as far as we can get without a context
1911  return true;
1912  }
1913 
1914  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
1915  QRegularExpressionMatch match = rx.match( input.toString() );
1916  if ( match.hasMatch() )
1917  {
1918  bool xMinOk = false;
1919  ( void )match.captured( 1 ).toDouble( &xMinOk );
1920  bool xMaxOk = false;
1921  ( void )match.captured( 2 ).toDouble( &xMaxOk );
1922  bool yMinOk = false;
1923  ( void )match.captured( 3 ).toDouble( &yMinOk );
1924  bool yMaxOk = false;
1925  ( void )match.captured( 4 ).toDouble( &yMaxOk );
1926  if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
1927  return true;
1928  }
1929 
1930  // try as layer extent
1931  return QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
1932 }
1933 
1934 QString QgsProcessingParameterExtent::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
1935 {
1936  if ( !value.isValid() )
1937  return QStringLiteral( "None" );
1938 
1939  if ( value.canConvert<QgsProperty>() )
1940  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1941 
1942  if ( value.canConvert< QgsRectangle >() )
1943  {
1944  QgsRectangle r = value.value<QgsRectangle>();
1945  return QStringLiteral( "'%1, %3, %2, %4'" ).arg( qgsDoubleToString( r.xMinimum() ),
1946  qgsDoubleToString( r.yMinimum() ),
1947  qgsDoubleToString( r.xMaximum() ),
1948  qgsDoubleToString( r.yMaximum() ) );
1949  }
1950  if ( value.canConvert< QgsReferencedRectangle >() )
1951  {
1953  return QStringLiteral( "'%1, %3, %2, %4 [%5]'" ).arg( qgsDoubleToString( r.xMinimum() ),
1954  qgsDoubleToString( r.yMinimum() ),
1955  qgsDoubleToString( r.xMaximum() ),
1956  qgsDoubleToString( r.yMaximum() ), r.crs().authid() );
1957  }
1958 
1959  QVariantMap p;
1960  p.insert( name(), value );
1961  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
1962  if ( layer )
1964 
1966 }
1967 
1968 QgsProcessingParameterExtent *QgsProcessingParameterExtent::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1969 {
1970  return new QgsProcessingParameterExtent( name, description, definition, isOptional );
1971 }
1972 
1973 QgsProcessingParameterPoint::QgsProcessingParameterPoint( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1974  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1975 {
1976 
1977 }
1978 
1980 {
1981  return new QgsProcessingParameterPoint( *this );
1982 }
1983 
1985 {
1986  if ( !input.isValid() )
1987  return mFlags & FlagOptional;
1988 
1989  if ( input.canConvert<QgsProperty>() )
1990  {
1991  return true;
1992  }
1993 
1994  if ( input.canConvert< QgsPointXY >() )
1995  {
1996  return true;
1997  }
1998  if ( input.canConvert< QgsReferencedPointXY >() )
1999  {
2000  return true;
2001  }
2002 
2003  if ( input.type() == QVariant::String )
2004  {
2005  if ( input.toString().isEmpty() )
2006  return mFlags & FlagOptional;
2007  }
2008 
2009  QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
2010 
2011  QRegularExpressionMatch match = rx.match( input.toString() );
2012  if ( match.hasMatch() )
2013  {
2014  bool xOk = false;
2015  ( void )match.captured( 1 ).toDouble( &xOk );
2016  bool yOk = false;
2017  ( void )match.captured( 2 ).toDouble( &yOk );
2018  return xOk && yOk;
2019  }
2020  else
2021  return false;
2022 }
2023 
2024 QString QgsProcessingParameterPoint::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
2025 {
2026  if ( !value.isValid() )
2027  return QStringLiteral( "None" );
2028 
2029  if ( value.canConvert<QgsProperty>() )
2030  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2031 
2032  if ( value.canConvert< QgsPointXY >() )
2033  {
2034  QgsPointXY r = value.value<QgsPointXY>();
2035  return QStringLiteral( "'%1,%2'" ).arg( qgsDoubleToString( r.x() ),
2036  qgsDoubleToString( r.y() ) );
2037  }
2038  if ( value.canConvert< QgsReferencedPointXY >() )
2039  {
2040  QgsReferencedPointXY r = value.value<QgsReferencedPointXY>();
2041  return QStringLiteral( "'%1,%2 [%3]'" ).arg( qgsDoubleToString( r.x() ),
2042  qgsDoubleToString( r.y() ),
2043  r.crs().authid() );
2044  }
2045 
2047 }
2048 
2049 QgsProcessingParameterPoint *QgsProcessingParameterPoint::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2050 {
2051  return new QgsProcessingParameterPoint( name, description, definition, isOptional );
2052 }
2053 
2054 QgsProcessingParameterFile::QgsProcessingParameterFile( const QString &name, const QString &description, Behavior behavior, const QString &extension, const QVariant &defaultValue, bool optional )
2055  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2056  , mBehavior( behavior )
2057  , mExtension( extension )
2058 {
2059 
2060 }
2061 
2063 {
2064  return new QgsProcessingParameterFile( *this );
2065 }
2066 
2068 {
2069  if ( !input.isValid() )
2070  return mFlags & FlagOptional;
2071 
2072  if ( input.canConvert<QgsProperty>() )
2073  {
2074  return true;
2075  }
2076 
2077  QString string = input.toString().trimmed();
2078 
2079  if ( input.type() != QVariant::String || string.isEmpty() )
2080  return mFlags & FlagOptional;
2081 
2082  switch ( mBehavior )
2083  {
2084  case File:
2085  {
2086  if ( !mExtension.isEmpty() )
2087  return string.endsWith( mExtension, Qt::CaseInsensitive );
2088  return true;
2089  }
2090 
2091  case Folder:
2092  return true;
2093  }
2094  return true;
2095 }
2096 
2098 {
2099  QString code = QStringLiteral( "##%1=" ).arg( mName );
2100  if ( mFlags & FlagOptional )
2101  code += QStringLiteral( "optional " );
2102  code += ( mBehavior == File ? QStringLiteral( "file" ) : QStringLiteral( "folder" ) ) + ' ';
2103  code += mDefault.toString();
2104  return code.trimmed();
2105 }
2106 
2108 {
2110  map.insert( QStringLiteral( "behavior" ), mBehavior );
2111  map.insert( QStringLiteral( "extension" ), mExtension );
2112  return map;
2113 }
2114 
2115 bool QgsProcessingParameterFile::fromVariantMap( const QVariantMap &map )
2116 {
2118  mBehavior = static_cast< Behavior >( map.value( QStringLiteral( "behavior" ) ).toInt() );
2119  mExtension = map.value( QStringLiteral( "extension" ) ).toString();
2120  return true;
2121 }
2122 
2124 {
2125  return new QgsProcessingParameterFile( name, description, behavior, QString(), definition, isOptional );
2126 }
2127 
2128 QgsProcessingParameterMatrix::QgsProcessingParameterMatrix( const QString &name, const QString &description, int numberRows, bool fixedNumberRows, const QStringList &headers, const QVariant &defaultValue, bool optional )
2129  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2130  , mHeaders( headers )
2131  , mNumberRows( numberRows )
2132  , mFixedNumberRows( fixedNumberRows )
2133 {
2134 
2135 }
2136 
2138 {
2139  return new QgsProcessingParameterMatrix( *this );
2140 }
2141 
2143 {
2144  if ( !input.isValid() )
2145  return mFlags & FlagOptional;
2146 
2147  if ( input.type() == QVariant::String )
2148  {
2149  if ( input.toString().isEmpty() )
2150  return mFlags & FlagOptional;
2151  return true;
2152  }
2153  else if ( input.type() == QVariant::List )
2154  {
2155  if ( input.toList().isEmpty() )
2156  return mFlags & FlagOptional;
2157  return true;
2158  }
2159  else if ( input.type() == QVariant::Double || input.type() == QVariant::Int )
2160  {
2161  return true;
2162  }
2163 
2164  return false;
2165 }
2166 
2167 QString QgsProcessingParameterMatrix::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
2168 {
2169  if ( !value.isValid() )
2170  return QStringLiteral( "None" );
2171 
2172  if ( value.canConvert<QgsProperty>() )
2173  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2174 
2175  QVariantMap p;
2176  p.insert( name(), value );
2177  QVariantList list = QgsProcessingParameters::parameterAsMatrix( this, p, context );
2178 
2179  QStringList parts;
2180  Q_FOREACH ( const QVariant &v, list )
2181  {
2182  if ( v.type() == QVariant::List )
2183  {
2184  QStringList parts2;
2185  Q_FOREACH ( const QVariant &v2, v.toList() )
2186  {
2187  if ( v2.isNull() || !v2.isValid() )
2188  parts2 << QStringLiteral( "None" );
2189  else if ( v2.toString().isEmpty() )
2190  parts2 << QStringLiteral( "''" );
2191  else
2192  parts2 << v2.toString();
2193  }
2194  parts << parts2.join( ',' ).prepend( '[' ).append( ']' );
2195  }
2196  else
2197  {
2198  if ( v.isNull() || !v.isValid() )
2199  parts << QStringLiteral( "None" );
2200  else if ( v.toString().isEmpty() )
2201  parts << QStringLiteral( "''" );
2202  else
2203  parts << v.toString();
2204  }
2205  }
2206 
2207  return parts.join( ',' ).prepend( '[' ).append( ']' );
2208 }
2209 
2211 {
2212  return mHeaders;
2213 }
2214 
2216 {
2217  mHeaders = headers;
2218 }
2219 
2221 {
2222  return mNumberRows;
2223 }
2224 
2226 {
2227  mNumberRows = numberRows;
2228 }
2229 
2231 {
2232  return mFixedNumberRows;
2233 }
2234 
2236 {
2237  mFixedNumberRows = fixedNumberRows;
2238 }
2239 
2241 {
2243  map.insert( QStringLiteral( "headers" ), mHeaders );
2244  map.insert( QStringLiteral( "rows" ), mNumberRows );
2245  map.insert( QStringLiteral( "fixed_number_rows" ), mFixedNumberRows );
2246  return map;
2247 }
2248 
2249 bool QgsProcessingParameterMatrix::fromVariantMap( const QVariantMap &map )
2250 {
2252  mHeaders = map.value( QStringLiteral( "headers" ) ).toStringList();
2253  mNumberRows = map.value( QStringLiteral( "rows" ) ).toInt();
2254  mFixedNumberRows = map.value( QStringLiteral( "fixed_number_rows" ) ).toBool();
2255  return true;
2256 }
2257 
2258 QgsProcessingParameterMatrix *QgsProcessingParameterMatrix::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2259 {
2260  return new QgsProcessingParameterMatrix( name, description, 0, false, QStringList(), definition.isEmpty() ? QVariant() : definition, isOptional );
2261 }
2262 
2264  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2265  , mLayerType( layerType )
2266 {
2267 
2268 }
2269 
2271 {
2272  return new QgsProcessingParameterMultipleLayers( *this );
2273 }
2274 
2276 {
2277  if ( !input.isValid() )
2278  return mFlags & FlagOptional;
2279 
2280  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
2281  {
2282  return true;
2283  }
2284 
2285  if ( input.type() == QVariant::String )
2286  {
2287  if ( input.toString().isEmpty() )
2288  return mFlags & FlagOptional;
2289 
2290  if ( mMinimumNumberInputs > 1 )
2291  return false;
2292 
2293  if ( !context )
2294  return true;
2295 
2296  return QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
2297  }
2298  else if ( input.type() == QVariant::List )
2299  {
2300  if ( input.toList().count() < mMinimumNumberInputs )
2301  return mFlags & FlagOptional;
2302 
2303  if ( mMinimumNumberInputs > input.toList().count() )
2304  return false;
2305 
2306  if ( !context )
2307  return true;
2308 
2309  Q_FOREACH ( const QVariant &v, input.toList() )
2310  {
2311  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( v ) ) )
2312  continue;
2313 
2314  if ( !QgsProcessingUtils::mapLayerFromString( v.toString(), *context ) )
2315  return false;
2316  }
2317  return true;
2318  }
2319  else if ( input.type() == QVariant::StringList )
2320  {
2321  if ( input.toStringList().count() < mMinimumNumberInputs )
2322  return mFlags & FlagOptional;
2323 
2324  if ( mMinimumNumberInputs > input.toStringList().count() )
2325  return false;
2326 
2327  if ( !context )
2328  return true;
2329 
2330  Q_FOREACH ( const QString &v, input.toStringList() )
2331  {
2332  if ( !QgsProcessingUtils::mapLayerFromString( v, *context ) )
2333  return false;
2334  }
2335  return true;
2336  }
2337  return false;
2338 }
2339 
2341 {
2342  if ( !value.isValid() )
2343  return QStringLiteral( "None" );
2344 
2345  if ( value.canConvert<QgsProperty>() )
2346  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2347 
2348  QVariantMap p;
2349  p.insert( name(), value );
2350  QList<QgsMapLayer *> list = QgsProcessingParameters::parameterAsLayerList( this, p, context );
2351  if ( !list.isEmpty() )
2352  {
2353  QStringList parts;
2354  Q_FOREACH ( const QgsMapLayer *layer, list )
2355  {
2357  }
2358  return parts.join( ',' ).prepend( '[' ).append( ']' );
2359  }
2360 
2362 }
2363 
2365 {
2366  QString code = QStringLiteral( "##%1=" ).arg( mName );
2367  if ( mFlags & FlagOptional )
2368  code += QStringLiteral( "optional " );
2369  switch ( mLayerType )
2370  {
2372  code += QStringLiteral( "multiple raster" );
2373  break;
2374 
2376  code += QStringLiteral( "multiple file" );
2377  break;
2378 
2379  default:
2380  code += QStringLiteral( "multiple vector" );
2381  break;
2382  }
2383  code += ' ';
2384  if ( mDefault.type() == QVariant::List )
2385  {
2386  QStringList parts;
2387  Q_FOREACH ( const QVariant &var, mDefault.toList() )
2388  {
2389  parts << var.toString();
2390  }
2391  code += parts.join( ',' );
2392  }
2393  else if ( mDefault.type() == QVariant::StringList )
2394  {
2395  code += mDefault.toStringList().join( ',' );
2396  }
2397  else
2398  {
2399  code += mDefault.toString();
2400  }
2401  return code.trimmed();
2402 }
2403 
2405 {
2406  return mLayerType;
2407 }
2408 
2410 {
2411  mLayerType = type;
2412 }
2413 
2415 {
2416  return mMinimumNumberInputs;
2417 }
2418 
2420 {
2421  if ( mMinimumNumberInputs >= 1 || !( flags() & QgsProcessingParameterDefinition::FlagOptional ) )
2422  mMinimumNumberInputs = minimumNumberInputs;
2423 }
2424 
2426 {
2428  map.insert( QStringLiteral( "layer_type" ), mLayerType );
2429  map.insert( QStringLiteral( "min_inputs" ), mMinimumNumberInputs );
2430  return map;
2431 }
2432 
2434 {
2436  mLayerType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "layer_type" ) ).toInt() );
2437  mMinimumNumberInputs = map.value( QStringLiteral( "min_inputs" ) ).toInt();
2438  return true;
2439 }
2440 
2441 QgsProcessingParameterMultipleLayers *QgsProcessingParameterMultipleLayers::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2442 {
2443  QString type = definition;
2444  QString defaultVal;
2445  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)" ) );
2446  QRegularExpressionMatch m = re.match( definition );
2447  if ( m.hasMatch() )
2448  {
2449  type = m.captured( 1 ).toLower().trimmed();
2450  defaultVal = m.captured( 2 );
2451  }
2453  if ( type == QStringLiteral( "vector" ) )
2455  else if ( type == QStringLiteral( "raster" ) )
2456  layerType = QgsProcessing::TypeRaster;
2457  else if ( type == QStringLiteral( "file" ) )
2458  layerType = QgsProcessing::TypeFile;
2459  return new QgsProcessingParameterMultipleLayers( name, description, layerType, defaultVal.isEmpty() ? QVariant() : defaultVal, isOptional );
2460 }
2461 
2462 QgsProcessingParameterNumber::QgsProcessingParameterNumber( const QString &name, const QString &description, Type type, const QVariant &defaultValue, bool optional, double minValue, double maxValue )
2463  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2464  , mMin( minValue )
2465  , mMax( maxValue )
2466  , mDataType( type )
2467 {
2468  if ( mMin >= mMax )
2469  {
2470  QgsMessageLog::logMessage( QObject::tr( "Invalid number parameter \"%1\": min value %2 is >= max value %3!" ).arg( name ).arg( mMin ).arg( mMax ), QObject::tr( "Processing" ) );
2471  }
2472 }
2473 
2475 {
2476  return new QgsProcessingParameterNumber( *this );
2477 }
2478 
2480 {
2481  QVariant input = value;
2482  if ( !input.isValid() )
2483  {
2484  if ( !defaultValue().isValid() )
2485  return mFlags & FlagOptional;
2486 
2487  input = defaultValue();
2488  }
2489 
2490  if ( input.canConvert<QgsProperty>() )
2491  {
2492  return true;
2493  }
2494 
2495  bool ok = false;
2496  double res = input.toDouble( &ok );
2497  if ( !ok )
2498  return mFlags & FlagOptional;
2499 
2500  return !( res < mMin || res > mMax );
2501 }
2502 
2504 {
2505  if ( !value.isValid() )
2506  return QStringLiteral( "None" );
2507 
2508  if ( value.canConvert<QgsProperty>() )
2509  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2510 
2511  return value.toString();
2512 }
2513 
2515 {
2517  QStringList parts;
2518  if ( mMin > std::numeric_limits<double>::lowest() + 1 )
2519  parts << QObject::tr( "Minimum value: %1" ).arg( mMin );
2520  if ( mMax < std::numeric_limits<double>::max() )
2521  parts << QObject::tr( "Maximum value: %1" ).arg( mMax );
2522  if ( mDefault.isValid() )
2523  parts << QObject::tr( "Default value: %1" ).arg( mDataType == Integer ? mDefault.toInt() : mDefault.toDouble() );
2524  QString extra = parts.join( QStringLiteral( "<br />" ) );
2525  if ( !extra.isEmpty() )
2526  text += QStringLiteral( "<p>%1</p>" ).arg( extra );
2527  return text;
2528 }
2529 
2531 {
2532  return mMin;
2533 }
2534 
2536 {
2537  mMin = min;
2538 }
2539 
2541 {
2542  return mMax;
2543 }
2544 
2546 {
2547  mMax = max;
2548 }
2549 
2551 {
2552  return mDataType;
2553 }
2554 
2556 {
2557  mDataType = dataType;
2558 }
2559 
2561 {
2563  map.insert( QStringLiteral( "min" ), mMin );
2564  map.insert( QStringLiteral( "max" ), mMax );
2565  map.insert( QStringLiteral( "data_type" ), mDataType );
2566  return map;
2567 }
2568 
2569 bool QgsProcessingParameterNumber::fromVariantMap( const QVariantMap &map )
2570 {
2572  mMin = map.value( QStringLiteral( "min" ) ).toDouble();
2573  mMax = map.value( QStringLiteral( "max" ) ).toDouble();
2574  mDataType = static_cast< Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
2575  return true;
2576 }
2577 
2578 QgsProcessingParameterNumber *QgsProcessingParameterNumber::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2579 {
2580  return new QgsProcessingParameterNumber( name, description, Double, definition.isEmpty() ? QVariant()
2581  : ( definition.toLower().trimmed() == QStringLiteral( "none" ) ? QVariant() : definition ), isOptional );
2582 }
2583 
2585  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2586  , mDataType( type )
2587 {
2588 
2589 }
2590 
2592 {
2593  return new QgsProcessingParameterRange( *this );
2594 }
2595 
2597 {
2598  if ( !input.isValid() )
2599  return mFlags & FlagOptional;
2600 
2601  if ( input.canConvert<QgsProperty>() )
2602  {
2603  return true;
2604  }
2605 
2606  if ( input.type() == QVariant::String )
2607  {
2608  QStringList list = input.toString().split( ',' );
2609  if ( list.count() != 2 )
2610  return mFlags & FlagOptional;
2611  bool ok = false;
2612  list.at( 0 ).toDouble( &ok );
2613  bool ok2 = false;
2614  list.at( 1 ).toDouble( &ok2 );
2615  if ( !ok || !ok2 )
2616  return mFlags & FlagOptional;
2617  return true;
2618  }
2619  else if ( input.type() == QVariant::List )
2620  {
2621  if ( input.toList().count() != 2 )
2622  return mFlags & FlagOptional;
2623 
2624  bool ok = false;
2625  input.toList().at( 0 ).toDouble( &ok );
2626  bool ok2 = false;
2627  input.toList().at( 1 ).toDouble( &ok2 );
2628  if ( !ok || !ok2 )
2629  return mFlags & FlagOptional;
2630  return true;
2631  }
2632 
2633  return false;
2634 }
2635 
2636 QString QgsProcessingParameterRange::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
2637 {
2638  if ( !value.isValid() )
2639  return QStringLiteral( "None" );
2640 
2641  if ( value.canConvert<QgsProperty>() )
2642  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2643 
2644  QVariantMap p;
2645  p.insert( name(), value );
2646  QList< double > parts = QgsProcessingParameters::parameterAsRange( this, p, context );
2647 
2648  QStringList stringParts;
2649  Q_FOREACH ( double v, parts )
2650  {
2651  stringParts << QString::number( v );
2652  }
2653  return stringParts.join( ',' ).prepend( '[' ).append( ']' );
2654 }
2655 
2657 {
2658  return mDataType;
2659 }
2660 
2662 {
2663  mDataType = dataType;
2664 }
2665 
2667 {
2669  map.insert( QStringLiteral( "data_type" ), mDataType );
2670  return map;
2671 }
2672 
2673 bool QgsProcessingParameterRange::fromVariantMap( const QVariantMap &map )
2674 {
2676  mDataType = static_cast< QgsProcessingParameterNumber::Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
2677  return true;
2678 }
2679 
2680 QgsProcessingParameterRange *QgsProcessingParameterRange::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2681 {
2682  return new QgsProcessingParameterRange( name, description, QgsProcessingParameterNumber::Double, definition.isEmpty() ? QVariant() : definition, isOptional );
2683 }
2684 
2685 QgsProcessingParameterRasterLayer::QgsProcessingParameterRasterLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
2686  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2687 {
2688 
2689 }
2690 
2692 {
2693  return new QgsProcessingParameterRasterLayer( *this );
2694 }
2695 
2697 {
2698  if ( !input.isValid() )
2699  return mFlags & FlagOptional;
2700 
2701  if ( input.canConvert<QgsProperty>() )
2702  {
2703  return true;
2704  }
2705 
2706  if ( qobject_cast< QgsRasterLayer * >( qvariant_cast<QObject *>( input ) ) )
2707  return true;
2708 
2709  if ( input.type() != QVariant::String || input.toString().isEmpty() )
2710  return mFlags & FlagOptional;
2711 
2712  if ( !context )
2713  {
2714  // that's as far as we can get without a context
2715  return true;
2716  }
2717 
2718  // try to load as layer
2719  if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context, true, QgsProcessingUtils::Raster ) )
2720  return true;
2721 
2722  return false;
2723 }
2724 
2726 {
2727  if ( !val.isValid() )
2728  return QStringLiteral( "None" );
2729 
2730  if ( val.canConvert<QgsProperty>() )
2731  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
2732 
2733  QVariantMap p;
2734  p.insert( name(), val );
2737  : QgsProcessingUtils::stringToPythonLiteral( val.toString() );
2738 }
2739 
2740 QgsProcessingParameterRasterLayer *QgsProcessingParameterRasterLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2741 {
2742  return new QgsProcessingParameterRasterLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
2743 }
2744 
2745 QgsProcessingParameterEnum::QgsProcessingParameterEnum( const QString &name, const QString &description, const QStringList &options, bool allowMultiple, const QVariant &defaultValue, bool optional )
2746  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2747  , mOptions( options )
2748  , mAllowMultiple( allowMultiple )
2749 {
2750 
2751 }
2752 
2754 {
2755  return new QgsProcessingParameterEnum( *this );
2756 }
2757 
2759 {
2760  QVariant input = value;
2761  if ( !input.isValid() )
2762  {
2763  if ( !defaultValue().isValid() )
2764  return mFlags & FlagOptional;
2765 
2766  input = defaultValue();
2767  }
2768 
2769  if ( input.canConvert<QgsProperty>() )
2770  {
2771  return true;
2772  }
2773 
2774  if ( input.type() == QVariant::List )
2775  {
2776  if ( !mAllowMultiple )
2777  return false;
2778 
2779  const QVariantList values = input.toList();
2780  if ( values.empty() && !( mFlags & FlagOptional ) )
2781  return false;
2782 
2783  for ( const QVariant &val : values )
2784  {
2785  bool ok = false;
2786  int res = val.toInt( &ok );
2787  if ( !ok )
2788  return false;
2789  else if ( res < 0 || res >= mOptions.count() )
2790  return false;
2791  }
2792 
2793  return true;
2794  }
2795  else if ( input.type() == QVariant::String )
2796  {
2797  QStringList parts = input.toString().split( ',' );
2798  if ( parts.count() > 1 && !mAllowMultiple )
2799  return false;
2800 
2801  Q_FOREACH ( const QString &part, parts )
2802  {
2803  bool ok = false;
2804  int res = part.toInt( &ok );
2805  if ( !ok )
2806  return false;
2807  else if ( res < 0 || res >= mOptions.count() )
2808  return false;
2809  }
2810  return true;
2811  }
2812  else if ( input.type() == QVariant::Int || input.type() == QVariant::Double )
2813  {
2814  bool ok = false;
2815  int res = input.toInt( &ok );
2816  if ( !ok )
2817  return false;
2818  else if ( res >= 0 && res < mOptions.count() )
2819  return true;
2820  }
2821  return false;
2822 }
2823 
2825 {
2826  if ( !value.isValid() )
2827  return QStringLiteral( "None" );
2828 
2829  if ( value.canConvert<QgsProperty>() )
2830  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2831 
2832  if ( value.type() == QVariant::List )
2833  {
2834  QStringList parts;
2835  Q_FOREACH ( const QVariant &val, value.toList() )
2836  {
2837  parts << QString::number( static_cast< int >( val.toDouble() ) );
2838  }
2839  return parts.join( ',' ).prepend( '[' ).append( ']' );
2840  }
2841  else if ( value.type() == QVariant::String )
2842  {
2843  QStringList parts = value.toString().split( ',' );
2844  if ( parts.count() > 1 )
2845  {
2846  return parts.join( ',' ).prepend( '[' ).append( ']' );
2847  }
2848  }
2849 
2850  return QString::number( static_cast< int >( value.toDouble() ) );
2851 }
2852 
2854 {
2855  QString code = QStringLiteral( "##%1=" ).arg( mName );
2856  if ( mFlags & FlagOptional )
2857  code += QStringLiteral( "optional " );
2858  code += QStringLiteral( "enum " );
2859 
2860  if ( mAllowMultiple )
2861  code += QStringLiteral( "multiple " );
2862 
2863  code += mOptions.join( ';' ) + ' ';
2864 
2865  code += mDefault.toString();
2866  return code.trimmed();
2867 }
2868 
2870 {
2871  return mOptions;
2872 }
2873 
2875 {
2876  mOptions = options;
2877 }
2878 
2880 {
2881  return mAllowMultiple;
2882 }
2883 
2885 {
2886  mAllowMultiple = allowMultiple;
2887 }
2888 
2890 {
2892  map.insert( QStringLiteral( "options" ), mOptions );
2893  map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
2894  return map;
2895 }
2896 
2897 bool QgsProcessingParameterEnum::fromVariantMap( const QVariantMap &map )
2898 {
2900  mOptions = map.value( QStringLiteral( "options" ) ).toStringList();
2901  mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
2902  return true;
2903 }
2904 
2905 QgsProcessingParameterEnum *QgsProcessingParameterEnum::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2906 {
2907  QString defaultVal;
2908  bool multiple = false;
2909  QString def = definition;
2910  if ( def.startsWith( QLatin1String( "multiple" ), Qt::CaseInsensitive ) )
2911  {
2912  multiple = true;
2913  def = def.mid( 9 );
2914  }
2915 
2916  QRegularExpression re( QStringLiteral( "(.*)\\s+(.*?)$" ) );
2917  QRegularExpressionMatch m = re.match( def );
2918  QString values = def;
2919  if ( m.hasMatch() )
2920  {
2921  values = m.captured( 1 ).trimmed();
2922  defaultVal = m.captured( 2 );
2923  }
2924 
2925  return new QgsProcessingParameterEnum( name, description, values.split( ';' ), multiple, defaultVal.isEmpty() ? QVariant() : defaultVal, isOptional );
2926 }
2927 
2928 QgsProcessingParameterString::QgsProcessingParameterString( const QString &name, const QString &description, const QVariant &defaultValue, bool multiLine, bool optional )
2929  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2930  , mMultiLine( multiLine )
2931 {
2932 
2933 }
2934 
2936 {
2937  return new QgsProcessingParameterString( *this );
2938 }
2939 
2941 {
2942  if ( !value.isValid() )
2943  return QStringLiteral( "None" );
2944 
2945  if ( value.canConvert<QgsProperty>() )
2946  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2947 
2948  QString s = value.toString();
2950 }
2951 
2953 {
2954  QString code = QStringLiteral( "##%1=" ).arg( mName );
2955  if ( mFlags & FlagOptional )
2956  code += QStringLiteral( "optional " );
2957  code += QStringLiteral( "string " );
2958 
2959  if ( mMultiLine )
2960  code += QStringLiteral( "long " );
2961 
2962  code += mDefault.toString();
2963  return code.trimmed();
2964 }
2965 
2967 {
2968  return mMultiLine;
2969 }
2970 
2972 {
2973  mMultiLine = multiLine;
2974 }
2975 
2977 {
2979  map.insert( QStringLiteral( "multiline" ), mMultiLine );
2980  return map;
2981 }
2982 
2983 bool QgsProcessingParameterString::fromVariantMap( const QVariantMap &map )
2984 {
2986  mMultiLine = map.value( QStringLiteral( "multiline" ) ).toBool();
2987  return true;
2988 }
2989 
2990 QgsProcessingParameterString *QgsProcessingParameterString::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2991 {
2992  QString def = definition;
2993  bool multiLine = false;
2994  if ( def.startsWith( QLatin1String( "long" ), Qt::CaseInsensitive ) )
2995  {
2996  multiLine = true;
2997  def = def.mid( 5 );
2998  }
2999 
3000  if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
3001  def = def.mid( 1 );
3002  if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
3003  def.chop( 1 );
3004 
3005  QVariant defaultValue = def;
3006  if ( def == QStringLiteral( "None" ) )
3007  defaultValue = QVariant();
3008 
3009  return new QgsProcessingParameterString( name, description, defaultValue, multiLine, isOptional );
3010 }
3011 
3012 QgsProcessingParameterExpression::QgsProcessingParameterExpression( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional )
3013  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3014  , mParentLayerParameterName( parentLayerParameterName )
3015 {
3016 
3017 }
3018 
3020 {
3021  return new QgsProcessingParameterExpression( *this );
3022 }
3023 
3025 {
3026  if ( !value.isValid() )
3027  return QStringLiteral( "None" );
3028 
3029  if ( value.canConvert<QgsProperty>() )
3030  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3031 
3032  QString s = value.toString();
3034 }
3035 
3037 {
3038  QStringList depends;
3039  if ( !mParentLayerParameterName.isEmpty() )
3040  depends << mParentLayerParameterName;
3041  return depends;
3042 }
3043 
3045 {
3046  return mParentLayerParameterName;
3047 }
3048 
3050 {
3051  mParentLayerParameterName = parentLayerParameterName;
3052 }
3053 
3055 {
3057  map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
3058  return map;
3059 }
3060 
3062 {
3064  mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
3065  return true;
3066 }
3067 
3068 QgsProcessingParameterExpression *QgsProcessingParameterExpression::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3069 {
3070  return new QgsProcessingParameterExpression( name, description, definition, QString(), isOptional );
3071 }
3072 
3073 QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
3074  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3076 {
3077 
3078 }
3079 
3081 {
3082  return new QgsProcessingParameterVectorLayer( *this );
3083 }
3084 
3086 {
3087  if ( !v.isValid() )
3088  return mFlags & FlagOptional;
3089 
3090  QVariant var = v;
3091 
3092  if ( var.canConvert<QgsProperty>() )
3093  {
3094  QgsProperty p = var.value< QgsProperty >();
3096  {
3097  var = p.staticValue();
3098  }
3099  else
3100  {
3101  return true;
3102  }
3103  }
3104 
3105  if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( var ) ) )
3106  return true;
3107 
3108  if ( var.type() != QVariant::String || var.toString().isEmpty() )
3109  return mFlags & FlagOptional;
3110 
3111  if ( !context )
3112  {
3113  // that's as far as we can get without a context
3114  return true;
3115  }
3116 
3117  // try to load as layer
3118  if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::Vector ) )
3119  return true;
3120 
3121  return false;
3122 }
3123 
3125 {
3126  if ( !val.isValid() )
3127  return QStringLiteral( "None" );
3128 
3129  if ( val.canConvert<QgsProperty>() )
3130  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
3131 
3132  QVariantMap p;
3133  p.insert( name(), val );
3136  : QgsProcessingUtils::stringToPythonLiteral( val.toString() );
3137 }
3138 
3140 {
3141  return mDataTypes;
3142 }
3143 
3145 {
3146  mDataTypes = types;
3147 }
3148 
3150 {
3152  QVariantList types;
3153  Q_FOREACH ( int type, mDataTypes )
3154  {
3155  types << type;
3156  }
3157  map.insert( QStringLiteral( "data_types" ), types );
3158  return map;
3159 }
3160 
3162 {
3164  mDataTypes.clear();
3165  QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
3166  Q_FOREACH ( const QVariant &val, values )
3167  {
3168  mDataTypes << val.toInt();
3169  }
3170  return true;
3171 }
3172 
3173 QgsProcessingParameterVectorLayer *QgsProcessingParameterVectorLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3174 {
3175  return new QgsProcessingParameterVectorLayer( name, description, QList< int>(), definition.isEmpty() ? QVariant() : definition, isOptional );
3176 }
3177 
3178 QgsProcessingParameterField::QgsProcessingParameterField( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, DataType type, bool allowMultiple, bool optional )
3179  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3180  , mParentLayerParameterName( parentLayerParameterName )
3181  , mDataType( type )
3182  , mAllowMultiple( allowMultiple )
3183 {
3184 
3185 }
3186 
3188 {
3189  return new QgsProcessingParameterField( *this );
3190 }
3191 
3193 {
3194  if ( !input.isValid() )
3195  return mFlags & FlagOptional;
3196 
3197  if ( input.canConvert<QgsProperty>() )
3198  {
3199  return true;
3200  }
3201 
3202  if ( input.type() == QVariant::List || input.type() == QVariant::StringList )
3203  {
3204  if ( !mAllowMultiple )
3205  return false;
3206 
3207  if ( input.toList().isEmpty() && !( mFlags & FlagOptional ) )
3208  return false;
3209  }
3210  else if ( input.type() == QVariant::String )
3211  {
3212  if ( input.toString().isEmpty() )
3213  return mFlags & FlagOptional;
3214 
3215  QStringList parts = input.toString().split( ';' );
3216  if ( parts.count() > 1 && !mAllowMultiple )
3217  return false;
3218  }
3219  else
3220  {
3221  if ( input.toString().isEmpty() )
3222  return mFlags & FlagOptional;
3223  }
3224  return true;
3225 }
3226 
3228 {
3229  if ( !value.isValid() )
3230  return QStringLiteral( "None" );
3231 
3232  if ( value.canConvert<QgsProperty>() )
3233  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3234 
3235  if ( value.type() == QVariant::List )
3236  {
3237  QStringList parts;
3238  Q_FOREACH ( const QVariant &val, value.toList() )
3239  {
3240  parts << QgsProcessingUtils::stringToPythonLiteral( val.toString() );
3241  }
3242  return parts.join( ',' ).prepend( '[' ).append( ']' );
3243  }
3244  else if ( value.type() == QVariant::StringList )
3245  {
3246  QStringList parts;
3247  Q_FOREACH ( QString s, value.toStringList() )
3248  {
3250  }
3251  return parts.join( ',' ).prepend( '[' ).append( ']' );
3252  }
3253 
3254  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
3255 }
3256 
3258 {
3259  QString code = QStringLiteral( "##%1=" ).arg( mName );
3260  if ( mFlags & FlagOptional )
3261  code += QStringLiteral( "optional " );
3262  code += QStringLiteral( "field " );
3263 
3264  switch ( mDataType )
3265  {
3266  case Numeric:
3267  code += QStringLiteral( "numeric " );
3268  break;
3269 
3270  case String:
3271  code += QStringLiteral( "string " );
3272  break;
3273 
3274  case DateTime:
3275  code += QStringLiteral( "datetime " );
3276  break;
3277 
3278  case Any:
3279  break;
3280  }
3281 
3282  if ( mAllowMultiple )
3283  code += QStringLiteral( "multiple " );
3284 
3285  code += mParentLayerParameterName + ' ';
3286 
3287  code += mDefault.toString();
3288  return code.trimmed();
3289 }
3290 
3292 {
3293  QStringList depends;
3294  if ( !mParentLayerParameterName.isEmpty() )
3295  depends << mParentLayerParameterName;
3296  return depends;
3297 }
3298 
3300 {
3301  return mParentLayerParameterName;
3302 }
3303 
3305 {
3306  mParentLayerParameterName = parentLayerParameterName;
3307 }
3308 
3310 {
3311  return mDataType;
3312 }
3313 
3315 {
3316  mDataType = dataType;
3317 }
3318 
3320 {
3321  return mAllowMultiple;
3322 }
3323 
3325 {
3326  mAllowMultiple = allowMultiple;
3327 }
3328 
3330 {
3332  map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
3333  map.insert( QStringLiteral( "data_type" ), mDataType );
3334  map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
3335  return map;
3336 }
3337 
3338 bool QgsProcessingParameterField::fromVariantMap( const QVariantMap &map )
3339 {
3341  mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
3342  mDataType = static_cast< DataType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
3343  mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
3344  return true;
3345 }
3346 
3347 QgsProcessingParameterField *QgsProcessingParameterField::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3348 {
3349  QString parent;
3350  DataType type = Any;
3351  bool allowMultiple = false;
3352  QString def = definition;
3353 
3354  if ( def.startsWith( QLatin1String( "numeric " ), Qt::CaseInsensitive ) )
3355  {
3356  type = Numeric;
3357  def = def.mid( 8 );
3358  }
3359  else if ( def.startsWith( QLatin1String( "string " ), Qt::CaseInsensitive ) )
3360  {
3361  type = String;
3362  def = def.mid( 7 );
3363  }
3364  else if ( def.startsWith( QLatin1String( "datetime " ), Qt::CaseInsensitive ) )
3365  {
3366  type = DateTime;
3367  def = def.mid( 9 );
3368  }
3369 
3370  if ( def.startsWith( QLatin1String( "multiple" ), Qt::CaseInsensitive ) )
3371  {
3372  allowMultiple = true;
3373  def = def.mid( 8 ).trimmed();
3374  }
3375 
3376  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
3377  QRegularExpressionMatch m = re.match( def );
3378  if ( m.hasMatch() )
3379  {
3380  parent = m.captured( 1 ).trimmed();
3381  def = m.captured( 2 );
3382  }
3383  else
3384  {
3385  parent = def;
3386  def.clear();
3387  }
3388 
3389  return new QgsProcessingParameterField( name, description, def.isEmpty() ? QVariant() : def, parent, type, allowMultiple, isOptional );
3390 }
3391 
3392 QgsProcessingParameterFeatureSource::QgsProcessingParameterFeatureSource( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
3393  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3395 {
3396 
3397 }
3398 
3400 {
3401  return new QgsProcessingParameterFeatureSource( *this );
3402 }
3403 
3405 {
3406  QVariant var = input;
3407  if ( !var.isValid() )
3408  return mFlags & FlagOptional;
3409 
3410  if ( var.canConvert<QgsProcessingFeatureSourceDefinition>() )
3411  {
3413  var = fromVar.source;
3414  }
3415  else if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
3416  {
3417  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
3419  var = fromVar.sink;
3420  }
3421 
3422  if ( var.canConvert<QgsProperty>() )
3423  {
3424  QgsProperty p = var.value< QgsProperty >();
3426  {
3427  var = p.staticValue();
3428  }
3429  else
3430  {
3431  return true;
3432  }
3433  }
3434  if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( input ) ) )
3435  {
3436  return true;
3437  }
3438 
3439  if ( var.type() != QVariant::String || var.toString().isEmpty() )
3440  return mFlags & FlagOptional;
3441 
3442  if ( !context )
3443  {
3444  // that's as far as we can get without a context
3445  return true;
3446  }
3447 
3448  // try to load as layer
3449  if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::Vector ) )
3450  return true;
3451 
3452  return false;
3453 }
3454 
3456 {
3457  if ( !value.isValid() )
3458  return QStringLiteral( "None" );
3459 
3460  if ( value.canConvert<QgsProperty>() )
3461  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3462 
3463  if ( value.canConvert<QgsProcessingFeatureSourceDefinition>() )
3464  {
3466  if ( fromVar.source.propertyType() == QgsProperty::StaticProperty )
3467  {
3468  if ( fromVar.selectedFeaturesOnly )
3469  {
3470  return QStringLiteral( "QgsProcessingFeatureSourceDefinition('%1', True)" ).arg( fromVar.source.staticValue().toString() );
3471  }
3472  else
3473  {
3474  QString layerString = fromVar.source.staticValue().toString();
3475  // prefer to use layer source instead of id if possible (since it's persistent)
3476  if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context, true, QgsProcessingUtils::Vector ) ) )
3477  layerString = layer->source();
3478  return QgsProcessingUtils::stringToPythonLiteral( layerString );
3479  }
3480  }
3481  else
3482  {
3483  if ( fromVar.selectedFeaturesOnly )
3484  {
3485  return QStringLiteral( "QgsProcessingFeatureSourceDefinition(QgsProperty.fromExpression('%1'), True)" ).arg( fromVar.source.asExpression() );
3486  }
3487  else
3488  {
3489  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.source.asExpression() );
3490  }
3491  }
3492  }
3493  else if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( value ) ) )
3494  {
3495  return QgsProcessingUtils::stringToPythonLiteral( layer->source() );
3496  }
3497 
3498  QString layerString = value.toString();
3499 
3500  // prefer to use layer source if possible (since it's persistent)
3501  if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context, true, QgsProcessingUtils::Vector ) ) )
3502  layerString = layer->source();
3503 
3504  return QgsProcessingUtils::stringToPythonLiteral( layerString );
3505 }
3506 
3508 {
3509  QString code = QStringLiteral( "##%1=" ).arg( mName );
3510  if ( mFlags & FlagOptional )
3511  code += QStringLiteral( "optional " );
3512  code += QStringLiteral( "source " );
3513 
3514  Q_FOREACH ( int type, mDataTypes )
3515  {
3516  switch ( type )
3517  {
3519  code += QStringLiteral( "point " );
3520  break;
3521 
3523  code += QStringLiteral( "line " );
3524  break;
3525 
3527  code += QStringLiteral( "polygon " );
3528  break;
3529 
3530  }
3531  }
3532 
3533  code += mDefault.toString();
3534  return code.trimmed();
3535 }
3536 
3538  : mDataTypes( types )
3539 {
3540 
3541 }
3542 
3544 {
3546  QVariantList types;
3547  Q_FOREACH ( int type, mDataTypes )
3548  {
3549  types << type;
3550  }
3551  map.insert( QStringLiteral( "data_types" ), types );
3552  return map;
3553 }
3554 
3556 {
3558  mDataTypes.clear();
3559  QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
3560  Q_FOREACH ( const QVariant &val, values )
3561  {
3562  mDataTypes << val.toInt();
3563  }
3564  return true;
3565 }
3566 
3567 QgsProcessingParameterFeatureSource *QgsProcessingParameterFeatureSource::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3568 {
3569  QList< int > types;
3570  QString def = definition;
3571  while ( true )
3572  {
3573  if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
3574  {
3576  def = def.mid( 6 );
3577  continue;
3578  }
3579  else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
3580  {
3582  def = def.mid( 5 );
3583  continue;
3584  }
3585  else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
3586  {
3588  def = def.mid( 8 );
3589  continue;
3590  }
3591  break;
3592  }
3593 
3594  return new QgsProcessingParameterFeatureSource( name, description, types, def, isOptional );
3595 }
3596 
3597 QgsProcessingParameterFeatureSink::QgsProcessingParameterFeatureSink( const QString &name, const QString &description, QgsProcessing::SourceType type, const QVariant &defaultValue, bool optional, bool createByDefault )
3598  : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
3599  , mDataType( type )
3600 {
3601 }
3602 
3604 {
3605  return new QgsProcessingParameterFeatureSink( *this );
3606 }
3607 
3609 {
3610  QVariant var = input;
3611  if ( !var.isValid() )
3612  return mFlags & FlagOptional;
3613 
3614  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
3615  {
3617  var = fromVar.sink;
3618  }
3619 
3620  if ( var.canConvert<QgsProperty>() )
3621  {
3622  QgsProperty p = var.value< QgsProperty >();
3624  {
3625  var = p.staticValue();
3626  }
3627  else
3628  {
3629  return true;
3630  }
3631  }
3632 
3633  if ( var.type() != QVariant::String )
3634  return false;
3635 
3636  if ( var.toString().isEmpty() )
3637  return mFlags & FlagOptional;
3638 
3639  return true;
3640 }
3641 
3643 {
3644  if ( !value.isValid() )
3645  return QStringLiteral( "None" );
3646 
3647  if ( value.canConvert<QgsProperty>() )
3648  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3649 
3650  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
3651  {
3653  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
3654  {
3655  return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
3656  }
3657  else
3658  {
3659  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
3660  }
3661  }
3662 
3663  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
3664 }
3665 
3667 {
3668  QString code = QStringLiteral( "##%1=" ).arg( mName );
3669  if ( mFlags & FlagOptional )
3670  code += QStringLiteral( "optional " );
3671  code += QStringLiteral( "sink " );
3672 
3673  switch ( mDataType )
3674  {
3676  code += QStringLiteral( "point " );
3677  break;
3678 
3680  code += QStringLiteral( "line " );
3681  break;
3682 
3684  code += QStringLiteral( "polygon " );
3685  break;
3686 
3688  code += QStringLiteral( "table " );
3689  break;
3690 
3691  default:
3692  break;
3693  }
3694 
3695  code += mDefault.toString();
3696  return code.trimmed();
3697 }
3698 
3700 {
3701  return new QgsProcessingOutputVectorLayer( name(), description(), mDataType );
3702 }
3703 
3705 {
3706  if ( originalProvider() )
3707  {
3709  }
3710  else if ( QgsProcessingProvider *p = provider() )
3711  {
3712  return p->defaultVectorFileExtension( hasGeometry() );
3713  }
3714  else
3715  {
3716  QgsSettings settings;
3717  if ( hasGeometry() )
3718  {
3719  return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
3720  }
3721  else
3722  {
3723  return QStringLiteral( "dbf" );
3724  }
3725  }
3726 }
3727 
3729 {
3730  if ( originalProvider() )
3731  {
3732  if ( hasGeometry() )
3734  else
3736  }
3737  else if ( QgsProcessingProvider *p = provider() )
3738  {
3739  if ( hasGeometry() )
3740  return p->supportedOutputVectorLayerExtensions();
3741  else
3742  return p->supportedOutputTableExtensions();
3743  }
3744  else
3745  {
3747  }
3748 }
3749 
3751 {
3752  return mDataType;
3753 }
3754 
3756 {
3757  switch ( mDataType )
3758  {
3764  return true;
3765 
3769  return false;
3770  }
3771  return true;
3772 }
3773 
3775 {
3776  mDataType = type;
3777 }
3778 
3780 {
3782  map.insert( QStringLiteral( "data_type" ), mDataType );
3783  return map;
3784 }
3785 
3787 {
3789  mDataType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
3790  return true;
3791 }
3792 
3794 {
3796  return QStringLiteral( "memory:%1" ).arg( description() );
3797  else
3799 }
3800 
3801 QgsProcessingParameterFeatureSink *QgsProcessingParameterFeatureSink::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3802 {
3804  QString def = definition;
3805  if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
3806  {
3808  def = def.mid( 6 );
3809  }
3810  else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
3811  {
3813  def = def.mid( 5 );
3814  }
3815  else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
3816  {
3818  def = def.mid( 8 );
3819  }
3820  else if ( def.startsWith( QLatin1String( "table" ), Qt::CaseInsensitive ) )
3821  {
3823  def = def.mid( 6 );
3824  }
3825 
3826  return new QgsProcessingParameterFeatureSink( name, description, type, definition, isOptional );
3827 }
3828 
3830  : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
3831 {
3832 }
3833 
3835 {
3836  return new QgsProcessingParameterRasterDestination( *this );
3837 }
3838 
3840 {
3841  QVariant var = input;
3842  if ( !var.isValid() )
3843  return mFlags & FlagOptional;
3844 
3845  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
3846  {
3848  var = fromVar.sink;
3849  }
3850 
3851  if ( var.canConvert<QgsProperty>() )
3852  {
3853  QgsProperty p = var.value< QgsProperty >();
3855  {
3856  var = p.staticValue();
3857  }
3858  else
3859  {
3860  return true;
3861  }
3862  }
3863 
3864  if ( var.type() != QVariant::String )
3865  return false;
3866 
3867  if ( var.toString().isEmpty() )
3868  return mFlags & FlagOptional;
3869 
3870  return true;
3871 }
3872 
3874 {
3875  if ( !value.isValid() )
3876  return QStringLiteral( "None" );
3877 
3878  if ( value.canConvert<QgsProperty>() )
3879  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3880 
3881  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
3882  {
3884  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
3885  {
3886  return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
3887  }
3888  else
3889  {
3890  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
3891  }
3892  }
3893 
3894  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
3895 }
3896 
3898 {
3899  return new QgsProcessingOutputRasterLayer( name(), description() );
3900 }
3901 
3903 {
3904  if ( originalProvider() )
3905  {
3907  }
3908  else if ( QgsProcessingProvider *p = provider() )
3909  {
3910  return p->defaultRasterFileExtension();
3911  }
3912  else
3913  {
3914  QgsSettings settings;
3915  return settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), QStringLiteral( "tif" ), QgsSettings::Core ).toString();
3916  }
3917 }
3918 
3920 {
3921  if ( originalProvider() )
3922  {
3924  }
3925  else if ( QgsProcessingProvider *p = provider() )
3926  {
3927  return p->supportedOutputRasterLayerExtensions();
3928  }
3929  else
3930  {
3932  }
3933 }
3934 
3935 QgsProcessingParameterRasterDestination *QgsProcessingParameterRasterDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3936 {
3937  return new QgsProcessingParameterRasterDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
3938 }
3939 
3940 
3941 QgsProcessingParameterFileDestination::QgsProcessingParameterFileDestination( const QString &name, const QString &description, const QString &fileFilter, const QVariant &defaultValue, bool optional, bool createByDefault )
3942  : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
3943  , mFileFilter( fileFilter.isEmpty() ? QObject::tr( "All files (*.*)" ) : fileFilter )
3944 {
3945 
3946 }
3947 
3949 {
3950  return new QgsProcessingParameterFileDestination( *this );
3951 }
3952 
3954 {
3955  QVariant var = input;
3956  if ( !var.isValid() )
3957  return mFlags & FlagOptional;
3958 
3959  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
3960  {
3962  var = fromVar.sink;
3963  }
3964 
3965  if ( var.canConvert<QgsProperty>() )
3966  {
3967  QgsProperty p = var.value< QgsProperty >();
3969  {
3970  var = p.staticValue();
3971  }
3972  else
3973  {
3974  return true;
3975  }
3976  }
3977 
3978  if ( var.type() != QVariant::String )
3979  return false;
3980 
3981  if ( var.toString().isEmpty() )
3982  return mFlags & FlagOptional;
3983 
3984  // possible enhancement - check that value is compatible with file filter?
3985 
3986  return true;
3987 }
3988 
3990 {
3991  if ( !value.isValid() )
3992  return QStringLiteral( "None" );
3993 
3994  if ( value.canConvert<QgsProperty>() )
3995  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3996 
3997  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
3998  {
4000  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
4001  {
4002  return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
4003  }
4004  else
4005  {
4006  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
4007  }
4008  }
4009 
4010  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
4011 }
4012 
4014 {
4015  if ( !mFileFilter.isEmpty() && mFileFilter.contains( QStringLiteral( "htm" ), Qt::CaseInsensitive ) )
4016  {
4017  return new QgsProcessingOutputHtml( name(), description() );
4018  }
4019  else
4020  {
4021  return new QgsProcessingOutputFile( name(), description() );
4022  }
4023 }
4024 
4026 {
4027  if ( mFileFilter.isEmpty() || mFileFilter == QObject::tr( "All files (*.*)" ) )
4028  return QStringLiteral( "file" );
4029 
4030  // get first extension from filter
4031  QRegularExpression rx( QStringLiteral( ".*?\\(\\*\\.([a-zA-Z0-9._]+).*" ) );
4032  QRegularExpressionMatch match = rx.match( mFileFilter );
4033  if ( !match.hasMatch() )
4034  return QStringLiteral( "file" );
4035 
4036  return match.captured( 1 );
4037 }
4038 
4040 {
4041  return mFileFilter;
4042 }
4043 
4045 {
4046  mFileFilter = fileFilter;
4047 }
4048 
4050 {
4052  map.insert( QStringLiteral( "file_filter" ), mFileFilter );
4053  return map;
4054 }
4055 
4057 {
4059  mFileFilter = map.value( QStringLiteral( "file_filter" ) ).toString();
4060  return true;
4061 
4062 }
4063 
4064 QgsProcessingParameterFileDestination *QgsProcessingParameterFileDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4065 {
4066  return new QgsProcessingParameterFileDestination( name, description, QString(), definition.isEmpty() ? QVariant() : definition, isOptional );
4067 }
4068 
4070  : QgsProcessingDestinationParameter( name, description, defaultValue, optional )
4071 {}
4072 
4074 {
4075  return new QgsProcessingParameterFolderDestination( *this );
4076 }
4077 
4079 {
4080  QVariant var = input;
4081  if ( !var.isValid() )
4082  return mFlags & FlagOptional;
4083 
4084  if ( var.canConvert<QgsProperty>() )
4085  {
4086  QgsProperty p = var.value< QgsProperty >();
4088  {
4089  var = p.staticValue();
4090  }
4091  else
4092  {
4093  return true;
4094  }
4095  }
4096 
4097  if ( var.type() != QVariant::String )
4098  return false;
4099 
4100  if ( var.toString().isEmpty() )
4101  return mFlags & FlagOptional;
4102 
4103  return true;
4104 }
4105 
4107 {
4108  return new QgsProcessingOutputFolder( name(), description() );
4109 }
4110 
4112 {
4113  return QString();
4114 }
4115 
4116 QgsProcessingParameterFolderDestination *QgsProcessingParameterFolderDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4117 {
4118  return new QgsProcessingParameterFolderDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
4119 }
4120 
4121 QgsProcessingDestinationParameter::QgsProcessingDestinationParameter( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, bool createByDefault )
4122  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4123  , mCreateByDefault( createByDefault )
4124 {
4125 
4126 }
4127 
4129 {
4131  map.insert( QStringLiteral( "supports_non_file_outputs" ), mSupportsNonFileBasedOutputs );
4132  map.insert( QStringLiteral( "create_by_default" ), mCreateByDefault );
4133  return map;
4134 }
4135 
4137 {
4139  mSupportsNonFileBasedOutputs = map.value( QStringLiteral( "supports_non_file_outputs" ) ).toBool();
4140  mCreateByDefault = map.value( QStringLiteral( "create_by_default" ), QStringLiteral( "1" ) ).toBool();
4141  return true;
4142 }
4143 
4145 {
4146  if ( defaultFileExtension().isEmpty() )
4147  {
4149  }
4150  else
4151  {
4153  }
4154 }
4155 
4157 {
4158  return mCreateByDefault;
4159 }
4160 
4162 {
4163  mCreateByDefault = createByDefault;
4164 }
4165 
4167  : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
4168  , mDataType( type )
4169 {
4170 
4171 }
4172 
4174 {
4175  return new QgsProcessingParameterVectorDestination( *this );
4176 }
4177 
4179 {
4180  QVariant var = input;
4181  if ( !var.isValid() )
4182  return mFlags & FlagOptional;
4183 
4184  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
4185  {
4187  var = fromVar.sink;
4188  }
4189 
4190  if ( var.canConvert<QgsProperty>() )
4191  {
4192  QgsProperty p = var.value< QgsProperty >();
4194  {
4195  var = p.staticValue();
4196  }
4197  else
4198  {
4199  return true;
4200  }
4201  }
4202 
4203  if ( var.type() != QVariant::String )
4204  return false;
4205 
4206  if ( var.toString().isEmpty() )
4207  return mFlags & FlagOptional;
4208 
4209  return true;
4210 }
4211 
4213 {
4214  if ( !value.isValid() )
4215  return QStringLiteral( "None" );
4216 
4217  if ( value.canConvert<QgsProperty>() )
4218  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4219 
4220  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
4221  {
4223  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
4224  {
4225  return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
4226  }
4227  else
4228  {
4229  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
4230  }
4231  }
4232 
4233  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
4234 }
4235 
4237 {
4238  QString code = QStringLiteral( "##%1=" ).arg( mName );
4239  if ( mFlags & FlagOptional )
4240  code += QStringLiteral( "optional " );
4241  code += QStringLiteral( "vectorDestination " );
4242 
4243  switch ( mDataType )
4244  {
4246  code += QStringLiteral( "point " );
4247  break;
4248 
4250  code += QStringLiteral( "line " );
4251  break;
4252 
4254  code += QStringLiteral( "polygon " );
4255  break;
4256 
4257  default:
4258  break;
4259  }
4260 
4261  code += mDefault.toString();
4262  return code.trimmed();
4263 }
4264 
4266 {
4267  return new QgsProcessingOutputVectorLayer( name(), description(), mDataType );
4268 }
4269 
4271 {
4272  if ( originalProvider() )
4273  {
4275  }
4276  else if ( QgsProcessingProvider *p = provider() )
4277  {
4278  return p->defaultVectorFileExtension( hasGeometry() );
4279  }
4280  else
4281  {
4282  QgsSettings settings;
4283  if ( hasGeometry() )
4284  {
4285  return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
4286  }
4287  else
4288  {
4289  return QStringLiteral( "dbf" );
4290  }
4291  }
4292 }
4293 
4295 {
4296  if ( originalProvider() )
4297  {
4298  if ( hasGeometry() )
4300  else
4302  }
4303  else if ( QgsProcessingProvider *p = provider() )
4304  {
4305  if ( hasGeometry() )
4306  return p->supportedOutputVectorLayerExtensions();
4307  else
4308  return p->supportedOutputTableExtensions();
4309  }
4310  else
4311  {
4313  }
4314 }
4315 
4317 {
4318  return mDataType;
4319 }
4320 
4322 {
4323  switch ( mDataType )
4324  {
4330  return true;
4331 
4335  return false;
4336  }
4337  return true;
4338 }
4339 
4341 {
4342  mDataType = type;
4343 }
4344 
4346 {
4348  map.insert( QStringLiteral( "data_type" ), mDataType );
4349  return map;
4350 }
4351 
4353 {
4355  mDataType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
4356  return true;
4357 }
4358 
4359 QgsProcessingParameterVectorDestination *QgsProcessingParameterVectorDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4360 {
4362  QString def = definition;
4363  if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
4364  {
4366  def = def.mid( 6 );
4367  }
4368  else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
4369  {
4371  def = def.mid( 5 );
4372  }
4373  else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
4374  {
4376  def = def.mid( 8 );
4377  }
4378 
4379  return new QgsProcessingParameterVectorDestination( name, description, type, definition, isOptional );
4380 }
4381 
4382 QgsProcessingParameterBand::QgsProcessingParameterBand( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional, bool allowMultiple )
4383  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4384  , mParentLayerParameterName( parentLayerParameterName )
4385  , mAllowMultiple( allowMultiple )
4386 {
4387 
4388 }
4389 
4391 {
4392  return new QgsProcessingParameterBand( *this );
4393 }
4394 
4396 {
4397  if ( !input.isValid() )
4398  return mFlags & FlagOptional;
4399 
4400  if ( input.canConvert<QgsProperty>() )
4401  {
4402  return true;
4403  }
4404 
4405  if ( input.type() == QVariant::List || input.type() == QVariant::StringList )
4406  {
4407  if ( !mAllowMultiple )
4408  return false;
4409 
4410  if ( input.toList().isEmpty() && !( mFlags & FlagOptional ) )
4411  return false;
4412  }
4413  else
4414  {
4415  bool ok = false;
4416  double res = input.toInt( &ok );
4417  Q_UNUSED( res );
4418  if ( !ok )
4419  return mFlags & FlagOptional;
4420  }
4421  return true;
4422 }
4423 
4425 {
4426  return mAllowMultiple;
4427 }
4428 
4430 {
4431  mAllowMultiple = allowMultiple;
4432 }
4433 
4435 {
4436  if ( !value.isValid() )
4437  return QStringLiteral( "None" );
4438 
4439  if ( value.canConvert<QgsProperty>() )
4440  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4441 
4442  if ( value.type() == QVariant::List )
4443  {
4444  QStringList parts;
4445  QVariantList values = value.toList();
4446  for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
4447  {
4448  parts << QString::number( static_cast< int >( it->toDouble() ) );
4449  }
4450  return parts.join( ',' ).prepend( '[' ).append( ']' );
4451  }
4452  else if ( value.type() == QVariant::StringList )
4453  {
4454  QStringList parts;
4455  QStringList values = value.toStringList();
4456  for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
4457  {
4458  parts << QString::number( static_cast< int >( it->toDouble() ) );
4459  }
4460  return parts.join( ',' ).prepend( '[' ).append( ']' );
4461  }
4462 
4463  return value.toString();
4464 }
4465 
4467 {
4468  QString code = QStringLiteral( "##%1=" ).arg( mName );
4469  if ( mFlags & FlagOptional )
4470  code += QStringLiteral( "optional " );
4471  code += QStringLiteral( "band " );
4472 
4473  if ( mAllowMultiple )
4474  code += QStringLiteral( "multiple " );
4475 
4476  code += mParentLayerParameterName + ' ';
4477 
4478  code += mDefault.toString();
4479  return code.trimmed();
4480 }
4481 
4483 {
4484  QStringList depends;
4485  if ( !mParentLayerParameterName.isEmpty() )
4486  depends << mParentLayerParameterName;
4487  return depends;
4488 }
4489 
4491 {
4492  return mParentLayerParameterName;
4493 }
4494 
4496 {
4497  mParentLayerParameterName = parentLayerParameterName;
4498 }
4499 
4501 {
4503  map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
4504  map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
4505  return map;
4506 }
4507 
4508 bool QgsProcessingParameterBand::fromVariantMap( const QVariantMap &map )
4509 {
4511  mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
4512  mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
4513  return true;
4514 }
4515 
4516 QgsProcessingParameterBand *QgsProcessingParameterBand::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4517 {
4518  QString parent;
4519  QString def = definition;
4520  bool allowMultiple = false;
4521 
4522  if ( def.startsWith( QLatin1String( "multiple" ), Qt::CaseInsensitive ) )
4523  {
4524  allowMultiple = true;
4525  def = def.mid( 8 ).trimmed();
4526  }
4527 
4528  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
4529  QRegularExpressionMatch m = re.match( def );
4530  if ( m.hasMatch() )
4531  {
4532  parent = m.captured( 1 ).trimmed();
4533  def = m.captured( 2 );
4534  }
4535  else
4536  {
4537  parent = def;
4538  def.clear();
4539  }
4540 
4541  return new QgsProcessingParameterBand( name, description, def.isEmpty() ? QVariant() : def, parent, isOptional, allowMultiple );
4542 }
4543 
4544 //
4545 // QgsProcessingParameterDistance
4546 //
4547 
4548 QgsProcessingParameterDistance::QgsProcessingParameterDistance( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentParameterName, bool optional, double minValue, double maxValue )
4549  : QgsProcessingParameterNumber( name, description, Double, defaultValue, optional, minValue, maxValue )
4550  , mParentParameterName( parentParameterName )
4551 {
4552 
4553 }
4554 
4556 {
4557  return new QgsProcessingParameterDistance( *this );
4558 }
4559 
4561 {
4562  return typeName();
4563 }
4564 
4566 {
4567  QStringList depends;
4568  if ( !mParentParameterName.isEmpty() )
4569  depends << mParentParameterName;
4570  return depends;
4571 }
4572 
4574 {
4575  return mParentParameterName;
4576 }
4577 
4579 {
4580  mParentParameterName = parentParameterName;
4581 }
4582 
4584 {
4585  QVariantMap map = QgsProcessingParameterNumber::toVariantMap();
4586  map.insert( QStringLiteral( "parent" ), mParentParameterName );
4587  map.insert( QStringLiteral( "default_unit" ), static_cast< int >( mDefaultUnit ) );
4588  return map;
4589 }
4590 
4592 {
4594  mParentParameterName = map.value( QStringLiteral( "parent" ) ).toString();
4595  mDefaultUnit = static_cast< QgsUnitTypes::DistanceUnit>( map.value( QStringLiteral( "default_unit" ), QgsUnitTypes::DistanceUnknownUnit ).toInt() );
4596  return true;
4597 }
QgsProcessingParameterDefinition(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterDefinition.
QgsProperty sink
Sink/layer definition.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QgsCoordinateReferenceSystem parameterAsCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a coordinate reference system.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
A boolean parameter for processing algorithms.
Class for parsing and evaluation of expressions (formerly called "search strings").
void setDataTypes(const QList< int > &types)
Sets the geometry types for sources acceptable by the parameter.
static QString typeName()
Returns the type name for the parameter class.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
An input file or folder parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set...
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 rectangle specified with double values.
Definition: qgsrectangle.h:40
Base class for all map layer types.
Definition: qgsmaplayer.h:63
static QString descriptionFromName(const QString &name)
Creates an autogenerated parameter description from a parameter name.
QVariant toVariant() const
Saves this output layer definition to a QVariantMap, wrapped in a QVariant.
static QString parameterAsString(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static string value.
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this provider.
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.
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
bool hasFixedNumberRows() const
Returns whether the table has a fixed number of rows.
static int parameterAsEnum(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a enum value.
static QString typeName()
Returns the type name for the parameter class.
Base class for providing feedback from a processing algorithm.
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...
double maximum() const
Returns the maximum value acceptable by the parameter.
virtual bool fromVariantMap(const QVariantMap &map)
Restores this parameter to a QVariantMap.
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.
static QVariantList parameterAsMatrix(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a matrix/table of values.
Encapsulates settings relating to a feature sink or output raster layer for a processing algorithm...
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString parentParameterName() const
Returns the name of the parent parameter, or an empty string if this is not set.
QString type() const override
Unique parameter type name.
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
bool createFromString(const QString &definition)
Set up this CRS from a string definition.
QString type() const override
Unique parameter type name.
A vector layer or feature source field parameter for processing algorithms.
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.
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.
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.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
void setHasFixedNumberRows(bool hasFixedNumberRows)
Sets whether the table has a fixed number of rows.
QgsProcessingParameterMultipleLayers(const QString &name, const QString &description=QString(), QgsProcessing::SourceType layerType=QgsProcessing::TypeVectorAnyGeometry, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterMultipleLayers.
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this parameter.
QgsProcessingParameterExpression(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentLayerParameterName=QString(), bool optional=false)
Constructor for QgsProcessingParameterExpression.
QString name() const
Returns the name of the parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QString typeName()
Returns the type name for the parameter class.
QgsProcessingParameterRange(const QString &name, const QString &description=QString(), QgsProcessingParameterNumber::Type type=QgsProcessingParameterNumber::Integer, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterRange.
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.
QgsProcessingParameterFeatureSource(const QString &name, const QString &description=QString(), const QList< int > &types=QList< int >(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterFeatureSource.
void setDataType(QgsProcessingParameterNumber::Type dataType)
Sets the acceptable data type for the range.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:171
static QString typeName()
Returns the type name for the parameter class.
LayerHint
Layer type hints.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
static QList< int > parameterAsEnums(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to list of enum values.
QgsPointXY transform(const QgsPointXY &point, TransformDirection direction=ForwardTransform) const SIP_THROW(QgsCsException)
Transform the point from the source CRS to the destination CRS.
static QString typeName()
Returns the type name for the parameter class.
QString type() const override
Unique parameter type name.
QgsProcessing::SourceType layerType() const
Returns the layer type for layers acceptable by the parameter.
double y
Definition: qgspointxy.h:48
A map 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.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
void setParentLayerParameterName(const QString &parentLayerParameterName)
Sets the name of the parent layer parameter.
A class to represent a 2D point.
Definition: qgspointxy.h:43
bool createByDefault() const
Returns true if the destination should be created by default.
A HTML file output for processing algorithms.
An expression parameter for processing algorithms.
A QgsPointXY with associated coordinate reference system.
static QString typeName()
Returns the type name for the parameter class.
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.
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...
An interface for objects which accept features via addFeature(s) methods.
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...
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 isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:435
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter...
QgsProcessingAlgorithm * mAlgorithm
Pointer to algorithm which owns this parameter.
static QString typeName()
Returns the type name for the parameter class.
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
bool multiLine() const
Returns true if the parameter allows multiline strings.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QString typeName()
Returns the type name for the parameter class.
QVariantMap mMetadata
Freeform metadata for parameter. Mostly used by widget wrappers to customize their appearance and beh...
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource, modifying results according to the settings in a QgsProcessingContext.
Container of fields for a vector layer.
Definition: qgsfields.h:42
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.
static QgsRasterLayer * parameterAsRasterLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a raster layer.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
static QgsMapLayer * parameterAsLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a map layer.
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.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString source() const
Returns the source for the layer.
Abstract base class for processing providers.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QStringList headers() const
Returns a list of column headers (if set).
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
QgsProcessingParameterMapLayer(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterMapLayer.
A raster band parameter for Processing algorithms.
QString type() const override
Unique parameter type name.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QVariant toVariant() const
Saves this property to a QVariantMap, wrapped in a QVariant.
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 QgsProcessingParameterDefinition * parameterFromVariantMap(const QVariantMap &map)
Creates a new QgsProcessingParameterDefinition using the configuration from a supplied variant map...
QgsProcessingParameterEnum(const QString &name, const QString &description=QString(), const QStringList &options=QStringList(), bool allowMultiple=false, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterEnum.
QgsProcessingParameterBoolean(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterBoolean.
QVariant staticValue() const
Returns the current static value for the property.
QString type() const override
Unique parameter type name.
bool hasGeometry() const
Returns true if sink is likely to include geometries.
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.
static QString typeName()
Returns the type name for the parameter class.
Type dataType() const
Returns the acceptable data type for the parameter.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
const QgsCoordinateReferenceSystem & crs
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 convertToCompatibleFormat(const QgsVectorLayer *layer, bool selectedFeaturesOnly, const QString &baseName, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingContext &context, QgsProcessingFeedback *feedback)
Converts a source vector layer to a file path to a vector layer of compatible format.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
static QString typeName()
Returns the type name for the parameter class.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
void setMaximum(double maximum)
Sets the maximum value acceptable by the parameter.
void setParentLayerParameterName(const QString &parentLayerParameterName)
Sets the name of the parent layer parameter.
static QString normalizeLayerSource(const QString &source)
Normalizes a layer source string for safe comparison across different operating system environments...
static QgsVectorLayer * parameterAsVectorLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a vector layer.
Base class for all parameter definitions which represent file or layer destinations, e.g.
Abstract base class for processing algorithms.
static QgsPointXY parameterAsPoint(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching definition to a point.
A vector layer output for processing algorithms.
QgsProcessingParameterNumber::Type dataType() const
Returns the acceptable data type for the range.
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.
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 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...
virtual QString defaultVectorFileExtension(bool hasGeometry=true) const
Returns the default file extension to use for vector outputs created by the provider.
QgsProcessing::SourceType dataType() const
Returns the layer type for sinks associated with the parameter.
static QString parameterAsFile(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a file/folder name.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
A feature sink output 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 QList< QgsMapLayer * > parameterAsLayerList(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of map layers.
Files (i.e. non map layer sources, such as text files)
Definition: qgsprocessing.h:52
bool selectedFeaturesOnly
True if only selected features in the source should be used by algorithms.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
double minimum() const
Returns the minimum value acceptable by the parameter.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
void setAllowMultiple(bool allowMultiple)
Sets whether multiple field selections are permitted.
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.
QgsProcessingProvider * provider() const
Returns a pointer to the provider for the algorithm which owns this parameter.
static QString typeName()
Returns the type name for the parameter class.
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 QgsCoordinateReferenceSystem parameterAsExtentCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the coordinate reference system associated with an extent parameter value.
Behavior behavior() const
Returns the parameter behavior (e.g.
A numeric range parameter for processing algorithms.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
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.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
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.
QgsProcessingParameterCrs(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterCrs.
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 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)
Constructor for QgsProcessingParameterFolderDestination.
static QString typeName()
Returns the type name for the parameter class.
static QgsFeatureSink * createFeatureSink(QString &destination, QgsProcessingContext &context, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &createOptions=QVariantMap(), QgsFeatureSink::SinkFlags sinkFlags=nullptr)
Creates a feature sink ready for adding features.
static QString typeName()
Returns the type name for the parameter class.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
Can be inherited by parameters which require limits to their acceptable data types.
QString parentLayerParameterName() const
Returns the name of the parent layer parameter, or an empty string if this is not set...
A raster layer parameter for processing algorithms.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsProcessingParameterFeatureSink(const QString &name, const QString &description=QString(), QgsProcessing::SourceType type=QgsProcessing::TypeVectorAnyGeometry, const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterFeatureSink.
QList< int > dataTypes() const
Returns the geometry types for sources acceptable by the parameter.
QgsProcessingParameterFile(const QString &name, const QString &description=QString(), Behavior behavior=File, const QString &extension=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterFile.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
static QStringList supportedFormatExtensions(RasterFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats.
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.
static QgsCoordinateReferenceSystem parameterAsPointCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the coordinate reference system associated with an point parameter value. ...
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
static QString typeName()
Returns the type name for the parameter class.
virtual bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const
Checks whether the specified input value is acceptable for the parameter.
static QString typeName()
Returns the type name for the parameter class.
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=nullptr)
Evaluates the parameter with matching definition to a feature sink.
QgsProperty source
Source definition.
virtual QgsProcessingParameterDefinition * create(const QString &name) const =0
Creates a new parameter of this type.
QVariantMap toVariantMap() const override
Saves 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.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:176
An enum based parameter for processing algorithms, allowing for selection from predefined values...
bool loadVariant(const QVariant &property)
Loads this property from a QVariantMap, wrapped in a QVariant.
bool allowMultiple() const
Returns true if the parameter allows multiple selected values.
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:161
void setMinimum(double minimum)
Sets the minimum value acceptable by the parameter.
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 QgsRectangle extent() const
Returns the extent of the layer.
static QString typeName()
Returns the type name for the parameter class.
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.
QStringList options() const
Returns the list of acceptable options for the parameter.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterDistance * clone() const override
Creates a clone of the parameter definition.
QgsProcessingProvider * originalProvider() const
Original (source) provider which this parameter has been derived from.
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...
QString fileFilter() const
Returns the file filter string for file destinations compatible with this parameter.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:238
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
Flags flags() const
Returns any flags associated with the parameter.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
A double numeric parameter for distance values.
Type propertyType() const
Returns the property type.
A file output for processing algorithms.
QgsCoordinateReferenceSystem crs
Definition: qgsproject.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).
QgsProcessingDestinationParameter(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingDestinationParameter.
QString parentLayerParameterName() const
Returns the name of the parent layer 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 Python processing scr...
static QList< double > parameterAsRange(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a range of values.
QString type() const override
Unique parameter type name.
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.
static QStringList supportedFormatExtensions(VectorFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats, e.g "shp", "gpkg".
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
virtual QString type() const =0
Unique parameter type name.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
Reads and writes project states.
Definition: qgsproject.h:89
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
Vector polygon layers.
Definition: qgsprocessing.h:50
DataType dataType() const
Returns the acceptable data type for the field.
A vector layer (with or without geometry) parameter for processing algorithms.
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.
virtual QString defaultFileExtension() const =0
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. ...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
virtual QStringList supportedOutputRasterLayerExtensions() const
Returns a list of the raster format file extensions supported by this provider.
bool allowMultiple() const
Returns whether multiple band selections are permitted.
void setParentLayerParameterName(const QString &parentLayerParameterName)
Sets the name of the parent layer parameter.
void setParentParameterName(const QString &parentParameterName)
Sets the name of the parent layer parameter.
A QgsRectangle with associated coordinate reference system.
QgsProcessingAlgorithm * algorithm() const
Returns a pointer to the algorithm which owns this parameter.
QgsProcessingProvider * provider() const
Returns the provider to which this algorithm belongs.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
int minimumNumberInputs() const
Returns the minimum number of layers required for the parameter.
QString description() const
Returns the description for 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.
A coordinate reference system parameter for processing algorithms.
static QgsProcessingFeatureSource * variantToSource(const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue=QVariant())
Converts a variant value to a new feature source.
A store for object properties.
Definition: qgsproperty.h:229
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 rectangular map extent parameter for processing algorithms.
void setMinimumNumberInputs(int minimum)
Sets the minimum number of layers required for 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...
void setLayerType(QgsProcessing::SourceType type)
Sets the layer type for layers acceptable by the parameter.
void setAllowMultiple(bool allowMultiple)
Sets whether multiple band selections are permitted.
Details for layers to load into projects.
void setAllowMultiple(bool allowMultiple)
Sets whether the parameter allows multiple selected values.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
A numeric parameter for processing algorithms.
bool loadVariant(const QVariantMap &map)
Loads this output layer definition from a QVariantMap, wrapped in a QVariant.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
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.
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this parameter.
QgsExpressionContext & expressionContext()
Returns the expression context.
double x
Definition: qgspointxy.h:47
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...
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.
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 fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
QList< int > mDataTypes
List of acceptable data types for the parameter.
Encapsulates settings relating to a feature source input to a processing algorithm.
void setMultiLine(bool multiLine)
Sets whether the parameter allows multiline strings.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
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.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
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.
virtual QString asScriptCode() const
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QString generateTempFilename(const QString &basename)
Returns a temporary filename for a given file, putting it into a temporary folder (creating that fold...
QString type() const override
Unique parameter type name.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:53
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...
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 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...
virtual QStringList supportedOutputTableExtensions() const
Returns a list of the table (geometry-less vector layers) file extensions supported by this provider...
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.
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.
Base class for the definition of processing outputs.
void setCreateByDefault(bool createByDefault)
Sets whether the destination should be created by default.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
Unknown distance unit.
Definition: qgsunittypes.h:64
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...
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter...
QgsProcessingParameterLimitedDataTypes(const QList< int > &types=QList< int >())
Constructor for QgsProcessingParameterLimitedDataTypes, with a list of acceptable data types...
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
void setNumberRows(int rows)
Sets the fixed number of rows in the table.
A vector layer destination parameter, for specifying the destination path for a vector layer created ...
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsGeometry densifyByCount(int extraNodesPerSegment) const
Returns a copy of the geometry which has been densified by adding the specified number of extra nodes...
virtual QVariantMap toVariantMap() const
Saves this parameter to a QVariantMap.
virtual QString generateTemporaryDestination() const
Generates a temporary destination value for this parameter.
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.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
void setDataType(DataType type)
Sets the acceptable data type for the field.
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...
Vector point layers.
Definition: qgsprocessing.h:48
QString asScriptCode() const override
Returns the parameter definition encoded in a string 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 Python processing scr...
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.
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 defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter...
An input feature source (such as vector layers) parameter for processing algorithms.
A folder destination parameter, for specifying the destination path for a folder created by the algor...
QString destinationName
Name to use for sink if it&#39;s to be loaded into a destination project.
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.
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.
Any map layer type (raster or vector)
Definition: qgsprocessing.h:46
Makes metadata of processing parameters available.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QString toolTip() const override
Returns a formatted tooltip for use with the parameter, which gives helpful information like paramete...
QString asExpression() const
Returns an expression string representing the state of the property, or an empty string if the proper...
QgsProject * destinationProject
Destination project.
This class represents a coordinate reference system (CRS).
Base class for the definition of processing parameters.
Vector line layers.
Definition: qgsprocessing.h:49
static QgsProcessingParameterDefinition * parameterFromScriptCode(const QString &code)
Creates a new QgsProcessingParameterDefinition using the configuration from a supplied script code st...
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:53
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 authid() const
Returns the authority identifier for the CRS.
Class for doing transforms between two map coordinate systems.
static double parameterAsDouble(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static double value.
QVariant mDefault
Default value for parameter.
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
static QString parameterAsOutputLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a output layer destination.
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.
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 typeName()
Returns the type name for the parameter class.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
virtual QString toolTip() const
Returns a formatted tooltip for use with the parameter, which gives helpful information like paramete...
static QgsProcessingFeatureSource * parameterAsSource(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a feature source.
bool fromVariantMap(const QVariantMap &map) override
Restores 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.
static QString typeName()
Returns the type name for the parameter class.
static bool parameterAsBool(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static boolean value.
int valueAsInt(const QgsExpressionContext &context, int defaultValue=0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as an integer.
A folder output for processing algorithms.
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 Python processing scr...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
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.
A table (matrix) parameter for processing algorithms.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
int numberRows() const
Returns the fixed number of rows in the table.
QString type() const override
Unique parameter type name.
QVariant defaultValue() const
Returns the default value for the parameter.
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 mDescription
Parameter description.
void setFileFilter(const QString &filter)
Sets 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.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, LayerHint typeHint=UnknownType)
Interprets a string as a map layer within the supplied context.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessing::SourceType dataType() const
Returns the layer type for this created vector 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...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterExtent(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterExtent.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
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)
Constructor for QgsProcessingParameterField.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
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.
Represents a vector layer which manages a vector based data sets.
static QString parameterAsExpression(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to an expression.
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...
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
Invalid (not set) property.
Definition: qgsproperty.h:237
Contains information about the context in which a processing algorithm is executed.
bool supportsNonFileBasedOutput() const
Returns true if the destination parameter supports non filed-based outputs, such as memory layers or ...
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter...
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:166
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 setDataType(QgsProcessing::SourceType type)
Sets the layer type for the created vector layer.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
A string parameter for processing algorithms.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
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...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
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...
QgsProcessingParameterVectorLayer(const QString &name, const QString &description=QString(), const QList< int > &types=QList< int >(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterVectorLayer.
void setDataType(QgsProcessing::SourceType type)
Sets the layer type for the sinks associated with the parameter.
Any vector layer with geometry.
Definition: qgsprocessing.h:47
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.
QString type() const override
Unique parameter type name.
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...
QString generateTemporaryDestination() const override
Generates a temporary destination value for this parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
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.
virtual QString defaultRasterFileExtension() const
Returns the default file extension to use for raster outputs created by the provider.
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:70
QgsProcessingParameterRasterLayer(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterRasterLayer.
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.
QVariantMap createOptions
Map of optional sink/layer creation options, which are passed to the underlying provider when creatin...
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
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.
QgsProcessingParameterRasterDestination(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterRasterDestination.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
bool allowMultiple() const
Returns whether multiple field selections are permitted.
static QString typeName()
Returns the type name for the parameter class.
A raster layer output for processing algorithms.
static QgsProcessingRegistry * processingRegistry()
Returns the application&#39;s processing registry, used for managing processing providers, algorithms, and various parameters and outputs.
static QString parameterAsFileOutput(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a file based output destination.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
static QStringList parameterAsFields(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of fields.
void setHeaders(const QStringList &headers)
Sets the list of column headers.