QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
21#include "qgsprocessingutils.h"
24#include "qgsvectorfilewriter.h"
28#include "qgsrasterfilewriter.h"
29#include "qgsvectorlayer.h"
30#include "qgsmeshlayer.h"
31#include "qgspointcloudlayer.h"
32#include "qgsannotationlayer.h"
33#include "qgsapplication.h"
34#include "qgslayoutmanager.h"
35#include "qgsprintlayout.h"
36#include "qgssymbollayerutils.h"
37#include "qgsfileutils.h"
38#include "qgsproviderregistry.h"
39#include "qgsvariantutils.h"
40#include <functional>
41#include <QRegularExpression>
42
43
45{
46 QVariantMap map;
47 map.insert( QStringLiteral( "source" ), source.toVariant() );
48 map.insert( QStringLiteral( "selected_only" ), selectedFeaturesOnly );
49 map.insert( QStringLiteral( "feature_limit" ), featureLimit );
50 map.insert( QStringLiteral( "flags" ), static_cast< int >( flags ) );
51 map.insert( QStringLiteral( "geometry_check" ), static_cast< int >( geometryCheck ) );
52 return map;
53}
54
56{
57 source.loadVariant( map.value( QStringLiteral( "source" ) ) );
58 selectedFeaturesOnly = map.value( QStringLiteral( "selected_only" ), false ).toBool();
59 featureLimit = map.value( QStringLiteral( "feature_limit" ), -1 ).toLongLong();
60 flags = static_cast< Flags >( map.value( QStringLiteral( "flags" ), 0 ).toInt() );
61 geometryCheck = static_cast< QgsFeatureRequest::InvalidGeometryCheck >( map.value( QStringLiteral( "geometry_check" ), QgsFeatureRequest::GeometryAbortOnInvalid ).toInt() );
62 return true;
63}
64
65
66//
67// QgsProcessingOutputLayerDefinition
68//
69
71{
72 mUseRemapping = true;
73 mRemappingDefinition = definition;
74}
75
77{
78 QVariantMap map;
79 map.insert( QStringLiteral( "sink" ), sink.toVariant() );
80 map.insert( QStringLiteral( "create_options" ), createOptions );
81 if ( mUseRemapping )
82 map.insert( QStringLiteral( "remapping" ), QVariant::fromValue( mRemappingDefinition ) );
83 return map;
84}
85
87{
88 sink.loadVariant( map.value( QStringLiteral( "sink" ) ) );
89 createOptions = map.value( QStringLiteral( "create_options" ) ).toMap();
90 if ( map.contains( QStringLiteral( "remapping" ) ) )
91 {
92 mUseRemapping = true;
93 mRemappingDefinition = map.value( QStringLiteral( "remapping" ) ).value< QgsRemappingSinkDefinition >();
94 }
95 else
96 {
97 mUseRemapping = false;
98 }
99 return true;
100}
101
103{
105 && mUseRemapping == other.mUseRemapping && mRemappingDefinition == other.mRemappingDefinition;
106}
107
109{
110 return !( *this == other );
111}
112
113bool QgsProcessingParameters::isDynamic( const QVariantMap &parameters, const QString &name )
114{
115 const QVariant val = parameters.value( name );
116 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
117 return val.value< QgsProperty >().propertyType() != QgsProperty::StaticProperty;
118 else
119 return false;
120}
121
122QString QgsProcessingParameters::parameterAsString( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
123{
124 if ( !definition )
125 return QString();
126
127 return parameterAsString( definition, parameters.value( definition->name() ), context );
128}
129
130QString QgsProcessingParameters::parameterAsString( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
131{
132 if ( !definition )
133 return QString();
134
135 QVariant val = value;
136 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
137 return val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
138
139 if ( !val.isValid() )
140 {
141 // fall back to default
142 val = definition->defaultValue();
143 }
144
146 {
147 if ( const QgsProcessingDestinationParameter *destParam = dynamic_cast< const QgsProcessingDestinationParameter * >( definition ) )
148 return destParam->generateTemporaryDestination();
149 }
150
151 return val.toString();
152}
153
154QString QgsProcessingParameters::parameterAsExpression( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
155{
156 if ( !definition )
157 return QString();
158
159 return parameterAsExpression( definition, parameters.value( definition->name() ), context );
160}
161
162QString QgsProcessingParameters::parameterAsExpression( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
163{
164 if ( !definition )
165 return QString();
166
167 const QVariant val = value;
168 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
169 return val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
170
171 if ( val.isValid() && !val.toString().isEmpty() )
172 {
173 const QgsExpression e( val.toString() );
174 if ( e.isValid() )
175 return val.toString();
176 }
177
178 // fall back to default
179 return definition->defaultValue().toString();
180}
181
182double QgsProcessingParameters::parameterAsDouble( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
183{
184 if ( !definition )
185 return 0;
186
187 return parameterAsDouble( definition, parameters.value( definition->name() ), context );
188}
189
190double QgsProcessingParameters::parameterAsDouble( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
191{
192 if ( !definition )
193 return 0;
194
195 QVariant val = value;
196 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
197 return val.value< QgsProperty >().valueAsDouble( context.expressionContext(), definition->defaultValue().toDouble() );
198
199 bool ok = false;
200 const double res = val.toDouble( &ok );
201 if ( ok )
202 return res;
203
204 // fall back to default
205 val = definition->defaultValue();
206 return val.toDouble();
207}
208
209int QgsProcessingParameters::parameterAsInt( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
210{
211 if ( !definition )
212 return 0;
213
214 return parameterAsInt( definition, parameters.value( definition->name() ), context );
215}
216
217int QgsProcessingParameters::parameterAsInt( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
218{
219 if ( !definition )
220 return 0;
221
222 QVariant val = value;
223 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
224 return val.value< QgsProperty >().valueAsInt( context.expressionContext(), definition->defaultValue().toInt() );
225
226 bool ok = false;
227 double dbl = val.toDouble( &ok );
228 if ( !ok )
229 {
230 // fall back to default
231 val = definition->defaultValue();
232 dbl = val.toDouble( &ok );
233 }
234
235 //String representations of doubles in QVariant will not convert to int
236 //work around this by first converting to double, and then checking whether the double is convertible to int
237 if ( ok )
238 {
239 const double round = std::round( dbl );
240 if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
241 {
242 //double too large to fit in int
243 return 0;
244 }
245 return static_cast< int >( std::round( dbl ) );
246 }
247
248 return val.toInt();
249}
250
251QList< int > QgsProcessingParameters::parameterAsInts( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
252{
253 if ( !definition )
254 return QList< int >();
255
256 return parameterAsInts( definition, parameters.value( definition->name() ), context );
257}
258
259QList< int > QgsProcessingParameters::parameterAsInts( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
260{
261 if ( !definition )
262 return QList< int >();
263
264 QList< int > resultList;
265 const QVariant val = value;
266 if ( val.isValid() )
267 {
268 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
269 resultList << val.value< QgsProperty >().valueAsInt( context.expressionContext(), definition->defaultValue().toInt() );
270 else if ( val.type() == QVariant::List )
271 {
272 const QVariantList list = val.toList();
273 for ( auto it = list.constBegin(); it != list.constEnd(); ++it )
274 resultList << it->toInt();
275 }
276 else
277 {
278 const QStringList parts = val.toString().split( ';' );
279 for ( auto it = parts.constBegin(); it != parts.constEnd(); ++it )
280 resultList << it->toInt();
281 }
282 }
283
284 if ( resultList.isEmpty() )
285 {
286 // check default
287 if ( definition->defaultValue().isValid() )
288 {
289 if ( definition->defaultValue().type() == QVariant::List )
290 {
291 const QVariantList list = definition->defaultValue().toList();
292 for ( auto it = list.constBegin(); it != list.constEnd(); ++it )
293 resultList << it->toInt();
294 }
295 else
296 {
297 const QStringList parts = definition->defaultValue().toString().split( ';' );
298 for ( auto it = parts.constBegin(); it != parts.constEnd(); ++it )
299 resultList << it->toInt();
300 }
301 }
302 }
303
304 return resultList;
305}
306
307QDateTime QgsProcessingParameters::parameterAsDateTime( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
308{
309 if ( !definition )
310 return QDateTime();
311
312 return parameterAsDateTime( definition, parameters.value( definition->name() ), context );
313}
314
315QDateTime QgsProcessingParameters::parameterAsDateTime( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
316{
317 if ( !definition )
318 return QDateTime();
319
320 QVariant val = value;
321 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
322 val = val.value< QgsProperty >().value( context.expressionContext(), definition->defaultValue() );
323
324 QDateTime d = val.toDateTime();
325 if ( !d.isValid() && val.type() == QVariant::String )
326 {
327 d = QDateTime::fromString( val.toString() );
328 }
329
330 if ( !d.isValid() )
331 {
332 // fall back to default
333 val = definition->defaultValue();
334 d = val.toDateTime();
335 }
336 if ( !d.isValid() && val.type() == QVariant::String )
337 {
338 d = QDateTime::fromString( val.toString() );
339 }
340
341 return d;
342}
343
344QDate QgsProcessingParameters::parameterAsDate( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
345{
346 if ( !definition )
347 return QDate();
348
349 return parameterAsDate( definition, parameters.value( definition->name() ), context );
350}
351
352QDate QgsProcessingParameters::parameterAsDate( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
353{
354 if ( !definition )
355 return QDate();
356
357 QVariant val = value;
358 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
359 val = val.value< QgsProperty >().value( context.expressionContext(), definition->defaultValue() );
360
361 QDate d = val.toDate();
362 if ( !d.isValid() && val.type() == QVariant::String )
363 {
364 d = QDate::fromString( val.toString() );
365 }
366
367 if ( !d.isValid() )
368 {
369 // fall back to default
370 val = definition->defaultValue();
371 d = val.toDate();
372 }
373 if ( !d.isValid() && val.type() == QVariant::String )
374 {
375 d = QDate::fromString( val.toString() );
376 }
377
378 return d;
379}
380
381QTime QgsProcessingParameters::parameterAsTime( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
382{
383 if ( !definition )
384 return QTime();
385
386 return parameterAsTime( definition, parameters.value( definition->name() ), context );
387}
388
389QTime QgsProcessingParameters::parameterAsTime( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
390{
391 if ( !definition )
392 return QTime();
393
394 QVariant val = value;
395 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
396 val = val.value< QgsProperty >().value( context.expressionContext(), definition->defaultValue() );
397
398 QTime d;
399
400 if ( val.type() == QVariant::DateTime )
401 d = val.toDateTime().time();
402 else
403 d = val.toTime();
404
405 if ( !d.isValid() && val.type() == QVariant::String )
406 {
407 d = QTime::fromString( val.toString() );
408 }
409
410 if ( !d.isValid() )
411 {
412 // fall back to default
413 val = definition->defaultValue();
414 d = val.toTime();
415 }
416 if ( !d.isValid() && val.type() == QVariant::String )
417 {
418 d = QTime::fromString( val.toString() );
419 }
420
421 return d;
422}
423
424int QgsProcessingParameters::parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
425{
426 if ( !definition )
427 return 0;
428
429 return parameterAsEnum( definition, parameters.value( definition->name() ), context );
430}
431
432int QgsProcessingParameters::parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
433{
434 if ( !definition )
435 return 0;
436
437 const int val = parameterAsInt( definition, value, context );
438 const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
439 if ( enumDef && val >= enumDef->options().size() )
440 {
441 return enumDef->defaultValue().toInt();
442 }
443 return val;
444}
445
446QList<int> QgsProcessingParameters::parameterAsEnums( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
447{
448 if ( !definition )
449 return QList<int>();
450
451 return parameterAsEnums( definition, parameters.value( definition->name() ), context );
452}
453
454QList<int> QgsProcessingParameters::parameterAsEnums( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
455{
456 if ( !definition )
457 return QList<int>();
458
459 QVariantList resultList;
460 const QVariant val = value;
461 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
462 resultList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
463 else if ( val.type() == QVariant::List )
464 {
465 const auto constToList = val.toList();
466 for ( const QVariant &var : constToList )
467 resultList << var;
468 }
469 else if ( val.type() == QVariant::String )
470 {
471 const auto constSplit = val.toString().split( ',' );
472 for ( const QString &var : constSplit )
473 resultList << var;
474 }
475 else
476 resultList << val;
477
478 if ( resultList.isEmpty() )
479 return QList< int >();
480
481 if ( ( !val.isValid() || !resultList.at( 0 ).isValid() ) && definition )
482 {
483 resultList.clear();
484 // check default
485 if ( definition->defaultValue().type() == QVariant::List )
486 {
487 const auto constToList = definition->defaultValue().toList();
488 for ( const QVariant &var : constToList )
489 resultList << var;
490 }
491 else if ( definition->defaultValue().type() == QVariant::String )
492 {
493 const auto constSplit = definition->defaultValue().toString().split( ',' );
494 for ( const QString &var : constSplit )
495 resultList << var;
496 }
497 else
498 resultList << definition->defaultValue();
499 }
500
501 QList< int > result;
502 const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
503 const auto constResultList = resultList;
504 for ( const QVariant &var : constResultList )
505 {
506 const int resInt = var.toInt();
507 if ( !enumDef || resInt < enumDef->options().size() )
508 {
509 result << resInt;
510 }
511 }
512 return result;
513}
514
515QString QgsProcessingParameters::parameterAsEnumString( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
516{
517 if ( !definition )
518 return QString();
519
520 return parameterAsEnumString( definition, parameters.value( definition->name() ), context );
521}
522
523QString QgsProcessingParameters::parameterAsEnumString( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
524{
525 if ( !definition )
526 return QString();
527
528 QString enumText = parameterAsString( definition, value, context );
529 const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
530 if ( enumText.isEmpty() || !enumDef->options().contains( enumText ) )
531 enumText = definition->defaultValue().toString();
532
533 return enumText;
534}
535
536QStringList QgsProcessingParameters::parameterAsEnumStrings( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
537{
538 if ( !definition )
539 return QStringList();
540
541 return parameterAsEnumStrings( definition, parameters.value( definition->name() ), context );
542}
543
544QStringList QgsProcessingParameters::parameterAsEnumStrings( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
545{
546 if ( !definition )
547 return QStringList();
548
549 const QVariant val = value;
550
551 QStringList enumValues;
552
553 std::function< void( const QVariant &var ) > processVariant;
554 processVariant = [ &enumValues, &context, &definition, &processVariant ]( const QVariant & var )
555 {
556 if ( var.type() == QVariant::List )
557 {
558 const auto constToList = var.toList();
559 for ( const QVariant &listVar : constToList )
560 {
561 processVariant( listVar );
562 }
563 }
564 else if ( var.type() == QVariant::StringList )
565 {
566 const auto constToStringList = var.toStringList();
567 for ( const QString &s : constToStringList )
568 {
569 processVariant( s );
570 }
571 }
572 else if ( var.userType() == QMetaType::type( "QgsProperty" ) )
573 processVariant( var.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() ) );
574 else
575 {
576 const QStringList parts = var.toString().split( ',' );
577 for ( const QString &s : parts )
578 {
579 enumValues << s;
580 }
581 }
582 };
583
584 processVariant( val );
585
586 const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
587 // check that values are valid enum values. The resulting set will be empty
588 // if all values are present in the enumDef->options(), otherwise it will contain
589 // values which are invalid
590 const QStringList options = enumDef->options();
591 const QSet<QString> subtraction = QSet<QString>( enumValues.begin(), enumValues.end() ).subtract( QSet<QString>( options.begin(), options.end() ) );
592
593 if ( enumValues.isEmpty() || !subtraction.isEmpty() )
594 {
595 enumValues.clear();
596 processVariant( definition->defaultValue() );
597 }
598
599 return enumValues;
600}
601
602bool QgsProcessingParameters::parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
603{
604 if ( !definition )
605 return false;
606
607 return parameterAsBool( definition, parameters.value( definition->name() ), context );
608}
609
610bool QgsProcessingParameters::parameterAsBoolean( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
611{
612 if ( !definition )
613 return false;
614
615 return parameterAsBoolean( definition, parameters.value( definition->name() ), context );
616}
617
618bool QgsProcessingParameters::parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
619{
620 if ( !definition )
621 return false;
622
623 const QVariant def = definition->defaultValue();
624
625 const QVariant val = value;
626 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
627 return val.value< QgsProperty >().valueAsBool( context.expressionContext(), def.toBool() );
628 else if ( val.isValid() )
629 return val.toBool();
630 else
631 return def.toBool();
632}
633
634bool QgsProcessingParameters::parameterAsBoolean( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
635{
636 if ( !definition )
637 return false;
638
639 const QVariant def = definition->defaultValue();
640
641 const QVariant val = value;
642 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
643 return val.value< QgsProperty >().valueAsBool( context.expressionContext(), def.toBool() );
644 else if ( val.isValid() )
645 return val.toBool();
646 else
647 return def.toBool();
648}
649
650QgsFeatureSink *QgsProcessingParameters::parameterAsSink( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsFields &fields,
652 QgsProcessingContext &context, QString &destinationIdentifier, QgsFeatureSink::SinkFlags sinkFlags,
653 const QVariantMap &createOptions, const QStringList &datasourceOptions, const QStringList &layerOptions )
654{
655 QVariant val;
656 if ( definition )
657 {
658 val = parameters.value( definition->name() );
659 }
660
661 return parameterAsSink( definition, val, fields, geometryType, crs, context, destinationIdentifier, sinkFlags, createOptions, datasourceOptions, layerOptions );
662}
663
664QgsFeatureSink *QgsProcessingParameters::parameterAsSink( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context, QString &destinationIdentifier, QgsFeatureSink::SinkFlags sinkFlags, const QVariantMap &createOptions, const QStringList &datasourceOptions, const QStringList &layerOptions )
665{
666 QVariantMap options = createOptions;
667 QVariant val = value;
668
669 QgsProject *destinationProject = nullptr;
670 QString destName;
671 QgsRemappingSinkDefinition remapDefinition;
672 bool useRemapDefinition = false;
673 if ( val.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
674 {
675 // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
676 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
677 destinationProject = fromVar.destinationProject;
678 options = fromVar.createOptions;
679
680 val = fromVar.sink;
681 destName = fromVar.destinationName;
682 if ( fromVar.useRemapping() )
683 {
684 useRemapDefinition = true;
685 remapDefinition = fromVar.remappingDefinition();
686 }
687 }
688
689 QString dest;
690 if ( definition && val.userType() == QMetaType::type( "QgsProperty" ) )
691 {
692 dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
693 }
694 else if ( !val.isValid() || val.toString().isEmpty() )
695 {
696 if ( definition && definition->flags() & QgsProcessingParameterDefinition::FlagOptional && !definition->defaultValue().isValid() )
697 {
698 // unset, optional sink, no default => no sink
699 return nullptr;
700 }
701 // fall back to default
702 if ( !definition )
703 {
704 throw QgsProcessingException( QObject::tr( "No parameter definition for the sink" ) );
705 }
706 dest = definition->defaultValue().toString();
707 }
708 else
709 {
710 dest = val.toString();
711 }
713 {
714 if ( const QgsProcessingDestinationParameter *destParam = dynamic_cast< const QgsProcessingDestinationParameter * >( definition ) )
715 dest = destParam->generateTemporaryDestination();
716 }
717
718 if ( dest.isEmpty() )
719 return nullptr;
720
721 std::unique_ptr< QgsFeatureSink > sink( QgsProcessingUtils::createFeatureSink( dest, context, fields, geometryType, crs, options, datasourceOptions, layerOptions, sinkFlags, useRemapDefinition ? &remapDefinition : nullptr ) );
722 destinationIdentifier = dest;
723
724 if ( destinationProject )
725 {
726 if ( destName.isEmpty() && definition )
727 {
728 destName = definition->description();
729 }
730 QString outputName;
731 if ( definition )
732 outputName = definition->name();
733 context.addLayerToLoadOnCompletion( destinationIdentifier, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName, QgsProcessingUtils::LayerHint::Vector ) );
734 }
735
736 return sink.release();
737}
738
740{
741 if ( !definition )
742 return nullptr;
743
744 return parameterAsSource( definition, parameters.value( definition->name() ), context );
745}
746
748{
749 if ( !definition )
750 return nullptr;
751
752 return QgsProcessingUtils::variantToSource( value, context, definition->defaultValue() );
753}
754
755QString parameterAsCompatibleSourceLayerPathInternal( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback, QString *layerName )
756{
757 if ( !definition )
758 return QString();
759
760 QVariant val = parameters.value( definition->name() );
761
762 bool selectedFeaturesOnly = false;
763 long long featureLimit = -1;
764 if ( val.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
765 {
766 // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
767 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
768 selectedFeaturesOnly = fromVar.selectedFeaturesOnly;
769 featureLimit = fromVar.featureLimit;
770 val = fromVar.source;
771 }
772 else if ( val.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
773 {
774 // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
775 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
776 val = fromVar.sink;
777 }
778
779 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
780 {
781 val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
782 }
783
784 QgsVectorLayer *vl = nullptr;
785 vl = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) );
786
787 if ( !vl )
788 {
789 QString layerRef;
790 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
791 {
792 layerRef = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
793 }
794 else if ( !val.isValid() || val.toString().isEmpty() )
795 {
796 // fall back to default
797 val = definition->defaultValue();
798
799 // default value may be a vector layer
800 vl = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) );
801 if ( !vl )
802 layerRef = definition->defaultValue().toString();
803 }
804 else
805 {
806 layerRef = val.toString();
807 }
808
809 if ( !vl )
810 {
811 if ( layerRef.isEmpty() )
812 return QString();
813
814 vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context, true, QgsProcessingUtils::LayerHint::Vector ) );
815 }
816 }
817
818 if ( !vl )
819 return QString();
820
821 if ( layerName )
822 return QgsProcessingUtils::convertToCompatibleFormatAndLayerName( vl, selectedFeaturesOnly, definition->name(),
823 compatibleFormats, preferredFormat, context, feedback, *layerName, featureLimit );
824 else
825 return QgsProcessingUtils::convertToCompatibleFormat( vl, selectedFeaturesOnly, definition->name(),
826 compatibleFormats, preferredFormat, context, feedback, featureLimit );
827}
828
829QString QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback )
830{
831 return parameterAsCompatibleSourceLayerPathInternal( definition, parameters, context, compatibleFormats, preferredFormat, feedback, nullptr );
832}
833
834QString QgsProcessingParameters::parameterAsCompatibleSourceLayerPathAndLayerName( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback, QString *layerName )
835{
836 QString *destLayer = layerName;
837 QString tmp;
838 if ( destLayer )
839 destLayer->clear();
840 else
841 destLayer = &tmp;
842
843 return parameterAsCompatibleSourceLayerPathInternal( definition, parameters, context, compatibleFormats, preferredFormat, feedback, destLayer );
844}
845
847{
848 if ( !definition )
849 return nullptr;
850
851 return parameterAsLayer( definition, parameters.value( definition->name() ), context, layerHint );
852}
853
855{
856 if ( !definition )
857 return nullptr;
858
859 QVariant val = value;
860 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
861 {
862 val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
863 }
864
865 if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
866 {
867 return layer;
868 }
869
870 if ( val.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
871 {
872 // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
873 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
874 val = fromVar.sink;
875 }
876
877 if ( val.userType() == QMetaType::type( "QgsProperty" ) && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
878 {
879 val = val.value< QgsProperty >().staticValue();
880 }
881
882 if ( !val.isValid() || val.toString().isEmpty() )
883 {
884 // fall back to default
885 val = definition->defaultValue();
886 }
887
888 if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
889 {
890 return layer;
891 }
892
893 QString layerRef = val.toString();
894 if ( layerRef.isEmpty() )
895 layerRef = definition->defaultValue().toString();
896
897 if ( layerRef.isEmpty() )
898 return nullptr;
899
900 return QgsProcessingUtils::mapLayerFromString( layerRef, context, true, layerHint );
901}
902
904{
905 return qobject_cast< QgsRasterLayer *>( parameterAsLayer( definition, parameters, context, QgsProcessingUtils::LayerHint::Raster ) );
906}
907
909{
910 return qobject_cast< QgsRasterLayer *>( parameterAsLayer( definition, value, context, QgsProcessingUtils::LayerHint::Raster ) );
911}
912
914{
915 return qobject_cast< QgsMeshLayer *>( parameterAsLayer( definition, parameters, context, QgsProcessingUtils::LayerHint::Mesh ) );
916}
917
919{
920 return qobject_cast< QgsMeshLayer *>( parameterAsLayer( definition, value, context, QgsProcessingUtils::LayerHint::Mesh ) );
921}
922
923QString QgsProcessingParameters::parameterAsOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
924{
925 QVariant val;
926 if ( definition )
927 {
928 val = parameters.value( definition->name() );
929 }
930 return parameterAsOutputLayer( definition, val, context );
931}
932
934{
935 QVariant val = value;
936
937 QgsProject *destinationProject = nullptr;
938 QString destName;
939 if ( val.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
940 {
941 // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
942 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
943 destinationProject = fromVar.destinationProject;
944 val = fromVar.sink;
945 destName = fromVar.destinationName;
946 }
947
948 QString dest;
949 if ( definition && val.userType() == QMetaType::type( "QgsProperty" ) )
950 {
951 dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
952 }
953 else if ( definition && ( !val.isValid() || val.toString().isEmpty() ) )
954 {
955 // fall back to default
956 dest = definition->defaultValue().toString();
957 }
958 else
959 {
960 dest = val.toString();
961 }
963 {
964 if ( const QgsProcessingDestinationParameter *destParam = dynamic_cast< const QgsProcessingDestinationParameter * >( definition ) )
965 dest = destParam->generateTemporaryDestination();
966 }
967
968 if ( destinationProject )
969 {
970 QString outputName;
971 if ( destName.isEmpty() && definition )
972 {
973 destName = definition->description();
974 }
975 if ( definition )
976 outputName = definition->name();
977
979 if ( definition && definition->type() == QgsProcessingParameterVectorDestination::typeName() )
981 else if ( definition && definition->type() == QgsProcessingParameterRasterDestination::typeName() )
983 else if ( definition && definition->type() == QgsProcessingParameterPointCloudDestination::typeName() )
985
986 context.addLayerToLoadOnCompletion( dest, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName, layerTypeHint ) );
987 }
988
989 return dest;
990}
991
992QString QgsProcessingParameters::parameterAsFileOutput( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
993{
994 QVariant val;
995 if ( definition )
996 {
997 val = parameters.value( definition->name() );
998 }
999 return parameterAsFileOutput( definition, val, context );
1000}
1001
1003{
1004 QVariant val = value;
1005
1006 if ( val.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
1007 {
1008 // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1009 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
1010 val = fromVar.sink;
1011 }
1012
1013 QString dest;
1014 if ( definition && val.userType() == QMetaType::type( "QgsProperty" ) )
1015 {
1016 dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1017 }
1018 else if ( definition && ( !val.isValid() || val.toString().isEmpty() ) )
1019 {
1020 // fall back to default
1021 dest = definition->defaultValue().toString();
1022 }
1023 else
1024 {
1025 dest = val.toString();
1026 }
1027 if ( dest == QgsProcessing::TEMPORARY_OUTPUT )
1028 {
1029 if ( const QgsProcessingDestinationParameter *destParam = dynamic_cast< const QgsProcessingDestinationParameter * >( definition ) )
1030 dest = destParam->generateTemporaryDestination();
1031 }
1032 return dest;
1033}
1034
1036{
1037 return qobject_cast< QgsVectorLayer *>( parameterAsLayer( definition, parameters, context, QgsProcessingUtils::LayerHint::Vector ) );
1038}
1039
1041{
1042 return qobject_cast< QgsVectorLayer *>( parameterAsLayer( definition, value, context, QgsProcessingUtils::LayerHint::Vector ) );
1043}
1044
1046{
1047 if ( !definition )
1049
1050 return parameterAsCrs( definition, parameters.value( definition->name() ), context );
1051}
1052
1054{
1055 if ( !definition )
1057
1058 return QgsProcessingUtils::variantToCrs( value, context, definition->defaultValue() );
1059}
1060
1063{
1064 if ( !definition )
1065 return QgsRectangle();
1066
1067 return parameterAsExtent( definition, parameters.value( definition->name() ), context, crs );
1068}
1069
1071{
1072 if ( !definition )
1073 return QgsRectangle();
1074
1075 QVariant val = value;
1076
1077 if ( val.userType() == QMetaType::type( "QgsRectangle" ) )
1078 {
1079 return val.value<QgsRectangle>();
1080 }
1081 if ( val.userType() == QMetaType::type( "QgsGeometry" ) )
1082 {
1083 const QgsGeometry geom = val.value<QgsGeometry>();
1084 if ( !geom.isNull() )
1085 return geom.boundingBox();
1086 }
1087 if ( val.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
1088 {
1089 const QgsReferencedRectangle rr = val.value<QgsReferencedRectangle>();
1090 if ( crs.isValid() && rr.crs().isValid() && crs != rr.crs() )
1091 {
1092 QgsCoordinateTransform ct( rr.crs(), crs, context.project() );
1094 try
1095 {
1096 return ct.transformBoundingBox( rr );
1097 }
1098 catch ( QgsCsException & )
1099 {
1100 QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
1101 }
1102 }
1103 return rr;
1104 }
1105
1106 if ( val.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
1107 {
1108 // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
1109 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
1110 val = fromVar.source;
1111 }
1112 else if ( val.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
1113 {
1114 // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1115 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
1116 val = fromVar.sink;
1117 }
1118
1119 if ( val.userType() == QMetaType::type( "QgsProperty" ) && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
1120 {
1121 val = val.value< QgsProperty >().staticValue();
1122 }
1123
1124 // maybe parameter is a direct layer value?
1125 QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) );
1126
1127 QString rectText;
1128 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
1129 rectText = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1130 else
1131 rectText = val.toString();
1132
1133 if ( rectText.isEmpty() && !layer )
1134 return QgsRectangle();
1135
1136 const QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
1137 const QRegularExpressionMatch match = rx.match( rectText );
1138 if ( match.hasMatch() )
1139 {
1140 bool xMinOk = false;
1141 const double xMin = match.captured( 1 ).toDouble( &xMinOk );
1142 bool xMaxOk = false;
1143 const double xMax = match.captured( 2 ).toDouble( &xMaxOk );
1144 bool yMinOk = false;
1145 const double yMin = match.captured( 3 ).toDouble( &yMinOk );
1146 bool yMaxOk = false;
1147 const double yMax = match.captured( 4 ).toDouble( &yMaxOk );
1148 if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
1149 {
1150 const QgsRectangle rect( xMin, yMin, xMax, yMax );
1151 const QgsCoordinateReferenceSystem rectCrs( match.captured( 5 ) );
1152 if ( crs.isValid() && rectCrs.isValid() && crs != rectCrs )
1153 {
1154 QgsCoordinateTransform ct( rectCrs, crs, context.project() );
1156 try
1157 {
1158 return ct.transformBoundingBox( rect );
1159 }
1160 catch ( QgsCsException & )
1161 {
1162 QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
1163 }
1164 }
1165 return rect;
1166 }
1167 }
1168
1169 // try as layer extent
1170 if ( !layer )
1171 layer = QgsProcessingUtils::mapLayerFromString( rectText, context );
1172
1173 if ( layer )
1174 {
1175 const QgsRectangle rect = layer->extent();
1176 if ( crs.isValid() && layer->crs().isValid() && crs != layer->crs() )
1177 {
1178 QgsCoordinateTransform ct( layer->crs(), crs, context.project() );
1180 try
1181 {
1182 return ct.transformBoundingBox( rect );
1183 }
1184 catch ( QgsCsException & )
1185 {
1186 QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
1187 }
1188 }
1189 return rect;
1190 }
1191 return QgsRectangle();
1192}
1193
1195{
1196 if ( !definition )
1197 return QgsGeometry();
1198
1199 QVariant val = parameters.value( definition->name() );
1200
1201 if ( val.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
1202 {
1203 const QgsReferencedRectangle rr = val.value<QgsReferencedRectangle>();
1205 if ( crs.isValid() && rr.crs().isValid() && crs != rr.crs() )
1206 {
1207 g = g.densifyByCount( 20 );
1208 const QgsCoordinateTransform ct( rr.crs(), crs, context.project() );
1209 try
1210 {
1211 g.transform( ct );
1212 }
1213 catch ( QgsCsException & )
1214 {
1215 QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
1216 }
1217 return g;
1218 }
1219 }
1220
1221 if ( val.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
1222 {
1223 // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
1224 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
1225 val = fromVar.source;
1226 }
1227 else if ( val.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
1228 {
1229 // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1230 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
1231 val = fromVar.sink;
1232 }
1233
1234 if ( val.userType() == QMetaType::type( "QgsProperty" ) && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
1235 {
1236 val = val.value< QgsProperty >().staticValue();
1237 }
1238
1239 QString rectText;
1240 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
1241 rectText = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1242 else
1243 rectText = val.toString();
1244
1245 if ( !rectText.isEmpty() )
1246 {
1247 const QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
1248 const QRegularExpressionMatch match = rx.match( rectText );
1249 if ( match.hasMatch() )
1250 {
1251 bool xMinOk = false;
1252 const double xMin = match.captured( 1 ).toDouble( &xMinOk );
1253 bool xMaxOk = false;
1254 const double xMax = match.captured( 2 ).toDouble( &xMaxOk );
1255 bool yMinOk = false;
1256 const double yMin = match.captured( 3 ).toDouble( &yMinOk );
1257 bool yMaxOk = false;
1258 const double yMax = match.captured( 4 ).toDouble( &yMaxOk );
1259 if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
1260 {
1261 const QgsRectangle rect( xMin, yMin, xMax, yMax );
1262 const QgsCoordinateReferenceSystem rectCrs( match.captured( 5 ) );
1264 if ( crs.isValid() && rectCrs.isValid() && crs != rectCrs )
1265 {
1266 g = g.densifyByCount( 20 );
1267 const QgsCoordinateTransform ct( rectCrs, crs, context.project() );
1268 try
1269 {
1270 g.transform( ct );
1271 }
1272 catch ( QgsCsException & )
1273 {
1274 QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
1275 }
1276 return g;
1277 }
1278 }
1279 }
1280 }
1281
1282 // try as layer extent
1283
1284 // maybe parameter is a direct layer value?
1285 QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) );
1286 if ( !layer )
1287 layer = QgsProcessingUtils::mapLayerFromString( rectText, context );
1288
1289 if ( layer )
1290 {
1291 const QgsRectangle rect = layer->extent();
1293 if ( crs.isValid() && layer->crs().isValid() && crs != layer->crs() )
1294 {
1295 g = g.densifyByCount( 20 );
1296 const QgsCoordinateTransform ct( layer->crs(), crs, context.project() );
1297 try
1298 {
1299 g.transform( ct );
1300 }
1301 catch ( QgsCsException & )
1302 {
1303 QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
1304 }
1305 }
1306 return g;
1307 }
1308
1309 return QgsGeometry::fromRect( parameterAsExtent( definition, parameters, context, crs ) );
1310}
1311
1313{
1314 const QVariant val = parameters.value( definition->name() );
1315 return parameterAsExtentCrs( definition, val, context );
1316}
1317
1319{
1320 QVariant val = value;
1321 if ( val.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
1322 {
1323 const QgsReferencedRectangle rr = val.value<QgsReferencedRectangle>();
1324 if ( rr.crs().isValid() )
1325 {
1326 return rr.crs();
1327 }
1328 }
1329
1330 if ( val.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
1331 {
1332 // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
1333 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
1334 val = fromVar.source;
1335 }
1336 else if ( val.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
1337 {
1338 // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1339 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
1340 val = fromVar.sink;
1341 }
1342
1343 if ( val.userType() == QMetaType::type( "QgsProperty" ) && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
1344 {
1345 val = val.value< QgsProperty >().staticValue();
1346 }
1347
1348 QString valueAsString;
1349 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
1350 valueAsString = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1351 else
1352 valueAsString = val.toString();
1353
1354 const QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
1355
1356 const QRegularExpressionMatch match = rx.match( valueAsString );
1357 if ( match.hasMatch() )
1358 {
1359 const QgsCoordinateReferenceSystem crs( match.captured( 5 ) );
1360 if ( crs.isValid() )
1361 return crs;
1362 }
1363
1364 if ( val.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
1365 {
1366 // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
1367 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
1368 val = fromVar.source;
1369 }
1370 else if ( val.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
1371 {
1372 // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1373 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
1374 val = fromVar.sink;
1375 }
1376
1377 if ( val.userType() == QMetaType::type( "QgsProperty" ) && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
1378 {
1379 val = val.value< QgsProperty >().staticValue();
1380 }
1381
1382 // try as layer crs
1383 if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
1384 return layer->crs();
1385 else if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( valueAsString, context ) )
1386 return layer->crs();
1387
1388 if ( auto *lProject = context.project() )
1389 return lProject->crs();
1390 else
1392}
1393
1395{
1396 if ( !definition )
1397 return QgsPointXY();
1398
1399 return parameterAsPoint( definition, parameters.value( definition->name() ), context, crs );
1400}
1401
1403{
1404 if ( !definition )
1405 return QgsPointXY();
1406
1407 const QVariant val = value;
1408 if ( val.userType() == QMetaType::type( "QgsPointXY" ) )
1409 {
1410 return val.value<QgsPointXY>();
1411 }
1412 if ( val.userType() == QMetaType::type( "QgsGeometry" ) )
1413 {
1414 const QgsGeometry geom = val.value<QgsGeometry>();
1415 if ( !geom.isNull() )
1416 return geom.centroid().asPoint();
1417 }
1418 if ( val.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
1419 {
1420 const QgsReferencedPointXY rp = val.value<QgsReferencedPointXY>();
1421 if ( crs.isValid() && rp.crs().isValid() && crs != rp.crs() )
1422 {
1423 const QgsCoordinateTransform ct( rp.crs(), crs, context.project() );
1424 try
1425 {
1426 return ct.transform( rp );
1427 }
1428 catch ( QgsCsException & )
1429 {
1430 QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
1431 }
1432 }
1433 return rp;
1434 }
1435
1436 QString pointText = parameterAsString( definition, value, context );
1437 if ( pointText.isEmpty() )
1438 pointText = definition->defaultValue().toString();
1439
1440 if ( pointText.isEmpty() )
1441 return QgsPointXY();
1442
1443 const QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
1444
1445 const QString valueAsString = parameterAsString( definition, value, context );
1446 const QRegularExpressionMatch match = rx.match( valueAsString );
1447 if ( match.hasMatch() )
1448 {
1449 bool xOk = false;
1450 const double x = match.captured( 1 ).toDouble( &xOk );
1451 bool yOk = false;
1452 const double y = match.captured( 2 ).toDouble( &yOk );
1453
1454 if ( xOk && yOk )
1455 {
1456 const QgsPointXY pt( x, y );
1457
1458 const QgsCoordinateReferenceSystem pointCrs( match.captured( 3 ) );
1459 if ( crs.isValid() && pointCrs.isValid() && crs != pointCrs )
1460 {
1461 const QgsCoordinateTransform ct( pointCrs, crs, context.project() );
1462 try
1463 {
1464 return ct.transform( pt );
1465 }
1466 catch ( QgsCsException & )
1467 {
1468 QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
1469 }
1470 }
1471 return pt;
1472 }
1473 }
1474
1475 return QgsPointXY();
1476}
1477
1479{
1480 const QVariant val = parameters.value( definition->name() );
1481 return parameterAsPointCrs( definition, val, context );
1482}
1483
1485{
1486 if ( value.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
1487 {
1488 const QgsReferencedPointXY rr = value.value<QgsReferencedPointXY>();
1489 if ( rr.crs().isValid() )
1490 {
1491 return rr.crs();
1492 }
1493 }
1494
1495 const QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
1496
1497 const QString valueAsString = parameterAsString( definition, value, context );
1498 const QRegularExpressionMatch match = rx.match( valueAsString );
1499 if ( match.hasMatch() )
1500 {
1501 const QgsCoordinateReferenceSystem crs( match.captured( 3 ) );
1502 if ( crs.isValid() )
1503 return crs;
1504 }
1505
1506 if ( auto *lProject = context.project() )
1507 return lProject->crs();
1508 else
1510}
1511
1513{
1514 if ( !definition )
1515 return QgsGeometry();
1516
1517 return parameterAsGeometry( definition, parameters.value( definition->name() ), context, crs );
1518}
1519
1521{
1522 if ( !definition )
1523 return QgsGeometry();
1524
1525 const QVariant val = value;
1526 if ( val.userType() == QMetaType::type( "QgsGeometry" ) )
1527 {
1528 return val.value<QgsGeometry>();
1529 }
1530
1531 if ( val.userType() == QMetaType::type( "QgsPointXY" ) )
1532 {
1533 return QgsGeometry::fromPointXY( val.value<QgsPointXY>() );
1534 }
1535
1536 if ( val.userType() == QMetaType::type( "QgsRectangle" ) )
1537 {
1538 return QgsGeometry::fromRect( val.value<QgsRectangle>() );
1539 }
1540
1541 if ( val.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
1542 {
1543 const QgsReferencedPointXY rp = val.value<QgsReferencedPointXY>();
1544 if ( crs.isValid() && rp.crs().isValid() && crs != rp.crs() )
1545 {
1546 const QgsCoordinateTransform ct( rp.crs(), crs, context.project() );
1547 try
1548 {
1549 return QgsGeometry::fromPointXY( ct.transform( rp ) );
1550 }
1551 catch ( QgsCsException & )
1552 {
1553 QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
1554 }
1555 }
1556 return QgsGeometry::fromPointXY( rp );
1557 }
1558
1559 if ( val.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
1560 {
1561 const QgsReferencedRectangle rr = val.value<QgsReferencedRectangle>();
1563 if ( crs.isValid() && rr.crs().isValid() && crs != rr.crs() )
1564 {
1565 g = g.densifyByCount( 20 );
1566 const QgsCoordinateTransform ct( rr.crs(), crs, context.project() );
1567 try
1568 {
1569 g.transform( ct );
1570 }
1571 catch ( QgsCsException & )
1572 {
1573 QgsMessageLog::logMessage( QObject::tr( "Error transforming rectangle geometry" ) );
1574 }
1575 }
1576 return g;
1577 }
1578
1579 if ( val.userType() == QMetaType::type( "QgsReferencedGeometry" ) )
1580 {
1582 if ( crs.isValid() && rg.crs().isValid() && crs != rg.crs() )
1583 {
1584 const QgsCoordinateTransform ct( rg.crs(), crs, context.project() );
1585 try
1586 {
1587 rg.transform( ct );
1588 }
1589 catch ( QgsCsException & )
1590 {
1591 QgsMessageLog::logMessage( QObject::tr( "Error transforming geometry" ) );
1592 }
1593 }
1594 return rg;
1595 }
1596
1597 QString valueAsString = parameterAsString( definition, value, context );
1598 if ( valueAsString.isEmpty() )
1599 valueAsString = definition->defaultValue().toString();
1600
1601 if ( valueAsString.isEmpty() )
1602 return QgsGeometry();
1603
1604 const QRegularExpression rx( QStringLiteral( "^\\s*(?:CRS=(.*);)?(.*?)$" ) );
1605
1606 const QRegularExpressionMatch match = rx.match( valueAsString );
1607 if ( match.hasMatch() )
1608 {
1609 QgsGeometry g = QgsGeometry::fromWkt( match.captured( 2 ) );
1610 if ( !g.isNull() )
1611 {
1612 const QgsCoordinateReferenceSystem geomCrs( match.captured( 1 ) );
1613 if ( crs.isValid() && geomCrs.isValid() && crs != geomCrs )
1614 {
1615 const QgsCoordinateTransform ct( geomCrs, crs, context.project() );
1616 try
1617 {
1618 g.transform( ct );
1619 }
1620 catch ( QgsCsException & )
1621 {
1622 QgsMessageLog::logMessage( QObject::tr( "Error transforming geometry" ) );
1623 }
1624 }
1625 return g;
1626 }
1627 }
1628
1629 return QgsGeometry();
1630}
1631
1633{
1634 const QVariant val = parameters.value( definition->name() );
1635 return parameterAsGeometryCrs( definition, val, context );
1636}
1637
1639{
1640 if ( value.userType() == QMetaType::type( "QgsReferencedGeometry" ) )
1641 {
1642 const QgsReferencedGeometry rg = value.value<QgsReferencedGeometry>();
1643 if ( rg.crs().isValid() )
1644 {
1645 return rg.crs();
1646 }
1647 }
1648
1649 if ( value.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
1650 {
1651 const QgsReferencedPointXY rp = value.value<QgsReferencedPointXY>();
1652 if ( rp.crs().isValid() )
1653 {
1654 return rp.crs();
1655 }
1656 }
1657
1658 if ( value.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
1659 {
1660 const QgsReferencedRectangle rr = value.value<QgsReferencedRectangle>();
1661 if ( rr.crs().isValid() )
1662 {
1663 return rr.crs();
1664 }
1665 }
1666
1667 // Match against EWKT
1668 const QRegularExpression rx( QStringLiteral( "^\\s*(?:CRS=(.*);)?(.*?)$" ) );
1669
1670 const QString valueAsString = parameterAsString( definition, value, context );
1671 const QRegularExpressionMatch match = rx.match( valueAsString );
1672 if ( match.hasMatch() )
1673 {
1674 const QgsCoordinateReferenceSystem crs( match.captured( 1 ) );
1675 if ( crs.isValid() )
1676 return crs;
1677 }
1678
1679 if ( auto *lProject = context.project() )
1680 return lProject->crs();
1681 else
1683}
1684
1685QString QgsProcessingParameters::parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1686{
1687 if ( !definition )
1688 return QString();
1689
1690 QString fileText = parameterAsString( definition, parameters, context );
1691 if ( fileText.isEmpty() )
1692 fileText = definition->defaultValue().toString();
1693 return fileText;
1694}
1695
1697{
1698 if ( !definition )
1699 return QString();
1700
1701 QString fileText = parameterAsString( definition, value, context );
1702 if ( fileText.isEmpty() )
1703 fileText = definition->defaultValue().toString();
1704 return fileText;
1705}
1706
1707QVariantList QgsProcessingParameters::parameterAsMatrix( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1708{
1709 if ( !definition )
1710 return QVariantList();
1711
1712 return parameterAsMatrix( definition, parameters.value( definition->name() ), context );
1713}
1714
1715QVariantList QgsProcessingParameters::parameterAsMatrix( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1716{
1717 if ( !definition )
1718 return QVariantList();
1719
1720 QString resultString;
1721 const QVariant val = value;
1722 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
1723 resultString = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1724 else if ( val.type() == QVariant::List )
1725 return val.toList();
1726 else
1727 resultString = val.toString();
1728
1729 if ( resultString.isEmpty() )
1730 {
1731 // check default
1732 if ( definition->defaultValue().type() == QVariant::List )
1733 return definition->defaultValue().toList();
1734 else
1735 resultString = definition->defaultValue().toString();
1736 }
1737
1738 QVariantList result;
1739 const auto constSplit = resultString.split( ',' );
1740 bool ok;
1741 double number;
1742 for ( const QString &s : constSplit )
1743 {
1744 number = s.toDouble( &ok );
1745 result << ( ok ? QVariant( number ) : s );
1746 }
1747
1748 return result;
1749}
1750
1751QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1752{
1753 if ( !definition )
1754 return QList<QgsMapLayer *>();
1755
1756 return parameterAsLayerList( definition, parameters.value( definition->name() ), context );
1757}
1758
1759QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1760{
1761 if ( !definition )
1762 return QList<QgsMapLayer *>();
1763
1764 const QVariant val = value;
1765 if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
1766 {
1767 return QList<QgsMapLayer *>() << layer;
1768 }
1769
1770 QList<QgsMapLayer *> layers;
1771
1772 std::function< void( const QVariant &var ) > processVariant;
1773 processVariant = [ &layers, &context, &definition, &processVariant ]( const QVariant & var )
1774 {
1775 if ( var.type() == QVariant::List )
1776 {
1777 const auto constToList = var.toList();
1778 for ( const QVariant &listVar : constToList )
1779 {
1780 processVariant( listVar );
1781 }
1782 }
1783 else if ( var.type() == QVariant::StringList )
1784 {
1785 const auto constToStringList = var.toStringList();
1786 for ( const QString &s : constToStringList )
1787 {
1788 processVariant( s );
1789 }
1790 }
1791 else if ( var.userType() == QMetaType::type( "QgsProperty" ) )
1792 processVariant( var.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() ) );
1793 else if ( var.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
1794 {
1795 // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
1796 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
1797 const QVariant sink = fromVar.sink;
1798 if ( sink.userType() == QMetaType::type( "QgsProperty" ) )
1799 {
1800 processVariant( sink.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() ) );
1801 }
1802 }
1803 else if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
1804 {
1805 layers << layer;
1806 }
1807 else
1808 {
1809 QgsMapLayer *alayer = QgsProcessingUtils::mapLayerFromString( var.toString(), context );
1810 if ( alayer )
1811 layers << alayer;
1812 }
1813 };
1814
1815 processVariant( val );
1816
1817 if ( layers.isEmpty() )
1818 {
1819 // check default
1820 if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( definition->defaultValue() ) ) )
1821 {
1822 layers << layer;
1823 }
1824 else if ( definition->defaultValue().type() == QVariant::List )
1825 {
1826 const auto constToList = definition->defaultValue().toList();
1827 for ( const QVariant &var : constToList )
1828 {
1829 if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
1830 {
1831 layers << layer;
1832 }
1833 else
1834 {
1835 processVariant( var );
1836 }
1837 }
1838 }
1839 else
1840 processVariant( definition->defaultValue() );
1841 }
1842
1843 return layers;
1844}
1845
1846QStringList QgsProcessingParameters::parameterAsFileList( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1847{
1848 if ( !definition )
1849 return QStringList();
1850
1851 const QVariant val = value;
1852
1853 QStringList files;
1854
1855 std::function< void( const QVariant &var ) > processVariant;
1856 processVariant = [ &files, &context, &definition, &processVariant ]( const QVariant & var )
1857 {
1858 if ( var.type() == QVariant::List )
1859 {
1860 const auto constToList = var.toList();
1861 for ( const QVariant &listVar : constToList )
1862 {
1863 processVariant( listVar );
1864 }
1865 }
1866 else if ( var.type() == QVariant::StringList )
1867 {
1868 const auto constToStringList = var.toStringList();
1869 for ( const QString &s : constToStringList )
1870 {
1871 processVariant( s );
1872 }
1873 }
1874 else if ( var.userType() == QMetaType::type( "QgsProperty" ) )
1875 processVariant( var.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() ) );
1876 else
1877 {
1878 files << var.toString();
1879 }
1880 };
1881
1882 processVariant( val );
1883
1884 if ( files.isEmpty() )
1885 {
1886 processVariant( definition->defaultValue() );
1887 }
1888
1889 return files;
1890}
1891
1892QStringList QgsProcessingParameters::parameterAsFileList( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1893{
1894 if ( !definition )
1895 return QStringList();
1896
1897 return parameterAsFileList( definition, parameters.value( definition->name() ), context );
1898}
1899
1900QList<double> QgsProcessingParameters::parameterAsRange( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1901{
1902 if ( !definition )
1903 return QList<double>();
1904
1905 return parameterAsRange( definition, parameters.value( definition->name() ), context );
1906}
1907
1908QList<double> QgsProcessingParameters::parameterAsRange( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1909{
1910 if ( !definition )
1911 return QList<double>();
1912
1913 QStringList resultStringList;
1914 const QVariant val = value;
1915
1916 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
1917 resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1918 else if ( val.type() == QVariant::List )
1919 {
1920 const auto constToList = val.toList();
1921 for ( const QVariant &var : constToList )
1922 resultStringList << var.toString();
1923 }
1924 else
1925 resultStringList << val.toString();
1926
1927 if ( ( resultStringList.isEmpty() || ( resultStringList.size() == 1 && resultStringList.at( 0 ).isEmpty() ) ) )
1928 {
1929 resultStringList.clear();
1930 // check default
1931 if ( definition->defaultValue().type() == QVariant::List )
1932 {
1933 const auto constToList = definition->defaultValue().toList();
1934 for ( const QVariant &var : constToList )
1935 resultStringList << var.toString();
1936 }
1937 else
1938 resultStringList << definition->defaultValue().toString();
1939 }
1940
1941 if ( resultStringList.size() == 1 )
1942 {
1943 resultStringList = resultStringList.at( 0 ).split( ',' );
1944 }
1945
1946 if ( resultStringList.size() < 2 )
1947 return QList< double >() << std::numeric_limits<double>::quiet_NaN() << std::numeric_limits<double>::quiet_NaN() ;
1948
1949 QList< double > result;
1950 bool ok = false;
1951 double n = resultStringList.at( 0 ).toDouble( &ok );
1952 if ( ok )
1953 result << n;
1954 else
1955 result << std::numeric_limits<double>::quiet_NaN() ;
1956 ok = false;
1957 n = resultStringList.at( 1 ).toDouble( &ok );
1958 if ( ok )
1959 result << n;
1960 else
1961 result << std::numeric_limits<double>::quiet_NaN() ;
1962
1963 return result;
1964}
1965
1966QStringList QgsProcessingParameters::parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1967{
1968 if ( !definition )
1969 return QStringList();
1970
1971 return parameterAsFields( definition, parameters.value( definition->name() ), context );
1972}
1973
1974QStringList QgsProcessingParameters::parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1975{
1976 if ( !definition )
1977 return QStringList();
1978
1979 QStringList resultStringList;
1980 const QVariant val = value;
1981 if ( val.isValid() )
1982 {
1983 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
1984 resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1985 else if ( val.type() == QVariant::List )
1986 {
1987 const auto constToList = val.toList();
1988 for ( const QVariant &var : constToList )
1989 resultStringList << var.toString();
1990 }
1991 else if ( val.type() == QVariant::StringList )
1992 {
1993 resultStringList = val.toStringList();
1994 }
1995 else
1996 resultStringList.append( val.toString().split( ';' ) );
1997 }
1998
1999 if ( ( resultStringList.isEmpty() || resultStringList.at( 0 ).isEmpty() ) )
2000 {
2001 resultStringList.clear();
2002 // check default
2003 if ( definition->defaultValue().isValid() )
2004 {
2005 if ( definition->defaultValue().type() == QVariant::List )
2006 {
2007 const auto constToList = definition->defaultValue().toList();
2008 for ( const QVariant &var : constToList )
2009 resultStringList << var.toString();
2010 }
2011 else if ( definition->defaultValue().type() == QVariant::StringList )
2012 {
2013 resultStringList = definition->defaultValue().toStringList();
2014 }
2015 else
2016 resultStringList.append( definition->defaultValue().toString().split( ';' ) );
2017 }
2018 }
2019
2020 return resultStringList;
2021}
2022
2024{
2025 if ( !definition )
2026 return nullptr;
2027
2028 return parameterAsLayout( definition, parameters.value( definition->name() ), context );
2029}
2030
2032{
2033 const QString layoutName = parameterAsString( definition, value, context );
2034 if ( layoutName.isEmpty() )
2035 return nullptr;
2036
2037 if ( !context.project() )
2038 return nullptr;
2039
2040 QgsMasterLayoutInterface *l = context.project()->layoutManager()->layoutByName( layoutName );
2042 return static_cast< QgsPrintLayout * >( l );
2043 else
2044 return nullptr;
2045}
2046
2048{
2049 if ( !definition )
2050 return nullptr;
2051
2052 return parameterAsLayoutItem( definition, parameters.value( definition->name() ), context, layout );
2053}
2054
2056{
2057 if ( !layout )
2058 return nullptr;
2059
2060 const QString id = parameterAsString( definition, value, context );
2061 if ( id.isEmpty() )
2062 return nullptr;
2063
2064 // prefer matching by uuid, since it's guaranteed to be unique.
2065 if ( QgsLayoutItem *item = layout->itemByUuid( id ) )
2066 return item;
2067 else if ( QgsLayoutItem *item = layout->itemById( id ) )
2068 return item;
2069 else
2070 return nullptr;
2071}
2072
2073QColor QgsProcessingParameters::parameterAsColor( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
2074{
2075 if ( !definition )
2076 return QColor();
2077
2078 return parameterAsColor( definition, parameters.value( definition->name() ), context );
2079}
2080
2082{
2083 if ( !definition )
2084 return QColor();
2085
2086 QVariant val = value;
2087 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
2088 {
2089 val = val.value< QgsProperty >().value( context.expressionContext(), definition->defaultValue() );
2090 }
2091 if ( val.type() == QVariant::Color )
2092 {
2093 QColor c = val.value< QColor >();
2094 if ( const QgsProcessingParameterColor *colorParam = dynamic_cast< const QgsProcessingParameterColor * >( definition ) )
2095 if ( !colorParam->opacityEnabled() )
2096 c.setAlpha( 255 );
2097 return c;
2098 }
2099
2100 QString colorText = parameterAsString( definition, value, context );
2101 if ( colorText.isEmpty() && !( definition->flags() & QgsProcessingParameterDefinition::FlagOptional ) )
2102 {
2103 if ( definition->defaultValue().type() == QVariant::Color )
2104 return definition->defaultValue().value< QColor >();
2105 else
2106 colorText = definition->defaultValue().toString();
2107 }
2108
2109 if ( colorText.isEmpty() )
2110 return QColor();
2111
2112 bool containsAlpha = false;
2113 QColor c = QgsSymbolLayerUtils::parseColorWithAlpha( colorText, containsAlpha );
2114 if ( const QgsProcessingParameterColor *colorParam = dynamic_cast< const QgsProcessingParameterColor * >( definition ) )
2115 if ( c.isValid() && !colorParam->opacityEnabled() )
2116 c.setAlpha( 255 );
2117 return c;
2118}
2119
2120QString QgsProcessingParameters::parameterAsConnectionName( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
2121{
2122 if ( !definition )
2123 return QString();
2124
2125 return parameterAsConnectionName( definition, parameters.value( definition->name() ), context );
2126}
2127
2129{
2130 // for now it's just treated identical to strings, but in future we may want flexibility to amend this
2131 // (hence the new method)
2132 return parameterAsString( definition, value, context );
2133}
2134
2135QString QgsProcessingParameters::parameterAsSchema( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
2136{
2137 if ( !definition )
2138 return QString();
2139
2140 return parameterAsSchema( definition, parameters.value( definition->name() ), context );
2141}
2142
2143QString QgsProcessingParameters::parameterAsSchema( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
2144{
2145 // for now it's just treated identical to strings, but in future we may want flexibility to amend this (e.g. if we want to embed connection details into the schema
2146 // parameter values, such as via a delimiter separated string)
2147 return parameterAsString( definition, value, context );
2148}
2149
2150QString QgsProcessingParameters::parameterAsDatabaseTableName( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
2151{
2152 if ( !definition )
2153 return QString();
2154
2155 return parameterAsDatabaseTableName( definition, parameters.value( definition->name() ), context );
2156}
2157
2159{
2160 // for now it's just treated identical to strings, but in future we may want flexibility to amend this (e.g. if we want to embed connection details into the table name
2161 // parameter values, such as via a delimiter separated string)
2162 return parameterAsString( definition, value, context );
2163}
2164
2166{
2167 return qobject_cast< QgsPointCloudLayer *>( parameterAsLayer( definition, parameters, context, QgsProcessingUtils::LayerHint::PointCloud ) );
2168}
2169
2171{
2172 return qobject_cast< QgsPointCloudLayer *>( parameterAsLayer( definition, value, context, QgsProcessingUtils::LayerHint::PointCloud ) );
2173}
2174
2176{
2177 return qobject_cast< QgsAnnotationLayer *>( parameterAsLayer( definition, parameters, context, QgsProcessingUtils::LayerHint::Annotation ) );
2178}
2179
2181{
2182 return qobject_cast< QgsAnnotationLayer *>( parameterAsLayer( definition, value, context, QgsProcessingUtils::LayerHint::Annotation ) );
2183}
2184
2186{
2187 const QString type = map.value( QStringLiteral( "parameter_type" ) ).toString();
2188 const QString name = map.value( QStringLiteral( "name" ) ).toString();
2189 std::unique_ptr< QgsProcessingParameterDefinition > def;
2190
2191 // probably all these hardcoded values aren't required anymore, and we could
2192 // always resort to the registry lookup...
2193 // TODO: confirm
2195 def.reset( new QgsProcessingParameterBoolean( name ) );
2196 else if ( type == QgsProcessingParameterCrs::typeName() )
2197 def.reset( new QgsProcessingParameterCrs( name ) );
2198 else if ( type == QgsProcessingParameterMapLayer::typeName() )
2199 def.reset( new QgsProcessingParameterMapLayer( name ) );
2200 else if ( type == QgsProcessingParameterExtent::typeName() )
2201 def.reset( new QgsProcessingParameterExtent( name ) );
2202 else if ( type == QgsProcessingParameterPoint::typeName() )
2203 def.reset( new QgsProcessingParameterPoint( name ) );
2204 else if ( type == QgsProcessingParameterFile::typeName() )
2205 def.reset( new QgsProcessingParameterFile( name ) );
2206 else if ( type == QgsProcessingParameterMatrix::typeName() )
2207 def.reset( new QgsProcessingParameterMatrix( name ) );
2209 def.reset( new QgsProcessingParameterMultipleLayers( name ) );
2210 else if ( type == QgsProcessingParameterNumber::typeName() )
2211 def.reset( new QgsProcessingParameterNumber( name ) );
2212 else if ( type == QgsProcessingParameterRange::typeName() )
2213 def.reset( new QgsProcessingParameterRange( name ) );
2215 def.reset( new QgsProcessingParameterRasterLayer( name ) );
2216 else if ( type == QgsProcessingParameterEnum::typeName() )
2217 def.reset( new QgsProcessingParameterEnum( name ) );
2218 else if ( type == QgsProcessingParameterString::typeName() )
2219 def.reset( new QgsProcessingParameterString( name ) );
2220 else if ( type == QgsProcessingParameterAuthConfig::typeName() )
2221 def.reset( new QgsProcessingParameterAuthConfig( name ) );
2222 else if ( type == QgsProcessingParameterExpression::typeName() )
2223 def.reset( new QgsProcessingParameterExpression( name ) );
2225 def.reset( new QgsProcessingParameterVectorLayer( name ) );
2226 else if ( type == QgsProcessingParameterField::typeName() )
2227 def.reset( new QgsProcessingParameterField( name ) );
2229 def.reset( new QgsProcessingParameterFeatureSource( name ) );
2231 def.reset( new QgsProcessingParameterFeatureSink( name ) );
2233 def.reset( new QgsProcessingParameterVectorDestination( name ) );
2235 def.reset( new QgsProcessingParameterRasterDestination( name ) );
2237 def.reset( new QgsProcessingParameterPointCloudDestination( name ) );
2239 def.reset( new QgsProcessingParameterFileDestination( name ) );
2241 def.reset( new QgsProcessingParameterFolderDestination( name ) );
2242 else if ( type == QgsProcessingParameterBand::typeName() )
2243 def.reset( new QgsProcessingParameterBand( name ) );
2244 else if ( type == QgsProcessingParameterMeshLayer::typeName() )
2245 def.reset( new QgsProcessingParameterMeshLayer( name ) );
2246 else if ( type == QgsProcessingParameterLayout::typeName() )
2247 def.reset( new QgsProcessingParameterLayout( name ) );
2248 else if ( type == QgsProcessingParameterLayoutItem::typeName() )
2249 def.reset( new QgsProcessingParameterLayoutItem( name ) );
2250 else if ( type == QgsProcessingParameterColor::typeName() )
2251 def.reset( new QgsProcessingParameterColor( name ) );
2253 def.reset( new QgsProcessingParameterCoordinateOperation( name ) );
2255 def.reset( new QgsProcessingParameterPointCloudLayer( name ) );
2257 def.reset( new QgsProcessingParameterAnnotationLayer( name ) );
2258 else
2259 {
2261 if ( paramType )
2262 def.reset( paramType->create( name ) );
2263 }
2264
2265 if ( !def )
2266 return nullptr;
2267
2268 def->fromVariantMap( map );
2269 return def.release();
2270}
2271
2273{
2274 QString desc = name;
2275 desc.replace( '_', ' ' );
2276 return desc;
2277}
2278
2280{
2281 bool isOptional = false;
2282 QString name;
2283 QString definition;
2284 QString type;
2285 if ( !parseScriptCodeParameterOptions( code, isOptional, name, type, definition ) )
2286 return nullptr;
2287
2288 const QString description = descriptionFromName( name );
2289
2290 if ( type == QLatin1String( "boolean" ) )
2291 return QgsProcessingParameterBoolean::fromScriptCode( name, description, isOptional, definition );
2292 else if ( type == QLatin1String( "crs" ) )
2293 return QgsProcessingParameterCrs::fromScriptCode( name, description, isOptional, definition );
2294 else if ( type == QLatin1String( "layer" ) )
2295 return QgsProcessingParameterMapLayer::fromScriptCode( name, description, isOptional, definition );
2296 else if ( type == QLatin1String( "extent" ) )
2297 return QgsProcessingParameterExtent::fromScriptCode( name, description, isOptional, definition );
2298 else if ( type == QLatin1String( "point" ) )
2299 return QgsProcessingParameterPoint::fromScriptCode( name, description, isOptional, definition );
2300 else if ( type == QLatin1String( "geometry" ) )
2301 return QgsProcessingParameterGeometry::fromScriptCode( name, description, isOptional, definition );
2302 else if ( type == QLatin1String( "file" ) )
2303 return QgsProcessingParameterFile::fromScriptCode( name, description, isOptional, definition, QgsProcessingParameterFile::File );
2304 else if ( type == QLatin1String( "folder" ) )
2305 return QgsProcessingParameterFile::fromScriptCode( name, description, isOptional, definition, QgsProcessingParameterFile::Folder );
2306 else if ( type == QLatin1String( "matrix" ) )
2307 return QgsProcessingParameterMatrix::fromScriptCode( name, description, isOptional, definition );
2308 else if ( type == QLatin1String( "multiple" ) )
2309 return QgsProcessingParameterMultipleLayers::fromScriptCode( name, description, isOptional, definition );
2310 else if ( type == QLatin1String( "number" ) )
2311 return QgsProcessingParameterNumber::fromScriptCode( name, description, isOptional, definition );
2312 else if ( type == QLatin1String( "distance" ) )
2313 return QgsProcessingParameterDistance::fromScriptCode( name, description, isOptional, definition );
2314 else if ( type == QLatin1String( "duration" ) )
2315 return QgsProcessingParameterDuration::fromScriptCode( name, description, isOptional, definition );
2316 else if ( type == QLatin1String( "scale" ) )
2317 return QgsProcessingParameterScale::fromScriptCode( name, description, isOptional, definition );
2318 else if ( type == QLatin1String( "range" ) )
2319 return QgsProcessingParameterRange::fromScriptCode( name, description, isOptional, definition );
2320 else if ( type == QLatin1String( "raster" ) )
2321 return QgsProcessingParameterRasterLayer::fromScriptCode( name, description, isOptional, definition );
2322 else if ( type == QLatin1String( "enum" ) )
2323 return QgsProcessingParameterEnum::fromScriptCode( name, description, isOptional, definition );
2324 else if ( type == QLatin1String( "string" ) )
2325 return QgsProcessingParameterString::fromScriptCode( name, description, isOptional, definition );
2326 else if ( type == QLatin1String( "authcfg" ) )
2327 return QgsProcessingParameterAuthConfig::fromScriptCode( name, description, isOptional, definition );
2328 else if ( type == QLatin1String( "expression" ) )
2329 return QgsProcessingParameterExpression::fromScriptCode( name, description, isOptional, definition );
2330 else if ( type == QLatin1String( "field" ) )
2331 return QgsProcessingParameterField::fromScriptCode( name, description, isOptional, definition );
2332 else if ( type == QLatin1String( "vector" ) )
2333 return QgsProcessingParameterVectorLayer::fromScriptCode( name, description, isOptional, definition );
2334 else if ( type == QLatin1String( "source" ) )
2335 return QgsProcessingParameterFeatureSource::fromScriptCode( name, description, isOptional, definition );
2336 else if ( type == QLatin1String( "sink" ) )
2337 return QgsProcessingParameterFeatureSink::fromScriptCode( name, description, isOptional, definition );
2338 else if ( type == QLatin1String( "vectordestination" ) )
2339 return QgsProcessingParameterVectorDestination::fromScriptCode( name, description, isOptional, definition );
2340 else if ( type == QLatin1String( "rasterdestination" ) )
2341 return QgsProcessingParameterRasterDestination::fromScriptCode( name, description, isOptional, definition );
2342 else if ( type == QLatin1String( "pointclouddestination" ) )
2343 return QgsProcessingParameterPointCloudDestination::fromScriptCode( name, description, isOptional, definition );
2344 else if ( type == QLatin1String( "filedestination" ) )
2345 return QgsProcessingParameterFileDestination::fromScriptCode( name, description, isOptional, definition );
2346 else if ( type == QLatin1String( "folderdestination" ) )
2347 return QgsProcessingParameterFolderDestination::fromScriptCode( name, description, isOptional, definition );
2348 else if ( type == QLatin1String( "band" ) )
2349 return QgsProcessingParameterBand::fromScriptCode( name, description, isOptional, definition );
2350 else if ( type == QLatin1String( "mesh" ) )
2351 return QgsProcessingParameterMeshLayer::fromScriptCode( name, description, isOptional, definition );
2352 else if ( type == QLatin1String( "layout" ) )
2353 return QgsProcessingParameterLayout::fromScriptCode( name, description, isOptional, definition );
2354 else if ( type == QLatin1String( "layoutitem" ) )
2355 return QgsProcessingParameterLayoutItem::fromScriptCode( name, description, isOptional, definition );
2356 else if ( type == QLatin1String( "color" ) )
2357 return QgsProcessingParameterColor::fromScriptCode( name, description, isOptional, definition );
2358 else if ( type == QLatin1String( "coordinateoperation" ) )
2359 return QgsProcessingParameterCoordinateOperation::fromScriptCode( name, description, isOptional, definition );
2360 else if ( type == QLatin1String( "maptheme" ) )
2361 return QgsProcessingParameterMapTheme::fromScriptCode( name, description, isOptional, definition );
2362 else if ( type == QLatin1String( "datetime" ) )
2363 return QgsProcessingParameterDateTime::fromScriptCode( name, description, isOptional, definition );
2364 else if ( type == QLatin1String( "providerconnection" ) )
2365 return QgsProcessingParameterProviderConnection::fromScriptCode( name, description, isOptional, definition );
2366 else if ( type == QLatin1String( "databaseschema" ) )
2367 return QgsProcessingParameterDatabaseSchema::fromScriptCode( name, description, isOptional, definition );
2368 else if ( type == QLatin1String( "databasetable" ) )
2369 return QgsProcessingParameterDatabaseTable::fromScriptCode( name, description, isOptional, definition );
2370 else if ( type == QLatin1String( "pointcloud" ) )
2371 return QgsProcessingParameterPointCloudLayer::fromScriptCode( name, description, isOptional, definition );
2372 else if ( type == QLatin1String( "annotation" ) )
2373 return QgsProcessingParameterAnnotationLayer::fromScriptCode( name, description, isOptional, definition );
2374
2375 return nullptr;
2376}
2377
2378bool QgsProcessingParameters::parseScriptCodeParameterOptions( const QString &code, bool &isOptional, QString &name, QString &type, QString &definition )
2379{
2380 const QRegularExpression re( QStringLiteral( "(?:#*)(.*?)=\\s*(.*)" ) );
2381 QRegularExpressionMatch m = re.match( code );
2382 if ( !m.hasMatch() )
2383 return false;
2384
2385 name = m.captured( 1 );
2386 QString tokens = m.captured( 2 );
2387 if ( tokens.startsWith( QLatin1String( "optional" ), Qt::CaseInsensitive ) )
2388 {
2389 isOptional = true;
2390 tokens.remove( 0, 8 ); // length "optional" = 8
2391 }
2392 else
2393 {
2394 isOptional = false;
2395 }
2396
2397 tokens = tokens.trimmed();
2398
2399 const QRegularExpression re2( QStringLiteral( "(.*?)\\s+(.*)" ) );
2400 m = re2.match( tokens );
2401 if ( !m.hasMatch() )
2402 {
2403 type = tokens.toLower().trimmed();
2404 definition.clear();
2405 }
2406 else
2407 {
2408 type = m.captured( 1 ).toLower().trimmed();
2409 definition = m.captured( 2 );
2410 }
2411 return true;
2412}
2413
2414//
2415// QgsProcessingParameterDefinition
2416//
2417
2418QgsProcessingParameterDefinition::QgsProcessingParameterDefinition( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, const QString &help )
2419 : mName( name )
2420 , mDescription( description )
2421 , mHelp( help )
2422 , mDefault( defaultValue )
2423 , mFlags( optional ? FlagOptional : 0 )
2424{}
2425
2427{
2428 if ( !input.isValid() && !mDefault.isValid() )
2429 return mFlags & FlagOptional;
2430
2431 if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
2432 || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
2433 return mFlags & FlagOptional;
2434
2435 return true;
2436}
2437
2439{
2440 if ( !value.isValid() )
2441 return QStringLiteral( "None" );
2442
2443 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
2444 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2445
2446 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
2447}
2448
2449QVariant QgsProcessingParameterDefinition::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
2450{
2451 return valueAsJsonObjectPrivate( value, context, ValueAsStringFlags() );
2452}
2453
2454QVariant QgsProcessingParameterDefinition::valueAsJsonObjectPrivate( const QVariant &value, QgsProcessingContext &context, ValueAsStringFlags flags ) const
2455{
2456 if ( !value.isValid() )
2457 return value;
2458
2459 // dive into map and list types and convert each value
2460 if ( value.type() == QVariant::Type::Map )
2461 {
2462 const QVariantMap sourceMap = value.toMap();
2463 QVariantMap resultMap;
2464 for ( auto it = sourceMap.constBegin(); it != sourceMap.constEnd(); it++ )
2465 {
2466 resultMap[ it.key() ] = valueAsJsonObject( it.value(), context );
2467 }
2468 return resultMap;
2469 }
2470 else if ( value.type() == QVariant::Type::List || value.type() == QVariant::Type::StringList )
2471 {
2472 const QVariantList sourceList = value.toList();
2473 QVariantList resultList;
2474 resultList.reserve( sourceList.size() );
2475 for ( const QVariant &v : sourceList )
2476 {
2477 resultList.push_back( valueAsJsonObject( v, context ) );
2478 }
2479 return resultList;
2480 }
2481 else
2482 {
2483 switch ( value.userType() )
2484 {
2485 // simple types which can be directly represented in JSON -- note that strings are NOT handled here yet!
2486 case QMetaType::Bool:
2487 case QMetaType::Char:
2488 case QMetaType::Int:
2489 case QMetaType::Double:
2490 case QMetaType::Float:
2491 case QMetaType::LongLong:
2492 case QMetaType::ULongLong:
2493 case QMetaType::UInt:
2494 case QMetaType::ULong:
2495 case QMetaType::UShort:
2496 return value;
2497
2498 default:
2499 break;
2500 }
2501
2502 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
2503 {
2504 const QgsProperty prop = value.value< QgsProperty >();
2505 switch ( prop.propertyType() )
2506 {
2508 return QVariant();
2510 return valueAsJsonObject( prop.staticValue(), context );
2512 return QVariantMap( {{QStringLiteral( "type" ), QStringLiteral( "data_defined" )}, {QStringLiteral( "field" ), prop.field() }} );
2514 return QVariantMap( {{QStringLiteral( "type" ), QStringLiteral( "data_defined" )}, {QStringLiteral( "expression" ), prop.expressionString() }} );
2515 }
2516 }
2517
2518 // value may be a CRS
2519 if ( value.userType() == QMetaType::type( "QgsCoordinateReferenceSystem" ) )
2520 {
2522 if ( !crs.isValid() )
2523 return QString();
2524 else if ( !crs.authid().isEmpty() )
2525 return crs.authid();
2526 else
2528 }
2529 else if ( value.userType() == QMetaType::type( "QgsRectangle" ) )
2530 {
2531 const QgsRectangle r = value.value<QgsRectangle>();
2532 return QStringLiteral( "%1, %3, %2, %4" ).arg( qgsDoubleToString( r.xMinimum() ),
2535 qgsDoubleToString( r.yMaximum() ) );
2536 }
2537 else if ( value.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
2538 {
2539 const QgsReferencedRectangle r = value.value<QgsReferencedRectangle>();
2540 return QStringLiteral( "%1, %3, %2, %4 [%5]" ).arg( qgsDoubleToString( r.xMinimum() ),
2544 r.crs().authid() );
2545 }
2546 else if ( value.userType() == QMetaType::type( "QgsGeometry" ) )
2547 {
2548 const QgsGeometry g = value.value<QgsGeometry>();
2549 if ( !g.isNull() )
2550 {
2551 return g.asWkt();
2552 }
2553 else
2554 {
2555 return QString();
2556 }
2557 }
2558 else if ( value.userType() == QMetaType::type( "QgsReferencedGeometry" ) )
2559 {
2560 const QgsReferencedGeometry g = value.value<QgsReferencedGeometry>();
2561 if ( !g.isNull() )
2562 {
2563 if ( !g.crs().isValid() )
2564 return g.asWkt();
2565 else
2566 return QStringLiteral( "CRS=%1;%2" ).arg( g.crs().authid().isEmpty() ? g.crs().toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) : g.crs().authid(), g.asWkt() );
2567 }
2568 else
2569 {
2570 return QString();
2571 }
2572 }
2573 else if ( value.userType() == QMetaType::type( "QgsPointXY" ) )
2574 {
2575 const QgsPointXY r = value.value<QgsPointXY>();
2576 return QStringLiteral( "%1,%2" ).arg( qgsDoubleToString( r.x() ),
2577 qgsDoubleToString( r.y() ) );
2578 }
2579 else if ( value.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
2580 {
2581 const QgsReferencedPointXY r = value.value<QgsReferencedPointXY>();
2582 return QStringLiteral( "%1,%2 [%3]" ).arg( qgsDoubleToString( r.x() ),
2583 qgsDoubleToString( r.y() ),
2584 r.crs().authid() );
2585 }
2586 else if ( value.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
2587 {
2588 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( value );
2589
2590 // TODO -- we could consider also serializating the additional properties like invalid feature handling, limits, etc
2591 return valueAsJsonObject( fromVar.source, context );
2592 }
2593 else if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
2594 {
2595 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
2596 return valueAsJsonObject( fromVar.sink, context );
2597 }
2598 else if ( value.userType() == QMetaType::type( "QColor" ) )
2599 {
2600 const QColor fromVar = value.value< QColor >();
2601 if ( !fromVar.isValid() )
2602 return QString();
2603
2604 return QStringLiteral( "rgba( %1, %2, %3, %4 )" ).arg( fromVar.red() ).arg( fromVar.green() ).arg( fromVar.blue() ).arg( QString::number( fromVar.alphaF(), 'f', 2 ) );
2605 }
2606 else if ( value.userType() == QMetaType::type( "QDateTime" ) )
2607 {
2608 const QDateTime fromVar = value.toDateTime();
2609 if ( !fromVar.isValid() )
2610 return QString();
2611
2612 return fromVar.toString( Qt::ISODate );
2613 }
2614 else if ( value.userType() == QMetaType::type( "QDate" ) )
2615 {
2616 const QDate fromVar = value.toDate();
2617 if ( !fromVar.isValid() )
2618 return QString();
2619
2620 return fromVar.toString( Qt::ISODate );
2621 }
2622 else if ( value.userType() == QMetaType::type( "QTime" ) )
2623 {
2624 const QTime fromVar = value.toTime();
2625 if ( !fromVar.isValid() )
2626 return QString();
2627
2628 return fromVar.toString( Qt::ISODate );
2629 }
2630
2632 {
2633 // value may be a map layer
2634 QVariantMap p;
2635 p.insert( name(), value );
2636 if ( QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context ) )
2637 {
2638 const QString source = QgsProcessingUtils::normalizeLayerSource( layer->source() );
2639 if ( !source.isEmpty() )
2640 return source;
2641 return layer->id();
2642 }
2643 }
2644
2645 // now we handle strings, after any other specific logic has already been applied
2646 if ( value.userType() == QMetaType::QString )
2647 return value;
2648 }
2649
2650 // unhandled type
2651 Q_ASSERT_X( false, "QgsProcessingParameterDefinition::valueAsJsonObject", QStringLiteral( "unsupported variant type %1" ).arg( QMetaType::typeName( value.userType() ) ).toLocal8Bit() );
2652 return value;
2653}
2654
2655QString QgsProcessingParameterDefinition::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
2656{
2657 return valueAsStringPrivate( value, context, ok, ValueAsStringFlags() );
2658}
2659
2660QString QgsProcessingParameterDefinition::valueAsStringPrivate( const QVariant &value, QgsProcessingContext &context, bool &ok, ValueAsStringFlags flags ) const
2661{
2662 ok = true;
2663
2664 if ( !value.isValid() )
2665 return QString();
2666
2667 switch ( value.userType() )
2668 {
2669 // simple types which can be directly represented in JSON -- note that strings are NOT handled here yet!
2670 case QMetaType::Bool:
2671 case QMetaType::Char:
2672 case QMetaType::Int:
2673 case QMetaType::Double:
2674 case QMetaType::Float:
2675 case QMetaType::LongLong:
2676 case QMetaType::ULongLong:
2677 case QMetaType::UInt:
2678 case QMetaType::ULong:
2679 case QMetaType::UShort:
2680 return value.toString();
2681
2682 default:
2683 break;
2684 }
2685
2686 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
2687 {
2688 const QgsProperty prop = value.value< QgsProperty >();
2689 switch ( prop.propertyType() )
2690 {
2692 return QString();
2694 return valueAsString( prop.staticValue(), context, ok );
2696 return QStringLiteral( "field:%1" ).arg( prop.field() );
2698 return QStringLiteral( "expression:%1" ).arg( prop.expressionString() );
2699 }
2700 }
2701
2702 // value may be a CRS
2703 if ( value.userType() == QMetaType::type( "QgsCoordinateReferenceSystem" ) )
2704 {
2706 if ( !crs.isValid() )
2707 return QString();
2708 else if ( !crs.authid().isEmpty() )
2709 return crs.authid();
2710 else
2712 }
2713 else if ( value.userType() == QMetaType::type( "QgsRectangle" ) )
2714 {
2715 const QgsRectangle r = value.value<QgsRectangle>();
2716 return QStringLiteral( "%1, %3, %2, %4" ).arg( qgsDoubleToString( r.xMinimum() ),
2719 qgsDoubleToString( r.yMaximum() ) );
2720 }
2721 else if ( value.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
2722 {
2723 const QgsReferencedRectangle r = value.value<QgsReferencedRectangle>();
2724 return QStringLiteral( "%1, %3, %2, %4 [%5]" ).arg( qgsDoubleToString( r.xMinimum() ),
2727 qgsDoubleToString( r.yMaximum() ), r.crs().authid() );
2728 }
2729 else if ( value.userType() == QMetaType::type( "QgsGeometry" ) )
2730 {
2731 const QgsGeometry g = value.value<QgsGeometry>();
2732 if ( !g.isNull() )
2733 {
2734 return g.asWkt();
2735 }
2736 else
2737 {
2738 return QString();
2739 }
2740 }
2741 else if ( value.userType() == QMetaType::type( "QgsReferencedGeometry" ) )
2742 {
2743 const QgsReferencedGeometry g = value.value<QgsReferencedGeometry>();
2744 if ( !g.isNull() )
2745 {
2746 if ( !g.crs().isValid() )
2747 return g.asWkt();
2748 else
2749 return QStringLiteral( "CRS=%1;%2" ).arg( g.crs().authid().isEmpty() ? g.crs().toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) : g.crs().authid(), g.asWkt() );
2750 }
2751 else
2752 {
2753 return QString();
2754 }
2755 }
2756 else if ( value.userType() == QMetaType::type( "QgsPointXY" ) )
2757 {
2758 const QgsPointXY r = value.value<QgsPointXY>();
2759 return QStringLiteral( "%1,%2" ).arg( qgsDoubleToString( r.x() ),
2760 qgsDoubleToString( r.y() ) );
2761 }
2762 else if ( value.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
2763 {
2764 const QgsReferencedPointXY r = value.value<QgsReferencedPointXY>();
2765 return QStringLiteral( "%1,%2 [%3]" ).arg( qgsDoubleToString( r.x() ),
2766 qgsDoubleToString( r.y() ),
2767 r.crs().authid() );
2768 }
2769 else if ( value.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
2770 {
2771 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( value );
2772 return valueAsString( fromVar.source, context, ok );
2773 }
2774 else if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
2775 {
2776 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
2777 return valueAsString( fromVar.sink, context, ok );
2778 }
2779 else if ( value.userType() == QMetaType::type( "QColor" ) )
2780 {
2781 const QColor fromVar = value.value< QColor >();
2782 if ( !fromVar.isValid() )
2783 return QString();
2784
2785 return QStringLiteral( "rgba( %1, %2, %3, %4 )" ).arg( fromVar.red() ).arg( fromVar.green() ).arg( fromVar.blue() ).arg( QString::number( fromVar.alphaF(), 'f', 2 ) );
2786 }
2787 else if ( value.userType() == QMetaType::type( "QDateTime" ) )
2788 {
2789 const QDateTime fromVar = value.toDateTime();
2790 if ( !fromVar.isValid() )
2791 return QString();
2792
2793 return fromVar.toString( Qt::ISODate );
2794 }
2795 else if ( value.userType() == QMetaType::type( "QDate" ) )
2796 {
2797 const QDate fromVar = value.toDate();
2798 if ( !fromVar.isValid() )
2799 return QString();
2800
2801 return fromVar.toString( Qt::ISODate );
2802 }
2803 else if ( value.userType() == QMetaType::type( "QTime" ) )
2804 {
2805 const QTime fromVar = value.toTime();
2806 if ( !fromVar.isValid() )
2807 return QString();
2808
2809 return fromVar.toString( Qt::ISODate );
2810 }
2811
2813 {
2814 // value may be a map layer
2815 QVariantMap p;
2816 p.insert( name(), value );
2817 if ( QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context ) )
2818 {
2819 const QString source = QgsProcessingUtils::normalizeLayerSource( layer->source() );
2820 if ( !source.isEmpty() )
2821 return source;
2822 return layer->id();
2823 }
2824 }
2825
2826 // now we handle strings, after any other specific logic has already been applied
2827 if ( value.userType() == QMetaType::QString )
2828 return value.toString();
2829
2830 // unhandled type
2831 QgsDebugMsg( QStringLiteral( "unsupported variant type %1" ).arg( QMetaType::typeName( value.userType() ) ) );
2832 ok = false;
2833 return value.toString();
2834}
2835
2836QStringList QgsProcessingParameterDefinition::valueAsStringList( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
2837{
2838 ok = true;
2839 if ( !value.isValid( ) )
2840 return QStringList();
2841
2842 if ( value.type() == QVariant::Type::List || value.type() == QVariant::Type::StringList )
2843 {
2844 const QVariantList sourceList = value.toList();
2845 QStringList resultList;
2846 resultList.reserve( sourceList.size() );
2847 for ( const QVariant &v : sourceList )
2848 {
2849 resultList.append( valueAsStringList( v, context, ok ) );
2850 }
2851 return resultList;
2852 }
2853
2854 const QString res = valueAsString( value, context, ok );
2855 if ( !ok )
2856 return QStringList();
2857
2858 return {res};
2859}
2860
2862{
2863 return QString();
2864}
2865
2867{
2868 QString code = QStringLiteral( "##%1=" ).arg( mName );
2869 if ( mFlags & FlagOptional )
2870 code += QLatin1String( "optional " );
2871 code += type() + ' ';
2872 code += mDefault.toString();
2873 return code.trimmed();
2874}
2875
2877{
2878 // base class method is probably not much use
2880 {
2881 switch ( outputType )
2882 {
2884 {
2885 QString code = t->className() + QStringLiteral( "('%1', %2" )
2887 if ( mFlags & FlagOptional )
2888 code += QLatin1String( ", optional=True" );
2889
2891 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
2892 return code;
2893 }
2894 }
2895 }
2896
2897 // oh well, we tried
2898 return QString();
2899}
2900
2902{
2903 QVariantMap map;
2904 map.insert( QStringLiteral( "parameter_type" ), type() );
2905 map.insert( QStringLiteral( "name" ), mName );
2906 map.insert( QStringLiteral( "description" ), mDescription );
2907 map.insert( QStringLiteral( "help" ), mHelp );
2908 map.insert( QStringLiteral( "default" ), mDefault );
2909 map.insert( QStringLiteral( "defaultGui" ), mGuiDefault );
2910 map.insert( QStringLiteral( "flags" ), static_cast< int >( mFlags ) );
2911 map.insert( QStringLiteral( "metadata" ), mMetadata );
2912 return map;
2913}
2914
2916{
2917 mName = map.value( QStringLiteral( "name" ) ).toString();
2918 mDescription = map.value( QStringLiteral( "description" ) ).toString();
2919 mHelp = map.value( QStringLiteral( "help" ) ).toString();
2920 mDefault = map.value( QStringLiteral( "default" ) );
2921 mGuiDefault = map.value( QStringLiteral( "defaultGui" ) );
2922 mFlags = static_cast< Flags >( map.value( QStringLiteral( "flags" ) ).toInt() );
2923 mMetadata = map.value( QStringLiteral( "metadata" ) ).toMap();
2924 return true;
2925}
2926
2928{
2929 return mAlgorithm;
2930}
2931
2933{
2934 return mAlgorithm ? mAlgorithm->provider() : nullptr;
2935}
2936
2938{
2939 QString text = QStringLiteral( "<p><b>%1</b></p>" ).arg( description() );
2940 if ( !help().isEmpty() )
2941 {
2942 text += QStringLiteral( "<p>%1</p>" ).arg( help() );
2943 }
2944 text += QStringLiteral( "<p>%1</p>" ).arg( QObject::tr( "Python identifier: ‘%1’" ).arg( QStringLiteral( "<i>%1</i>" ).arg( name() ) ) );
2945 return text;
2946}
2947
2948QgsProcessingParameterBoolean::QgsProcessingParameterBoolean( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
2949 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2950{}
2951
2953{
2954 return new QgsProcessingParameterBoolean( *this );
2955}
2956
2958{
2959 if ( !val.isValid() )
2960 return QStringLiteral( "None" );
2961
2962 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
2963 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
2964 return val.toBool() ? QStringLiteral( "True" ) : QStringLiteral( "False" );
2965}
2966
2968{
2969 QString code = QStringLiteral( "##%1=" ).arg( mName );
2970 if ( mFlags & FlagOptional )
2971 code += QLatin1String( "optional " );
2972 code += type() + ' ';
2973 code += mDefault.toBool() ? QStringLiteral( "true" ) : QStringLiteral( "false" );
2974 return code.trimmed();
2975}
2976
2977QgsProcessingParameterBoolean *QgsProcessingParameterBoolean::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2978{
2979 return new QgsProcessingParameterBoolean( name, description, definition.toLower().trimmed() != QStringLiteral( "false" ), isOptional );
2980}
2981
2982QgsProcessingParameterCrs::QgsProcessingParameterCrs( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
2983 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2984{
2985
2986}
2987
2989{
2990 return new QgsProcessingParameterCrs( *this );
2991}
2992
2994{
2995 if ( !input.isValid() )
2996 return mFlags & FlagOptional;
2997
2998 if ( input.userType() == QMetaType::type( "QgsCoordinateReferenceSystem" ) )
2999 {
3000 return true;
3001 }
3002 else if ( input.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
3003 {
3004 return true;
3005 }
3006 else if ( input.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
3007 {
3008 return true;
3009 }
3010
3011 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
3012 {
3013 return true;
3014 }
3015
3016 // direct map layer value
3017 if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
3018 return true;
3019
3020 if ( input.type() != QVariant::String || input.toString().isEmpty() )
3021 return mFlags & FlagOptional;
3022
3023 return true;
3024}
3025
3026QString QgsProcessingParameterCrs::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
3027{
3028 if ( !value.isValid() )
3029 return QStringLiteral( "None" );
3030
3031 if ( value.userType() == QMetaType::type( "QgsCoordinateReferenceSystem" ) )
3032 {
3033 if ( !value.value< QgsCoordinateReferenceSystem >().isValid() )
3034 return QStringLiteral( "QgsCoordinateReferenceSystem()" );
3035 else
3036 return QStringLiteral( "QgsCoordinateReferenceSystem('%1')" ).arg( value.value< QgsCoordinateReferenceSystem >().authid() );
3037 }
3038
3039 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
3040 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3041
3042 QVariantMap p;
3043 p.insert( name(), value );
3044 QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
3045 if ( layer )
3047
3049}
3050
3051QString QgsProcessingParameterCrs::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
3052{
3054}
3055
3056QVariant QgsProcessingParameterCrs::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
3057{
3059}
3060
3061QgsProcessingParameterCrs *QgsProcessingParameterCrs::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3062{
3063 return new QgsProcessingParameterCrs( name, description, definition.compare( QLatin1String( "none" ), Qt::CaseInsensitive ) == 0 ? QVariant() : definition, isOptional );
3064}
3065
3066QgsProcessingParameterMapLayer::QgsProcessingParameterMapLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, const QList<int> &types )
3067 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3069{
3070
3071}
3072
3074{
3075 return new QgsProcessingParameterMapLayer( *this );
3076}
3077
3079{
3080 if ( !input.isValid() )
3081 return mFlags & FlagOptional;
3082
3083 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
3084 {
3085 return true;
3086 }
3087
3088 if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
3089 {
3090 return true;
3091 }
3092
3093 if ( input.type() != QVariant::String || input.toString().isEmpty() )
3094 return mFlags & FlagOptional;
3095
3096 if ( !context )
3097 {
3098 // that's as far as we can get without a context
3099 return true;
3100 }
3101
3102 // try to load as layer
3103 if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context ) )
3104 return true;
3105
3106 return false;
3107}
3108
3110{
3111 if ( !val.isValid() )
3112 return QStringLiteral( "None" );
3113
3114 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
3115 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
3116
3117 QVariantMap p;
3118 p.insert( name(), val );
3119 QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
3122}
3123
3124QString QgsProcessingParameterMapLayer::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
3125{
3127}
3128
3129QVariant QgsProcessingParameterMapLayer::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
3130{
3132}
3133
3135{
3136 QStringList vectors = QgsProviderRegistry::instance()->fileVectorFilters().split( QStringLiteral( ";;" ) );
3137 const QStringList rasters = QgsProviderRegistry::instance()->fileRasterFilters().split( QStringLiteral( ";;" ) );
3138 for ( const QString &raster : rasters )
3139 {
3140 if ( !vectors.contains( raster ) )
3141 vectors << raster;
3142 }
3143 const QStringList meshFilters = QgsProviderRegistry::instance()->fileMeshFilters().split( QStringLiteral( ";;" ) );
3144 for ( const QString &mesh : meshFilters )
3145 {
3146 if ( !vectors.contains( mesh ) )
3147 vectors << mesh;
3148 }
3149 const QStringList pointCloudFilters = QgsProviderRegistry::instance()->filePointCloudFilters().split( QStringLiteral( ";;" ) );
3150 for ( const QString &pointCloud : pointCloudFilters )
3151 {
3152 if ( !vectors.contains( pointCloud ) )
3153 vectors << pointCloud;
3154 }
3155 vectors.removeAll( QObject::tr( "All files (*.*)" ) );
3156 std::sort( vectors.begin(), vectors.end() );
3157
3158 return QObject::tr( "All files (*.*)" ) + QStringLiteral( ";;" ) + vectors.join( QLatin1String( ";;" ) );
3159}
3160
3162{
3164}
3165
3167{
3168 QString code = QStringLiteral( "##%1=" ).arg( mName );
3169 if ( mFlags & FlagOptional )
3170 code += QLatin1String( "optional " );
3171 code += QLatin1String( "layer " );
3172
3173 for ( const int type : mDataTypes )
3174 {
3175 switch ( type )
3176 {
3178 code += QLatin1String( "hasgeometry " );
3179 break;
3180
3182 code += QLatin1String( "point " );
3183 break;
3184
3186 code += QLatin1String( "line " );
3187 break;
3188
3190 code += QLatin1String( "polygon " );
3191 break;
3192
3194 code += QLatin1String( "raster " );
3195 break;
3196
3198 code += QLatin1String( "mesh " );
3199 break;
3200
3202 code += QLatin1String( "plugin " );
3203 break;
3204
3206 code += QLatin1String( "pointcloud " );
3207 break;
3208
3210 code += QLatin1String( "annotation " );
3211 break;
3212 }
3213 }
3214
3215 code += mDefault.toString();
3216 return code.trimmed();
3217}
3218
3219QgsProcessingParameterMapLayer *QgsProcessingParameterMapLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3220{
3221 QList< int > types;
3222 QString def = definition;
3223 while ( true )
3224 {
3225 if ( def.startsWith( QLatin1String( "hasgeometry" ), Qt::CaseInsensitive ) )
3226 {
3228 def = def.mid( 12 );
3229 continue;
3230 }
3231 else if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
3232 {
3234 def = def.mid( 6 );
3235 continue;
3236 }
3237 else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
3238 {
3240 def = def.mid( 5 );
3241 continue;
3242 }
3243 else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
3244 {
3246 def = def.mid( 8 );
3247 continue;
3248 }
3249 else if ( def.startsWith( QLatin1String( "raster" ), Qt::CaseInsensitive ) )
3250 {
3252 def = def.mid( 7 );
3253 continue;
3254 }
3255 else if ( def.startsWith( QLatin1String( "mesh" ), Qt::CaseInsensitive ) )
3256 {
3257 types << QgsProcessing::TypeMesh;
3258 def = def.mid( 5 );
3259 continue;
3260 }
3261 else if ( def.startsWith( QLatin1String( "plugin" ), Qt::CaseInsensitive ) )
3262 {
3264 def = def.mid( 7 );
3265 continue;
3266 }
3267 else if ( def.startsWith( QLatin1String( "pointcloud" ), Qt::CaseInsensitive ) )
3268 {
3270 def = def.mid( 11 );
3271 continue;
3272 }
3273 else if ( def.startsWith( QLatin1String( "annotation" ), Qt::CaseInsensitive ) )
3274 {
3276 def = def.mid( 11 );
3277 continue;
3278 }
3279 break;
3280 }
3281
3282 return new QgsProcessingParameterMapLayer( name, description, def, isOptional, types );
3283}
3284
3286{
3287 switch ( outputType )
3288 {
3290 {
3291 QString code = QStringLiteral( "QgsProcessingParameterMapLayer('%1', %2" )
3293 if ( mFlags & FlagOptional )
3294 code += QLatin1String( ", optional=True" );
3295
3297 code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );
3298
3299 if ( !mDataTypes.empty() )
3300 {
3301 QStringList options;
3302 options.reserve( mDataTypes.size() );
3303 for ( const int t : mDataTypes )
3304 options << QStringLiteral( "QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( static_cast< QgsProcessing::SourceType >( t ) ) );
3305 code += QStringLiteral( ", types=[%1])" ).arg( options.join( ',' ) );
3306 }
3307 else
3308 {
3309 code += QLatin1Char( ')' );
3310 }
3311
3312 return code;
3313 }
3314 }
3315 return QString();
3316}
3317
3319{
3321 QVariantList types;
3322 for ( const int type : mDataTypes )
3323 {
3324 types << type;
3325 }
3326 map.insert( QStringLiteral( "data_types" ), types );
3327 return map;
3328}
3329
3331{
3333 mDataTypes.clear();
3334 const QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
3335 for ( const QVariant &val : values )
3336 {
3337 mDataTypes << val.toInt();
3338 }
3339 return true;
3340}
3341
3342QgsProcessingParameterExtent::QgsProcessingParameterExtent( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
3343 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3344{
3345
3346}
3347
3349{
3350 return new QgsProcessingParameterExtent( *this );
3351}
3352
3354{
3355 if ( !input.isValid() )
3356 return mFlags & FlagOptional;
3357
3358 if ( input.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
3359 {
3360 return true;
3361 }
3362 else if ( input.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
3363 {
3364 return true;
3365 }
3366
3367 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
3368 {
3369 return true;
3370 }
3371
3372 if ( input.userType() == QMetaType::type( "QgsRectangle" ) )
3373 {
3374 const QgsRectangle r = input.value<QgsRectangle>();
3375 return !r.isNull();
3376 }
3377 if ( input.userType() == QMetaType::type( "QgsGeometry" ) )
3378 {
3379 return true;
3380 }
3381 if ( input.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
3382 {
3383 const QgsReferencedRectangle r = input.value<QgsReferencedRectangle>();
3384 return !r.isNull();
3385 }
3386
3387 // direct map layer value
3388 if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
3389 return true;
3390
3391 if ( input.type() != QVariant::String || input.toString().isEmpty() )
3392 return mFlags & FlagOptional;
3393
3394 if ( !context )
3395 {
3396 // that's as far as we can get without a context
3397 return true;
3398 }
3399
3400 const QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
3401 const QRegularExpressionMatch match = rx.match( input.toString() );
3402 if ( match.hasMatch() )
3403 {
3404 bool xMinOk = false;
3405 ( void )match.captured( 1 ).toDouble( &xMinOk );
3406 bool xMaxOk = false;
3407 ( void )match.captured( 2 ).toDouble( &xMaxOk );
3408 bool yMinOk = false;
3409 ( void )match.captured( 3 ).toDouble( &yMinOk );
3410 bool yMaxOk = false;
3411 ( void )match.captured( 4 ).toDouble( &yMaxOk );
3412 if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
3413 return true;
3414 }
3415
3416 // try as layer extent
3417 return QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
3418}
3419
3420QString QgsProcessingParameterExtent::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
3421{
3422 if ( !value.isValid() )
3423 return QStringLiteral( "None" );
3424
3425 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
3426 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3427
3428 if ( value.userType() == QMetaType::type( "QgsRectangle" ) )
3429 {
3430 const QgsRectangle r = value.value<QgsRectangle>();
3431 return QStringLiteral( "'%1, %3, %2, %4'" ).arg( qgsDoubleToString( r.xMinimum() ),
3434 qgsDoubleToString( r.yMaximum() ) );
3435 }
3436 else if ( value.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
3437 {
3438 const QgsReferencedRectangle r = value.value<QgsReferencedRectangle>();
3439 return QStringLiteral( "'%1, %3, %2, %4 [%5]'" ).arg( qgsDoubleToString( r.xMinimum() ),
3442 qgsDoubleToString( r.yMaximum() ), r.crs().authid() );
3443 }
3444 else if ( value.userType() == QMetaType::type( "QgsGeometry" ) )
3445 {
3446 const QgsGeometry g = value.value<QgsGeometry>();
3447 if ( !g.isNull() )
3448 {
3449 const QString wkt = g.asWkt();
3450 return QStringLiteral( "QgsGeometry.fromWkt('%1')" ).arg( wkt );
3451 }
3452 }
3453
3454 QVariantMap p;
3455 p.insert( name(), value );
3456 QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
3457 if ( layer )
3459
3461}
3462
3463QString QgsProcessingParameterExtent::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
3464{
3466}
3467
3468QVariant QgsProcessingParameterExtent::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
3469{
3471}
3472
3473QgsProcessingParameterExtent *QgsProcessingParameterExtent::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3474{
3475 return new QgsProcessingParameterExtent( name, description, definition, isOptional );
3476}
3477
3478QgsProcessingParameterPoint::QgsProcessingParameterPoint( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
3479 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3480{
3481
3482}
3483
3485{
3486 return new QgsProcessingParameterPoint( *this );
3487}
3488
3490{
3491 if ( !input.isValid() )
3492 return mFlags & FlagOptional;
3493
3494 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
3495 {
3496 return true;
3497 }
3498
3499 if ( input.userType() == QMetaType::type( "QgsPointXY" ) )
3500 {
3501 return true;
3502 }
3503 if ( input.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
3504 {
3505 return true;
3506 }
3507 if ( input.userType() == QMetaType::type( "QgsGeometry" ) )
3508 {
3509 return true;
3510 }
3511
3512 if ( input.type() == QVariant::String )
3513 {
3514 if ( input.toString().isEmpty() )
3515 return mFlags & FlagOptional;
3516 }
3517
3518 const QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
3519
3520 const QRegularExpressionMatch match = rx.match( input.toString() );
3521 if ( match.hasMatch() )
3522 {
3523 bool xOk = false;
3524 ( void )match.captured( 1 ).toDouble( &xOk );
3525 bool yOk = false;
3526 ( void )match.captured( 2 ).toDouble( &yOk );
3527 return xOk && yOk;
3528 }
3529 else
3530 return false;
3531}
3532
3533QString QgsProcessingParameterPoint::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
3534{
3535 if ( !value.isValid() )
3536 return QStringLiteral( "None" );
3537
3538 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
3539 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3540
3541 if ( value.userType() == QMetaType::type( "QgsPointXY" ) )
3542 {
3543 const QgsPointXY r = value.value<QgsPointXY>();
3544 return QStringLiteral( "'%1,%2'" ).arg( qgsDoubleToString( r.x() ),
3545 qgsDoubleToString( r.y() ) );
3546 }
3547 else if ( value.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
3548 {
3549 const QgsReferencedPointXY r = value.value<QgsReferencedPointXY>();
3550 return QStringLiteral( "'%1,%2 [%3]'" ).arg( qgsDoubleToString( r.x() ),
3551 qgsDoubleToString( r.y() ),
3552 r.crs().authid() );
3553 }
3554 else if ( value.userType() == QMetaType::type( "QgsGeometry" ) )
3555 {
3556 const QgsGeometry g = value.value<QgsGeometry>();
3557 if ( !g.isNull() )
3558 {
3559 const QString wkt = g.asWkt();
3560 return QStringLiteral( "QgsGeometry.fromWkt('%1')" ).arg( wkt );
3561 }
3562 }
3563
3565}
3566
3567QgsProcessingParameterPoint *QgsProcessingParameterPoint::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3568{
3569 return new QgsProcessingParameterPoint( name, description, definition, isOptional );
3570}
3571
3572QgsProcessingParameterGeometry::QgsProcessingParameterGeometry( const QString &name, const QString &description,
3573 const QVariant &defaultValue, bool optional, const QList<int> &geometryTypes, bool allowMultipart )
3574 : QgsProcessingParameterDefinition( name, description, defaultValue, optional ),
3575 mGeomTypes( geometryTypes ),
3576 mAllowMultipart( allowMultipart )
3577{
3578
3579}
3580
3582{
3583 return new QgsProcessingParameterGeometry( *this );
3584}
3585
3587{
3588 if ( !input.isValid() )
3589 return mFlags & FlagOptional;
3590
3591 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
3592 {
3593 return true;
3594 }
3595
3596 const bool anyTypeAllowed = mGeomTypes.isEmpty() || mGeomTypes.contains( static_cast< int >( Qgis::GeometryType::Unknown ) );
3597
3598 if ( input.userType() == QMetaType::type( "QgsGeometry" ) )
3599 {
3600 return ( anyTypeAllowed || mGeomTypes.contains( static_cast< int >( input.value<QgsGeometry>().type() ) ) ) &&
3601 ( mAllowMultipart || !input.value<QgsGeometry>().isMultipart() );
3602 }
3603
3604 if ( input.userType() == QMetaType::type( "QgsReferencedGeometry" ) )
3605 {
3606 return ( anyTypeAllowed || mGeomTypes.contains( static_cast<int>( input.value<QgsReferencedGeometry>().type() ) ) ) &&
3607 ( mAllowMultipart || !input.value<QgsReferencedGeometry>().isMultipart() );
3608 }
3609
3610 if ( input.userType() == QMetaType::type( "QgsPointXY" ) )
3611 {
3612 return anyTypeAllowed || mGeomTypes.contains( static_cast< int >( Qgis::GeometryType::Point ) );
3613 }
3614
3615 if ( input.userType() == QMetaType::type( "QgsRectangle" ) )
3616 {
3617 return anyTypeAllowed || mGeomTypes.contains( static_cast< int >( Qgis::GeometryType::Polygon ) );
3618 }
3619
3620 if ( input.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
3621 {
3622 return anyTypeAllowed || mGeomTypes.contains( static_cast< int >( Qgis::GeometryType::Point ) );
3623 }
3624
3625 if ( input.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
3626 {
3627 return anyTypeAllowed || mGeomTypes.contains( static_cast< int >( Qgis::GeometryType::Polygon ) );
3628 }
3629
3630 if ( input.type() == QVariant::String )
3631 {
3632 if ( input.toString().isEmpty() )
3633 return mFlags & FlagOptional;
3634 }
3635
3636 // Match against EWKT
3637 const QRegularExpression rx( QStringLiteral( "^\\s*(?:CRS=(.*);)?(.*?)$" ) );
3638
3639 const QRegularExpressionMatch match = rx.match( input.toString() );
3640 if ( match.hasMatch() )
3641 {
3642 const QgsGeometry g = QgsGeometry::fromWkt( match.captured( 2 ) );
3643 if ( ! g.isNull() )
3644 {
3645 return ( anyTypeAllowed || mGeomTypes.contains( static_cast< int >( g.type() ) ) ) && ( mAllowMultipart || !g.isMultipart() );
3646 }
3647 else
3648 {
3649 QgsMessageLog::logMessage( QObject::tr( "Error creating geometry: \"%1\"" ).arg( g.lastError() ), QObject::tr( "Processing" ) );
3650 }
3651 }
3652 return false;
3653}
3654
3656{
3658 {
3659 if ( !crs.isValid() )
3661 else
3662 return QgsProcessingUtils::stringToPythonLiteral( QStringLiteral( "CRS=%1;%2" ).arg( crs.authid().isEmpty() ? crs.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) : crs.authid(), g.asWkt() ) );
3663 };
3664
3665 if ( !value.isValid() )
3666 return QStringLiteral( "None" );
3667
3668 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
3669 return QStringLiteral( "QgsProperty.fromExpression(%1)" ).arg( QgsProcessingUtils::stringToPythonLiteral( value.value< QgsProperty >().asExpression() ) );
3670
3671 if ( value.userType() == QMetaType::type( "QgsGeometry" ) )
3672 {
3673 const QgsGeometry g = value.value<QgsGeometry>();
3674 if ( !g.isNull() )
3675 return asPythonString( g );
3676 }
3677
3678 if ( value.userType() == QMetaType::type( "QgsReferencedGeometry" ) )
3679 {
3680 const QgsReferencedGeometry g = value.value<QgsReferencedGeometry>();
3681 if ( !g.isNull() )
3682 return asPythonString( g, g.crs() );
3683 }
3684
3685 if ( value.userType() == QMetaType::type( "QgsPointXY" ) )
3686 {
3687 const QgsGeometry g = QgsGeometry::fromPointXY( value.value<QgsPointXY>() );
3688 if ( !g.isNull() )
3689 return asPythonString( g );
3690 }
3691
3692 if ( value.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
3693 {
3695 if ( !g.isNull() )
3696 return asPythonString( g, g.crs() );
3697 }
3698
3699 if ( value.userType() == QMetaType::type( "QgsRectangle" ) )
3700 {
3701 const QgsGeometry g = QgsGeometry::fromRect( value.value<QgsRectangle>() );
3702 if ( !g.isNull() )
3703 return asPythonString( g );
3704 }
3705
3706 if ( value.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
3707 {
3709 if ( !g.isNull() )
3710 return asPythonString( g, g.crs() );
3711 }
3712
3714}
3715
3717{
3718 QString code = QStringLiteral( "##%1=" ).arg( mName );
3719 if ( mFlags & FlagOptional )
3720 code += QLatin1String( "optional " );
3721 code += type() + ' ';
3722
3723 for ( const int type : mGeomTypes )
3724 {
3725 switch ( static_cast<Qgis::GeometryType>( type ) )
3726 {
3727 case Qgis::GeometryType::Point:
3728 code += QLatin1String( "point " );
3729 break;
3730
3731 case Qgis::GeometryType::Line:
3732 code += QLatin1String( "line " );
3733 break;
3734
3735 case Qgis::GeometryType::Polygon:
3736 code += QLatin1String( "polygon " );
3737 break;
3738
3739 default:
3740 code += QLatin1String( "unknown " );
3741 break;
3742 }
3743 }
3744
3745 code += mDefault.toString();
3746 return code.trimmed();
3747}
3748
3750{
3751 switch ( outputType )
3752 {
3754 {
3755 QString code = QStringLiteral( "QgsProcessingParameterGeometry('%1', %2" )
3757 if ( mFlags & FlagOptional )
3758 code += QLatin1String( ", optional=True" );
3759
3760 if ( !mGeomTypes.empty() )
3761 {
3762 auto geomTypeToString = []( Qgis::GeometryType t ) -> QString
3763 {
3764 switch ( t )
3765 {
3766 case Qgis::GeometryType::Point:
3767 return QStringLiteral( "PointGeometry" );
3768
3769 case Qgis::GeometryType::Line:
3770 return QStringLiteral( "LineGeometry" );
3771
3772 case Qgis::GeometryType::Polygon:
3773 return QStringLiteral( "PolygonGeometry" );
3774
3775 case Qgis::GeometryType::Unknown:
3776 return QStringLiteral( "UnknownGeometry" );
3777
3778 case Qgis::GeometryType::Null:
3779 return QStringLiteral( "NullGeometry" );
3780 }
3781 return QString();
3782 };
3783
3784 QStringList options;
3785 options.reserve( mGeomTypes.size() );
3786 for ( const int type : mGeomTypes )
3787 {
3788 options << QStringLiteral( " QgsWkbTypes.%1" ).arg( geomTypeToString( static_cast<Qgis::GeometryType>( type ) ) );
3789 }
3790 code += QStringLiteral( ", geometryTypes=[%1 ]" ).arg( options.join( ',' ) );
3791 }
3792
3793 if ( ! mAllowMultipart )
3794 {
3795 code += QLatin1String( ", allowMultipart=False" );
3796 }
3797
3799 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
3800 return code;
3801 }
3802 }
3803 return QString();
3804}
3805
3807{
3809 QVariantList types;
3810 for ( const int type : mGeomTypes )
3811 {
3812 types << type;
3813 }
3814 map.insert( QStringLiteral( "geometrytypes" ), types );
3815 map.insert( QStringLiteral( "multipart" ), mAllowMultipart );
3816 return map;
3817}
3818
3820{
3822 mGeomTypes.clear();
3823 const QVariantList values = map.value( QStringLiteral( "geometrytypes" ) ).toList();
3824 for ( const QVariant &val : values )
3825 {
3826 mGeomTypes << val.toInt();
3827 }
3828 mAllowMultipart = map.value( QStringLiteral( "multipart" ) ).toBool();
3829 return true;
3830}
3831
3832QgsProcessingParameterGeometry *QgsProcessingParameterGeometry::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3833{
3834 return new QgsProcessingParameterGeometry( name, description, definition, isOptional );
3835}
3836
3837QgsProcessingParameterFile::QgsProcessingParameterFile( const QString &name, const QString &description, Behavior behavior, const QString &extension, const QVariant &defaultValue, bool optional, const QString &fileFilter )
3838 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3839 , mBehavior( behavior )
3840 , mExtension( fileFilter.isEmpty() ? extension : QString() )
3841 , mFileFilter( fileFilter.isEmpty() && extension.isEmpty() ? QObject::tr( "All files (*.*)" ) : fileFilter )
3842{
3843
3844}
3845
3847{
3848 return new QgsProcessingParameterFile( *this );
3849}
3850
3852{
3853 if ( !input.isValid() )
3854 return mFlags & FlagOptional;
3855
3856 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
3857 {
3858 return true;
3859 }
3860
3861 const QString string = input.toString().trimmed();
3862
3863 if ( input.type() != QVariant::String || string.isEmpty() )
3864 return mFlags & FlagOptional;
3865
3866 switch ( mBehavior )
3867 {
3868 case File:
3869 {
3870 if ( !mExtension.isEmpty() )
3871 {
3872 return string.endsWith( mExtension, Qt::CaseInsensitive );
3873 }
3874 else if ( !mFileFilter.isEmpty() )
3875 {
3876 return QgsFileUtils::fileMatchesFilter( string, mFileFilter );
3877 }
3878 else
3879 {
3880 return true;
3881 }
3882 }
3883
3884 case Folder:
3885 return true;
3886 }
3887 return true;
3888}
3889
3891{
3892 QString code = QStringLiteral( "##%1=" ).arg( mName );
3893 if ( mFlags & FlagOptional )
3894 code += QLatin1String( "optional " );
3895 code += ( mBehavior == File ? QStringLiteral( "file" ) : QStringLiteral( "folder" ) ) + ' ';
3896 code += mDefault.toString();
3897 return code.trimmed();
3898}
3899
3901{
3902 switch ( outputType )
3903 {
3905 {
3906
3907 QString code = QStringLiteral( "QgsProcessingParameterFile('%1', %2" )
3909 if ( mFlags & FlagOptional )
3910 code += QLatin1String( ", optional=True" );
3911 code += QStringLiteral( ", behavior=%1" ).arg( mBehavior == File ? QStringLiteral( "QgsProcessingParameterFile.File" ) : QStringLiteral( "QgsProcessingParameterFile.Folder" ) );
3912 if ( !mExtension.isEmpty() )
3913 code += QStringLiteral( ", extension='%1'" ).arg( mExtension );
3914 if ( !mFileFilter.isEmpty() )
3915 code += QStringLiteral( ", fileFilter='%1'" ).arg( mFileFilter );
3917 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
3918 return code;
3919 }
3920 }
3921 return QString();
3922}
3923
3925{
3926 switch ( mBehavior )
3927 {
3928 case File:
3929 {
3930 if ( !mFileFilter.isEmpty() )
3931 return mFileFilter != QObject::tr( "All files (*.*)" ) ? mFileFilter + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" ) : mFileFilter;
3932 else if ( !mExtension.isEmpty() )
3933 return QObject::tr( "%1 files" ).arg( mExtension.toUpper() ) + QStringLiteral( " (*." ) + mExtension.toLower() + QStringLiteral( ");;" ) + QObject::tr( "All files (*.*)" );
3934 else
3935 return QObject::tr( "All files (*.*)" );
3936 }
3937
3938 case Folder:
3939 return QString();
3940 }
3941 return QString();
3942}
3943
3944void QgsProcessingParameterFile::setExtension( const QString &extension )
3945{
3946 mExtension = extension;
3947 mFileFilter.clear();
3948}
3949
3951{
3952 return mFileFilter;
3953}
3954
3956{
3957 mFileFilter = filter;
3958 mExtension.clear();
3959}
3960
3962{
3964 map.insert( QStringLiteral( "behavior" ), mBehavior );
3965 map.insert( QStringLiteral( "extension" ), mExtension );
3966 map.insert( QStringLiteral( "filefilter" ), mFileFilter );
3967 return map;
3968}
3969
3971{
3973 mBehavior = static_cast< Behavior >( map.value( QStringLiteral( "behavior" ) ).toInt() );
3974 mExtension = map.value( QStringLiteral( "extension" ) ).toString();
3975 mFileFilter = map.value( QStringLiteral( "filefilter" ) ).toString();
3976 return true;
3977}
3978
3979QgsProcessingParameterFile *QgsProcessingParameterFile::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition, QgsProcessingParameterFile::Behavior behavior )
3980{
3981 return new QgsProcessingParameterFile( name, description, behavior, QString(), definition, isOptional );
3982}
3983
3984QgsProcessingParameterMatrix::QgsProcessingParameterMatrix( const QString &name, const QString &description, int numberRows, bool fixedNumberRows, const QStringList &headers, const QVariant &defaultValue, bool optional )
3985 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3986 , mHeaders( headers )
3987 , mNumberRows( numberRows )
3988 , mFixedNumberRows( fixedNumberRows )
3989{
3990
3991}
3992
3994{
3995 return new QgsProcessingParameterMatrix( *this );
3996}
3997
3999{
4000 if ( !input.isValid() )
4001 return mFlags & FlagOptional;
4002
4003 if ( input.type() == QVariant::String )
4004 {
4005 if ( input.toString().isEmpty() )
4006 return mFlags & FlagOptional;
4007 return true;
4008 }
4009 else if ( input.type() == QVariant::List )
4010 {
4011 if ( input.toList().isEmpty() )
4012 return mFlags & FlagOptional;
4013 return true;
4014 }
4015 else if ( input.type() == QVariant::Double || input.type() == QVariant::Int )
4016 {
4017 return true;
4018 }
4019
4020 return false;
4021}
4022
4023QString QgsProcessingParameterMatrix::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
4024{
4025 if ( !value.isValid() )
4026 return QStringLiteral( "None" );
4027
4028 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
4029 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4030
4031 QVariantMap p;
4032 p.insert( name(), value );
4033 const QVariantList list = QgsProcessingParameters::parameterAsMatrix( this, p, context );
4034
4036}
4037
4039{
4040 switch ( outputType )
4041 {
4043 {
4044 QString code = QStringLiteral( "QgsProcessingParameterMatrix('%1', %2" )
4046 if ( mFlags & FlagOptional )
4047 code += QLatin1String( ", optional=True" );
4048 code += QStringLiteral( ", numberRows=%1" ).arg( mNumberRows );
4049 code += QStringLiteral( ", hasFixedNumberRows=%1" ).arg( mFixedNumberRows ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
4050
4051 QStringList headers;
4052 headers.reserve( mHeaders.size() );
4053 for ( const QString &h : mHeaders )
4055 code += QStringLiteral( ", headers=[%1]" ).arg( headers.join( ',' ) );
4056
4058 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
4059 return code;
4060 }
4061 }
4062 return QString();
4063}
4064
4066{
4067 return mHeaders;
4068}
4069
4070void QgsProcessingParameterMatrix::setHeaders( const QStringList &headers )
4071{
4072 mHeaders = headers;
4073}
4074
4076{
4077 return mNumberRows;
4078}
4079
4081{
4082 mNumberRows = numberRows;
4083}
4084
4086{
4087 return mFixedNumberRows;
4088}
4089
4091{
4092 mFixedNumberRows = fixedNumberRows;
4093}
4094
4096{
4098 map.insert( QStringLiteral( "headers" ), mHeaders );
4099 map.insert( QStringLiteral( "rows" ), mNumberRows );
4100 map.insert( QStringLiteral( "fixed_number_rows" ), mFixedNumberRows );
4101 return map;
4102}
4103
4105{
4107 mHeaders = map.value( QStringLiteral( "headers" ) ).toStringList();
4108 mNumberRows = map.value( QStringLiteral( "rows" ) ).toInt();
4109 mFixedNumberRows = map.value( QStringLiteral( "fixed_number_rows" ) ).toBool();
4110 return true;
4111}
4112
4113QgsProcessingParameterMatrix *QgsProcessingParameterMatrix::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4114{
4115 return new QgsProcessingParameterMatrix( name, description, 0, false, QStringList(), definition.isEmpty() ? QVariant() : definition, isOptional );
4116}
4117
4118QgsProcessingParameterMultipleLayers::QgsProcessingParameterMultipleLayers( const QString &name, const QString &description, QgsProcessing::SourceType layerType, const QVariant &defaultValue, bool optional )
4119 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4120 , mLayerType( layerType )
4121{
4122
4123}
4124
4126{
4127 return new QgsProcessingParameterMultipleLayers( *this );
4128}
4129
4131{
4132 if ( !input.isValid() )
4133 return mFlags & FlagOptional;
4134
4135 if ( mLayerType != QgsProcessing::TypeFile )
4136 {
4137 if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
4138 {
4139 return true;
4140 }
4141 }
4142
4143 if ( input.type() == QVariant::String )
4144 {
4145 if ( input.toString().isEmpty() )
4146 return mFlags & FlagOptional;
4147
4148 if ( mMinimumNumberInputs > 1 )
4149 return false;
4150
4151 if ( !context )
4152 return true;
4153
4154 if ( mLayerType != QgsProcessing::TypeFile )
4155 return QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
4156 else
4157 return true;
4158 }
4159 else if ( input.type() == QVariant::List )
4160 {
4161 if ( input.toList().count() < mMinimumNumberInputs )
4162 return mFlags & FlagOptional;
4163
4164 if ( mMinimumNumberInputs > input.toList().count() )
4165 return false;
4166
4167 if ( !context )
4168 return true;
4169
4170 if ( mLayerType != QgsProcessing::TypeFile )
4171 {
4172 const auto constToList = input.toList();
4173 for ( const QVariant &v : constToList )
4174 {
4175 if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( v ) ) )
4176 continue;
4177
4178 if ( !QgsProcessingUtils::mapLayerFromString( v.toString(), *context ) )
4179 return false;
4180 }
4181 }
4182 return true;
4183 }
4184 else if ( input.type() == QVariant::StringList )
4185 {
4186 if ( input.toStringList().count() < mMinimumNumberInputs )
4187 return mFlags & FlagOptional;
4188
4189 if ( mMinimumNumberInputs > input.toStringList().count() )
4190 return false;
4191
4192 if ( !context )
4193 return true;
4194
4195 if ( mLayerType != QgsProcessing::TypeFile )
4196 {
4197 const auto constToStringList = input.toStringList();
4198 for ( const QString &v : constToStringList )
4199 {
4200 if ( !QgsProcessingUtils::mapLayerFromString( v, *context ) )
4201 return false;
4202 }
4203 }
4204 return true;
4205 }
4206 return false;
4207}
4208
4210{
4211 if ( !value.isValid() )
4212 return QStringLiteral( "None" );
4213
4214 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
4215 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4216
4217 if ( mLayerType == QgsProcessing::TypeFile )
4218 {
4219 QStringList parts;
4220 if ( value.type() == QVariant::StringList )
4221 {
4222 const QStringList list = value.toStringList();
4223 parts.reserve( list.count() );
4224 for ( const QString &v : list )
4226 }
4227 else if ( value.type() == QVariant::List )
4228 {
4229 const QVariantList list = value.toList();
4230 parts.reserve( list.count() );
4231 for ( const QVariant &v : list )
4232 parts << QgsProcessingUtils::stringToPythonLiteral( v.toString() );
4233 }
4234 if ( !parts.isEmpty() )
4235 return parts.join( ',' ).prepend( '[' ).append( ']' );
4236 }
4237 else
4238 {
4239 QVariantMap p;
4240 p.insert( name(), value );
4241 const QList<QgsMapLayer *> list = QgsProcessingParameters::parameterAsLayerList( this, p, context );
4242 if ( !list.isEmpty() )
4243 {
4244 QStringList parts;
4245 parts.reserve( list.count() );
4246 for ( const QgsMapLayer *layer : list )
4247 {
4249 }
4250 return parts.join( ',' ).prepend( '[' ).append( ']' );
4251 }
4252 }
4253
4255}
4256
4257QString QgsProcessingParameterMultipleLayers::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
4258{
4260}
4261
4263{
4265}
4266
4268{
4269 QString code = QStringLiteral( "##%1=" ).arg( mName );
4270 if ( mFlags & FlagOptional )
4271 code += QLatin1String( "optional " );
4272 switch ( mLayerType )
4273 {
4275 code += QLatin1String( "multiple raster" );
4276 break;
4277
4279 code += QLatin1String( "multiple file" );
4280 break;
4281
4282 default:
4283 code += QLatin1String( "multiple vector" );
4284 break;
4285 }
4286 code += ' ';
4287 if ( mDefault.type() == QVariant::List )
4288 {
4289 QStringList parts;
4290 const auto constToList = mDefault.toList();
4291 for ( const QVariant &var : constToList )
4292 {
4293 parts << var.toString();
4294 }
4295 code += parts.join( ',' );
4296 }
4297 else if ( mDefault.type() == QVariant::StringList )
4298 {
4299 code += mDefault.toStringList().join( ',' );
4300 }
4301 else
4302 {
4303 code += mDefault.toString();
4304 }
4305 return code.trimmed();
4306}
4307
4309{
4310 switch ( outputType )
4311 {
4313 {
4314 QString code = QStringLiteral( "QgsProcessingParameterMultipleLayers('%1', %2" )
4316 if ( mFlags & FlagOptional )
4317 code += QLatin1String( ", optional=True" );
4318
4319 const QString layerType = QStringLiteral( "QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( mLayerType ) );
4320
4321 code += QStringLiteral( ", layerType=%1" ).arg( layerType );
4323 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
4324 return code;
4325 }
4326 }
4327 return QString();
4328}
4329
4331{
4332 switch ( mLayerType )
4333 {
4335 return QObject::tr( "All files (*.*)" );
4336
4338 return QgsProviderRegistry::instance()->fileRasterFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
4339
4345 return QgsProviderRegistry::instance()->fileVectorFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
4346
4348 return QgsProviderRegistry::instance()->fileMeshFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
4349
4351 return QgsProviderRegistry::instance()->filePointCloudFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
4352
4357 }
4358 return QString();
4359}
4360
4362{
4363 return mLayerType;
4364}
4365
4367{
4368 mLayerType = type;
4369}
4370
4372{
4373 return mMinimumNumberInputs;
4374}
4375
4377{
4378 if ( mMinimumNumberInputs >= 1 || !( flags() & QgsProcessingParameterDefinition::FlagOptional ) )
4379 mMinimumNumberInputs = minimumNumberInputs;
4380}
4381
4383{
4385 map.insert( QStringLiteral( "layer_type" ), mLayerType );
4386 map.insert( QStringLiteral( "min_inputs" ), mMinimumNumberInputs );
4387 return map;
4388}
4389
4391{
4393 mLayerType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "layer_type" ) ).toInt() );
4394 mMinimumNumberInputs = map.value( QStringLiteral( "min_inputs" ) ).toInt();
4395 return true;
4396}
4397
4398QgsProcessingParameterMultipleLayers *QgsProcessingParameterMultipleLayers::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4399{
4400 QString type = definition;
4401 QString defaultVal;
4402 const QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)" ) );
4403 const QRegularExpressionMatch m = re.match( definition );
4404 if ( m.hasMatch() )
4405 {
4406 type = m.captured( 1 ).toLower().trimmed();
4407 defaultVal = m.captured( 2 );
4408 }
4410 if ( type == QLatin1String( "vector" ) )
4412 else if ( type == QLatin1String( "raster" ) )
4414 else if ( type == QLatin1String( "file" ) )
4416 return new QgsProcessingParameterMultipleLayers( name, description, layerType, defaultVal.isEmpty() ? QVariant() : defaultVal, isOptional );
4417}
4418
4419QgsProcessingParameterNumber::QgsProcessingParameterNumber( const QString &name, const QString &description, Type type, const QVariant &defaultValue, bool optional, double minValue, double maxValue )
4420 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4421 , mMin( minValue )
4422 , mMax( maxValue )
4423 , mDataType( type )
4424{
4425 if ( mMin >= mMax )
4426 {
4427 QgsMessageLog::logMessage( QObject::tr( "Invalid number parameter \"%1\": min value %2 is >= max value %3!" ).arg( name ).arg( mMin ).arg( mMax ), QObject::tr( "Processing" ) );
4428 }
4429}
4430
4432{
4433 return new QgsProcessingParameterNumber( *this );
4434}
4435
4437{
4438 QVariant input = value;
4439 if ( !input.isValid() )
4440 {
4441 if ( !defaultValue().isValid() )
4442 return mFlags & FlagOptional;
4443
4444 input = defaultValue();
4445 }
4446
4447 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
4448 {
4449 return true;
4450 }
4451
4452 bool ok = false;
4453 const double res = input.toDouble( &ok );
4454 if ( !ok )
4455 return mFlags & FlagOptional;
4456
4457 return !( res < mMin || res > mMax );
4458}
4459
4461{
4462 if ( !value.isValid() )
4463 return QStringLiteral( "None" );
4464
4465 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
4466 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4467
4468 return value.toString();
4469}
4470
4472{
4474 QStringList parts;
4475 if ( mMin > std::numeric_limits<double>::lowest() + 1 )
4476 parts << QObject::tr( "Minimum value: %1" ).arg( mMin );
4477 if ( mMax < std::numeric_limits<double>::max() )
4478 parts << QObject::tr( "Maximum value: %1" ).arg( mMax );
4479 if ( mDefault.isValid() )
4480 parts << QObject::tr( "Default value: %1" ).arg( mDataType == Integer ? mDefault.toInt() : mDefault.toDouble() );
4481 const QString extra = parts.join( QLatin1String( "<br />" ) );
4482 if ( !extra.isEmpty() )
4483 text += QStringLiteral( "<p>%1</p>" ).arg( extra );
4484 return text;
4485}
4486
4488{
4489 switch ( outputType )
4490 {
4492 {
4493 QString code = QStringLiteral( "QgsProcessingParameterNumber('%1', %2" )
4495 if ( mFlags & FlagOptional )
4496 code += QLatin1String( ", optional=True" );
4497
4498 code += QStringLiteral( ", type=%1" ).arg( mDataType == Integer ? QStringLiteral( "QgsProcessingParameterNumber.Integer" ) : QStringLiteral( "QgsProcessingParameterNumber.Double" ) );
4499
4500 if ( mMin != std::numeric_limits<double>::lowest() + 1 )
4501 code += QStringLiteral( ", minValue=%1" ).arg( mMin );
4502 if ( mMax != std::numeric_limits<double>::max() )
4503 code += QStringLiteral( ", maxValue=%1" ).arg( mMax );
4505 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
4506 return code;
4507 }
4508 }
4509 return QString();
4510}
4511
4513{
4514 return mMin;
4515}
4516
4518{
4519 mMin = min;
4520}
4521
4523{
4524 return mMax;
4525}
4526
4528{
4529 mMax = max;
4530}
4531
4533{
4534 return mDataType;
4535}
4536
4538{
4539 mDataType = dataType;
4540}
4541
4543{
4545 map.insert( QStringLiteral( "min" ), mMin );
4546 map.insert( QStringLiteral( "max" ), mMax );
4547 map.insert( QStringLiteral( "data_type" ), mDataType );
4548 return map;
4549}
4550
4552{
4554 mMin = map.value( QStringLiteral( "min" ) ).toDouble();
4555 mMax = map.value( QStringLiteral( "max" ) ).toDouble();
4556 mDataType = static_cast< Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
4557 return true;
4558}
4559
4560QgsProcessingParameterNumber *QgsProcessingParameterNumber::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4561{
4562 return new QgsProcessingParameterNumber( name, description, Double, definition.isEmpty() ? QVariant()
4563 : ( definition.toLower().trimmed() == QLatin1String( "none" ) ? QVariant() : definition ), isOptional );
4564}
4565
4566QgsProcessingParameterRange::QgsProcessingParameterRange( const QString &name, const QString &description, QgsProcessingParameterNumber::Type type, const QVariant &defaultValue, bool optional )
4567 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4568 , mDataType( type )
4569{
4570
4571}
4572
4574{
4575 return new QgsProcessingParameterRange( *this );
4576}
4577
4579{
4580 if ( !input.isValid() )
4581 return mFlags & FlagOptional;
4582
4583 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
4584 {
4585 return true;
4586 }
4587
4588 if ( input.type() == QVariant::String )
4589 {
4590 const QStringList list = input.toString().split( ',' );
4591 if ( list.count() != 2 )
4592 return mFlags & FlagOptional;
4593 bool ok = false;
4594 list.at( 0 ).toDouble( &ok );
4595 bool ok2 = false;
4596 list.at( 1 ).toDouble( &ok2 );
4597 if ( !ok || !ok2 )
4598 return mFlags & FlagOptional;
4599 return true;
4600 }
4601 else if ( input.type() == QVariant::List )
4602 {
4603 if ( input.toList().count() != 2 )
4604 return mFlags & FlagOptional;
4605
4606 bool ok = false;
4607 input.toList().at( 0 ).toDouble( &ok );
4608 bool ok2 = false;
4609 input.toList().at( 1 ).toDouble( &ok2 );
4610 if ( !ok || !ok2 )
4611 return mFlags & FlagOptional;
4612 return true;
4613 }
4614
4615 return false;
4616}
4617
4618QString QgsProcessingParameterRange::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
4619{
4620 if ( !value.isValid() )
4621 return QStringLiteral( "None" );
4622
4623 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
4624 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4625
4626 QVariantMap p;
4627 p.insert( name(), value );
4628 const QList< double > parts = QgsProcessingParameters::parameterAsRange( this, p, context );
4629
4630 QStringList stringParts;
4631 const auto constParts = parts;
4632 for ( const double v : constParts )
4633 {
4634 stringParts << QString::number( v );
4635 }
4636 return stringParts.join( ',' ).prepend( '[' ).append( ']' );
4637}
4638
4640{
4641 switch ( outputType )
4642 {
4644 {
4645 QString code = QStringLiteral( "QgsProcessingParameterRange('%1', %2" )
4647 if ( mFlags & FlagOptional )
4648 code += QLatin1String( ", optional=True" );
4649
4650 code += QStringLiteral( ", type=%1" ).arg( mDataType == QgsProcessingParameterNumber::Integer ? QStringLiteral( "QgsProcessingParameterNumber.Integer" ) : QStringLiteral( "QgsProcessingParameterNumber.Double" ) );
4651
4653 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
4654 return code;
4655 }
4656 }
4657 return QString();
4658}
4659
4661{
4662 return mDataType;
4663}
4664
4666{
4667 mDataType = dataType;
4668}
4669
4671{
4673 map.insert( QStringLiteral( "data_type" ), mDataType );
4674 return map;
4675}
4676
4678{
4680 mDataType = static_cast< QgsProcessingParameterNumber::Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
4681 return true;
4682}
4683
4684QgsProcessingParameterRange *QgsProcessingParameterRange::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4685{
4686 return new QgsProcessingParameterRange( name, description, QgsProcessingParameterNumber::Double, definition.isEmpty() ? QVariant()
4687 : ( definition.toLower().trimmed() == QLatin1String( "none" ) ? QVariant() : definition ), isOptional );
4688}
4689
4690QgsProcessingParameterRasterLayer::QgsProcessingParameterRasterLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
4691 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4692{
4693
4694}
4695
4697{
4698 return new QgsProcessingParameterRasterLayer( *this );
4699}
4700
4702{
4703 if ( !input.isValid() )
4704 return mFlags & FlagOptional;
4705
4706 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
4707 {
4708 return true;
4709 }
4710
4711 if ( qobject_cast< QgsRasterLayer * >( qvariant_cast<QObject *>( input ) ) )
4712 return true;
4713
4714 if ( input.type() != QVariant::String || input.toString().isEmpty() )
4715 return mFlags & FlagOptional;
4716
4717 if ( !context )
4718 {
4719 // that's as far as we can get without a context
4720 return true;
4721 }
4722
4723 // try to load as layer
4724 if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context, true, QgsProcessingUtils::LayerHint::Raster ) )
4725 return true;
4726
4727 return false;
4728}
4729
4731{
4732 if ( !val.isValid() )
4733 return QStringLiteral( "None" );
4734
4735 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
4736 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
4737
4738 QVariantMap p;
4739 p.insert( name(), val );
4743}
4744
4745QString QgsProcessingParameterRasterLayer::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
4746{
4748}
4749
4751{
4753}
4754
4756{
4757 return QgsProviderRegistry::instance()->fileRasterFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
4758}
4759
4760QgsProcessingParameterRasterLayer *QgsProcessingParameterRasterLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4761{
4762 return new QgsProcessingParameterRasterLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
4763}
4764
4765QgsProcessingParameterEnum::QgsProcessingParameterEnum( const QString &name, const QString &description, const QStringList &options, bool allowMultiple, const QVariant &defaultValue, bool optional, bool usesStaticStrings )
4766 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4767 , mOptions( options )
4768 , mAllowMultiple( allowMultiple )
4769 , mUsesStaticStrings( usesStaticStrings )
4770{
4771
4772}
4773
4775{
4776 return new QgsProcessingParameterEnum( *this );
4777}
4778
4780{
4781 QVariant input = value;
4782 if ( !input.isValid() )
4783 {
4784 if ( !defaultValue().isValid() )
4785 return mFlags & FlagOptional;
4786
4787 input = defaultValue();
4788 }
4789
4790 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
4791 {
4792 return true;
4793 }
4794
4795 if ( mUsesStaticStrings )
4796 {
4797 if ( input.type() == QVariant::List )
4798 {
4799 if ( !mAllowMultiple )
4800 return false;
4801
4802 const QVariantList values = input.toList();
4803 if ( values.empty() && !( mFlags & FlagOptional ) )
4804 return false;
4805
4806 for ( const QVariant &val : values )
4807 {
4808 if ( !mOptions.contains( val.toString() ) )
4809 return false;
4810 }
4811
4812 return true;
4813 }
4814 else if ( input.type() == QVariant::StringList )
4815 {
4816 if ( !mAllowMultiple )
4817 return false;
4818
4819 const QStringList values = input.toStringList();
4820
4821 if ( values.empty() && !( mFlags & FlagOptional ) )
4822 return false;
4823
4824 if ( values.count() > 1 && !mAllowMultiple )
4825 return false;
4826
4827 for ( const QString &val : values )
4828 {
4829 if ( !mOptions.contains( val ) )
4830 return false;
4831 }
4832 return true;
4833 }
4834 else if ( input.type() == QVariant::String )
4835 {
4836 const QStringList parts = input.toString().split( ',' );
4837 if ( parts.count() > 1 && !mAllowMultiple )
4838 return false;
4839
4840 const auto constParts = parts;
4841 for ( const QString &part : constParts )
4842 {
4843 if ( !mOptions.contains( part ) )
4844 return false;
4845 }
4846 return true;
4847 }
4848 }
4849 else
4850 {
4851 if ( input.type() == QVariant::List )
4852 {
4853 if ( !mAllowMultiple )
4854 return false;
4855
4856 const QVariantList values = input.toList();
4857 if ( values.empty() && !( mFlags & FlagOptional ) )
4858 return false;
4859
4860 for ( const QVariant &val : values )
4861 {
4862 bool ok = false;
4863 const int res = val.toInt( &ok );
4864 if ( !ok )
4865 return false;
4866 else if ( res < 0 || res >= mOptions.count() )
4867 return false;
4868 }
4869
4870 return true;
4871 }
4872 else if ( input.type() == QVariant::String )
4873 {
4874 const QStringList parts = input.toString().split( ',' );
4875 if ( parts.count() > 1 && !mAllowMultiple )
4876 return false;
4877
4878 const auto constParts = parts;
4879 for ( const QString &part : constParts )
4880 {
4881 bool ok = false;
4882 const int res = part.toInt( &ok );
4883 if ( !ok )
4884 return false;
4885 else if ( res < 0 || res >= mOptions.count() )
4886 return false;
4887 }
4888 return true;
4889 }
4890 else if ( input.type() == QVariant::Int || input.type() == QVariant::Double )
4891 {
4892 bool ok = false;
4893 const int res = input.toInt( &ok );
4894 if ( !ok )
4895 return false;
4896 else if ( res >= 0 && res < mOptions.count() )
4897 return true;
4898 }
4899 }
4900
4901 return false;
4902}
4903
4905{
4906 if ( !value.isValid() )
4907 return QStringLiteral( "None" );
4908
4909 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
4910 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4911
4912 if ( mUsesStaticStrings )
4913 {
4914 if ( value.type() == QVariant::StringList )
4915 {
4916 QStringList parts;
4917 const QStringList constList = value.toStringList();
4918 for ( const QString &val : constList )
4919 {
4921 }
4922 return parts.join( ',' ).prepend( '[' ).append( ']' );
4923 }
4924 else if ( value.type() == QVariant::String )
4925 {
4926 QStringList parts;
4927 const QStringList constList = value.toString().split( ',' );
4928 if ( constList.count() > 1 )
4929 {
4930 for ( const QString &val : constList )
4931 {
4933 }
4934 return parts.join( ',' ).prepend( '[' ).append( ']' );
4935 }
4936 }
4937
4938 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
4939 }
4940 else
4941 {
4942 if ( value.type() == QVariant::List )
4943 {
4944 QStringList parts;
4945 const auto constToList = value.toList();
4946 for ( const QVariant &val : constToList )
4947 {
4948 parts << QString::number( static_cast< int >( val.toDouble() ) );
4949 }
4950 return parts.join( ',' ).prepend( '[' ).append( ']' );
4951 }
4952 else if ( value.type() == QVariant::String )
4953 {
4954 const QStringList parts = value.toString().split( ',' );
4955 if ( parts.count() > 1 )
4956 {
4957 return parts.join( ',' ).prepend( '[' ).append( ']' );
4958 }
4959 }
4960
4961 return QString::number( static_cast< int >( value.toDouble() ) );
4962 }
4963}
4964
4966{
4967 if ( !value.isValid() )
4968 return QString();
4969
4970 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
4971 return QString();
4972
4973 if ( mUsesStaticStrings )
4974 {
4975 return QString();
4976 }
4977 else
4978 {
4979 if ( value.type() == QVariant::List )
4980 {
4981 QStringList parts;
4982 const QVariantList toList = value.toList();
4983 parts.reserve( toList.size() );
4984 for ( const QVariant &val : toList )
4985 {
4986 parts << mOptions.value( static_cast< int >( val.toDouble() ) );
4987 }
4988 return parts.join( ',' );
4989 }
4990 else if ( value.type() == QVariant::String )
4991 {
4992 const QStringList parts = value.toString().split( ',' );
4993 QStringList comments;
4994 if ( parts.count() > 1 )
4995 {
4996 for ( const QString &part : parts )
4997 {
4998 bool ok = false;
4999 const int val = part.toInt( &ok );
5000 if ( ok )
5001 comments << mOptions.value( val );
5002 }
5003 return comments.join( ',' );
5004 }
5005 }
5006
5007 return mOptions.value( static_cast< int >( value.toDouble() ) );
5008 }
5009}
5010
5012{
5013 QString code = QStringLiteral( "##%1=" ).arg( mName );
5014 if ( mFlags & FlagOptional )
5015 code += QLatin1String( "optional " );
5016 code += QLatin1String( "enum " );
5017
5018 if ( mAllowMultiple )
5019 code += QLatin1String( "multiple " );
5020
5021 if ( mUsesStaticStrings )
5022 code += QLatin1String( "static " );
5023
5024 code += mOptions.join( ';' ) + ' ';
5025
5026 code += mDefault.toString();
5027 return code.trimmed();
5028}
5029
5031{
5032 switch ( outputType )
5033 {
5035 {
5036 QString code = QStringLiteral( "QgsProcessingParameterEnum('%1', %2" )
5038 if ( mFlags & FlagOptional )
5039 code += QLatin1String( ", optional=True" );
5040
5041 QStringList options;
5042 options.reserve( mOptions.size() );
5043 for ( const QString &o : mOptions )
5045 code += QStringLiteral( ", options=[%1]" ).arg( options.join( ',' ) );
5046
5047 code += QStringLiteral( ", allowMultiple=%1" ).arg( mAllowMultiple ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
5048
5049 code += QStringLiteral( ", usesStaticStrings=%1" ).arg( mUsesStaticStrings ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
5050
5052 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
5053
5054 return code;
5055 }
5056 }
5057 return QString();
5058}
5059
5061{
5062 return mOptions;
5063}
5064
5065void QgsProcessingParameterEnum::setOptions( const QStringList &options )
5066{
5067 mOptions = options;
5068}
5069
5071{
5072 return mAllowMultiple;
5073}
5074
5076{
5077 mAllowMultiple = allowMultiple;
5078}
5079
5081{
5082 return mUsesStaticStrings;
5083}
5084
5086{
5087 mUsesStaticStrings = usesStaticStrings;
5088}
5089
5091{
5093 map.insert( QStringLiteral( "options" ), mOptions );
5094 map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
5095 map.insert( QStringLiteral( "uses_static_strings" ), mUsesStaticStrings );
5096 return map;
5097}
5098
5100{
5102 mOptions = map.value( QStringLiteral( "options" ) ).toStringList();
5103 mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
5104 mUsesStaticStrings = map.value( QStringLiteral( "uses_static_strings" ) ).toBool();
5105 return true;
5106}
5107
5108QgsProcessingParameterEnum *QgsProcessingParameterEnum::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5109{
5110 QString defaultVal;
5111 QString def = definition;
5112
5113 bool multiple = false;
5114 if ( def.startsWith( QLatin1String( "multiple" ), Qt::CaseInsensitive ) )
5115 {
5116 multiple = true;
5117 def = def.mid( 9 );
5118 }
5119
5120 bool staticStrings = false;
5121 if ( def.startsWith( QLatin1String( "static" ), Qt::CaseInsensitive ) )
5122 {
5123 staticStrings = true;
5124 def = def.mid( 7 );
5125 }
5126
5127 const QRegularExpression re( QStringLiteral( "(.*)\\s+(.*?)$" ) );
5128 const QRegularExpressionMatch m = re.match( def );
5129 QString values = def;
5130 if ( m.hasMatch() )
5131 {
5132 values = m.captured( 1 ).trimmed();
5133 defaultVal = m.captured( 2 );
5134 }
5135
5136 return new QgsProcessingParameterEnum( name, description, values.split( ';' ), multiple, defaultVal.isEmpty() ? QVariant() : defaultVal, isOptional, staticStrings );
5137}
5138
5139QgsProcessingParameterString::QgsProcessingParameterString( const QString &name, const QString &description, const QVariant &defaultValue, bool multiLine, bool optional )
5140 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5141 , mMultiLine( multiLine )
5142{
5143
5144}
5145
5147{
5148 return new QgsProcessingParameterString( *this );
5149}
5150
5152{
5153 if ( QgsVariantUtils::isNull( value ) )
5154 return QStringLiteral( "None" );
5155
5156 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
5157 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
5158
5159 const QString s = value.toString();
5161}
5162
5164{
5165 QString code = QStringLiteral( "##%1=" ).arg( mName );
5166 if ( mFlags & FlagOptional )
5167 code += QLatin1String( "optional " );
5168 code += QLatin1String( "string " );
5169
5170 if ( mMultiLine )
5171 code += QLatin1String( "long " );
5172
5173 code += mDefault.toString();
5174 return code.trimmed();
5175}
5176
5178{
5179 switch ( outputType )
5180 {
5182 {
5183 QString code = QStringLiteral( "QgsProcessingParameterString('%1', %2" )
5185 if ( mFlags & FlagOptional )
5186 code += QLatin1String( ", optional=True" );
5187 code += QStringLiteral( ", multiLine=%1" ).arg( mMultiLine ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
5188
5190 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
5191 return code;
5192 }
5193 }
5194 return QString();
5195}
5196
5198{
5199 return mMultiLine;
5200}
5201
5203{
5204 mMultiLine = multiLine;
5205}
5206
5208{
5210 map.insert( QStringLiteral( "multiline" ), mMultiLine );
5211 return map;
5212}
5213
5215{
5217 mMultiLine = map.value( QStringLiteral( "multiline" ) ).toBool();
5218 return true;
5219}
5220
5221QgsProcessingParameterString *QgsProcessingParameterString::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5222{
5223 QString def = definition;
5224 bool multiLine = false;
5225 if ( def.startsWith( QLatin1String( "long" ), Qt::CaseInsensitive ) )
5226 {
5227 multiLine = true;
5228 def = def.mid( 5 );
5229 }
5230
5231 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
5232 def = def.mid( 1 );
5233 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
5234 def.chop( 1 );
5235
5236 QVariant defaultValue = def;
5237 if ( def == QLatin1String( "None" ) )
5238 defaultValue = QVariant();
5239
5241}
5242
5243//
5244// QgsProcessingParameterAuthConfig
5245//
5246
5247QgsProcessingParameterAuthConfig::QgsProcessingParameterAuthConfig( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
5248 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5249{
5250
5251}
5252
5254{
5255 return new QgsProcessingParameterAuthConfig( *this );
5256}
5257
5259{
5260 if ( !value.isValid() )
5261 return QStringLiteral( "None" );
5262
5263 const QString s = value.toString();
5265}
5266
5268{
5269 QString code = QStringLiteral( "##%1=" ).arg( mName );
5270 if ( mFlags & FlagOptional )
5271 code += QLatin1String( "optional " );
5272 code += QLatin1String( "authcfg " );
5273
5274 code += mDefault.toString();
5275 return code.trimmed();
5276}
5277
5278QgsProcessingParameterAuthConfig *QgsProcessingParameterAuthConfig::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5279{
5280 QString def = definition;
5281
5282 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
5283 def = def.mid( 1 );
5284 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
5285 def.chop( 1 );
5286
5287 QVariant defaultValue = def;
5288 if ( def == QLatin1String( "None" ) )
5289 defaultValue = QVariant();
5290
5292}
5293
5294
5295//
5296// QgsProcessingParameterExpression
5297//
5298
5299QgsProcessingParameterExpression::QgsProcessingParameterExpression( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional )
5300 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5301 , mParentLayerParameterName( parentLayerParameterName )
5302{
5303
5304}
5305
5307{
5308 return new QgsProcessingParameterExpression( *this );
5309}
5310
5312{
5313 if ( !value.isValid() )
5314 return QStringLiteral( "None" );
5315
5316 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
5317 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
5318
5319 const QString s = value.toString();
5321}
5322
5324{
5325 QStringList depends;
5326 if ( !mParentLayerParameterName.isEmpty() )
5327 depends << mParentLayerParameterName;
5328 return depends;
5329}
5330
5332{
5333 switch ( outputType )
5334 {
5336 {
5337 QString code = QStringLiteral( "QgsProcessingParameterExpression('%1', %2" )
5339 if ( mFlags & FlagOptional )
5340 code += QLatin1String( ", optional=True" );
5341
5342 code += QStringLiteral( ", parentLayerParameterName='%1'" ).arg( mParentLayerParameterName );
5343
5345 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
5346 return code;
5347 }
5348 }
5349 return QString();
5350}
5351
5353{
5354 return mParentLayerParameterName;
5355}
5356
5357void QgsProcessingParameterExpression::setParentLayerParameterName( const QString &parentLayerParameterName )
5358{
5359 mParentLayerParameterName = parentLayerParameterName;
5360}
5361
5363{
5365 map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
5366 return map;
5367}
5368
5370{
5372 mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
5373 return true;
5374}
5375
5376QgsProcessingParameterExpression *QgsProcessingParameterExpression::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5377{
5378 return new QgsProcessingParameterExpression( name, description, definition, QString(), isOptional );
5379}
5380
5381QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
5382 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5384{
5385
5386}
5387
5389{
5390 return new QgsProcessingParameterVectorLayer( *this );
5391}
5392
5394{
5395 if ( !v.isValid() )
5396 return mFlags & FlagOptional;
5397
5398 QVariant var = v;
5399
5400 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
5401 {
5402 const QgsProperty p = var.value< QgsProperty >();
5404 {
5405 var = p.staticValue();
5406 }
5407 else
5408 {
5409 return true;
5410 }
5411 }
5412
5413 if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( var ) ) )
5414 return true;
5415
5416 if ( var.type() != QVariant::String || var.toString().isEmpty() )
5417 return mFlags & FlagOptional;
5418
5419 if ( !context )
5420 {
5421 // that's as far as we can get without a context
5422 return true;
5423 }
5424
5425 // try to load as layer
5427 return true;
5428
5429 return false;
5430}
5431
5433{
5434 if ( !val.isValid() )
5435 return QStringLiteral( "None" );
5436
5437 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
5438 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
5439
5440 QVariantMap p;
5441 p.insert( name(), val );
5445}
5446
5447QString QgsProcessingParameterVectorLayer::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
5448{
5450}
5451
5453{
5455}
5456
5458{
5459 switch ( outputType )
5460 {
5462 {
5463 QString code = QStringLiteral( "QgsProcessingParameterVectorLayer('%1', %2" )
5465 if ( mFlags & FlagOptional )
5466 code += QLatin1String( ", optional=True" );
5467
5468 if ( !mDataTypes.empty() )
5469 {
5470 QStringList options;
5471 for ( const int t : mDataTypes )
5472 options << QStringLiteral( "QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( static_cast< QgsProcessing::SourceType >( t ) ) );
5473 code += QStringLiteral( ", types=[%1]" ).arg( options.join( ',' ) );
5474 }
5475
5477 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
5478 return code;
5479 }
5480 }
5481 return QString();
5482}
5483
5485{
5486 return QgsProviderRegistry::instance()->fileVectorFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
5487}
5488
5490{
5491 return mDataTypes;
5492}
5493
5495{
5496 mDataTypes = types;
5497}
5498
5500{
5502 QVariantList types;
5503 for ( const int type : mDataTypes )
5504 {
5505 types << type;
5506 }
5507 map.insert( QStringLiteral( "data_types" ), types );
5508 return map;
5509}
5510
5512{
5514 mDataTypes.clear();
5515 const QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
5516 for ( const QVariant &val : values )
5517 {
5518 mDataTypes << val.toInt();
5519 }
5520 return true;
5521}
5522
5523QgsProcessingParameterVectorLayer *QgsProcessingParameterVectorLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5524{
5525 return new QgsProcessingParameterVectorLayer( name, description, QList< int>(), definition.isEmpty() ? QVariant() : definition, isOptional );
5526}
5527
5528QgsProcessingParameterMeshLayer::QgsProcessingParameterMeshLayer( const QString &name, const QString &description,
5529 const QVariant &defaultValue, bool optional )
5530 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5531{
5532
5533}
5534
5536{
5537 return new QgsProcessingParameterMeshLayer( *this );
5538}
5539
5541{
5542 if ( !v.isValid() )
5543 return mFlags & FlagOptional;
5544
5545 QVariant var = v;
5546
5547 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
5548 {
5549 const QgsProperty p = var.value< QgsProperty >();
5551 {
5552 var = p.staticValue();
5553 }
5554 else
5555 {
5556 return true;
5557 }
5558 }
5559
5560 if ( qobject_cast< QgsMeshLayer * >( qvariant_cast<QObject *>( var ) ) )
5561 return true;
5562
5563 if ( var.type() != QVariant::String || var.toString().isEmpty() )
5564 return mFlags & FlagOptional;
5565
5566 if ( !context )
5567 {
5568 // that's as far as we can get without a context
5569 return true;
5570 }
5571
5572 // try to load as layer
5573 if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::LayerHint::Mesh ) )
5574 return true;
5575
5576 return false;
5577}
5578
5580{
5581 if ( !val.isValid() )
5582 return QStringLiteral( "None" );
5583
5584 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
5585 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
5586
5587 QVariantMap p;
5588 p.insert( name(), val );
5592}
5593
5594QString QgsProcessingParameterMeshLayer::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
5595{
5597}
5598
5599QVariant QgsProcessingParameterMeshLayer::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
5600{
5602}
5603
5605{
5606 return QgsProviderRegistry::instance()->fileMeshFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
5607}
5608
5609QgsProcessingParameterMeshLayer *QgsProcessingParameterMeshLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5610{
5611 return new QgsProcessingParameterMeshLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
5612}
5613
5614QgsProcessingParameterField::QgsProcessingParameterField( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, DataType type, bool allowMultiple, bool optional, bool defaultToAllFields )
5615 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5616 , mParentLayerParameterName( parentLayerParameterName )
5617 , mDataType( type )
5618 , mAllowMultiple( allowMultiple )
5619 , mDefaultToAllFields( defaultToAllFields )
5620{
5621
5622}
5623
5624
5626{
5627 return new QgsProcessingParameterField( *this );
5628}
5629
5631{
5632 if ( !input.isValid() )
5633 return mFlags & FlagOptional;
5634
5635 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
5636 {
5637 return true;
5638 }
5639
5640 if ( input.type() == QVariant::List || input.type() == QVariant::StringList )
5641 {
5642 if ( !mAllowMultiple )
5643 return false;
5644
5645 if ( input.toList().isEmpty() && !( mFlags & FlagOptional ) )
5646 return false;
5647 }
5648 else if ( input.type() == QVariant::String )
5649 {
5650 if ( input.toString().isEmpty() )
5651 return mFlags & FlagOptional;
5652
5653 const QStringList parts = input.toString().split( ';' );
5654 if ( parts.count() > 1 && !mAllowMultiple )
5655 return false;
5656 }
5657 else
5658 {
5659 if ( input.toString().isEmpty() )
5660 return mFlags & FlagOptional;
5661 }
5662 return true;
5663}
5664
5666{
5667 if ( !value.isValid() )
5668 return QStringLiteral( "None" );
5669
5670 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
5671 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
5672
5673 if ( value.type() == QVariant::List )
5674 {
5675 QStringList parts;
5676 const auto constToList = value.toList();
5677 for ( const QVariant &val : constToList )
5678 {
5679 parts << QgsProcessingUtils::stringToPythonLiteral( val.toString() );
5680 }
5681 return parts.join( ',' ).prepend( '[' ).append( ']' );
5682 }
5683 else if ( value.type() == QVariant::StringList )
5684 {
5685 QStringList parts;
5686 const auto constToStringList = value.toStringList();
5687 for ( const QString &s : constToStringList )
5688 {
5690 }
5691 return parts.join( ',' ).prepend( '[' ).append( ']' );
5692 }
5693
5694 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
5695}
5696
5698{
5699 QString code = QStringLiteral( "##%1=" ).arg( mName );
5700 if ( mFlags & FlagOptional )
5701 code += QLatin1String( "optional " );
5702 code += QLatin1String( "field " );
5703
5704 switch ( mDataType )
5705 {
5706 case Numeric:
5707 code += QLatin1String( "numeric " );
5708 break;
5709
5710 case String:
5711 code += QLatin1String( "string " );
5712 break;
5713
5714 case DateTime:
5715 code += QLatin1String( "datetime " );
5716 break;
5717
5718 case Any:
5719 break;
5720 }
5721
5722 if ( mAllowMultiple )
5723 code += QLatin1String( "multiple " );
5724
5725 if ( mDefaultToAllFields )
5726 code += QLatin1String( "default_to_all_fields " );
5727
5728 code += mParentLayerParameterName + ' ';
5729
5730 code += mDefault.toString();
5731 return code.trimmed();
5732}
5733
5735{
5736 switch ( outputType )
5737 {
5739 {
5740 QString code = QStringLiteral( "QgsProcessingParameterField('%1', %2" )
5742 if ( mFlags & FlagOptional )
5743 code += QLatin1String( ", optional=True" );
5744
5745 QString dataType;
5746 switch ( mDataType )
5747 {
5748 case Any:
5749 dataType = QStringLiteral( "QgsProcessingParameterField.Any" );
5750 break;
5751
5752 case Numeric:
5753 dataType = QStringLiteral( "QgsProcessingParameterField.Numeric" );
5754 break;
5755
5756 case String:
5757 dataType = QStringLiteral( "QgsProcessingParameterField.String" );
5758 break;
5759
5760 case DateTime:
5761 dataType = QStringLiteral( "QgsProcessingParameterField.DateTime" );
5762 break;
5763 }
5764 code += QStringLiteral( ", type=%1" ).arg( dataType );
5765
5766 code += QStringLiteral( ", parentLayerParameterName='%1'" ).arg( mParentLayerParameterName );
5767 code += QStringLiteral( ", allowMultiple=%1" ).arg( mAllowMultiple ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
5769 code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );
5770
5771 if ( mDefaultToAllFields )
5772 code += QLatin1String( ", defaultToAllFields=True" );
5773
5774 code += ')';
5775
5776 return code;
5777 }
5778 }
5779 return QString();
5780}
5781
5783{
5784 QStringList depends;
5785 if ( !mParentLayerParameterName.isEmpty() )
5786 depends << mParentLayerParameterName;
5787 return depends;
5788}
5789
5791{
5792 return mParentLayerParameterName;
5793}
5794
5795void QgsProcessingParameterField::setParentLayerParameterName( const QString &parentLayerParameterName )
5796{
5797 mParentLayerParameterName = parentLayerParameterName;
5798}
5799
5801{
5802 return mDataType;
5803}
5804
5806{
5807 mDataType = dataType;
5808}
5809
5811{
5812 return mAllowMultiple;
5813}
5814
5816{
5817 mAllowMultiple = allowMultiple;
5818}
5819
5821{
5822 return mDefaultToAllFields;
5823}
5824
5826{
5827 mDefaultToAllFields = enabled;
5828}
5829
5831{
5833 map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
5834 map.insert( QStringLiteral( "data_type" ), mDataType );
5835 map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
5836 map.insert( QStringLiteral( "default_to_all_fields" ), mDefaultToAllFields );
5837 return map;
5838}
5839
5841{
5843 mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
5844 mDataType = static_cast< DataType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
5845 mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
5846 mDefaultToAllFields = map.value( QStringLiteral( "default_to_all_fields" ) ).toBool();
5847 return true;
5848}
5849
5850QgsProcessingParameterField *QgsProcessingParameterField::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5851{
5852 QString parent;
5853 DataType type = Any;
5854 bool allowMultiple = false;
5855 bool defaultToAllFields = false;
5856 QString def = definition;
5857
5858 if ( def.startsWith( QLatin1String( "numeric " ), Qt::CaseInsensitive ) )
5859 {
5860 type = Numeric;
5861 def = def.mid( 8 );
5862 }
5863 else if ( def.startsWith( QLatin1String( "string " ), Qt::CaseInsensitive ) )
5864 {
5865 type = String;
5866 def = def.mid( 7 );
5867 }
5868 else if ( def.startsWith( QLatin1String( "datetime " ), Qt::CaseInsensitive ) )
5869 {
5870 type = DateTime;
5871 def = def.mid( 9 );
5872 }
5873
5874 if ( def.startsWith( QLatin1String( "multiple" ), Qt::CaseInsensitive ) )
5875 {
5876 allowMultiple = true;
5877 def = def.mid( 8 ).trimmed();
5878 }
5879
5880 if ( def.startsWith( QLatin1String( "default_to_all_fields" ), Qt::CaseInsensitive ) )
5881 {
5882 defaultToAllFields = true;
5883 def = def.mid( 21 ).trimmed();
5884 }
5885
5886 const QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
5887 const QRegularExpressionMatch m = re.match( def );
5888 if ( m.hasMatch() )
5889 {
5890 parent = m.captured( 1 ).trimmed();
5891 def = m.captured( 2 );
5892 }
5893 else
5894 {
5895 parent = def;
5896 def.clear();
5897 }
5898
5899 return new QgsProcessingParameterField( name, description, def.isEmpty() ? QVariant() : def, parent, type, allowMultiple, isOptional, defaultToAllFields );
5900}
5901
5902QgsProcessingParameterFeatureSource::QgsProcessingParameterFeatureSource( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
5903 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5905{
5906
5907}
5908
5910{
5911 return new QgsProcessingParameterFeatureSource( *this );
5912}
5913
5915{
5916 QVariant var = input;
5917 if ( !var.isValid() )
5918 return mFlags & FlagOptional;
5919
5920 if ( var.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
5921 {
5922 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( var );
5923 var = fromVar.source;
5924 }
5925 else if ( var.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
5926 {
5927 // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
5928 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
5929 var = fromVar.sink;
5930 }
5931
5932 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
5933 {
5934 const QgsProperty p = var.value< QgsProperty >();
5936 {
5937 var = p.staticValue();
5938 }
5939 else
5940 {
5941 return true;
5942 }
5943 }
5944 if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( input ) ) )
5945 {
5946 return true;
5947 }
5948
5949 if ( var.type() != QVariant::String || var.toString().isEmpty() )
5950 return mFlags & FlagOptional;
5951
5952 if ( !context )
5953 {
5954 // that's as far as we can get without a context
5955 return true;
5956 }
5957
5958 // try to load as layer
5960 return true;
5961
5962 return false;
5963}
5964
5966{
5967 if ( !value.isValid() )
5968 return QStringLiteral( "None" );
5969
5970 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
5971 return QStringLiteral( "QgsProperty.fromExpression(%1)" ).arg( QgsProcessingUtils::stringToPythonLiteral( value.value< QgsProperty >().asExpression() ) );
5972
5973 if ( value.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
5974 {
5975 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( value );
5976 QString geometryCheckString;
5977 switch ( fromVar.geometryCheck )
5978 {
5980 geometryCheckString = QStringLiteral( "QgsFeatureRequest.GeometryNoCheck" );
5981 break;
5982
5984 geometryCheckString = QStringLiteral( "QgsFeatureRequest.GeometrySkipInvalid" );
5985 break;
5986
5988 geometryCheckString = QStringLiteral( "QgsFeatureRequest.GeometryAbortOnInvalid" );
5989 break;
5990 }
5991
5992 QStringList flags;
5993 QString flagString;
5994 if ( fromVar.flags & QgsProcessingFeatureSourceDefinition::Flag::FlagOverrideDefaultGeometryCheck )
5995 flags << QStringLiteral( "QgsProcessingFeatureSourceDefinition.FlagOverrideDefaultGeometryCheck" );
5996 if ( fromVar.flags & QgsProcessingFeatureSourceDefinition::Flag::FlagCreateIndividualOutputPerInputFeature )
5997 flags << QStringLiteral( "QgsProcessingFeatureSourceDefinition.FlagCreateIndividualOutputPerInputFeature" );
5998 if ( !flags.empty() )
5999 flagString = flags.join( QLatin1String( " | " ) );
6000
6002 {
6003 QString layerString = fromVar.source.staticValue().toString();
6004 // prefer to use layer source instead of id if possible (since it's persistent)
6005 if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context, true, QgsProcessingUtils::LayerHint::Vector ) ) )
6006 layerString = layer->source();
6007
6008 if ( fromVar.selectedFeaturesOnly || fromVar.featureLimit != -1 || fromVar.flags )
6009 {
6010 return QStringLiteral( "QgsProcessingFeatureSourceDefinition(%1, selectedFeaturesOnly=%2, featureLimit=%3%4, geometryCheck=%5)" ).arg( QgsProcessingUtils::stringToPythonLiteral( layerString ),
6011 fromVar.selectedFeaturesOnly ? QStringLiteral( "True" ) : QStringLiteral( "False" ),
6012 QString::number( fromVar.featureLimit ),
6013 flagString.isEmpty() ? QString() : ( QStringLiteral( ", flags=%1" ).arg( flagString ) ),
6014 geometryCheckString );
6015 }
6016 else
6017 {
6018 return QgsProcessingUtils::stringToPythonLiteral( layerString );
6019 }
6020 }
6021 else
6022 {
6023 if ( fromVar.selectedFeaturesOnly || fromVar.featureLimit != -1 || fromVar.flags )
6024 {
6025 return QStringLiteral( "QgsProcessingFeatureSourceDefinition(QgsProperty.fromExpression(%1), selectedFeaturesOnly=%2, featureLimit=%3%4, geometryCheck=%5)" )
6027 fromVar.selectedFeaturesOnly ? QStringLiteral( "True" ) : QStringLiteral( "False" ),
6028 QString::number( fromVar.featureLimit ),
6029 flagString.isEmpty() ? QString() : ( QStringLiteral( ", flags=%1" ).arg( flagString ) ),
6030 geometryCheckString );
6031 }
6032 else
6033 {
6034 return QStringLiteral( "QgsProperty.fromExpression(%1)" ).arg( QgsProcessingUtils::stringToPythonLiteral( fromVar.source.asExpression() ) );
6035 }
6036 }
6037 }
6038 else if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( value ) ) )
6039 {
6040 return QgsProcessingUtils::stringToPythonLiteral( layer->source() );
6041 }
6042
6043 QString layerString = value.toString();
6044
6045 // prefer to use layer source if possible (since it's persistent)
6046 if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context, true, QgsProcessingUtils::LayerHint::Vector ) ) )
6047 layerString = layer->providerType() != QLatin1String( "ogr" ) && layer->providerType() != QLatin1String( "gdal" ) && layer->providerType() != QLatin1String( "mdal" ) ? QgsProcessingUtils::encodeProviderKeyAndUri( layer->providerType(), layer->source() ) : layer->source();
6048
6049 return QgsProcessingUtils::stringToPythonLiteral( layerString );
6050}
6051
6052QString QgsProcessingParameterFeatureSource::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
6053{
6055}
6056
6058{
6060}
6061
6063{
6064 QString code = QStringLiteral( "##%1=" ).arg( mName );
6065 if ( mFlags & FlagOptional )
6066 code += QLatin1String( "optional " );
6067 code += QLatin1String( "source " );
6068
6069 for ( const int type : mDataTypes )
6070 {
6071 switch ( type )
6072 {
6074 code += QLatin1String( "point " );
6075 break;
6076
6078 code += QLatin1String( "line " );
6079 break;
6080
6082 code += QLatin1String( "polygon " );
6083 break;
6084
6085 }
6086 }
6087
6088 code += mDefault.toString();
6089 return code.trimmed();
6090}
6091
6093{
6094 switch ( outputType )
6095 {
6097 {
6098 QString code = QStringLiteral( "QgsProcessingParameterFeatureSource('%1', %2" )
6100 if ( mFlags & FlagOptional )
6101 code += QLatin1String( ", optional=True" );
6102
6103 if ( !mDataTypes.empty() )
6104 {
6105 QStringList options;
6106 options.reserve( mDataTypes.size() );
6107 for ( const int t : mDataTypes )
6108 options << QStringLiteral( "QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( static_cast< QgsProcessing::SourceType >( t ) ) );
6109 code += QStringLiteral( ", types=[%1]" ).arg( options.join( ',' ) );
6110 }
6111
6113 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6114 return code;
6115 }
6116 }
6117 return QString();
6118}
6119
6121{
6122 return QgsProviderRegistry::instance()->fileVectorFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
6123}
6124
6126 : mDataTypes( types )
6127{
6128
6129}
6130
6132{
6134 QVariantList types;
6135 for ( const int type : mDataTypes )
6136 {
6137 types << type;
6138 }
6139 map.insert( QStringLiteral( "data_types" ), types );
6140 return map;
6141}
6142
6144{
6146 mDataTypes.clear();
6147 const QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
6148 for ( const QVariant &val : values )
6149 {
6150 mDataTypes << val.toInt();
6151 }
6152 return true;
6153}
6154
6155QgsProcessingParameterFeatureSource *QgsProcessingParameterFeatureSource::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6156{
6157 QList< int > types;
6158 QString def = definition;
6159 while ( true )
6160 {
6161 if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
6162 {
6164 def = def.mid( 6 );
6165 continue;
6166 }
6167 else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
6168 {
6170 def = def.mid( 5 );
6171 continue;
6172 }
6173 else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
6174 {
6176 def = def.mid( 8 );
6177 continue;
6178 }
6179 break;
6180 }
6181
6182 return new QgsProcessingParameterFeatureSource( name, description, types, def, isOptional );
6183}
6184
6185QgsProcessingParameterFeatureSink::QgsProcessingParameterFeatureSink( const QString &name, const QString &description, QgsProcessing::SourceType type, const QVariant &defaultValue, bool optional, bool createByDefault, bool supportsAppend )
6186 : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
6187 , mDataType( type )
6188 , mSupportsAppend( supportsAppend )
6189{
6190}
6191
6193{
6194 return new QgsProcessingParameterFeatureSink( *this );
6195}
6196
6198{
6199 QVariant var = input;
6200 if ( !var.isValid() )
6201 return mFlags & FlagOptional;
6202
6203 if ( var.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6204 {
6205 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
6206 var = fromVar.sink;
6207 }
6208
6209 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
6210 {
6211 const QgsProperty p = var.value< QgsProperty >();
6213 {
6214 var = p.staticValue();
6215 }
6216 else
6217 {
6218 return true;
6219 }
6220 }
6221
6222 if ( var.type() != QVariant::String )
6223 return false;
6224
6225 if ( var.toString().isEmpty() )
6226 return mFlags & FlagOptional;
6227
6228 return true;
6229}
6230
6232{
6233 if ( !value.isValid() )
6234 return QStringLiteral( "None" );
6235
6236 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
6237 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
6238
6239 if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6240 {
6241 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
6243 {
6244 return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
6245 }
6246 else
6247 {
6248 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
6249 }
6250 }
6251
6252 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
6253}
6254
6256{
6257 QString code = QStringLiteral( "##%1=" ).arg( mName );
6258 if ( mFlags & FlagOptional )
6259 code += QLatin1String( "optional " );
6260 code += QLatin1String( "sink " );
6261
6262 switch ( mDataType )
6263 {
6265 code += QLatin1String( "point " );
6266 break;
6267
6269 code += QLatin1String( "line " );
6270 break;
6271
6273 code += QLatin1String( "polygon " );
6274 break;
6275
6277 code += QLatin1String( "table " );
6278 break;
6279
6280 default:
6281 break;
6282 }
6283
6284 code += mDefault.toString();
6285 return code.trimmed();
6286}
6287
6289{
6290 return new QgsProcessingOutputVectorLayer( name(), description(), mDataType );
6291}
6292
6294{
6295 if ( auto *lOriginalProvider = originalProvider() )
6296 {
6297 return lOriginalProvider->defaultVectorFileExtension( hasGeometry() );
6298 }
6299 else if ( QgsProcessingProvider *p = provider() )
6300 {
6301 return p->defaultVectorFileExtension( hasGeometry() );
6302 }
6303 else
6304 {
6305 if ( hasGeometry() )
6306 {
6308 }
6309 else
6310 {
6311 return QStringLiteral( "dbf" );
6312 }
6313 }
6314}
6315
6317{
6318 switch ( outputType )
6319 {
6321 {
6322 QString code = QStringLiteral( "QgsProcessingParameterFeatureSink('%1', %2" )
6324 if ( mFlags & FlagOptional )
6325 code += QLatin1String( ", optional=True" );
6326
6327 code += QStringLiteral( ", type=QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( mDataType ) );
6328
6329 code += QStringLiteral( ", createByDefault=%1" ).arg( createByDefault() ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
6330 if ( mSupportsAppend )
6331 code += QLatin1String( ", supportsAppend=True" );
6332
6334 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6335 return code;
6336 }
6337 }
6338 return QString();
6339}
6340
6342{
6343 const QStringList exts = supportedOutputVectorLayerExtensions();
6344 QStringList filters;
6345 for ( const QString &ext : exts )
6346 {
6347 filters << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
6348 }
6349 return filters.join( QLatin1String( ";;" ) ) + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
6350
6351}
6352
6354{
6355 if ( auto *lOriginalProvider = originalProvider() )
6356 {
6357 if ( hasGeometry() )
6358 return lOriginalProvider->supportedOutputVectorLayerExtensions();
6359 else
6360 return lOriginalProvider->supportedOutputTableExtensions();
6361 }
6362 else if ( QgsProcessingProvider *p = provider() )
6363 {
6364 if ( hasGeometry() )
6365 return p->supportedOutputVectorLayerExtensions();
6366 else
6367 return p->supportedOutputTableExtensions();
6368 }
6369 else
6370 {
6372 }
6373}
6374
6376{
6377 return mDataType;
6378}
6379
6381{
6382 switch ( mDataType )
6383 {
6389 return true;
6390
6398 return false;
6399 }
6400 return true;
6401}
6402
6404{
6405 mDataType = type;
6406}
6407
6409{
6411 map.insert( QStringLiteral( "data_type" ), mDataType );
6412 map.insert( QStringLiteral( "supports_append" ), mSupportsAppend );
6413 return map;
6414}
6415
6417{
6419 mDataType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
6420 mSupportsAppend = map.value( QStringLiteral( "supports_append" ), false ).toBool();
6421 return true;
6422}
6423
6425{
6427 return QStringLiteral( "memory:%1" ).arg( description() );
6428 else
6430}
6431
6432QgsProcessingParameterFeatureSink *QgsProcessingParameterFeatureSink::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6433{
6435 QString def = definition;
6436 if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
6437 {
6439 def = def.mid( 6 );
6440 }
6441 else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
6442 {
6444 def = def.mid( 5 );
6445 }
6446 else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
6447 {
6449 def = def.mid( 8 );
6450 }
6451 else if ( def.startsWith( QLatin1String( "table" ), Qt::CaseInsensitive ) )
6452 {
6454 def = def.mid( 6 );
6455 }
6456
6457 return new QgsProcessingParameterFeatureSink( name, description, type, definition, isOptional );
6458}
6459
6461{
6462 return mSupportsAppend;
6463}
6464
6466{
6467 mSupportsAppend = supportsAppend;
6468}
6469
6470QgsProcessingParameterRasterDestination::QgsProcessingParameterRasterDestination( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, bool createByDefault )
6471 : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
6472{
6473}
6474
6476{
6477 return new QgsProcessingParameterRasterDestination( *this );
6478}
6479
6481{
6482 QVariant var = input;
6483 if ( !var.isValid() )
6484 return mFlags & FlagOptional;
6485
6486 if ( var.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6487 {
6488 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
6489 var = fromVar.sink;
6490 }
6491
6492 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
6493 {
6494 const QgsProperty p = var.value< QgsProperty >();
6496 {
6497 var = p.staticValue();
6498 }
6499 else
6500 {
6501 return true;
6502 }
6503 }
6504
6505 if ( var.type() != QVariant::String )
6506 return false;
6507
6508 if ( var.toString().isEmpty() )
6509 return mFlags & FlagOptional;
6510
6511 return true;
6512}
6513
6515{
6516 if ( !value.isValid() )
6517 return QStringLiteral( "None" );
6518
6519 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
6520 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
6521
6522 if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6523 {
6524 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
6526 {
6527 return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
6528 }
6529 else
6530 {
6531 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
6532 }
6533 }
6534
6535 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
6536}
6537
6539{
6541}
6542
6544{
6545 if ( auto *lOriginalProvider = originalProvider() )
6546 {
6547 return lOriginalProvider->defaultRasterFileExtension();
6548 }
6549 else if ( QgsProcessingProvider *p = provider() )
6550 {
6551 return p->defaultRasterFileExtension();
6552 }
6553 else
6554 {
6556 }
6557}
6558
6560{
6561 const QStringList exts = supportedOutputRasterLayerExtensions();
6562 QStringList filters;
6563 for ( const QString &ext : exts )
6564 {
6565 filters << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
6566 }
6567 return filters.join( QLatin1String( ";;" ) ) + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
6568}
6569
6571{
6572 if ( auto *lOriginalProvider = originalProvider() )
6573 {
6574 return lOriginalProvider->supportedOutputRasterLayerExtensions();
6575 }
6576 else if ( QgsProcessingProvider *p = provider() )
6577 {
6578 return p->supportedOutputRasterLayerExtensions();
6579 }
6580 else
6581 {
6583 }
6584}
6585
6586QgsProcessingParameterRasterDestination *QgsProcessingParameterRasterDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6587{
6588 return new QgsProcessingParameterRasterDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
6589}
6590
6591
6592QgsProcessingParameterFileDestination::QgsProcessingParameterFileDestination( const QString &name, const QString &description, const QString &fileFilter, const QVariant &defaultValue, bool optional, bool createByDefault )
6593 : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
6594 , mFileFilter( fileFilter.isEmpty() ? QObject::tr( "All files (*.*)" ) : fileFilter )
6595{
6596
6597}
6598
6600{
6601 return new QgsProcessingParameterFileDestination( *this );
6602}
6603
6605{
6606 QVariant var = input;
6607 if ( !var.isValid() )
6608 return mFlags & FlagOptional;
6609
6610 if ( var.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6611 {
6612 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
6613 var = fromVar.sink;
6614 }
6615
6616 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
6617 {
6618 const QgsProperty p = var.value< QgsProperty >();
6620 {
6621 var = p.staticValue();
6622 }
6623 else
6624 {
6625 return true;
6626 }
6627 }
6628
6629 if ( var.type() != QVariant::String )
6630 return false;
6631
6632 if ( var.toString().isEmpty() )
6633 return mFlags & FlagOptional;
6634
6635 // possible enhancement - check that value is compatible with file filter?
6636
6637 return true;
6638}
6639
6641{
6642 if ( !value.isValid() )
6643 return QStringLiteral( "None" );
6644
6645 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
6646 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
6647
6648 if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6649 {
6650 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
6652 {
6653 return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
6654 }
6655 else
6656 {
6657 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
6658 }
6659 }
6660
6661 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
6662}
6663
6665{
6666 if ( !mFileFilter.isEmpty() && mFileFilter.contains( QStringLiteral( "htm" ), Qt::CaseInsensitive ) )
6667 {
6668 return new QgsProcessingOutputHtml( name(), description() );
6669 }
6670 else
6671 {
6672 return new QgsProcessingOutputFile( name(), description() );
6673 }
6674}
6675
6677{
6678 if ( mFileFilter.isEmpty() || mFileFilter == QObject::tr( "All files (*.*)" ) )
6679 return QStringLiteral( "file" );
6680
6681 // get first extension from filter
6682 const QRegularExpression rx( QStringLiteral( ".*?\\(\\*\\.([a-zA-Z0-9._]+).*" ) );
6683 const QRegularExpressionMatch match = rx.match( mFileFilter );
6684 if ( !match.hasMatch() )
6685 return QStringLiteral( "file" );
6686
6687 return match.captured( 1 );
6688}
6689
6691{
6692 switch ( outputType )
6693 {
6695 {
6696 QString code = QStringLiteral( "QgsProcessingParameterFileDestination('%1', %2" )
6698 if ( mFlags & FlagOptional )
6699 code += QLatin1String( ", optional=True" );
6700
6701 code += QStringLiteral( ", fileFilter=%1" ).arg( QgsProcessingUtils::stringToPythonLiteral( mFileFilter ) );
6702
6703 code += QStringLiteral( ", createByDefault=%1" ).arg( createByDefault() ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
6704
6706 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6707 return code;
6708 }
6709 }
6710 return QString();
6711}
6712
6714{
6715 return ( fileFilter().isEmpty() ? QString() : fileFilter() + QStringLiteral( ";;" ) ) + QObject::tr( "All files (*.*)" );
6716}
6717
6719{
6720 return mFileFilter;
6721}
6722
6724{
6725 mFileFilter = fileFilter;
6726}
6727
6729{
6731 map.insert( QStringLiteral( "file_filter" ), mFileFilter );
6732 return map;
6733}
6734
6736{
6738 mFileFilter = map.value( QStringLiteral( "file_filter" ) ).toString();
6739 return true;
6740
6741}
6742
6743QgsProcessingParameterFileDestination *QgsProcessingParameterFileDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6744{
6745 return new QgsProcessingParameterFileDestination( name, description, QString(), definition.isEmpty() ? QVariant() : definition, isOptional );
6746}
6747
6748QgsProcessingParameterFolderDestination::QgsProcessingParameterFolderDestination( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, bool createByDefault )
6749 : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
6750{}
6751
6753{
6754 return new QgsProcessingParameterFolderDestination( *this );
6755}
6756
6758{
6759 QVariant var = input;
6760 if ( !var.isValid() )
6761 return mFlags & FlagOptional;
6762
6763 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
6764 {
6765 const QgsProperty p = var.value< QgsProperty >();
6767 {
6768 var = p.staticValue();
6769 }
6770 else
6771 {
6772 return true;
6773 }
6774 }
6775
6776 if ( var.type() != QVariant::String )
6777 return false;
6778
6779 if ( var.toString().isEmpty() )
6780 return mFlags & FlagOptional;
6781
6782 return true;
6783}
6784
6786{
6787 return new QgsProcessingOutputFolder( name(), description() );
6788}
6789
6791{
6792 return QString();
6793}
6794
6795QgsProcessingParameterFolderDestination *QgsProcessingParameterFolderDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6796{
6797 return new QgsProcessingParameterFolderDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
6798}
6799
6800QgsProcessingDestinationParameter::QgsProcessingDestinationParameter( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, bool createByDefault )
6801 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
6802 , mCreateByDefault( createByDefault )
6803{
6804
6805}
6806
6808{
6810 map.insert( QStringLiteral( "supports_non_file_outputs" ), mSupportsNonFileBasedOutputs );
6811 map.insert( QStringLiteral( "create_by_default" ), mCreateByDefault );
6812 return map;
6813}
6814
6816{
6818 mSupportsNonFileBasedOutputs = map.value( QStringLiteral( "supports_non_file_outputs" ) ).toBool();
6819 mCreateByDefault = map.value( QStringLiteral( "create_by_default" ), QStringLiteral( "1" ) ).toBool();
6820 return true;
6821}
6822
6824{
6825 switch ( outputType )
6826 {
6828 {
6829 // base class method is probably not much use
6831 {
6832 QString code = t->className() + QStringLiteral( "('%1', %2" )
6834 if ( mFlags & FlagOptional )
6835 code += QLatin1String( ", optional=True" );
6836
6837 code += QStringLiteral( ", createByDefault=%1" ).arg( mCreateByDefault ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
6838
6840 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6841 return code;
6842 }
6843 break;
6844 }
6845 }
6846 // oh well, we tried
6847 return QString();
6848}
6849
6851{
6852 return QObject::tr( "Default extension" ) + QStringLiteral( " (*." ) + defaultFileExtension() + ')';
6853}
6854
6856{
6857 // sanitize name to avoid multiple . in the filename. E.g. when name() contain
6858 // backend command name having a "." inside as in case of grass commands
6859 const QRegularExpression rx( QStringLiteral( "[.]" ) );
6860 QString sanitizedName = name();
6861 sanitizedName.replace( rx, QStringLiteral( "_" ) );
6862
6863 if ( defaultFileExtension().isEmpty() )
6864 {
6865 return QgsProcessingUtils::generateTempFilename( sanitizedName );
6866 }
6867 else
6868 {
6869 return QgsProcessingUtils::generateTempFilename( sanitizedName + '.' + defaultFileExtension() );
6870 }
6871}
6872
6873bool QgsProcessingDestinationParameter::isSupportedOutputValue( const QVariant &value, QgsProcessingContext &context, QString &error ) const
6874{
6875 if ( auto *lOriginalProvider = originalProvider() )
6876 return lOriginalProvider->isSupportedOutputValue( value, this, context, error );
6877 else if ( provider() )
6878 return provider()->isSupportedOutputValue( value, this, context, error );
6879
6880 return true;
6881}
6882
6884{
6885 return mCreateByDefault;
6886}
6887
6889{
6890 mCreateByDefault = createByDefault;
6891}
6892
6893QgsProcessingParameterVectorDestination::QgsProcessingParameterVectorDestination( const QString &name, const QString &description, QgsProcessing::SourceType type, const QVariant &defaultValue, bool optional, bool createByDefault )
6894 : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
6895 , mDataType( type )
6896{
6897
6898}
6899
6901{
6902 return new QgsProcessingParameterVectorDestination( *this );
6903}
6904
6906{
6907 QVariant var = input;
6908 if ( !var.isValid() )
6909 return mFlags & FlagOptional;
6910
6911 if ( var.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6912 {
6913 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
6914 var = fromVar.sink;
6915 }
6916
6917 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
6918 {
6919 const QgsProperty p = var.value< QgsProperty >();
6921 {
6922 var = p.staticValue();
6923 }
6924 else
6925 {
6926 return true;
6927 }
6928 }
6929
6930 if ( var.type() != QVariant::String )
6931 return false;
6932
6933 if ( var.toString().isEmpty() )
6934 return mFlags & FlagOptional;
6935
6936 return true;
6937}
6938
6940{
6941 if ( !value.isValid() )
6942 return QStringLiteral( "None" );
6943
6944 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
6945 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
6946
6947 if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6948 {
6949 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
6951 {
6952 return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
6953 }
6954 else
6955 {
6956 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
6957 }
6958 }
6959
6960 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
6961}
6962
6964{
6965 QString code = QStringLiteral( "##%1=" ).arg( mName );
6966 if ( mFlags & FlagOptional )
6967 code += QLatin1String( "optional " );
6968 code += QLatin1String( "vectorDestination " );
6969
6970 switch ( mDataType )
6971 {
6973 code += QLatin1String( "point " );
6974 break;
6975
6977 code += QLatin1String( "line " );
6978 break;
6979
6981 code += QLatin1String( "polygon " );
6982 break;
6983
6984 default:
6985 break;
6986 }
6987
6988 code += mDefault.toString();
6989 return code.trimmed();
6990}
6991
6993{
6994 return new QgsProcessingOutputVectorLayer( name(), description(), mDataType );
6995}
6996
6998{
6999 if ( auto *lOriginalProvider = originalProvider() )
7000 {
7001 return lOriginalProvider->defaultVectorFileExtension( hasGeometry() );
7002 }
7003 else if ( QgsProcessingProvider *p = provider() )
7004 {
7005 return p->defaultVectorFileExtension( hasGeometry() );
7006 }
7007 else
7008 {
7009 if ( hasGeometry() )
7010 {
7012 }
7013 else
7014 {
7015 return QStringLiteral( "dbf" );
7016 }
7017 }
7018}
7019
7021{
7022 switch ( outputType )
7023 {
7025 {
7026 QString code = QStringLiteral( "QgsProcessingParameterVectorDestination('%1', %2" )
7028 if ( mFlags & FlagOptional )
7029 code += QLatin1String( ", optional=True" );
7030
7031 code += QStringLiteral( ", type=QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( mDataType ) );
7032
7033 code += QStringLiteral( ", createByDefault=%1" ).arg( createByDefault() ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
7034
7036 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7037 return code;
7038 }
7039 }
7040 return QString();
7041}
7042
7044{
7045 const QStringList exts = supportedOutputVectorLayerExtensions();
7046 QStringList filters;
7047 for ( const QString &ext : exts )
7048 {
7049 filters << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
7050 }
7051 return filters.join( QLatin1String( ";;" ) ) + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
7052}
7053
7055{
7056 if ( auto *lOriginalProvider = originalProvider() )
7057 {
7058 if ( hasGeometry() )
7059 return lOriginalProvider->supportedOutputVectorLayerExtensions();
7060 else
7061 return lOriginalProvider->supportedOutputTableExtensions();
7062 }
7063 else if ( QgsProcessingProvider *p = provider() )
7064 {
7065 if ( hasGeometry() )
7066 return p->supportedOutputVectorLayerExtensions();
7067 else
7068 return p->supportedOutputTableExtensions();
7069 }
7070 else
7071 {
7073 }
7074}
7075
7077{
7078 return mDataType;
7079}
7080
7082{
7083 switch ( mDataType )
7084 {
7090 return true;
7091
7099 return false;
7100 }
7101 return true;
7102}
7103
7105{
7106 mDataType = type;
7107}
7108
7110{
7112 map.insert( QStringLiteral( "data_type" ), mDataType );
7113 return map;
7114}
7115
7117{
7119 mDataType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
7120 return true;
7121}
7122
7123QgsProcessingParameterVectorDestination *QgsProcessingParameterVectorDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7124{
7126 QString def = definition;
7127 if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
7128 {
7130 def = def.mid( 6 );
7131 }
7132 else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
7133 {
7135 def = def.mid( 5 );
7136 }
7137 else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
7138 {
7140 def = def.mid( 8 );
7141 }
7142
7143 return new QgsProcessingParameterVectorDestination( name, description, type, definition, isOptional );
7144}
7145
7146QgsProcessingParameterBand::QgsProcessingParameterBand( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional, bool allowMultiple )
7147 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7148 , mParentLayerParameterName( parentLayerParameterName )
7149 , mAllowMultiple( allowMultiple )
7150{
7151
7152}
7153
7155{
7156 return new QgsProcessingParameterBand( *this );
7157}
7158
7160{
7161 if ( !input.isValid() )
7162 return mFlags & FlagOptional;
7163
7164 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
7165 {
7166 return true;
7167 }
7168
7169 if ( input.type() == QVariant::List || input.type() == QVariant::StringList )
7170 {
7171 if ( !mAllowMultiple )
7172 return false;
7173
7174 if ( input.toList().isEmpty() && !( mFlags & FlagOptional ) )
7175 return false;
7176 }
7177 else
7178 {
7179 bool ok = false;
7180 const double res = input.toInt( &ok );
7181 Q_UNUSED( res )
7182 if ( !ok )
7183 return mFlags & FlagOptional;
7184 }
7185 return true;
7186}
7187
7189{
7190 return mAllowMultiple;
7191}
7192
7194{
7195 mAllowMultiple = allowMultiple;
7196}
7197
7199{
7200 if ( !value.isValid() )
7201 return QStringLiteral( "None" );
7202
7203 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
7204 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7205
7206 if ( value.type() == QVariant::List )
7207 {
7208 QStringList parts;
7209 const QVariantList values = value.toList();
7210 for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
7211 {
7212 parts << QString::number( static_cast< int >( it->toDouble() ) );
7213 }
7214 return parts.join( ',' ).prepend( '[' ).append( ']' );
7215 }
7216 else if ( value.type() == QVariant::StringList )
7217 {
7218 QStringList parts;
7219 const QStringList values = value.toStringList();
7220 for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
7221 {
7222 parts << QString::number( static_cast< int >( it->toDouble() ) );
7223 }
7224 return parts.join( ',' ).prepend( '[' ).append( ']' );
7225 }
7226
7227 return value.toString();
7228}
7229
7231{
7232 QString code = QStringLiteral( "##%1=" ).arg( mName );
7233 if ( mFlags & FlagOptional )
7234 code += QLatin1String( "optional " );
7235 code += QLatin1String( "band " );
7236
7237 if ( mAllowMultiple )
7238 code += QLatin1String( "multiple " );
7239
7240 code += mParentLayerParameterName + ' ';
7241
7242 code += mDefault.toString();
7243 return code.trimmed();
7244}
7245
7247{
7248 QStringList depends;
7249 if ( !mParentLayerParameterName.isEmpty() )
7250 depends << mParentLayerParameterName;
7251 return depends;
7252}
7253
7255{
7256 switch ( outputType )
7257 {
7259 {
7260 QString code = QStringLiteral( "QgsProcessingParameterBand('%1', %2" )
7262 if ( mFlags & FlagOptional )
7263 code += QLatin1String( ", optional=True" );
7264
7265 code += QStringLiteral( ", parentLayerParameterName='%1'" ).arg( mParentLayerParameterName );
7266 code += QStringLiteral( ", allowMultiple=%1" ).arg( mAllowMultiple ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
7267
7269 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7270 return code;
7271 }
7272 }
7273 return QString();
7274}
7275
7277{
7278 return mParentLayerParameterName;
7279}
7280
7281void QgsProcessingParameterBand::setParentLayerParameterName( const QString &parentLayerParameterName )
7282{
7283 mParentLayerParameterName = parentLayerParameterName;
7284}
7285
7287{
7289 map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
7290 map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
7291 return map;
7292}
7293
7295{
7297 mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
7298 mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
7299 return true;
7300}
7301
7302QgsProcessingParameterBand *QgsProcessingParameterBand::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7303{
7304 QString parent;
7305 QString def = definition;
7306 bool allowMultiple = false;
7307
7308 if ( def.startsWith( QLatin1String( "multiple" ), Qt::CaseInsensitive ) )
7309 {
7310 allowMultiple = true;
7311 def = def.mid( 8 ).trimmed();
7312 }
7313
7314 const QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
7315 const QRegularExpressionMatch m = re.match( def );
7316 if ( m.hasMatch() )
7317 {
7318 parent = m.captured( 1 ).trimmed();
7319 def = m.captured( 2 );
7320 }
7321 else
7322 {
7323 parent = def;
7324 def.clear();
7325 }
7326
7327 return new QgsProcessingParameterBand( name, description, def.isEmpty() ? QVariant() : def, parent, isOptional, allowMultiple );
7328}
7329
7330//
7331// QgsProcessingParameterDistance
7332//
7333
7334QgsProcessingParameterDistance::QgsProcessingParameterDistance( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentParameterName, bool optional, double minValue, double maxValue )
7335 : QgsProcessingParameterNumber( name, description, Double, defaultValue, optional, minValue, maxValue )
7336 , mParentParameterName( parentParameterName )
7337{
7338
7339}
7340
7342{
7343 return new QgsProcessingParameterDistance( *this );
7344}
7345
7347{
7348 return typeName();
7349}
7350
7352{
7353 QStringList depends;
7354 if ( !mParentParameterName.isEmpty() )
7355 depends << mParentParameterName;
7356 return depends;
7357}
7358
7360{
7361 switch ( outputType )
7362 {
7364 {
7365 QString code = QStringLiteral( "QgsProcessingParameterDistance('%1', %2" )
7367 if ( mFlags & FlagOptional )
7368 code += QLatin1String( ", optional=True" );
7369
7370 code += QStringLiteral( ", parentParameterName='%1'" ).arg( mParentParameterName );
7371
7372 if ( minimum() != std::numeric_limits<double>::lowest() + 1 )
7373 code += QStringLiteral( ", minValue=%1" ).arg( minimum() );
7374 if ( maximum() != std::numeric_limits<double>::max() )
7375 code += QStringLiteral( ", maxValue=%1" ).arg( maximum() );
7377 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7378 return code;
7379 }
7380 }
7381 return QString();
7382}
7383
7385{
7386 return mParentParameterName;
7387}
7388
7389void QgsProcessingParameterDistance::setParentParameterName( const QString &parentParameterName )
7390{
7391 mParentParameterName = parentParameterName;
7392}
7393
7395{
7397 map.insert( QStringLiteral( "parent" ), mParentParameterName );
7398 map.insert( QStringLiteral( "default_unit" ), static_cast< int >( mDefaultUnit ) );
7399 return map;
7400}
7401
7403{
7405 mParentParameterName = map.value( QStringLiteral( "parent" ) ).toString();
7406 mDefaultUnit = static_cast< Qgis::DistanceUnit>( map.value( QStringLiteral( "default_unit" ), static_cast< int >( Qgis::DistanceUnit::Unknown ) ).toInt() );
7407 return true;
7408}
7409
7410
7411//
7412// QgsProcessingParameterDuration
7413//
7414
7415QgsProcessingParameterDuration::QgsProcessingParameterDuration( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, double minValue, double maxValue )
7416 : QgsProcessingParameterNumber( name, description, Double, defaultValue, optional, minValue, maxValue )
7417{
7418}
7419
7421{
7422 return new QgsProcessingParameterDuration( *this );
7423}
7424
7426{
7427 return typeName();
7428}
7429
7431{
7432 switch ( outputType )
7433 {
7435 {
7436 QString code = QStringLiteral( "QgsProcessingParameterDuration('%1', %2" )
7438 if ( mFlags & FlagOptional )
7439 code += QLatin1String( ", optional=True" );
7440
7441 if ( minimum() != std::numeric_limits<double>::lowest() + 1 )
7442 code += QStringLiteral( ", minValue=%1" ).arg( minimum() );
7443 if ( maximum() != std::numeric_limits<double>::max() )
7444 code += QStringLiteral( ", maxValue=%1" ).arg( maximum() );
7446 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7447 return code;
7448 }
7449 }
7450 return QString();
7451}
7452
7454{
7456 map.insert( QStringLiteral( "default_unit" ), static_cast< int >( mDefaultUnit ) );
7457 return map;
7458}
7459
7461{
7463 mDefaultUnit = static_cast< Qgis::TemporalUnit>( map.value( QStringLiteral( "default_unit" ), static_cast< int >( Qgis::TemporalUnit::Days ) ).toInt() );
7464 return true;
7465}
7466
7467
7468//
7469// QgsProcessingParameterScale
7470//
7471
7472QgsProcessingParameterScale::QgsProcessingParameterScale( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
7473 : QgsProcessingParameterNumber( name, description, Double, defaultValue, optional )
7474{
7475
7476}
7477
7479{
7480 return new QgsProcessingParameterScale( *this );
7481}
7482
7484{
7485 return typeName();
7486}
7487
7489{
7490 switch ( outputType )
7491 {
7493 {
7494 QString code = QStringLiteral( "QgsProcessingParameterScale('%1', %2" )
7496 if ( mFlags & FlagOptional )
7497 code += QLatin1String( ", optional=True" );
7499 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7500 return code;
7501 }
7502 }
7503 return QString();
7504}
7505
7506QgsProcessingParameterScale *QgsProcessingParameterScale::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7507{
7508 return new QgsProcessingParameterScale( name, description, definition.isEmpty() ? QVariant()
7509 : ( definition.toLower().trimmed() == QLatin1String( "none" ) ? QVariant() : definition ), isOptional );
7510}
7511
7512
7513//
7514// QgsProcessingParameterLayout
7515//
7516
7517QgsProcessingParameterLayout::QgsProcessingParameterLayout( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
7518 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7519{}
7520
7522{
7523 return new QgsProcessingParameterLayout( *this );
7524}
7525
7527{
7528 if ( QgsVariantUtils::isNull( value ) )
7529 return QStringLiteral( "None" );
7530
7531 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
7532 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7533
7534 const QString s = value.toString();
7536}
7537
7539{
7540 QString code = QStringLiteral( "##%1=" ).arg( mName );
7541 if ( mFlags & FlagOptional )
7542 code += QLatin1String( "optional " );
7543 code += QLatin1String( "layout " );
7544
7545 code += mDefault.toString();
7546 return code.trimmed();
7547}
7548
7550{
7551 switch ( outputType )
7552 {
7554 {
7555 QString code = QStringLiteral( "QgsProcessingParameterLayout('%1', %2" )
7557 if ( mFlags & FlagOptional )
7558 code += QLatin1String( ", optional=True" );
7560 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7561 return code;
7562 }
7563 }
7564 return QString();
7565}
7566
7567QgsProcessingParameterLayout *QgsProcessingParameterLayout::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7568{
7569 QString def = definition;
7570
7571 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
7572 def = def.mid( 1 );
7573 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
7574 def.chop( 1 );
7575
7576 QVariant defaultValue = def;
7577 if ( def == QLatin1String( "None" ) )
7578 defaultValue = QVariant();
7579
7580 return new QgsProcessingParameterLayout( name, description, defaultValue, isOptional );
7581}
7582
7583
7584//
7585// QString mParentLayerParameterName;
7586//
7587
7588QgsProcessingParameterLayoutItem::QgsProcessingParameterLayoutItem( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayoutParameterName, int itemType, bool optional )
7589 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7590 , mParentLayoutParameterName( parentLayoutParameterName )
7591 , mItemType( itemType )
7592{
7593
7594}
7595
7597{
7598 return new QgsProcessingParameterLayoutItem( *this );
7599}
7600
7602{
7603 if ( QgsVariantUtils::isNull( value ) )
7604 return QStringLiteral( "None" );
7605
7606 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
7607 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7608
7609 const QString s = value.toString();
7611}
7612
7614{
7615 QString code = QStringLiteral( "##%1=" ).arg( mName );
7616 if ( mFlags & FlagOptional )
7617 code += QLatin1String( "optional " );
7618 code += QLatin1String( "layoutitem " );
7619 if ( mItemType >= 0 )
7620 code += QString::number( mItemType ) + ' ';
7621
7622 code += mParentLayoutParameterName + ' ';
7623
7624 code += mDefault.toString();
7625 return code.trimmed();
7626}
7627
7629{
7630 switch ( outputType )
7631 {
7633 {
7634 QString code = QStringLiteral( "QgsProcessingParameterLayoutItem('%1', %2" )
7636 if ( mFlags & FlagOptional )
7637 code += QLatin1String( ", optional=True" );
7638
7639 if ( mItemType >= 0 )
7640 code += QStringLiteral( ", itemType=%1" ).arg( mItemType );
7641
7642 code += QStringLiteral( ", parentLayoutParameterName='%1'" ).arg( mParentLayoutParameterName );
7643
7645 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7646 return code;
7647 }
7648 }
7649 return QString();
7650}
7651
7653{
7655 map.insert( QStringLiteral( "parent_layout" ), mParentLayoutParameterName );
7656 map.insert( QStringLiteral( "item_type" ), mItemType );
7657 return map;
7658}
7659
7661{
7663 mParentLayoutParameterName = map.value( QStringLiteral( "parent_layout" ) ).toString();
7664 mItemType = map.value( QStringLiteral( "item_type" ) ).toInt();
7665 return true;
7666}
7667
7669{
7670 QStringList depends;
7671 if ( !mParentLayoutParameterName.isEmpty() )
7672 depends << mParentLayoutParameterName;
7673 return depends;
7674}
7675
7676QgsProcessingParameterLayoutItem *QgsProcessingParameterLayoutItem::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7677{
7678 QString parent;
7679 QString def = definition;
7680 int itemType = -1;
7681 const QRegularExpression re( QStringLiteral( "(\\d+)?\\s*(.*?)\\s+(.*)$" ) );
7682 const QRegularExpressionMatch m = re.match( def );
7683 if ( m.hasMatch() )
7684 {
7685 itemType = m.captured( 1 ).trimmed().isEmpty() ? -1 : m.captured( 1 ).trimmed().toInt();
7686 parent = m.captured( 2 ).trimmed().isEmpty() ? m.captured( 3 ).trimmed() : m.captured( 2 ).trimmed();
7687 def = !m.captured( 2 ).trimmed().isEmpty() ? m.captured( 3 ) : QString();
7688 }
7689 else
7690 {
7691 parent = def;
7692 def.clear();
7693 }
7694
7695 return new QgsProcessingParameterLayoutItem( name, description, def.isEmpty() ? QVariant() : def, parent, itemType, isOptional );
7696}
7697
7699{
7700 return mParentLayoutParameterName;
7701}
7702
7704{
7705 mParentLayoutParameterName = name;
7706}
7707
7709{
7710 return mItemType;
7711}
7712
7714{
7715 mItemType = type;
7716}
7717
7718//
7719// QgsProcessingParameterColor
7720//
7721
7722QgsProcessingParameterColor::QgsProcessingParameterColor( const QString &name, const QString &description, const QVariant &defaultValue, bool opacityEnabled, bool optional )
7723 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7724 , mAllowOpacity( opacityEnabled )
7725{
7726
7727}
7728
7730{
7731 return new QgsProcessingParameterColor( *this );
7732}
7733
7735{
7736 if ( QgsVariantUtils::isNull( value ) )
7737 return QStringLiteral( "None" );
7738
7739 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
7740 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7741
7742 if ( value.canConvert< QColor >() && !value.value< QColor >().isValid() )
7743 return QStringLiteral( "QColor()" );
7744
7745 if ( value.canConvert< QColor >() )
7746 {
7747 const QColor c = value.value< QColor >();
7748 if ( !mAllowOpacity || c.alpha() == 255 )
7749 return QStringLiteral( "QColor(%1, %2, %3)" ).arg( c.red() ).arg( c.green() ).arg( c.blue() );
7750 else
7751 return QStringLiteral( "QColor(%1, %2, %3, %4)" ).arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
7752 }
7753
7754 const QString s = value.toString();
7756}
7757
7759{
7760 QString code = QStringLiteral( "##%1=" ).arg( mName );
7761 if ( mFlags & FlagOptional )
7762 code += QLatin1String( "optional " );
7763 code += QLatin1String( "color " );
7764
7765 if ( mAllowOpacity )
7766 code += QLatin1String( "withopacity " );
7767
7768 code += mDefault.toString();
7769 return code.trimmed();
7770}
7771
7773{
7774 switch ( outputType )
7775 {
7777 {
7778 QString code = QStringLiteral( "QgsProcessingParameterColor('%1', %2" )
7780 if ( mFlags & FlagOptional )
7781 code += QLatin1String( ", optional=True" );
7782
7783 code += QStringLiteral( ", opacityEnabled=%1" ).arg( mAllowOpacity ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
7784
7786 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7787 return code;
7788 }
7789 }
7790 return QString();
7791}
7792
7794{
7795 if ( !input.isValid() && ( mDefault.isValid() && ( !mDefault.toString().isEmpty() || mDefault.value< QColor >().isValid() ) ) )
7796 return true;
7797
7798 if ( !input.isValid() )
7799 return mFlags & FlagOptional;
7800
7801 if ( input.type() == QVariant::Color )
7802 {
7803 return true;
7804 }
7805 else if ( input.userType() == QMetaType::type( "QgsProperty" ) )
7806 {
7807 return true;
7808 }
7809
7810 if ( input.type() != QVariant::String || input.toString().isEmpty() )
7811 return mFlags & FlagOptional;
7812
7813 bool containsAlpha = false;
7814 return QgsSymbolLayerUtils::parseColorWithAlpha( input.toString(), containsAlpha ).isValid();
7815}
7816
7818{
7820 map.insert( QStringLiteral( "opacityEnabled" ), mAllowOpacity );
7821 return map;
7822}
7823
7825{
7827 mAllowOpacity = map.value( QStringLiteral( "opacityEnabled" ) ).toBool();
7828 return true;
7829}
7830
7832{
7833 return mAllowOpacity;
7834}
7835
7837{
7838 mAllowOpacity = enabled;
7839}
7840
7841QgsProcessingParameterColor *QgsProcessingParameterColor::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7842{
7843 QString def = definition;
7844
7845 bool allowOpacity = false;
7846 if ( def.startsWith( QLatin1String( "withopacity" ), Qt::CaseInsensitive ) )
7847 {
7848 allowOpacity = true;
7849 def = def.mid( 12 );
7850 }
7851
7852 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
7853 def = def.mid( 1 );
7854 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
7855 def.chop( 1 );
7856
7857 QVariant defaultValue = def;
7858 if ( def == QLatin1String( "None" ) )
7859 defaultValue = QVariant();
7860
7861 return new QgsProcessingParameterColor( name, description, defaultValue, allowOpacity, isOptional );
7862}
7863
7864//
7865// QgsProcessingParameterCoordinateOperation
7866//
7867QgsProcessingParameterCoordinateOperation::QgsProcessingParameterCoordinateOperation( const QString &name, const QString &description, const QVariant &defaultValue, const QString &sourceCrsParameterName, const QString &destinationCrsParameterName, const QVariant &staticSourceCrs, const QVariant &staticDestinationCrs, bool optional )
7868 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7869 , mSourceParameterName( sourceCrsParameterName )
7870 , mDestParameterName( destinationCrsParameterName )
7871 , mSourceCrs( staticSourceCrs )
7872 , mDestCrs( staticDestinationCrs )
7873{
7874
7875}
7876
7878{
7879 return new QgsProcessingParameterCoordinateOperation( * this );
7880}
7881
7883{
7884 if ( QgsVariantUtils::isNull( value ) )
7885 return QStringLiteral( "None" );
7886
7887 if ( value.userType() == QMetaType::type( "QgsCoordinateReferenceSystem" ) )
7888 {
7889 if ( !value.value< QgsCoordinateReferenceSystem >().isValid() )
7890 return QStringLiteral( "QgsCoordinateReferenceSystem()" );
7891 else
7892 return QStringLiteral( "QgsCoordinateReferenceSystem('%1')" ).arg( value.value< QgsCoordinateReferenceSystem >().authid() );
7893 }
7894
7895 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
7896 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7897
7898 QVariantMap p;
7899 p.insert( name(), value );
7900 QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
7901 if ( layer )
7903
7904 const QString s = value.toString();
7906}
7907
7909{
7910 QString code = QStringLiteral( "##%1=" ).arg( mName );
7911 if ( mFlags & FlagOptional )
7912 code += QLatin1String( "optional " );
7913 code += QLatin1String( "coordinateoperation " );
7914
7915 code += mDefault.toString();
7916 return code.trimmed();
7917}
7918
7920{
7921 switch ( outputType )
7922 {
7924 {
7926 QString code = QStringLiteral( "QgsProcessingParameterCoordinateOperation('%1', %2" )
7928 if ( mFlags & FlagOptional )
7929 code += QLatin1String( ", optional=True" );
7930 if ( !mSourceParameterName.isEmpty() )
7931 code += QStringLiteral( ", sourceCrsParameterName=%1" ).arg( valueAsPythonString( mSourceParameterName, c ) );
7932 if ( !mDestParameterName.isEmpty() )
7933 code += QStringLiteral( ", destinationCrsParameterName=%1" ).arg( valueAsPythonString( mDestParameterName, c ) );
7934
7935 if ( mSourceCrs.isValid() )
7936 code += QStringLiteral( ", staticSourceCrs=%1" ).arg( valueAsPythonString( mSourceCrs, c ) );
7937 if ( mDestCrs.isValid() )
7938 code += QStringLiteral( ", staticDestinationCrs=%1" ).arg( valueAsPythonString( mDestCrs, c ) );
7939
7940 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7941 return code;
7942 }
7943 }
7944 return QString();
7945}
7946
7948{
7949 QStringList res;
7950 if ( !mSourceParameterName.isEmpty() )
7951 res << mSourceParameterName;
7952 if ( !mDestParameterName.isEmpty() )
7953 res << mDestParameterName;
7954 return res;
7955}
7956
7958{
7960 map.insert( QStringLiteral( "source_crs_parameter_name" ), mSourceParameterName );
7961 map.insert( QStringLiteral( "dest_crs_parameter_name" ), mDestParameterName );
7962 map.insert( QStringLiteral( "static_source_crs" ), mSourceCrs );
7963 map.insert( QStringLiteral( "static_dest_crs" ), mDestCrs );
7964 return map;
7965}
7966
7968{
7970 mSourceParameterName = map.value( QStringLiteral( "source_crs_parameter_name" ) ).toString();
7971 mDestParameterName = map.value( QStringLiteral( "dest_crs_parameter_name" ) ).toString();
7972 mSourceCrs = map.value( QStringLiteral( "static_source_crs" ) );
7973 mDestCrs = map.value( QStringLiteral( "static_dest_crs" ) );
7974 return true;
7975}
7976
7977QgsProcessingParameterCoordinateOperation *QgsProcessingParameterCoordinateOperation::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7978{
7979 QString def = definition;
7980
7981 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
7982 def = def.mid( 1 );
7983 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
7984 def.chop( 1 );
7985
7986 QVariant defaultValue = def;
7987 if ( def == QLatin1String( "None" ) )
7988 defaultValue = QVariant();
7989
7990 return new QgsProcessingParameterCoordinateOperation( name, description, defaultValue, QString(), QString(), QVariant(), QVariant(), isOptional );
7991}
7992
7993
7994//
7995// QgsProcessingParameterMapTheme
7996//
7997
7998QgsProcessingParameterMapTheme::QgsProcessingParameterMapTheme( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
7999 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8000{
8001
8002}
8003
8004
8006{
8007 return new QgsProcessingParameterMapTheme( *this );
8008}
8009
8011{
8012 if ( !input.isValid() && !mDefault.isValid() )
8013 return mFlags & FlagOptional;
8014
8015 if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
8016 || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
8017 return mFlags & FlagOptional;
8018
8019 return true;
8020}
8021
8023{
8024 if ( !value.isValid() )
8025 return QStringLiteral( "None" );
8026
8027 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
8028 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
8029
8030 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
8031}
8032
8034{
8035 QString code = QStringLiteral( "##%1=" ).arg( mName );
8036 if ( mFlags & FlagOptional )
8037 code += QLatin1String( "optional " );
8038 code += QLatin1String( "maptheme " );
8039
8040 code += mDefault.toString();
8041 return code.trimmed();
8042}
8043
8045{
8046 switch ( outputType )
8047 {
8049 {
8050 QString code = QStringLiteral( "QgsProcessingParameterMapTheme('%1', %2" )
8052 if ( mFlags & FlagOptional )
8053 code += QLatin1String( ", optional=True" );
8054
8056 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
8057
8058 return code;
8059 }
8060 }
8061 return QString();
8062}
8063
8065{
8067 return map;
8068}
8069
8071{
8073 return true;
8074}
8075
8076QgsProcessingParameterMapTheme *QgsProcessingParameterMapTheme::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8077{
8078 QString def = definition;
8079 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
8080 def = def.mid( 1 );
8081 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
8082 def.chop( 1 );
8083
8084 QVariant defaultValue = def;
8085
8086 if ( defaultValue == QLatin1String( "None" ) || defaultValue.toString().isEmpty() )
8087 defaultValue = QVariant();
8088
8089 return new QgsProcessingParameterMapTheme( name, description, defaultValue, isOptional );
8090}
8091
8092
8093//
8094// QgsProcessingParameterDateTime
8095//
8096
8097QgsProcessingParameterDateTime::QgsProcessingParameterDateTime( const QString &name, const QString &description, Type type, const QVariant &defaultValue, bool optional, const QDateTime &minValue, const QDateTime &maxValue )
8098 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8099 , mMin( minValue )
8100 , mMax( maxValue )
8101 , mDataType( type )
8102{
8103 if ( mMin.isValid() && mMax.isValid() && mMin >= mMax )
8104 {
8105 QgsMessageLog::logMessage( QObject::tr( "Invalid datetime parameter \"%1\": min value %2 is >= max value %3!" ).arg( name, mMin.toString(), mMax.toString() ), QObject::tr( "Processing" ) );
8106 }
8107}
8108
8110{
8111 return new QgsProcessingParameterDateTime( *this );
8112}
8113
8115{
8116 QVariant input = value;
8117 if ( !input.isValid() )
8118 {
8119 if ( !defaultValue().isValid() )
8120 return mFlags & FlagOptional;
8121
8122 input = defaultValue();
8123 }
8124
8125 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
8126 {
8127 return true;
8128 }
8129
8130 if ( input.type() != QVariant::DateTime && input.type() != QVariant::Date && input.type() != QVariant::Time && input.type() != QVariant::String )
8131 return false;
8132
8133 if ( ( input.type() == QVariant::DateTime || input.type() == QVariant::Date ) && mDataType == Time )
8134 return false;
8135
8136 if ( input.type() == QVariant::String )
8137 {
8138 const QString s = input.toString();
8139 if ( s.isEmpty() )
8140 return mFlags & FlagOptional;
8141
8142 input = QDateTime::fromString( s, Qt::ISODate );
8143 if ( mDataType == Time )
8144 {
8145 if ( !input.toDateTime().isValid() )
8146 input = QTime::fromString( s );
8147 else
8148 input = input.toDateTime().time();
8149 }
8150 }
8151
8152 if ( mDataType != Time )
8153 {
8154 const QDateTime res = input.toDateTime();
8155 return res.isValid() && ( res >= mMin || !mMin.isValid() ) && ( res <= mMax || !mMax.isValid() );
8156 }
8157 else
8158 {
8159 const QTime res = input.toTime();
8160 return res.isValid() && ( res >= mMin.time() || !mMin.isValid() ) && ( res <= mMax.time() || !mMax.isValid() );
8161 }
8162}
8163
8165{
8166 if ( !value.isValid() )
8167 return QStringLiteral( "None" );
8168
8169 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
8170 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
8171
8172 if ( value.type() == QVariant::DateTime )
8173 {
8174 const QDateTime dt = value.toDateTime();
8175 if ( !dt.isValid() )
8176 return QStringLiteral( "QDateTime()" );
8177 else
8178 return QStringLiteral( "QDateTime(QDate(%1, %2, %3), QTime(%4, %5, %6))" ).arg( dt.date().year() )
8179 .arg( dt.date().month() )
8180 .arg( dt.date().day() )
8181 .arg( dt.time().hour() )
8182 .arg( dt.time().minute() )
8183 .arg( dt.time().second() );
8184 }
8185 else if ( value.type() == QVariant::Date )
8186 {
8187 const QDate dt = value.toDate();
8188 if ( !dt.isValid() )
8189 return QStringLiteral( "QDate()" );
8190 else
8191 return QStringLiteral( "QDate(%1, %2, %3)" ).arg( dt.year() )
8192 .arg( dt.month() )
8193 .arg( dt.day() );
8194 }
8195 else if ( value.type() == QVariant::Time )
8196 {
8197 const QTime dt = value.toTime();
8198 if ( !dt.isValid() )
8199 return QStringLiteral( "QTime()" );
8200 else
8201 return QStringLiteral( "QTime(%4, %5, %6)" )
8202 .arg( dt.hour() )
8203 .arg( dt.minute() )
8204 .arg( dt.second() );
8205 }
8206 return value.toString();
8207}
8208
8210{
8212 QStringList parts;
8213 if ( mMin.isValid() )
8214 parts << QObject::tr( "Minimum value: %1" ).arg( mMin.toString( Qt::ISODate ) );
8215 if ( mMax.isValid() )
8216 parts << QObject::tr( "Maximum value: %1" ).arg( mMax.toString( Qt::ISODate ) );
8217 if ( mDefault.isValid() )
8218 parts << QObject::tr( "Default value: %1" ).arg( mDataType == DateTime ? mDefault.toDateTime().toString( Qt::ISODate ) :
8219 ( mDataType == Date ? mDefault.toDate().toString( Qt::ISODate ) : mDefault.toTime( ).toString() ) );
8220 const QString extra = parts.join( QLatin1String( "<br />" ) );
8221 if ( !extra.isEmpty() )
8222 text += QStringLiteral( "<p>%1</p>" ).arg( extra );
8223 return text;
8224}
8225
8227{
8228 switch ( outputType )
8229 {
8231 {
8232 QString code = QStringLiteral( "QgsProcessingParameterDateTime('%1', %2" )
8234 if ( mFlags & FlagOptional )
8235 code += QLatin1String( ", optional=True" );
8236
8237 code += QStringLiteral( ", type=%1" ).arg( mDataType == DateTime ? QStringLiteral( "QgsProcessingParameterDateTime.DateTime" )
8238 : mDataType == Date ? QStringLiteral( "QgsProcessingParameterDateTime.Date" )
8239 : QStringLiteral( "QgsProcessingParameterDateTime.Time" ) );
8240
8242 if ( mMin.isValid() )
8243 code += QStringLiteral( ", minValue=%1" ).arg( valueAsPythonString( mMin, c ) );
8244 if ( mMax.isValid() )
8245 code += QStringLiteral( ", maxValue=%1" ).arg( valueAsPythonString( mMax, c ) );
8246 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
8247 return code;
8248 }
8249 }
8250 return QString();
8251}
8252
8254{
8255 return mMin;
8256}
8257
8259{
8260 mMin = min;
8261}
8262
8264{
8265 return mMax;
8266}
8267
8269{
8270 mMax = max;
8271}
8272
8274{
8275 return mDataType;
8276}
8277
8279{
8280 mDataType = dataType;
8281}
8282
8284{
8286 map.insert( QStringLiteral( "min" ), mMin );
8287 map.insert( QStringLiteral( "max" ), mMax );
8288 map.insert( QStringLiteral( "data_type" ), mDataType );
8289 return map;
8290}
8291
8293{
8295 mMin = map.value( QStringLiteral( "min" ) ).toDateTime();
8296 mMax = map.value( QStringLiteral( "max" ) ).toDateTime();
8297 mDataType = static_cast< Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
8298 return true;
8299}
8300
8301QgsProcessingParameterDateTime *QgsProcessingParameterDateTime::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8302{
8303 return new QgsProcessingParameterDateTime( name, description, DateTime, definition.isEmpty() ? QVariant()
8304 : ( definition.toLower().trimmed() == QLatin1String( "none" ) ? QVariant() : definition ), isOptional );
8305}
8306
8307
8308
8309//
8310// QgsProcessingParameterProviderConnection
8311//
8312
8313QgsProcessingParameterProviderConnection::QgsProcessingParameterProviderConnection( const QString &name, const QString &description, const QString &provider, const QVariant &defaultValue, bool optional )
8314 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8315 , mProviderId( provider )
8316{
8317
8318}
8319
8320
8322{
8323 return new QgsProcessingParameterProviderConnection( *this );
8324}
8325
8327{
8328 if ( !input.isValid() && !mDefault.isValid() )
8329 return mFlags & FlagOptional;
8330
8331 if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
8332 || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
8333 return mFlags & FlagOptional;
8334
8335 return true;
8336}
8337
8339{
8340 if ( !value.isValid() )
8341 return QStringLiteral( "None" );
8342
8343 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
8344 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
8345
8346 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
8347}
8348
8350{
8351 QString code = QStringLiteral( "##%1=" ).arg( mName );
8352 if ( mFlags & FlagOptional )
8353 code += QLatin1String( "optional " );
8354 code += QLatin1String( "providerconnection " );
8355 code += mProviderId + ' ';
8356
8357 code += mDefault.toString();
8358 return code.trimmed();
8359}
8360
8362{
8363 switch ( outputType )
8364 {
8366 {
8367 QString code = QStringLiteral( "QgsProcessingParameterProviderConnection('%1', %2, '%3'" )
8368 .arg( name(), QgsProcessingUtils::stringToPythonLiteral( description() ), mProviderId );
8369 if ( mFlags & FlagOptional )
8370 code += QLatin1String( ", optional=True" );
8371
8373 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
8374
8375 return code;
8376 }
8377 }
8378 return QString();
8379}
8380
8382{
8384 map.insert( QStringLiteral( "provider" ), mProviderId );
8385 return map;
8386}
8387
8389{
8391 mProviderId = map.value( QStringLiteral( "provider" ) ).toString();
8392 return true;
8393}
8394
8395QgsProcessingParameterProviderConnection *QgsProcessingParameterProviderConnection::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8396{
8397 QString def = definition;
8398 QString provider;
8399 if ( def.contains( ' ' ) )
8400 {
8401 provider = def.left( def.indexOf( ' ' ) );
8402 def = def.mid( def.indexOf( ' ' ) + 1 );
8403 }
8404 else
8405 {
8406 provider = def;
8407 def.clear();
8408 }
8409
8410 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
8411 def = def.mid( 1 );
8412 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
8413 def.chop( 1 );
8414
8415 QVariant defaultValue = def;
8416
8417 if ( defaultValue == QLatin1String( "None" ) || defaultValue.toString().isEmpty() )
8418 defaultValue = QVariant();
8419
8421}
8422
8423
8424//
8425// QgsProcessingParameterDatabaseSchema
8426//
8427
8428QgsProcessingParameterDatabaseSchema::QgsProcessingParameterDatabaseSchema( const QString &name, const QString &description, const QString &parentLayerParameterName, const QVariant &defaultValue, bool optional )
8429 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8430 , mParentConnectionParameterName( parentLayerParameterName )
8431{
8432
8433}
8434
8435
8437{
8438 return new QgsProcessingParameterDatabaseSchema( *this );
8439}
8440
8442{
8443 if ( !input.isValid() && !mDefault.isValid() )
8444 return mFlags & FlagOptional;
8445
8446 if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
8447 || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
8448 return mFlags & FlagOptional;
8449
8450 return true;
8451}
8452
8454{
8455 if ( !value.isValid() )
8456 return QStringLiteral( "None" );
8457
8458 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
8459 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
8460
8461 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
8462}
8463
8465{
8466 QString code = QStringLiteral( "##%1=" ).arg( mName );
8467 if ( mFlags & FlagOptional )
8468 code += QLatin1String( "optional " );
8469 code += QLatin1String( "databaseschema " );
8470
8471 code += mParentConnectionParameterName + ' ';
8472
8473 code += mDefault.toString();
8474 return code.trimmed();
8475}
8476
8478{
8479 switch ( outputType )
8480 {
8482 {
8483 QString code = QStringLiteral( "QgsProcessingParameterDatabaseSchema('%1', %2" )
8485 if ( mFlags & FlagOptional )
8486 code += QLatin1String( ", optional=True" );
8487
8488 code += QStringLiteral( ", connectionParameterName='%1'" ).arg( mParentConnectionParameterName );
8490 code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );
8491
8492 code += ')';
8493
8494 return code;
8495 }
8496 }
8497 return QString();
8498}
8499
8501{
8502 QStringList depends;
8503 if ( !mParentConnectionParameterName.isEmpty() )
8504 depends << mParentConnectionParameterName;
8505 return depends;
8506}
8507
8509{
8510 return mParentConnectionParameterName;
8511}
8512
8514{
8515 mParentConnectionParameterName = name;
8516}
8517
8519{
8521 map.insert( QStringLiteral( "mParentConnectionParameterName" ), mParentConnectionParameterName );
8522 return map;
8523}
8524
8526{
8528 mParentConnectionParameterName = map.value( QStringLiteral( "mParentConnectionParameterName" ) ).toString();
8529 return true;
8530}
8531
8532QgsProcessingParameterDatabaseSchema *QgsProcessingParameterDatabaseSchema::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8533{
8534 QString parent;
8535 QString def = definition;
8536
8537 const QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
8538 const QRegularExpressionMatch m = re.match( def );
8539 if ( m.hasMatch() )
8540 {
8541 parent = m.captured( 1 ).trimmed();
8542 def = m.captured( 2 );
8543 }
8544 else
8545 {
8546 parent = def;
8547 def.clear();
8548 }
8549
8550 return new QgsProcessingParameterDatabaseSchema( name, description, parent, def.isEmpty() ? QVariant() : def, isOptional );
8551}
8552
8553//
8554// QgsProcessingParameterDatabaseTable
8555//
8556
8558 const QString &connectionParameterName,
8559 const QString &schemaParameterName,
8560 const QVariant &defaultValue, bool optional, bool allowNewTableNames )
8561 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8562 , mParentConnectionParameterName( connectionParameterName )
8563 , mParentSchemaParameterName( schemaParameterName )
8564 , mAllowNewTableNames( allowNewTableNames )
8565{
8566
8567}
8568
8569
8571{
8572 return new QgsProcessingParameterDatabaseTable( *this );
8573}
8574
8576{
8577 if ( !input.isValid() && !mDefault.isValid() )
8578 return mFlags & FlagOptional;
8579
8580 if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
8581 || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
8582 return mFlags & FlagOptional;
8583
8584 return true;
8585}
8586
8588{
8589 if ( !value.isValid() )
8590 return QStringLiteral( "None" );
8591
8592 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
8593 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
8594
8595 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
8596}
8597
8599{
8600 QString code = QStringLiteral( "##%1=" ).arg( mName );
8601 if ( mFlags & FlagOptional )
8602 code += QLatin1String( "optional " );
8603 code += QLatin1String( "databasetable " );
8604
8605 code += ( mParentConnectionParameterName.isEmpty() ? QStringLiteral( "none" ) : mParentConnectionParameterName ) + ' ';
8606 code += ( mParentSchemaParameterName.isEmpty() ? QStringLiteral( "none" ) : mParentSchemaParameterName ) + ' ';
8607
8608 code += mDefault.toString();
8609 return code.trimmed();
8610}
8611
8613{
8614 switch ( outputType )
8615 {
8617 {
8618 QString code = QStringLiteral( "QgsProcessingParameterDatabaseTable('%1', %2" )
8620 if ( mFlags & FlagOptional )
8621 code += QLatin1String( ", optional=True" );
8622
8623 if ( mAllowNewTableNames )
8624 code += QLatin1String( ", allowNewTableNames=True" );
8625
8626 code += QStringLiteral( ", connectionParameterName='%1'" ).arg( mParentConnectionParameterName );
8627 code += QStringLiteral( ", schemaParameterName='%1'" ).arg( mParentSchemaParameterName );
8629 code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );
8630
8631 code += ')';
8632
8633 return code;
8634 }
8635 }
8636 return QString();
8637}
8638
8640{
8641 QStringList depends;
8642 if ( !mParentConnectionParameterName.isEmpty() )
8643 depends << mParentConnectionParameterName;
8644 if ( !mParentSchemaParameterName.isEmpty() )
8645 depends << mParentSchemaParameterName;
8646 return depends;
8647}
8648
8650{
8651 return mParentConnectionParameterName;
8652}
8653
8655{
8656 mParentConnectionParameterName = name;
8657}
8658
8660{
8661 return mParentSchemaParameterName;
8662}
8663
8665{
8666 mParentSchemaParameterName = name;
8667}
8668
8670{
8672 map.insert( QStringLiteral( "mParentConnectionParameterName" ), mParentConnectionParameterName );
8673 map.insert( QStringLiteral( "mParentSchemaParameterName" ), mParentSchemaParameterName );
8674 map.insert( QStringLiteral( "mAllowNewTableNames" ), mAllowNewTableNames );
8675 return map;
8676}
8677
8679{
8681 mParentConnectionParameterName = map.value( QStringLiteral( "mParentConnectionParameterName" ) ).toString();
8682 mParentSchemaParameterName = map.value( QStringLiteral( "mParentSchemaParameterName" ) ).toString();
8683 mAllowNewTableNames = map.value( QStringLiteral( "mAllowNewTableNames" ), false ).toBool();
8684 return true;
8685}
8686
8687QgsProcessingParameterDatabaseTable *QgsProcessingParameterDatabaseTable::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8688{
8689 QString connection;
8690 QString schema;
8691 QString def = definition;
8692
8693 const QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*+)\\b\\s*(.*)$" ) );
8694 const QRegularExpressionMatch m = re.match( def );
8695 if ( m.hasMatch() )
8696 {
8697 connection = m.captured( 1 ).trimmed();
8698 if ( connection == QLatin1String( "none" ) )
8699 connection.clear();
8700 schema = m.captured( 2 ).trimmed();
8701 if ( schema == QLatin1String( "none" ) )
8702 schema.clear();
8703 def = m.captured( 3 );
8704 }
8705
8706 return new QgsProcessingParameterDatabaseTable( name, description, connection, schema, def.isEmpty() ? QVariant() : def, isOptional );
8707}
8708
8710{
8711 return mAllowNewTableNames;
8712}
8713
8715{
8716 mAllowNewTableNames = allowNewTableNames;
8717}
8718
8719//
8720// QgsProcessingParameterPointCloudLayer
8721//
8722
8724 const QVariant &defaultValue, bool optional )
8725 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8726{
8727}
8728
8730{
8731 return new QgsProcessingParameterPointCloudLayer( *this );
8732}
8733
8735{
8736 if ( !v.isValid() )
8737 return mFlags & FlagOptional;
8738
8739 QVariant var = v;
8740
8741 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
8742 {
8743 const QgsProperty p = var.value< QgsProperty >();
8745 {
8746 var = p.staticValue();
8747 }
8748 else
8749 {
8750 return true;
8751 }
8752 }
8753
8754 if ( qobject_cast< QgsPointCloudLayer * >( qvariant_cast<QObject *>( var ) ) )
8755 return true;
8756
8757 if ( var.type() != QVariant::String || var.toString().isEmpty() )
8758 return mFlags & FlagOptional;
8759
8760 if ( !context )
8761 {
8762 // that's as far as we can get without a context
8763 return true;
8764 }
8765
8766 // try to load as layer
8768 return true;
8769
8770 return false;
8771}
8772
8774{
8775 if ( !val.isValid() )
8776 return QStringLiteral( "None" );
8777
8778 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
8779 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
8780
8781 QVariantMap p;
8782 p.insert( name(), val );
8786}
8787
8788QString QgsProcessingParameterPointCloudLayer::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
8789{
8791}
8792
8794{
8796}
8797
8799{
8800 return QgsProviderRegistry::instance()->filePointCloudFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
8801}
8802
8803QgsProcessingParameterPointCloudLayer *QgsProcessingParameterPointCloudLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8804{
8805 return new QgsProcessingParameterPointCloudLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
8806}
8807
8808//
8809// QgsProcessingParameterAnnotationLayer
8810//
8811
8813 const QVariant &defaultValue, bool optional )
8814 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8815{
8816}
8817
8819{
8820 return new QgsProcessingParameterAnnotationLayer( *this );
8821}
8822
8824{
8825 if ( !v.isValid() )
8826 return mFlags & FlagOptional;
8827
8828 QVariant var = v;
8829
8830 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
8831 {
8832 const QgsProperty p = var.value< QgsProperty >();
8834 {
8835 var = p.staticValue();
8836 }
8837 else
8838 {
8839 return true;
8840 }
8841 }
8842
8843 if ( qobject_cast< QgsAnnotationLayer * >( qvariant_cast<QObject *>( var ) ) )
8844 return true;
8845
8846 if ( var.type() != QVariant::String || var.toString().isEmpty() )
8847 return mFlags & FlagOptional;
8848
8849 if ( !context )
8850 {
8851 // that's as far as we can get without a context
8852 return true;
8853 }
8854
8855 // try to load as layer
8857 return true;
8858
8859 return false;
8860}
8861
8863{
8864 if ( !val.isValid() )
8865 return QStringLiteral( "None" );
8866
8867 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
8868 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
8869
8870 QVariantMap p;
8871 p.insert( name(), val );
8873 return layer ? QgsProcessingUtils::stringToPythonLiteral( layer == context.project()->mainAnnotationLayer() ? QStringLiteral( "main" ) : layer->id() )
8875}
8876
8877QString QgsProcessingParameterAnnotationLayer::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
8878{
8880}
8881
8883{
8885}
8886
8887QgsProcessingParameterAnnotationLayer *QgsProcessingParameterAnnotationLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8888{
8889 return new QgsProcessingParameterAnnotationLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
8890}
8891
8892QgsProcessingParameterPointCloudDestination::QgsProcessingParameterPointCloudDestination( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, bool createByDefault )
8893 : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
8894{
8895}
8896
8898{
8900}
8901
8903{
8904 QVariant var = input;
8905 if ( !var.isValid() )
8906 return mFlags & FlagOptional;
8907
8908 if ( var.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
8909 {
8910 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
8911 var = fromVar.sink;
8912 }
8913
8914 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
8915 {
8916 const QgsProperty p = var.value< QgsProperty >();
8918 {
8919 var = p.staticValue();
8920 }
8921 else
8922 {
8923 return true;
8924 }
8925 }
8926
8927 if ( var.type() != QVariant::String )
8928 return false;
8929
8930 if ( var.toString().isEmpty() )
8931 return mFlags & FlagOptional;
8932
8933 return true;
8934}
8935
8937{
8938 if ( !value.isValid() )
8939 return QStringLiteral( "None" );
8940
8941 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
8942 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
8943
8944 if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
8945 {
8946 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
8948 {
8949 return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
8950 }
8951 else
8952 {
8953 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
8954 }
8955 }
8956
8957 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
8958}
8959
8961{
8963}
8964
8966{
8967 if ( auto *lOriginalProvider = originalProvider() )
8968 {
8969 return lOriginalProvider->defaultPointCloudFileExtension();
8970 }
8971 else if ( QgsProcessingProvider *p = provider() )
8972 {
8973 return p->defaultPointCloudFileExtension();
8974 }
8975 else
8976 {
8978 }
8979}
8980
8982{
8983 const QStringList exts = supportedOutputPointCloudLayerExtensions();
8984 QStringList filters;
8985 for ( const QString &ext : exts )
8986 {
8987 filters << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
8988 }
8989 return filters.join( QLatin1String( ";;" ) ) + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
8990}
8991
8993{
8994 if ( auto *lOriginalProvider = originalProvider() )
8995 {
8996 return lOriginalProvider->supportedOutputPointCloudLayerExtensions();
8997 }
8998 else if ( QgsProcessingProvider *p = provider() )
8999 {
9000 return p->supportedOutputPointCloudLayerExtensions();
9001 }
9002 else
9003 {
9005 return QStringList() << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
9006 }
9007}
9008
9009QgsProcessingParameterPointCloudDestination *QgsProcessingParameterPointCloudDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
9010{
9011 return new QgsProcessingParameterPointCloudDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
9012}
DistanceUnit
Units of distance.
Definition: qgis.h:3047
TemporalUnit
Temporal units.
Definition: qgis.h:3152
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition: qgis.h:228
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition: qgis.h:155
Represents a map layer containing a set of georeferenced annotations, e.g.
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
@ WKT_PREFERRED
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
QString toWkt(WktVariant variant=WKT1_GDAL, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
Class for doing transforms between two map coordinate systems.
void setBallparkTransformsAreAppropriate(bool appropriate)
Sets whether approximate "ballpark" results are appropriate for this coordinate transform.
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const SIP_THROW(QgsCsException)
Transforms a rectangle from the source CRS to the destination CRS.
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const SIP_THROW(QgsCsException)
Transform the point from the source CRS to the destination CRS.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
Class for parsing and evaluation of expressions (formerly called "search strings").
bool isValid() const
Checks if this expression is valid.
InvalidGeometryCheck
Handling of features with invalid geometries.
@ GeometryNoCheck
No invalid geometry checking.
@ GeometryAbortOnInvalid
Close iterator on encountering any features with invalid geometry. This requires a slow geometry vali...
@ GeometrySkipInvalid
Skip any features with invalid geometry. This requires a slow geometry validity check for every featu...
An interface for objects which accept features via addFeature(s) methods.
Container of fields for a vector layer.
Definition: qgsfields.h:45
static bool fileMatchesFilter(const QString &fileName, const QString &filter)
Returns true if the given fileName matches a file filter string.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
QgsGeometry densifyByCount(int extraNodesPerSegment) const
Returns a copy of the geometry which has been densified by adding the specified number of extra nodes...
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
Q_GADGET bool isNull
Definition: qgsgeometry.h:166
static QgsGeometry fromRect(const QgsRectangle &rect) SIP_HOLDGIL
Creates a new geometry from a QgsRectangle.
bool isMultipart() const SIP_HOLDGIL
Returns true if WKB of the geometry is of WKBMulti* type.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
Qgis::GeometryType type
Definition: qgsgeometry.h:167
static QgsGeometry fromPointXY(const QgsPointXY &point) SIP_HOLDGIL
Creates a new geometry from a QgsPointXY object.
QgsGeometry centroid() const
Returns the center of mass of a geometry.
QString lastError() const SIP_HOLDGIL
Returns an error string referring to the last error encountered either when this geometry was created...
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
QString asWkt(int precision=17) const
Exports the geometry to WKT.
Base class for graphical items within a QgsLayout.
QgsMasterLayoutInterface * layoutByName(const QString &name) const
Returns the layout with a matching name, or nullptr if no matching layouts were found.
QgsLayoutItem * itemById(const QString &id) const
Returns a layout item given its id.
Definition: qgslayout.cpp:272
QgsLayoutItem * itemByUuid(const QString &uuid, bool includeTemplateUuids=false) const
Returns the layout item with matching uuid unique identifier, or nullptr if a matching item could not...
Definition: qgslayout.cpp:244
Base class for all map layer types.
Definition: qgsmaplayer.h:73
virtual QgsRectangle extent() const
Returns the extent of the layer.
QString source() const
Returns the source for the layer.
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:79
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Interface for master layout type objects, such as print layouts and reports.
virtual QgsMasterLayoutInterface::Type layoutType() const =0
Returns the master layout type.
@ PrintLayout
Individual print layout (QgsPrintLayout)
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:100
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Represents a map layer supporting display of point clouds.
A class to represent a 2D point.
Definition: qgspointxy.h:59
double y
Definition: qgspointxy.h:63
Q_GADGET double x
Definition: qgspointxy.h:62
Print layout, a QgsLayout subclass for static or atlas-based layouts.
Abstract base class for processing algorithms.
QgsProcessingProvider * provider() const
Returns the provider to which this algorithm belongs.
Details for layers to load into projects.
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
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.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Base class for all parameter definitions which represent file or layer destinations,...
virtual QString defaultFileExtension() const =0
Returns the default file extension for destination file paths associated with this parameter.
void setCreateByDefault(bool createByDefault)
Sets whether the destination should be created by default.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
virtual QString generateTemporaryDestination() const
Generates a temporary destination value for this parameter.
bool supportsNonFileBasedOutput() const
Returns true if the destination parameter supports non filed-based outputs, such as memory layers or ...
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
bool createByDefault() const
Returns true if the destination should be created by default.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
virtual bool isSupportedOutputValue(const QVariant &value, QgsProcessingContext &context, QString &error) const
Tests whether a value is a supported value for this parameter.
QgsProcessingProvider * originalProvider() const
Original (source) provider which this parameter has been derived from.
QgsProcessingDestinationParameter(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingDestinationParameter.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
Encapsulates settings relating to a feature source input to a processing algorithm.
bool loadVariant(const QVariantMap &map)
Loads this source definition from a QVariantMap, wrapped in a QVariant.
Flags flags
Flags which dictate source behavior.
bool selectedFeaturesOnly
true if only selected features in the source should be used by algorithms.
QgsFeatureRequest::InvalidGeometryCheck geometryCheck
Geometry check method to apply to this source.
long long featureLimit
If set to a value > 0, places a limit on the maximum number of features which will be read from the s...
QVariant toVariant() const
Saves this source definition to a QVariantMap, wrapped in a QVariant.
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource,...
Base class for providing feedback from a processing algorithm.
Base class for the definition of processing outputs.
A file output for processing algorithms.
A folder output for processing algorithms.
A HTML file output for processing algorithms.
Encapsulates settings relating to a feature sink or output raster layer for a processing algorithm.
bool loadVariant(const QVariantMap &map)
Loads this output layer definition from a QVariantMap, wrapped in a QVariant.
bool operator!=(const QgsProcessingOutputLayerDefinition &other) const
QgsProject * destinationProject
Destination project.
bool operator==(const QgsProcessingOutputLayerDefinition &other) const
QgsProperty sink
Sink/layer definition.
bool useRemapping() const
Returns true if the output uses a remapping definition.
QgsRemappingSinkDefinition remappingDefinition() const
Returns the output remapping definition, if useRemapping() is true.
QVariant toVariant() const
Saves this output layer definition to a QVariantMap, wrapped in a QVariant.
QString destinationName
Name to use for sink if it's to be loaded into a destination project.
QVariantMap createOptions
Map of optional sink/layer creation options, which are passed to the underlying provider when creatin...
void setRemappingDefinition(const QgsRemappingSinkDefinition &definition)
Sets the remapping definition to use when adding features to the output layer.
A pointcloud layer output for processing algorithms.
A raster layer output for processing algorithms.
A vector layer output for processing algorithms.
An annotation layer parameter for processing algorithms.
QgsProcessingParameterAnnotationLayer(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterAnnotationLayer.
QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const override
Returns a version of the parameter input value, which is suitable for use in a JSON object.
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...
static QgsProcessingParameterAnnotationLayer * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
static QString typeName()
Returns the type name for the parameter class.
A string parameter for authentication configuration ID values.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterAuthConfig(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterAuthConfig.
static QString typeName()
Returns the type name for the parameter class.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QgsProcessingParameterAuthConfig * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
A raster band parameter for Processing algorithms.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
void setAllowMultiple(bool allowMultiple)
Sets whether multiple band selections are permitted.
void setParentLayerParameterName(const QString &parentLayerParameterName)
Sets the name of the parent layer parameter.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString parentLayerParameterName() const
Returns the name of the parent layer parameter, or an empty string if this is not set.
QgsProcessingParameterBand(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentLayerParameterName=QString(), bool optional=false, bool allowMultiple=false)
Constructor for QgsProcessingParameterBand.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QgsProcessingParameterBand * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
static QString typeName()
Returns the type name for the parameter class.
bool allowMultiple() const
Returns whether multiple band selections are permitted.
A boolean parameter for processing algorithms.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString type() const override
Unique parameter type name.
static QString typeName()
Returns the type name for the parameter class.
static QgsProcessingParameterBoolean * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterBoolean(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterBoolean.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
A color parameter for processing algorithms.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QgsProcessingParameterColor * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
bool opacityEnabled() const
Returns true if the parameter allows opacity control.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
void setOpacityEnabled(bool enabled)
Sets whether the parameter allows opacity control.
QgsProcessingParameterColor(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool opacityEnabled=true, bool optional=false)
Constructor for QgsProcessingParameterColor.
static QString typeName()
Returns the type name for the parameter class.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
A coordinate operation parameter for processing algorithms, for selection between available coordinat...
static QString typeName()
Returns the type name for the parameter class.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
static QgsProcessingParameterCoordinateOperation * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterCoordinateOperation(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &sourceCrsParameterName=QString(), const QString &destinationCrsParameterName=QString(), const QVariant &staticSourceCrs=QVariant(), const QVariant &staticDestinationCrs=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterCoordinateOperation.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
A coordinate reference system parameter for processing algorithms.
QgsProcessingParameterCrs(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterCrs.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QString typeName()
Returns the type name for the parameter class.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QgsProcessingParameterCrs * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const override
Returns a version of the parameter input value, which is suitable for use in a JSON object.
A database schema parameter for processing algorithms, allowing users to select from existing schemas...
void setParentConnectionParameterName(const QString &name)
Sets the name of the parent connection parameter.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterDatabaseSchema(const QString &name, const QString &description, const QString &connectionParameterName=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterDatabaseSchema.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QgsProcessingParameterDatabaseSchema * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString parentConnectionParameterName() const
Returns the name of the parent connection parameter, or an empty string if this is not set.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
A database table name parameter for processing algorithms, allowing users to select from existing dat...
QgsProcessingParameterDatabaseTable(const QString &name, const QString &description, const QString &connectionParameterName=QString(), const QString &schemaParameterName=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool allowNewTableNames=false)
Constructor for QgsProcessingParameterDatabaseTable.
void setParentSchemaParameterName(const QString &name)
Sets the name of the parent schema parameter.
QString parentConnectionParameterName() const
Returns the name of the parent connection parameter, or an empty string if this is not set.
QString parentSchemaParameterName() const
Returns the name of the parent schema parameter, or an empty string if this is not set.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
static QgsProcessingParameterDatabaseTable * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
bool allowNewTableNames() const
Returns true if the parameter allows users to enter names for a new (non-existing) tables.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
void setAllowNewTableNames(bool allowed)
Sets whether the parameter allows users to enter names for a new (non-existing) tables.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
void setParentConnectionParameterName(const QString &name)
Sets the name of the parent connection parameter.
A datetime (or pure date or time) parameter for processing algorithms.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
void setMaximum(const QDateTime &maximum)
Sets the maximum value acceptable by the parameter.
static QgsProcessingParameterDateTime * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QDateTime minimum() const
Returns the minimum value acceptable by the parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString toolTip() const override
Returns a formatted tooltip for use with the parameter, which gives helpful information like paramete...
void setMinimum(const QDateTime &minimum)
Sets the minimum value acceptable by the parameter.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QDateTime maximum() const
Returns the maximum value acceptable by the parameter.
QgsProcessingParameterDateTime(const QString &name, const QString &description=QString(), Type type=DateTime, const QVariant &defaultValue=QVariant(), bool optional=false, const QDateTime &minValue=QDateTime(), const QDateTime &maxValue=QDateTime())
Constructor for QgsProcessingParameterDateTime.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
Type dataType() const
Returns the acceptable data type for the parameter.
void setDataType(Type type)
Sets the acceptable data type for the parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
Base class for the definition of processing parameters.
QgsProcessingAlgorithm * mAlgorithm
Pointer to algorithm which owns this parameter.
virtual QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const
Returns a version of the parameter input value, which is suitable for use in a JSON object.
QVariant defaultValue() const
Returns the default value for the parameter.
QString valueAsStringPrivate(const QVariant &value, QgsProcessingContext &context, bool &ok, ValueAsStringFlags flags) const
Internal method for evaluating values as string.
QString help() const
Returns the help for the parameter.
virtual QString asScriptCode() const
Returns the parameter definition encoded in a string which can be used within a Processing script.
virtual QString toolTip() const
Returns a formatted tooltip for use with the parameter, which gives helpful information like paramete...
virtual QStringList valueAsStringList(const QVariant &value, QgsProcessingContext &context, bool &ok) const
Returns a string list version of the parameter input value (if possible).
QgsProcessingAlgorithm * algorithm() const
Returns a pointer to the algorithm which owns this parameter.
QgsProcessingProvider * provider() const
Returns a pointer to the provider for the algorithm which owns this parameter.
@ AllowMapLayerValues
Enable map layer value handling.
virtual QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString description() const
Returns the description for the parameter.
QgsProcessingParameterDefinition(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, const QString &help=QString())
Constructor for QgsProcessingParameterDefinition.
virtual QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const
Returns a string version of the parameter input value (if possible).
QVariantMap mMetadata
Freeform metadata for parameter. Mostly used by widget wrappers to customize their appearance and beh...
QString mDescription
Parameter description.
virtual QString type() const =0
Unique parameter type name.
Flags flags() const
Returns any flags associated with the parameter.
virtual QVariantMap toVariantMap() const
Saves this parameter to a QVariantMap.
QString name() const
Returns the name of the parameter.
QVariant mDefault
Default value for parameter.
QVariant mGuiDefault
Default value for parameter in GUI.
virtual QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
virtual bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const
Checks whether the specified input value is acceptable for the parameter.
virtual bool fromVariantMap(const QVariantMap &map)
Restores this parameter to a QVariantMap.
virtual QString valueAsPythonComment(const QVariant &value, QgsProcessingContext &context) const
Returns a Python comment explaining a parameter value, or an empty string if no comment is required.
QVariant valueAsJsonObjectPrivate(const QVariant &value, QgsProcessingContext &context, ValueAsStringFlags flags) const
Internal method for evaluating values as JSON objects.
A double numeric parameter for distance values.
void setParentParameterName(const QString &parentParameterName)
Sets the name of the parent layer parameter.
static QString typeName()
Returns the type name for the parameter class.
QString parentParameterName() const
Returns the name of the parent parameter, or an empty string if this is not set.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsProcessingParameterDistance * clone() const override
Creates a clone of the parameter definition.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString type() const override
Unique parameter type name.
QgsProcessingParameterDistance(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentParameterName=QString(), bool optional=false, double minValue=std::numeric_limits< double >::lowest()+1, double maxValue=std::numeric_limits< double >::max())
Constructor for QgsProcessingParameterDistance.
A double numeric parameter for duration values.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterDuration * clone() const override
Creates a clone of the parameter definition.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
static QString typeName()
Returns the type name for the parameter class.
QString type() const override
Unique parameter type name.
QgsProcessingParameterDuration(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, double minValue=std::numeric_limits< double >::lowest()+1, double maxValue=std::numeric_limits< double >::max())
Constructor for QgsProcessingParameterDuration.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
void setUsesStaticStrings(bool usesStaticStrings)
Sets whether the parameter uses static (non-translated) string values for its enumeration choice list...
static QgsProcessingParameterEnum * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
bool allowMultiple() const
Returns true if the parameter allows multiple selected values.
QStringList options() const
Returns the list of acceptable options for the parameter.
QgsProcessingParameterEnum(const QString &name, const QString &description=QString(), const QStringList &options=QStringList(), bool allowMultiple=false, const QVariant &defaultValue=QVariant(), bool optional=false, bool usesStaticStrings=false)
Constructor for QgsProcessingParameterEnum.
void setOptions(const QStringList &options)
Sets the list of acceptable options for the parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool usesStaticStrings() const
Returns true if the parameter uses static (non-translated) string values for its enumeration choice l...
QString valueAsPythonComment(const QVariant &value, QgsProcessingContext &context) const override
Returns a Python comment explaining a parameter value, or an empty string if no comment is required.
void setAllowMultiple(bool allowMultiple)
Sets whether the parameter allows multiple selected values.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
static QString typeName()
Returns the type name for the parameter class.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
An expression parameter for processing algorithms.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QgsProcessingParameterExpression * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
QgsProcessingParameterExpression(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentLayerParameterName=QString(), bool optional=false)
Constructor for QgsProcessingParameterExpression.
void setParentLayerParameterName(const QString &parentLayerParameterName)
Sets the name of the parent layer parameter.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString parentLayerParameterName() const
Returns the name of the parent layer parameter, or an empty string if this is not set.
static QString typeName()
Returns the type name for the parameter class.
A rectangular map extent parameter for processing algorithms.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterExtent(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterExtent.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QString typeName()
Returns the type name for the parameter class.
static QgsProcessingParameterExtent * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const override
Returns a version of the parameter input value, which is suitable for use in a JSON object.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
A feature sink output for processing algorithms.
QString generateTemporaryDestination() const override
Generates a temporary destination value for this parameter.
static QgsProcessingParameterFeatureSink * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
bool hasGeometry() const
Returns true if sink is likely to include geometries.
QString type() const override
Unique parameter type name.
QgsProcessing::SourceType dataType() const
Returns the layer type for sinks associated with the parameter.
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
void setSupportsAppend(bool supportsAppend)
Sets whether the sink supports appending features to an existing table.
QgsProcessingParameterFeatureSink(const QString &name, const QString &description=QString(), QgsProcessing::SourceType type=QgsProcessing::TypeVectorAnyGeometry, const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true, bool supportsAppend=false)
Constructor for QgsProcessingParameterFeatureSink.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter.
void setDataType(QgsProcessing::SourceType type)
Sets the layer type for the sinks associated with the parameter.
bool supportsAppend() const
Returns true if the sink supports appending features to an existing table.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
static QString typeName()
Returns the type name for the parameter class.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
An input feature source (such as vector layers) parameter for processing algorithms.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterFeatureSource(const QString &name, const QString &description=QString(), const QList< int > &types=QList< int >(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterFeatureSource.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
static QString typeName()
Returns the type name for the parameter class.
QString type() const override
Unique parameter type name.
static QgsProcessingParameterFeatureSource * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const override
Returns a version of the parameter input value, which is suitable for use in a JSON object.
A vector layer or feature source field parameter for processing algorithms.
QgsProcessingParameterField(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentLayerParameterName=QString(), DataType type=Any, bool allowMultiple=false, bool optional=false, bool defaultToAllFields=false)
Constructor for QgsProcessingParameterField.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
QString parentLayerParameterName() const
Returns the name of the parent layer parameter, or an empty string if this is not set.
void setDataType(DataType type)
Sets the acceptable data type for the field.
void setParentLayerParameterName(const QString &parentLayerParameterName)
Sets the name of the parent layer parameter.
bool allowMultiple() const
Returns whether multiple field selections are permitted.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString type() const override
Unique parameter type name.
bool defaultToAllFields() const
Returns whether a parameter which allows multiple selections (see allowMultiple()) should automatical...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
static QString typeName()
Returns the type name for the parameter class.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
void setAllowMultiple(bool allowMultiple)
Sets whether multiple field selections are permitted.
@ DateTime
Accepts datetime fields.
@ Numeric
Accepts numeric fields.
DataType dataType() const
Returns the acceptable data type for the field.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
void setDefaultToAllFields(bool enabled)
Sets whether a parameter which allows multiple selections (see allowMultiple()) should automatically ...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QgsProcessingParameterField * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
static QgsProcessingParameterFileDestination * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterFileDestination(const QString &name, const QString &description=QString(), const QString &fileFilter=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterFileDestination.
static QString typeName()
Returns the type name for the parameter class.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString fileFilter() const
Returns the file filter string for file destinations compatible with this parameter.
void setFileFilter(const QString &filter)
Sets the file filter string for file destinations compatible with this parameter.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
An input file or folder parameter for processing algorithms.
QString extension() const
Returns any specified file extension for the parameter.
void setExtension(const QString &extension)
Sets a file extension for the parameter.
static QString typeName()
Returns the type name for the parameter class.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
void setFileFilter(const QString &filter)
Sets the file filter string for file destinations compatible with this parameter.
static QgsProcessingParameterFile * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition, Behavior behavior=File)
Creates a new parameter using the definition from a script code.
@ File
Parameter is a single file.
Behavior behavior() const
Returns the parameter behavior (e.g.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString fileFilter() const
Returns the file filter string for file destinations compatible with this parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingParameterFile(const QString &name, const QString &description=QString(), Behavior behavior=File, const QString &extension=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, const QString &fileFilter=QString())
Constructor for QgsProcessingParameterFile.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
A folder destination parameter, for specifying the destination path for a folder created by the algor...
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
static QgsProcessingParameterFolderDestination * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QString typeName()
Returns the type name for the parameter class.
QgsProcessingParameterFolderDestination(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterFolderDestination.
A geometry parameter for processing algorithms.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString type() const override
Unique parameter type name.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterGeometry(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, const QList< int > &geometryTypes=QList< int >(), bool allowMultipart=true)
Constructor for QgsProcessingParameterGeometry.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QgsProcessingParameterGeometry * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
A print layout item parameter, allowing users to select a particular item from a print layout.
QString type() const override
Unique parameter type name.
static QgsProcessingParameterLayoutItem * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterLayoutItem(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentLayoutParameterName=QString(), int itemType=-1, bool optional=false)
Constructor for QgsProcessingParameterLayoutItem.
void setParentLayoutParameterName(const QString &name)
Sets the name of the parent layout parameter.
QString parentLayoutParameterName() const
Returns the name of the parent layout parameter, or an empty string if this is not set.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
static QString typeName()
Returns the type name for the parameter class.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
int itemType() const
Returns the acceptable item type, or -1 if any item type is allowed.
void setItemType(int type)
Sets the acceptable item type, or -1 if any item type is allowed.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
A print layout parameter, allowing users to select a print layout.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
static QgsProcessingParameterLayout * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QgsProcessingParameterLayout(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterLayout.
static QString typeName()
Returns the type name for the parameter class.
Can be inherited by parameters which require limits to their acceptable data types.
void setDataTypes(const QList< int > &types)
Sets the geometry types for sources acceptable by the parameter.
QgsProcessingParameterLimitedDataTypes(const QList< int > &types=QList< int >())
Constructor for QgsProcessingParameterLimitedDataTypes, with a list of acceptable data types.
QList< int > mDataTypes
List of acceptable data types for the parameter.
QList< int > dataTypes() const
Returns the geometry types for sources acceptable by the parameter.
A map layer parameter for processing algorithms.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
static QgsProcessingParameterMapLayer * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString type() const override
Unique parameter type name.
QgsProcessingParameterMapLayer(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, const QList< int > &types=QList< int >())
Constructor for QgsProcessingParameterMapLayer.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const override
Returns a version of the parameter input value, which is suitable for use in a JSON object.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QString typeName()
Returns the type name for the parameter class.
A map theme parameter for processing algorithms, allowing users to select an existing map theme from ...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QgsProcessingParameterMapTheme * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterMapTheme(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterMapTheme.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
A table (matrix) parameter for processing algorithms.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QStringList headers() const
Returns a list of column headers (if set).
void setHeaders(const QStringList &headers)
Sets the list of column headers.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
void setHasFixedNumberRows(bool hasFixedNumberRows)
Sets whether the table has a fixed number of rows.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
void setNumberRows(int rows)
Sets the fixed number of rows in the table.
int numberRows() const
Returns the fixed number of rows in the table.
static QgsProcessingParameterMatrix * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QString typeName()
Returns the type name for the parameter class.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterMatrix(const QString &name, const QString &description=QString(), int numberRows=3, bool hasFixedNumberRows=false, const QStringList &headers=QStringList(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterMatrix.
bool hasFixedNumberRows() const
Returns whether the table has a fixed number of rows.
A mesh layer parameter for processing algorithms.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
QgsProcessingParameterMeshLayer(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterMeshLayer.
QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const override
Returns a version of the parameter input value, which is suitable for use in a JSON object.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QgsProcessingParameterMeshLayer * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
static QString typeName()
Returns the type name for the parameter class.
A parameter for processing algorithms which accepts multiple map layers.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const override
Returns a version of the parameter input value, which is suitable for use in a JSON object.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
void setMinimumNumberInputs(int minimum)
Sets the minimum number of layers required for the parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QgsProcessingParameterMultipleLayers(const QString &name, const QString &description=QString(), QgsProcessing::SourceType layerType=QgsProcessing::TypeVectorAnyGeometry, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterMultipleLayers.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QString typeName()
Returns the type name for the parameter class.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
void setLayerType(QgsProcessing::SourceType type)
Sets the layer type for layers acceptable by the parameter.
static QgsProcessingParameterMultipleLayers * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessing::SourceType layerType() const
Returns the layer type for layers acceptable by the parameter.
QString type() const override
Unique parameter type name.
int minimumNumberInputs() const
Returns the minimum number of layers required for the parameter.
A numeric parameter for processing algorithms.
double minimum() const
Returns the minimum value acceptable by the parameter.
static QgsProcessingParameterNumber * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
void setMinimum(double minimum)
Sets the minimum value acceptable by the parameter.
void setMaximum(double maximum)
Sets the maximum value acceptable by the parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
double maximum() const
Returns the maximum value acceptable by the parameter.
QgsProcessingParameterNumber(const QString &name, const QString &description=QString(), Type type=Integer, const QVariant &defaultValue=QVariant(), bool optional=false, double minValue=std::numeric_limits< double >::lowest()+1, double maxValue=std::numeric_limits< double >::max())
Constructor for QgsProcessingParameterNumber.
void setDataType(Type type)
Sets the acceptable data type for the parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
Type dataType() const
Returns the acceptable data type for the parameter.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString toolTip() const override
Returns a formatted tooltip for use with the parameter, which gives helpful information like paramete...
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QString typeName()
Returns the type name for the parameter class.
A point cloud layer destination parameter, for specifying the destination path for a point cloud laye...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QString 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 QgsProcessingParameterPointCloudDestination * 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 supportedOutputPointCloudLayerExtensions() const
Returns a list of the point cloud format file extensions supported for this parameter.
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.
static QString typeName()
Returns the type name for the parameter class.
QgsProcessingParameterPointCloudDestination(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterPointCloudDestination.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
A point cloud layer parameter for processing algorithms.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
static QgsProcessingParameterPointCloudLayer * 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.
QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const override
Returns a version of the parameter input value, which is suitable for use in a JSON object.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
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.
QgsProcessingParameterPointCloudLayer(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterPointCloudLayer.
A point parameter for processing algorithms.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QgsProcessingParameterPoint * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterPoint(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterPoint.
static QString typeName()
Returns the type name for the parameter class.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
A data provider connection parameter for processing algorithms, allowing users to select from availab...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterProviderConnection(const QString &name, const QString &description, const QString &provider, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterProviderConnection, for the specified provider type.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
static QgsProcessingParameterProviderConnection * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
A numeric range parameter for processing algorithms.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterNumber::Type dataType() const
Returns the acceptable data type for the range.
static QString typeName()
Returns the type name for the parameter class.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
void setDataType(QgsProcessingParameterNumber::Type dataType)
Sets the acceptable data type for the range.
QgsProcessingParameterRange(const QString &name, const QString &description=QString(), QgsProcessingParameterNumber::Type type=QgsProcessingParameterNumber::Integer, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterRange.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QgsProcessingParameterRange * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QgsProcessingParameterRasterDestination(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterRasterDestination.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QgsProcessingParameterRasterDestination * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter.
virtual QStringList supportedOutputRasterLayerExtensions() const
Returns a list of the raster format file extensions supported for this parameter.
static QString typeName()
Returns the type name for the parameter class.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
A raster layer parameter for processing algorithms.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QgsProcessingParameterRasterLayer * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const override
Returns a version of the parameter input value, which is suitable for use in a JSON object.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QString typeName()
Returns the type name for the parameter class.
QgsProcessingParameterRasterLayer(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterRasterLayer.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
A double numeric parameter for map scale values.
QString type() const override
Unique parameter type name.
static QString typeName()
Returns the type name for the parameter class.
static QgsProcessingParameterScale * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterScale * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterScale(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterScale.
A string parameter for processing algorithms.
static QString typeName()
Returns the type name for the parameter class.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
void setMultiLine(bool multiLine)
Sets whether the parameter allows multiline strings.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
static QgsProcessingParameterString * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool multiLine() const
Returns true if the parameter allows multiline strings.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterString(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool multiLine=false, bool optional=false)
Constructor for QgsProcessingParameterString.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
Makes metadata of processing parameters available.
virtual QgsProcessingParameterDefinition * create(const QString &name) const =0
Creates a new parameter of this type.
A vector layer destination parameter, for specifying the destination path for a vector layer created ...
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QgsProcessing::SourceType dataType() const
Returns the layer type for this created vector layer.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QString type() const override
Unique parameter type name.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
static QString typeName()
Returns the type name for the parameter class.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
void setDataType(QgsProcessing::SourceType type)
Sets the layer type for the created vector layer.
QgsProcessingParameterVectorDestination(const QString &name, const QString &description=QString(), QgsProcessing::SourceType type=QgsProcessing::TypeVectorAnyGeometry, const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterVectorDestination.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Processing script.
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this parameter.
bool hasGeometry() const
Returns true if the created layer is likely to include geometries.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QgsProcessingParameterVectorDestination * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
A vector layer (with or without geometry) parameter for processing algorithms.
static QgsProcessingParameterVectorLayer * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString type() const override
Unique parameter type name.
QString createFileFilter() const override
This method needs to be reimplemented in all classes which implement this interface and return a file...
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
QgsProcessingParameterVectorLayer(const QString &name, const QString &description=QString(), const QList< int > &types=QList< int >(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterVectorLayer.
static QString typeName()
Returns the type name for the parameter class.
QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const override
Returns a version of the parameter input value, which is suitable for use in a JSON object.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QList< QgsMapLayer * > parameterAsLayerList(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of map layers.
static QString descriptionFromName(const QString &name)
Creates an autogenerated parameter description from a parameter name.
static int parameterAsEnum(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a enum value.
static double parameterAsDouble(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static double value.
static QgsPointXY parameterAsPoint(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching definition to a point.
static QString parameterAsOutputLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a output layer destination.
static QgsFeatureSink * parameterAsSink(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context, QString &destinationIdentifier, QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), const QVariantMap &createOptions=QVariantMap(), const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList())
Evaluates the parameter with matching definition to a feature sink.
static QgsPrintLayout * parameterAsLayout(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a print layout.
static QTime parameterAsTime(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static time value.
static QgsProcessingParameterDefinition * parameterFromVariantMap(const QVariantMap &map)
Creates a new QgsProcessingParameterDefinition using the configuration from a supplied variant map.
static QgsRectangle parameterAsExtent(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching definition to a rectangular extent.
static QgsCoordinateReferenceSystem parameterAsGeometryCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the coordinate reference system associated with a geometry parameter value.
static QgsAnnotationLayer * parameterAsAnnotationLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to an annotation layer.
static QString parameterAsEnumString(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static enum string.
static QList< double > parameterAsRange(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a range of values.
static QgsMapLayer * parameterAsLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingUtils::LayerHint layerHint=QgsProcessingUtils::LayerHint::UnknownType)
Evaluates the parameter with matching definition to a map layer.
static QList< int > parameterAsInts(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of integer values.
static QString parameterAsConnectionName(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a connection name string.
static QgsProcessingFeatureSource * parameterAsSource(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a feature source.
static QString parameterAsFileOutput(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a file based output destination.
static bool parameterAsBoolean(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static boolean value.
static QgsCoordinateReferenceSystem parameterAsPointCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the coordinate reference system associated with an point parameter value.
static QgsLayoutItem * parameterAsLayoutItem(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsPrintLayout *layout)
Evaluates the parameter with matching definition to a print layout item, taken from the specified lay...
static bool parameterAsBool(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static boolean value.
static QString parameterAsCompatibleSourceLayerPathAndLayerName(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat=QString("shp"), QgsProcessingFeedback *feedback=nullptr, QString *layerName=nullptr)
Evaluates the parameter with matching definition to a source vector layer file path and layer name of...
static QgsMeshLayer * parameterAsMeshLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition and value to a mesh layer.
static QString parameterAsCompatibleSourceLayerPath(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat=QString("shp"), QgsProcessingFeedback *feedback=nullptr)
Evaluates the parameter with matching definition to a source vector layer file path of compatible for...
static QgsProcessingParameterDefinition * parameterFromScriptCode(const QString &code)
Creates a new QgsProcessingParameterDefinition using the configuration from a supplied script code st...
static QColor parameterAsColor(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the color associated with an point parameter value, or an invalid color if the parameter was ...
static QgsVectorLayer * parameterAsVectorLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a vector layer.
static QgsPointCloudLayer * parameterAsPointCloudLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a point cloud layer.
static int parameterAsInt(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static integer value.
static QString parameterAsDatabaseTableName(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a database table name.
static QString parameterAsSchema(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a database schema name.
static QgsGeometry parameterAsGeometry(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching definition to a geometry.
static QString parameterAsExpression(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to an expression.
static QString parameterAsString(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static string value.
static QgsRasterLayer * parameterAsRasterLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a raster layer.
static QList< int > parameterAsEnums(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to list of enum values.
static QStringList parameterAsEnumStrings(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to list of static enum strings.
static QgsGeometry parameterAsExtentGeometry(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching definition to a rectangular extent, and returns a geometry cove...
static QStringList parameterAsFileList(const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of files (for QgsProcessingParameterMultip...
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
static QStringList parameterAsFields(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of fields.
static QgsCoordinateReferenceSystem parameterAsExtentCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the coordinate reference system associated with an extent parameter value.
static QDateTime parameterAsDateTime(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static datetime value.
static QString parameterAsFile(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a file/folder name.
static QDate parameterAsDate(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static date value.
static QVariantList parameterAsMatrix(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a matrix/table of values.
static QgsCoordinateReferenceSystem parameterAsCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a coordinate reference system.
Abstract base class for processing providers.
virtual bool isSupportedOutputValue(const QVariant &outputValue, const QgsProcessingDestinationParameter *parameter, QgsProcessingContext &context, QString &error) const
Returns true if the specified outputValue is of a supported file format for the given destination par...
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
static QString convertToCompatibleFormat(const QgsVectorLayer *layer, bool selectedFeaturesOnly, const QString &baseName, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingContext &context, QgsProcessingFeedback *feedback, long long featureLimit=-1)
Converts a source vector layer to a file path of a vector layer of compatible format.
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
static QString defaultVectorExtension()
Returns the default vector extension to use, in the absence of all other constraints (e....
static QString normalizeLayerSource(const QString &source)
Normalizes a layer source string for safe comparison across different operating system environments.
static QString encodeProviderKeyAndUri(const QString &providerKey, const QString &uri)
Encodes a provider key and layer uri to a single string, for use with decodeProviderKeyAndUri()
LayerHint
Layer type hints.
@ Annotation
Annotation layer type, since QGIS 3.22.
@ Vector
Vector layer type.
@ Mesh
Mesh layer type, since QGIS 3.6.
@ Raster
Raster layer type.
@ UnknownType
Unknown layer type.
@ PointCloud
Point cloud layer type, since QGIS 3.22.
static QString generateTempFilename(const QString &basename)
Returns a temporary filename for a given file, putting it into a temporary folder (creating that fold...
static QgsProcessingFeatureSource * variantToSource(const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue=QVariant())
Converts a variant value to a new feature source.
static QString convertToCompatibleFormatAndLayerName(const QgsVectorLayer *layer, bool selectedFeaturesOnly, const QString &baseName, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingContext &context, QgsProcessingFeedback *feedback, QString &layerName, long long featureLimit=-1)
Converts a source vector layer to a file path and layer name of a vector layer of compatible format.
static QString variantToPythonLiteral(const QVariant &value)
Converts a variant to a Python literal.
static QgsCoordinateReferenceSystem variantToCrs(const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue=QVariant())
Converts a variant value to a coordinate reference system.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType)
Interprets a string as a map layer within the supplied context.
static QgsFeatureSink * createFeatureSink(QString &destination, QgsProcessingContext &context, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &createOptions=QVariantMap(), const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), QgsRemappingSinkDefinition *remappingDefinition=nullptr)
Creates a feature sink ready for adding features.
static QString defaultRasterExtension()
Returns the default raster extension to use, in the absence of all other constraints (e....
static QString defaultPointCloudExtension()
Returns the default point cloud extension to use, in the absence of all other constraints (e....
static const QString TEMPORARY_OUTPUT
Constant used to indicate that a Processing algorithm output should be a temporary layer/file.
PythonOutputType
Available Python output types.
Definition: qgsprocessing.h:64
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
Definition: qgsprocessing.h:65
SourceType
Data source types enum.
Definition: qgsprocessing.h:47
@ TypePlugin
Plugin layers.
Definition: qgsprocessing.h:57
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:51
@ TypeMapLayer
Any map layer type (raster, vector, mesh, point cloud, annotation or plugin layer)
Definition: qgsprocessing.h:48
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:52
@ TypeFile
Files (i.e. non map layer sources, such as text files)
Definition: qgsprocessing.h:54
@ TypeAnnotation
Annotation layers.
Definition: qgsprocessing.h:59
@ TypePointCloud
Point cloud layers.
Definition: qgsprocessing.h:58
@ TypeMesh
Mesh layers.
Definition: qgsprocessing.h:56
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:55
@ TypeRaster
Raster layers.
Definition: qgsprocessing.h:53
@ TypeVectorPoint
Vector point layers.
Definition: qgsprocessing.h:50
@ TypeVectorAnyGeometry
Any vector layer with geometry.
Definition: qgsprocessing.h:49
static QString sourceTypeToString(SourceType type)
Converts a source type to a string representation.
Definition: qgsprocessing.h:73
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:105
QgsAnnotationLayer * mainAnnotationLayer()
Returns the main annotation layer associated with the project.
const QgsLayoutManager * layoutManager() const
Returns the project's layout manager, which manages print layouts, atlases and reports within the pro...
A store for object properties.
Definition: qgsproperty.h:230
@ ExpressionBasedProperty
Expression based property (QgsExpressionBasedProperty)
Definition: qgsproperty.h:239
@ StaticProperty
Static property (QgsStaticProperty)
Definition: qgsproperty.h:237
@ FieldBasedProperty
Field based property (QgsFieldBasedProperty)
Definition: qgsproperty.h:238
@ InvalidProperty
Invalid (not set) property.
Definition: qgsproperty.h:236
QString asExpression() const
Returns an expression string representing the state of the property, or an empty string if the proper...
QString expressionString() const
Returns the expression used for the property value.
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.
QString field() const
Returns the current field name the property references.
QVariant value(const QgsExpressionContext &context, const QVariant &defaultValue=QVariant(), bool *ok=nullptr) const
Calculates the current value of the property, including any transforms which are set for the property...
QVariant toVariant() const
Saves this property to a QVariantMap, wrapped in a QVariant.
bool loadVariant(const QVariant &property)
Loads this property from a QVariantMap, wrapped in a QVariant.
QVariant staticValue() const
Returns the current static value for the property.
Type propertyType() const
Returns the property type.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QString fileVectorFilters() const
Returns a file filter string for supported vector files.
QString fileRasterFilters() const
Returns a file filter string for supported raster files.
QString fileMeshFilters() const
Returns a file filter string for supported mesh files.
QString filePointCloudFilters() const
Returns a file filter string for supported point clouds.
static QStringList supportedFormatExtensions(RasterFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats.
Represents a raster layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:193
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:183
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:198
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:479
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
A QgsGeometry with associated coordinate reference system.
static QgsReferencedGeometry fromReferencedPointXY(const QgsReferencedPointXY &point)
Construct a new QgsReferencedGeometry from referenced point.
static QgsReferencedGeometry fromReferencedRect(const QgsReferencedRectangle &rectangle)
Construct a new QgsReferencedGeometry from referenced rectangle.
A QgsPointXY with associated coordinate reference system.
A QgsRectangle with associated coordinate reference system.
Defines the parameters used to remap features when creating a QgsRemappingProxyFeatureSink.
static QColor parseColorWithAlpha(const QString &colorStr, bool &containsAlpha, bool strictEval=false)
Attempts to parse a string as a color using a variety of common formats, including hex codes,...
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.
static QStringList supportedFormatExtensions(VectorFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats, e.g "shp", "gpkg".
Represents a vector layer which manages a vector based data sets.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:3448
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QString parameterAsCompatibleSourceLayerPathInternal(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback, QString *layerName)
QString createAllMapLayerFileFilter()
const QgsCoordinateReferenceSystem & crs
const QString & typeName