QGIS API Documentation 3.28.14-Firenze (exported)
Loading...
Searching...
No Matches
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, QgsWkbTypes::Type 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 const QStringList resultStringList;
1972 return parameterAsFields( definition, parameters.value( definition->name() ), context );
1973}
1974
1975QStringList QgsProcessingParameters::parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
1976{
1977 if ( !definition )
1978 return QStringList();
1979
1980 QStringList resultStringList;
1981 const QVariant val = value;
1982 if ( val.isValid() )
1983 {
1984 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
1985 resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1986 else if ( val.type() == QVariant::List )
1987 {
1988 const auto constToList = val.toList();
1989 for ( const QVariant &var : constToList )
1990 resultStringList << var.toString();
1991 }
1992 else if ( val.type() == QVariant::StringList )
1993 {
1994 resultStringList = val.toStringList();
1995 }
1996 else
1997 resultStringList.append( val.toString().split( ';' ) );
1998 }
1999
2000 if ( ( resultStringList.isEmpty() || resultStringList.at( 0 ).isEmpty() ) )
2001 {
2002 resultStringList.clear();
2003 // check default
2004 if ( definition->defaultValue().isValid() )
2005 {
2006 if ( definition->defaultValue().type() == QVariant::List )
2007 {
2008 const auto constToList = definition->defaultValue().toList();
2009 for ( const QVariant &var : constToList )
2010 resultStringList << var.toString();
2011 }
2012 else if ( definition->defaultValue().type() == QVariant::StringList )
2013 {
2014 resultStringList = definition->defaultValue().toStringList();
2015 }
2016 else
2017 resultStringList.append( definition->defaultValue().toString().split( ';' ) );
2018 }
2019 }
2020
2021 return resultStringList;
2022}
2023
2025{
2026 if ( !definition )
2027 return nullptr;
2028
2029 return parameterAsLayout( definition, parameters.value( definition->name() ), context );
2030}
2031
2033{
2034 const QString layoutName = parameterAsString( definition, value, context );
2035 if ( layoutName.isEmpty() )
2036 return nullptr;
2037
2038 if ( !context.project() )
2039 return nullptr;
2040
2041 QgsMasterLayoutInterface *l = context.project()->layoutManager()->layoutByName( layoutName );
2043 return static_cast< QgsPrintLayout * >( l );
2044 else
2045 return nullptr;
2046}
2047
2049{
2050 if ( !definition )
2051 return nullptr;
2052
2053 return parameterAsLayoutItem( definition, parameters.value( definition->name() ), context, layout );
2054}
2055
2057{
2058 if ( !layout )
2059 return nullptr;
2060
2061 const QString id = parameterAsString( definition, value, context );
2062 if ( id.isEmpty() )
2063 return nullptr;
2064
2065 // prefer matching by uuid, since it's guaranteed to be unique.
2066 if ( QgsLayoutItem *item = layout->itemByUuid( id ) )
2067 return item;
2068 else if ( QgsLayoutItem *item = layout->itemById( id ) )
2069 return item;
2070 else
2071 return nullptr;
2072}
2073
2074QColor QgsProcessingParameters::parameterAsColor( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
2075{
2076 if ( !definition )
2077 return QColor();
2078
2079 return parameterAsColor( definition, parameters.value( definition->name() ), context );
2080}
2081
2083{
2084 if ( !definition )
2085 return QColor();
2086
2087 QVariant val = value;
2088 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
2089 {
2090 val = val.value< QgsProperty >().value( context.expressionContext(), definition->defaultValue() );
2091 }
2092 if ( val.type() == QVariant::Color )
2093 {
2094 QColor c = val.value< QColor >();
2095 if ( const QgsProcessingParameterColor *colorParam = dynamic_cast< const QgsProcessingParameterColor * >( definition ) )
2096 if ( !colorParam->opacityEnabled() )
2097 c.setAlpha( 255 );
2098 return c;
2099 }
2100
2101 QString colorText = parameterAsString( definition, value, context );
2102 if ( colorText.isEmpty() && !( definition->flags() & QgsProcessingParameterDefinition::FlagOptional ) )
2103 {
2104 if ( definition->defaultValue().type() == QVariant::Color )
2105 return definition->defaultValue().value< QColor >();
2106 else
2107 colorText = definition->defaultValue().toString();
2108 }
2109
2110 if ( colorText.isEmpty() )
2111 return QColor();
2112
2113 bool containsAlpha = false;
2114 QColor c = QgsSymbolLayerUtils::parseColorWithAlpha( colorText, containsAlpha );
2115 if ( const QgsProcessingParameterColor *colorParam = dynamic_cast< const QgsProcessingParameterColor * >( definition ) )
2116 if ( c.isValid() && !colorParam->opacityEnabled() )
2117 c.setAlpha( 255 );
2118 return c;
2119}
2120
2121QString QgsProcessingParameters::parameterAsConnectionName( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
2122{
2123 if ( !definition )
2124 return QString();
2125
2126 return parameterAsConnectionName( definition, parameters.value( definition->name() ), context );
2127}
2128
2130{
2131 // for now it's just treated identical to strings, but in future we may want flexibility to amend this
2132 // (hence the new method)
2133 return parameterAsString( definition, value, context );
2134}
2135
2136QString QgsProcessingParameters::parameterAsSchema( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
2137{
2138 if ( !definition )
2139 return QString();
2140
2141 return parameterAsSchema( definition, parameters.value( definition->name() ), context );
2142}
2143
2144QString QgsProcessingParameters::parameterAsSchema( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
2145{
2146 // 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
2147 // parameter values, such as via a delimiter separated string)
2148 return parameterAsString( definition, value, context );
2149}
2150
2151QString QgsProcessingParameters::parameterAsDatabaseTableName( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
2152{
2153 if ( !definition )
2154 return QString();
2155
2156 return parameterAsDatabaseTableName( definition, parameters.value( definition->name() ), context );
2157}
2158
2160{
2161 // 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
2162 // parameter values, such as via a delimiter separated string)
2163 return parameterAsString( definition, value, context );
2164}
2165
2167{
2168 return qobject_cast< QgsPointCloudLayer *>( parameterAsLayer( definition, parameters, context, QgsProcessingUtils::LayerHint::PointCloud ) );
2169}
2170
2172{
2173 return qobject_cast< QgsPointCloudLayer *>( parameterAsLayer( definition, value, context, QgsProcessingUtils::LayerHint::PointCloud ) );
2174}
2175
2177{
2178 return qobject_cast< QgsAnnotationLayer *>( parameterAsLayer( definition, parameters, context, QgsProcessingUtils::LayerHint::Annotation ) );
2179}
2180
2182{
2183 return qobject_cast< QgsAnnotationLayer *>( parameterAsLayer( definition, value, context, QgsProcessingUtils::LayerHint::Annotation ) );
2184}
2185
2187{
2188 const QString type = map.value( QStringLiteral( "parameter_type" ) ).toString();
2189 const QString name = map.value( QStringLiteral( "name" ) ).toString();
2190 std::unique_ptr< QgsProcessingParameterDefinition > def;
2191
2192 // probably all these hardcoded values aren't required anymore, and we could
2193 // always resort to the registry lookup...
2194 // TODO: confirm
2196 def.reset( new QgsProcessingParameterBoolean( name ) );
2197 else if ( type == QgsProcessingParameterCrs::typeName() )
2198 def.reset( new QgsProcessingParameterCrs( name ) );
2199 else if ( type == QgsProcessingParameterMapLayer::typeName() )
2200 def.reset( new QgsProcessingParameterMapLayer( name ) );
2201 else if ( type == QgsProcessingParameterExtent::typeName() )
2202 def.reset( new QgsProcessingParameterExtent( name ) );
2203 else if ( type == QgsProcessingParameterPoint::typeName() )
2204 def.reset( new QgsProcessingParameterPoint( name ) );
2205 else if ( type == QgsProcessingParameterFile::typeName() )
2206 def.reset( new QgsProcessingParameterFile( name ) );
2207 else if ( type == QgsProcessingParameterMatrix::typeName() )
2208 def.reset( new QgsProcessingParameterMatrix( name ) );
2210 def.reset( new QgsProcessingParameterMultipleLayers( name ) );
2211 else if ( type == QgsProcessingParameterNumber::typeName() )
2212 def.reset( new QgsProcessingParameterNumber( name ) );
2213 else if ( type == QgsProcessingParameterRange::typeName() )
2214 def.reset( new QgsProcessingParameterRange( name ) );
2216 def.reset( new QgsProcessingParameterRasterLayer( name ) );
2217 else if ( type == QgsProcessingParameterEnum::typeName() )
2218 def.reset( new QgsProcessingParameterEnum( name ) );
2219 else if ( type == QgsProcessingParameterString::typeName() )
2220 def.reset( new QgsProcessingParameterString( name ) );
2221 else if ( type == QgsProcessingParameterAuthConfig::typeName() )
2222 def.reset( new QgsProcessingParameterAuthConfig( name ) );
2223 else if ( type == QgsProcessingParameterExpression::typeName() )
2224 def.reset( new QgsProcessingParameterExpression( name ) );
2226 def.reset( new QgsProcessingParameterVectorLayer( name ) );
2227 else if ( type == QgsProcessingParameterField::typeName() )
2228 def.reset( new QgsProcessingParameterField( name ) );
2230 def.reset( new QgsProcessingParameterFeatureSource( name ) );
2232 def.reset( new QgsProcessingParameterFeatureSink( name ) );
2234 def.reset( new QgsProcessingParameterVectorDestination( name ) );
2236 def.reset( new QgsProcessingParameterRasterDestination( name ) );
2238 def.reset( new QgsProcessingParameterPointCloudDestination( name ) );
2240 def.reset( new QgsProcessingParameterFileDestination( name ) );
2242 def.reset( new QgsProcessingParameterFolderDestination( name ) );
2243 else if ( type == QgsProcessingParameterBand::typeName() )
2244 def.reset( new QgsProcessingParameterBand( name ) );
2245 else if ( type == QgsProcessingParameterMeshLayer::typeName() )
2246 def.reset( new QgsProcessingParameterMeshLayer( name ) );
2247 else if ( type == QgsProcessingParameterLayout::typeName() )
2248 def.reset( new QgsProcessingParameterLayout( name ) );
2249 else if ( type == QgsProcessingParameterLayoutItem::typeName() )
2250 def.reset( new QgsProcessingParameterLayoutItem( name ) );
2251 else if ( type == QgsProcessingParameterColor::typeName() )
2252 def.reset( new QgsProcessingParameterColor( name ) );
2254 def.reset( new QgsProcessingParameterCoordinateOperation( name ) );
2256 def.reset( new QgsProcessingParameterPointCloudLayer( name ) );
2258 def.reset( new QgsProcessingParameterAnnotationLayer( name ) );
2259 else
2260 {
2262 if ( paramType )
2263 def.reset( paramType->create( name ) );
2264 }
2265
2266 if ( !def )
2267 return nullptr;
2268
2269 def->fromVariantMap( map );
2270 return def.release();
2271}
2272
2274{
2275 QString desc = name;
2276 desc.replace( '_', ' ' );
2277 return desc;
2278}
2279
2281{
2282 bool isOptional = false;
2283 QString name;
2284 QString definition;
2285 QString type;
2286 if ( !parseScriptCodeParameterOptions( code, isOptional, name, type, definition ) )
2287 return nullptr;
2288
2289 const QString description = descriptionFromName( name );
2290
2291 if ( type == QLatin1String( "boolean" ) )
2292 return QgsProcessingParameterBoolean::fromScriptCode( name, description, isOptional, definition );
2293 else if ( type == QLatin1String( "crs" ) )
2294 return QgsProcessingParameterCrs::fromScriptCode( name, description, isOptional, definition );
2295 else if ( type == QLatin1String( "layer" ) )
2296 return QgsProcessingParameterMapLayer::fromScriptCode( name, description, isOptional, definition );
2297 else if ( type == QLatin1String( "extent" ) )
2298 return QgsProcessingParameterExtent::fromScriptCode( name, description, isOptional, definition );
2299 else if ( type == QLatin1String( "point" ) )
2300 return QgsProcessingParameterPoint::fromScriptCode( name, description, isOptional, definition );
2301 else if ( type == QLatin1String( "geometry" ) )
2302 return QgsProcessingParameterGeometry::fromScriptCode( name, description, isOptional, definition );
2303 else if ( type == QLatin1String( "file" ) )
2304 return QgsProcessingParameterFile::fromScriptCode( name, description, isOptional, definition, QgsProcessingParameterFile::File );
2305 else if ( type == QLatin1String( "folder" ) )
2306 return QgsProcessingParameterFile::fromScriptCode( name, description, isOptional, definition, QgsProcessingParameterFile::Folder );
2307 else if ( type == QLatin1String( "matrix" ) )
2308 return QgsProcessingParameterMatrix::fromScriptCode( name, description, isOptional, definition );
2309 else if ( type == QLatin1String( "multiple" ) )
2310 return QgsProcessingParameterMultipleLayers::fromScriptCode( name, description, isOptional, definition );
2311 else if ( type == QLatin1String( "number" ) )
2312 return QgsProcessingParameterNumber::fromScriptCode( name, description, isOptional, definition );
2313 else if ( type == QLatin1String( "distance" ) )
2314 return QgsProcessingParameterDistance::fromScriptCode( name, description, isOptional, definition );
2315 else if ( type == QLatin1String( "duration" ) )
2316 return QgsProcessingParameterDuration::fromScriptCode( name, description, isOptional, definition );
2317 else if ( type == QLatin1String( "scale" ) )
2318 return QgsProcessingParameterScale::fromScriptCode( name, description, isOptional, definition );
2319 else if ( type == QLatin1String( "range" ) )
2320 return QgsProcessingParameterRange::fromScriptCode( name, description, isOptional, definition );
2321 else if ( type == QLatin1String( "raster" ) )
2322 return QgsProcessingParameterRasterLayer::fromScriptCode( name, description, isOptional, definition );
2323 else if ( type == QLatin1String( "enum" ) )
2324 return QgsProcessingParameterEnum::fromScriptCode( name, description, isOptional, definition );
2325 else if ( type == QLatin1String( "string" ) )
2326 return QgsProcessingParameterString::fromScriptCode( name, description, isOptional, definition );
2327 else if ( type == QLatin1String( "authcfg" ) )
2328 return QgsProcessingParameterAuthConfig::fromScriptCode( name, description, isOptional, definition );
2329 else if ( type == QLatin1String( "expression" ) )
2330 return QgsProcessingParameterExpression::fromScriptCode( name, description, isOptional, definition );
2331 else if ( type == QLatin1String( "field" ) )
2332 return QgsProcessingParameterField::fromScriptCode( name, description, isOptional, definition );
2333 else if ( type == QLatin1String( "vector" ) )
2334 return QgsProcessingParameterVectorLayer::fromScriptCode( name, description, isOptional, definition );
2335 else if ( type == QLatin1String( "source" ) )
2336 return QgsProcessingParameterFeatureSource::fromScriptCode( name, description, isOptional, definition );
2337 else if ( type == QLatin1String( "sink" ) )
2338 return QgsProcessingParameterFeatureSink::fromScriptCode( name, description, isOptional, definition );
2339 else if ( type == QLatin1String( "vectordestination" ) )
2340 return QgsProcessingParameterVectorDestination::fromScriptCode( name, description, isOptional, definition );
2341 else if ( type == QLatin1String( "rasterdestination" ) )
2342 return QgsProcessingParameterRasterDestination::fromScriptCode( name, description, isOptional, definition );
2343 else if ( type == QLatin1String( "pointclouddestination" ) )
2344 return QgsProcessingParameterPointCloudDestination::fromScriptCode( name, description, isOptional, definition );
2345 else if ( type == QLatin1String( "filedestination" ) )
2346 return QgsProcessingParameterFileDestination::fromScriptCode( name, description, isOptional, definition );
2347 else if ( type == QLatin1String( "folderdestination" ) )
2348 return QgsProcessingParameterFolderDestination::fromScriptCode( name, description, isOptional, definition );
2349 else if ( type == QLatin1String( "band" ) )
2350 return QgsProcessingParameterBand::fromScriptCode( name, description, isOptional, definition );
2351 else if ( type == QLatin1String( "mesh" ) )
2352 return QgsProcessingParameterMeshLayer::fromScriptCode( name, description, isOptional, definition );
2353 else if ( type == QLatin1String( "layout" ) )
2354 return QgsProcessingParameterLayout::fromScriptCode( name, description, isOptional, definition );
2355 else if ( type == QLatin1String( "layoutitem" ) )
2356 return QgsProcessingParameterLayoutItem::fromScriptCode( name, description, isOptional, definition );
2357 else if ( type == QLatin1String( "color" ) )
2358 return QgsProcessingParameterColor::fromScriptCode( name, description, isOptional, definition );
2359 else if ( type == QLatin1String( "coordinateoperation" ) )
2360 return QgsProcessingParameterCoordinateOperation::fromScriptCode( name, description, isOptional, definition );
2361 else if ( type == QLatin1String( "maptheme" ) )
2362 return QgsProcessingParameterMapTheme::fromScriptCode( name, description, isOptional, definition );
2363 else if ( type == QLatin1String( "datetime" ) )
2364 return QgsProcessingParameterDateTime::fromScriptCode( name, description, isOptional, definition );
2365 else if ( type == QLatin1String( "providerconnection" ) )
2366 return QgsProcessingParameterProviderConnection::fromScriptCode( name, description, isOptional, definition );
2367 else if ( type == QLatin1String( "databaseschema" ) )
2368 return QgsProcessingParameterDatabaseSchema::fromScriptCode( name, description, isOptional, definition );
2369 else if ( type == QLatin1String( "databasetable" ) )
2370 return QgsProcessingParameterDatabaseTable::fromScriptCode( name, description, isOptional, definition );
2371 else if ( type == QLatin1String( "pointcloud" ) )
2372 return QgsProcessingParameterPointCloudLayer::fromScriptCode( name, description, isOptional, definition );
2373 else if ( type == QLatin1String( "annotation" ) )
2374 return QgsProcessingParameterAnnotationLayer::fromScriptCode( name, description, isOptional, definition );
2375
2376 return nullptr;
2377}
2378
2379bool QgsProcessingParameters::parseScriptCodeParameterOptions( const QString &code, bool &isOptional, QString &name, QString &type, QString &definition )
2380{
2381 const QRegularExpression re( QStringLiteral( "(?:#*)(.*?)=\\s*(.*)" ) );
2382 QRegularExpressionMatch m = re.match( code );
2383 if ( !m.hasMatch() )
2384 return false;
2385
2386 name = m.captured( 1 );
2387 QString tokens = m.captured( 2 );
2388 if ( tokens.startsWith( QLatin1String( "optional" ), Qt::CaseInsensitive ) )
2389 {
2390 isOptional = true;
2391 tokens.remove( 0, 8 ); // length "optional" = 8
2392 }
2393 else
2394 {
2395 isOptional = false;
2396 }
2397
2398 tokens = tokens.trimmed();
2399
2400 const QRegularExpression re2( QStringLiteral( "(.*?)\\s+(.*)" ) );
2401 m = re2.match( tokens );
2402 if ( !m.hasMatch() )
2403 {
2404 type = tokens.toLower().trimmed();
2405 definition.clear();
2406 }
2407 else
2408 {
2409 type = m.captured( 1 ).toLower().trimmed();
2410 definition = m.captured( 2 );
2411 }
2412 return true;
2413}
2414
2415//
2416// QgsProcessingParameterDefinition
2417//
2418
2419QgsProcessingParameterDefinition::QgsProcessingParameterDefinition( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, const QString &help )
2420 : mName( name )
2421 , mDescription( description )
2422 , mHelp( help )
2423 , mDefault( defaultValue )
2424 , mFlags( optional ? FlagOptional : 0 )
2425{}
2426
2428{
2429 if ( !input.isValid() && !mDefault.isValid() )
2430 return mFlags & FlagOptional;
2431
2432 if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
2433 || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
2434 return mFlags & FlagOptional;
2435
2436 return true;
2437}
2438
2440{
2441 if ( !value.isValid() )
2442 return QStringLiteral( "None" );
2443
2444 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
2445 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2446
2447 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
2448}
2449
2450QVariant QgsProcessingParameterDefinition::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
2451{
2452 return valueAsJsonObjectPrivate( value, context, ValueAsStringFlags() );
2453}
2454
2455QVariant QgsProcessingParameterDefinition::valueAsJsonObjectPrivate( const QVariant &value, QgsProcessingContext &context, ValueAsStringFlags flags ) const
2456{
2457 if ( !value.isValid() )
2458 return value;
2459
2460 // dive into map and list types and convert each value
2461 if ( value.type() == QVariant::Type::Map )
2462 {
2463 const QVariantMap sourceMap = value.toMap();
2464 QVariantMap resultMap;
2465 for ( auto it = sourceMap.constBegin(); it != sourceMap.constEnd(); it++ )
2466 {
2467 resultMap[ it.key() ] = valueAsJsonObject( it.value(), context );
2468 }
2469 return resultMap;
2470 }
2471 else if ( value.type() == QVariant::Type::List || value.type() == QVariant::Type::StringList )
2472 {
2473 const QVariantList sourceList = value.toList();
2474 QVariantList resultList;
2475 resultList.reserve( sourceList.size() );
2476 for ( const QVariant &v : sourceList )
2477 {
2478 resultList.push_back( valueAsJsonObject( v, context ) );
2479 }
2480 return resultList;
2481 }
2482 else
2483 {
2484 switch ( value.userType() )
2485 {
2486 // simple types which can be directly represented in JSON -- note that strings are NOT handled here yet!
2487 case QMetaType::Bool:
2488 case QMetaType::Char:
2489 case QMetaType::Int:
2490 case QMetaType::Double:
2491 case QMetaType::Float:
2492 case QMetaType::LongLong:
2493 case QMetaType::ULongLong:
2494 case QMetaType::UInt:
2495 case QMetaType::ULong:
2496 case QMetaType::UShort:
2497 return value;
2498
2499 default:
2500 break;
2501 }
2502
2503
2504 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
2505 {
2506 const QgsProperty prop = value.value< QgsProperty >();
2507 switch ( prop.propertyType() )
2508 {
2510 return QVariant();
2512 return valueAsJsonObject( prop.staticValue(), context );
2513
2514 // these are not supported for serialization
2517 QgsDebugMsg( QStringLiteral( "could not convert expression/field based property to JSON object" ) );
2518 return QVariant();
2519 }
2520 }
2521
2522 // value may be a CRS
2523 if ( value.userType() == QMetaType::type( "QgsCoordinateReferenceSystem" ) )
2524 {
2526 if ( !crs.isValid() )
2527 return QString();
2528 else if ( !crs.authid().isEmpty() )
2529 return crs.authid();
2530 else
2532 }
2533 else if ( value.userType() == QMetaType::type( "QgsRectangle" ) )
2534 {
2535 const QgsRectangle r = value.value<QgsRectangle>();
2536 return QStringLiteral( "%1, %3, %2, %4" ).arg( qgsDoubleToString( r.xMinimum() ),
2539 qgsDoubleToString( r.yMaximum() ) );
2540 }
2541 else if ( value.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
2542 {
2543 const QgsReferencedRectangle r = value.value<QgsReferencedRectangle>();
2544 return QStringLiteral( "%1, %3, %2, %4 [%5]" ).arg( qgsDoubleToString( r.xMinimum() ),
2548 r.crs().authid() );
2549 }
2550 else if ( value.userType() == QMetaType::type( "QgsGeometry" ) )
2551 {
2552 const QgsGeometry g = value.value<QgsGeometry>();
2553 if ( !g.isNull() )
2554 {
2555 return g.asWkt();
2556 }
2557 else
2558 {
2559 return QString();
2560 }
2561 }
2562 else if ( value.userType() == QMetaType::type( "QgsReferencedGeometry" ) )
2563 {
2564 const QgsReferencedGeometry g = value.value<QgsReferencedGeometry>();
2565 if ( !g.isNull() )
2566 {
2567 if ( !g.crs().isValid() )
2568 return g.asWkt();
2569 else
2570 return QStringLiteral( "CRS=%1;%2" ).arg( g.crs().authid().isEmpty() ? g.crs().toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) : g.crs().authid(), g.asWkt() );
2571 }
2572 else
2573 {
2574 return QString();
2575 }
2576 }
2577 else if ( value.userType() == QMetaType::type( "QgsPointXY" ) )
2578 {
2579 const QgsPointXY r = value.value<QgsPointXY>();
2580 return QStringLiteral( "%1,%2" ).arg( qgsDoubleToString( r.x() ),
2581 qgsDoubleToString( r.y() ) );
2582 }
2583 else if ( value.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
2584 {
2585 const QgsReferencedPointXY r = value.value<QgsReferencedPointXY>();
2586 return QStringLiteral( "%1,%2 [%3]" ).arg( qgsDoubleToString( r.x() ),
2587 qgsDoubleToString( r.y() ),
2588 r.crs().authid() );
2589 }
2590 else if ( value.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
2591 {
2592 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( value );
2593
2594 // TODO -- we could consider also serializating the additional properties like invalid feature handling, limits, etc
2595 return valueAsJsonObject( fromVar.source, context );
2596 }
2597 else if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
2598 {
2599 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
2600 return valueAsJsonObject( fromVar.sink, context );
2601 }
2602 else if ( value.userType() == QMetaType::type( "QColor" ) )
2603 {
2604 const QColor fromVar = value.value< QColor >();
2605 if ( !fromVar.isValid() )
2606 return QString();
2607
2608 return QStringLiteral( "rgba( %1, %2, %3, %4 )" ).arg( fromVar.red() ).arg( fromVar.green() ).arg( fromVar.blue() ).arg( QString::number( fromVar.alphaF(), 'f', 2 ) );
2609 }
2610 else if ( value.userType() == QMetaType::type( "QDateTime" ) )
2611 {
2612 const QDateTime fromVar = value.toDateTime();
2613 if ( !fromVar.isValid() )
2614 return QString();
2615
2616 return fromVar.toString( Qt::ISODate );
2617 }
2618 else if ( value.userType() == QMetaType::type( "QDate" ) )
2619 {
2620 const QDate fromVar = value.toDate();
2621 if ( !fromVar.isValid() )
2622 return QString();
2623
2624 return fromVar.toString( Qt::ISODate );
2625 }
2626 else if ( value.userType() == QMetaType::type( "QTime" ) )
2627 {
2628 const QTime fromVar = value.toTime();
2629 if ( !fromVar.isValid() )
2630 return QString();
2631
2632 return fromVar.toString( Qt::ISODate );
2633 }
2634
2636 {
2637 // value may be a map layer
2638 QVariantMap p;
2639 p.insert( name(), value );
2640 if ( QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context ) )
2641 {
2642 const QString source = QgsProcessingUtils::normalizeLayerSource( layer->source() );
2643 if ( !source.isEmpty() )
2644 return source;
2645 return layer->id();
2646 }
2647 }
2648
2649 // now we handle strings, after any other specific logic has already been applied
2650 if ( value.userType() == QMetaType::QString )
2651 return value;
2652 }
2653
2654 // unhandled type
2655 Q_ASSERT_X( false, "QgsProcessingParameterDefinition::valueAsJsonObject", QStringLiteral( "unsupported variant type %1" ).arg( QMetaType::typeName( value.userType() ) ).toLocal8Bit() );
2656 return value;
2657}
2658
2659QString QgsProcessingParameterDefinition::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
2660{
2661 return valueAsStringPrivate( value, context, ok, ValueAsStringFlags() );
2662}
2663
2664QString QgsProcessingParameterDefinition::valueAsStringPrivate( const QVariant &value, QgsProcessingContext &context, bool &ok, ValueAsStringFlags flags ) const
2665{
2666 ok = true;
2667
2668 if ( !value.isValid() )
2669 return QString();
2670
2671 switch ( value.userType() )
2672 {
2673 // simple types which can be directly represented in JSON -- note that strings are NOT handled here yet!
2674 case QMetaType::Bool:
2675 case QMetaType::Char:
2676 case QMetaType::Int:
2677 case QMetaType::Double:
2678 case QMetaType::Float:
2679 case QMetaType::LongLong:
2680 case QMetaType::ULongLong:
2681 case QMetaType::UInt:
2682 case QMetaType::ULong:
2683 case QMetaType::UShort:
2684 return value.toString();
2685
2686 default:
2687 break;
2688 }
2689
2690 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
2691 {
2692 const QgsProperty prop = value.value< QgsProperty >();
2693 switch ( prop.propertyType() )
2694 {
2696 return QString();
2698 return valueAsString( prop.staticValue(), context, ok );
2699
2700 // these are not supported for serialization
2703 QgsDebugMsg( QStringLiteral( "could not convert expression/field based property to string" ) );
2704 return QString();
2705 }
2706 }
2707
2708 // value may be a CRS
2709 if ( value.userType() == QMetaType::type( "QgsCoordinateReferenceSystem" ) )
2710 {
2712 if ( !crs.isValid() )
2713 return QString();
2714 else if ( !crs.authid().isEmpty() )
2715 return crs.authid();
2716 else
2718 }
2719 else if ( value.userType() == QMetaType::type( "QgsRectangle" ) )
2720 {
2721 const QgsRectangle r = value.value<QgsRectangle>();
2722 return QStringLiteral( "%1, %3, %2, %4" ).arg( qgsDoubleToString( r.xMinimum() ),
2725 qgsDoubleToString( r.yMaximum() ) );
2726 }
2727 else if ( value.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
2728 {
2729 const QgsReferencedRectangle r = value.value<QgsReferencedRectangle>();
2730 return QStringLiteral( "%1, %3, %2, %4 [%5]" ).arg( qgsDoubleToString( r.xMinimum() ),
2733 qgsDoubleToString( r.yMaximum() ), r.crs().authid() );
2734 }
2735 else if ( value.userType() == QMetaType::type( "QgsGeometry" ) )
2736 {
2737 const QgsGeometry g = value.value<QgsGeometry>();
2738 if ( !g.isNull() )
2739 {
2740 return g.asWkt();
2741 }
2742 else
2743 {
2744 return QString();
2745 }
2746 }
2747 else if ( value.userType() == QMetaType::type( "QgsReferencedGeometry" ) )
2748 {
2749 const QgsReferencedGeometry g = value.value<QgsReferencedGeometry>();
2750 if ( !g.isNull() )
2751 {
2752 if ( !g.crs().isValid() )
2753 return g.asWkt();
2754 else
2755 return QStringLiteral( "CRS=%1;%2" ).arg( g.crs().authid().isEmpty() ? g.crs().toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) : g.crs().authid(), g.asWkt() );
2756 }
2757 else
2758 {
2759 return QString();
2760 }
2761 }
2762 else if ( value.userType() == QMetaType::type( "QgsPointXY" ) )
2763 {
2764 const QgsPointXY r = value.value<QgsPointXY>();
2765 return QStringLiteral( "%1,%2" ).arg( qgsDoubleToString( r.x() ),
2766 qgsDoubleToString( r.y() ) );
2767 }
2768 else if ( value.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
2769 {
2770 const QgsReferencedPointXY r = value.value<QgsReferencedPointXY>();
2771 return QStringLiteral( "%1,%2 [%3]" ).arg( qgsDoubleToString( r.x() ),
2772 qgsDoubleToString( r.y() ),
2773 r.crs().authid() );
2774 }
2775 else if ( value.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
2776 {
2777 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( value );
2778 return valueAsString( fromVar.source, context, ok );
2779 }
2780 else if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
2781 {
2782 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
2783 return valueAsString( fromVar.sink, context, ok );
2784 }
2785 else if ( value.userType() == QMetaType::type( "QColor" ) )
2786 {
2787 const QColor fromVar = value.value< QColor >();
2788 if ( !fromVar.isValid() )
2789 return QString();
2790
2791 return QStringLiteral( "rgba( %1, %2, %3, %4 )" ).arg( fromVar.red() ).arg( fromVar.green() ).arg( fromVar.blue() ).arg( QString::number( fromVar.alphaF(), 'f', 2 ) );
2792 }
2793 else if ( value.userType() == QMetaType::type( "QDateTime" ) )
2794 {
2795 const QDateTime fromVar = value.toDateTime();
2796 if ( !fromVar.isValid() )
2797 return QString();
2798
2799 return fromVar.toString( Qt::ISODate );
2800 }
2801 else if ( value.userType() == QMetaType::type( "QDate" ) )
2802 {
2803 const QDate fromVar = value.toDate();
2804 if ( !fromVar.isValid() )
2805 return QString();
2806
2807 return fromVar.toString( Qt::ISODate );
2808 }
2809 else if ( value.userType() == QMetaType::type( "QTime" ) )
2810 {
2811 const QTime fromVar = value.toTime();
2812 if ( !fromVar.isValid() )
2813 return QString();
2814
2815 return fromVar.toString( Qt::ISODate );
2816 }
2817
2819 {
2820 // value may be a map layer
2821 QVariantMap p;
2822 p.insert( name(), value );
2823 if ( QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context ) )
2824 {
2825 const QString source = QgsProcessingUtils::normalizeLayerSource( layer->source() );
2826 if ( !source.isEmpty() )
2827 return source;
2828 return layer->id();
2829 }
2830 }
2831
2832 // now we handle strings, after any other specific logic has already been applied
2833 if ( value.userType() == QMetaType::QString )
2834 return value.toString();
2835
2836 // unhandled type
2837 QgsDebugMsg( QStringLiteral( "unsupported variant type %1" ).arg( QMetaType::typeName( value.userType() ) ) );
2838 ok = false;
2839 return value.toString();
2840}
2841
2842QStringList QgsProcessingParameterDefinition::valueAsStringList( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
2843{
2844 ok = true;
2845 if ( !value.isValid( ) )
2846 return QStringList();
2847
2848 if ( value.type() == QVariant::Type::List || value.type() == QVariant::Type::StringList )
2849 {
2850 const QVariantList sourceList = value.toList();
2851 QStringList resultList;
2852 resultList.reserve( sourceList.size() );
2853 for ( const QVariant &v : sourceList )
2854 {
2855 resultList.append( valueAsStringList( v, context, ok ) );
2856 }
2857 return resultList;
2858 }
2859
2860 const QString res = valueAsString( value, context, ok );
2861 if ( !ok )
2862 return QStringList();
2863
2864 return {res};
2865}
2866
2868{
2869 return QString();
2870}
2871
2873{
2874 QString code = QStringLiteral( "##%1=" ).arg( mName );
2875 if ( mFlags & FlagOptional )
2876 code += QLatin1String( "optional " );
2877 code += type() + ' ';
2878 code += mDefault.toString();
2879 return code.trimmed();
2880}
2881
2883{
2884 // base class method is probably not much use
2886 {
2887 switch ( outputType )
2888 {
2890 {
2891 QString code = t->className() + QStringLiteral( "('%1', %2" )
2893 if ( mFlags & FlagOptional )
2894 code += QLatin1String( ", optional=True" );
2895
2897 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
2898 return code;
2899 }
2900 }
2901 }
2902
2903 // oh well, we tried
2904 return QString();
2905}
2906
2908{
2909 QVariantMap map;
2910 map.insert( QStringLiteral( "parameter_type" ), type() );
2911 map.insert( QStringLiteral( "name" ), mName );
2912 map.insert( QStringLiteral( "description" ), mDescription );
2913 map.insert( QStringLiteral( "help" ), mHelp );
2914 map.insert( QStringLiteral( "default" ), mDefault );
2915 map.insert( QStringLiteral( "defaultGui" ), mGuiDefault );
2916 map.insert( QStringLiteral( "flags" ), static_cast< int >( mFlags ) );
2917 map.insert( QStringLiteral( "metadata" ), mMetadata );
2918 return map;
2919}
2920
2922{
2923 mName = map.value( QStringLiteral( "name" ) ).toString();
2924 mDescription = map.value( QStringLiteral( "description" ) ).toString();
2925 mHelp = map.value( QStringLiteral( "help" ) ).toString();
2926 mDefault = map.value( QStringLiteral( "default" ) );
2927 mGuiDefault = map.value( QStringLiteral( "defaultGui" ) );
2928 mFlags = static_cast< Flags >( map.value( QStringLiteral( "flags" ) ).toInt() );
2929 mMetadata = map.value( QStringLiteral( "metadata" ) ).toMap();
2930 return true;
2931}
2932
2937
2942
2944{
2945 QString text = QStringLiteral( "<p><b>%1</b></p>" ).arg( description() );
2946 if ( !help().isEmpty() )
2947 {
2948 text += QStringLiteral( "<p>%1</p>" ).arg( help() );
2949 }
2950 text += QStringLiteral( "<p>%1</p>" ).arg( QObject::tr( "Python identifier: ‘%1’" ).arg( QStringLiteral( "<i>%1</i>" ).arg( name() ) ) );
2951 return text;
2952}
2953
2954QgsProcessingParameterBoolean::QgsProcessingParameterBoolean( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
2955 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2956{}
2957
2962
2964{
2965 if ( !val.isValid() )
2966 return QStringLiteral( "None" );
2967
2968 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
2969 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
2970 return val.toBool() ? QStringLiteral( "True" ) : QStringLiteral( "False" );
2971}
2972
2974{
2975 QString code = QStringLiteral( "##%1=" ).arg( mName );
2976 if ( mFlags & FlagOptional )
2977 code += QLatin1String( "optional " );
2978 code += type() + ' ';
2979 code += mDefault.toBool() ? QStringLiteral( "true" ) : QStringLiteral( "false" );
2980 return code.trimmed();
2981}
2982
2983QgsProcessingParameterBoolean *QgsProcessingParameterBoolean::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2984{
2985 return new QgsProcessingParameterBoolean( name, description, definition.toLower().trimmed() != QStringLiteral( "false" ), isOptional );
2986}
2987
2988QgsProcessingParameterCrs::QgsProcessingParameterCrs( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
2989 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2990{
2991
2992}
2993
2998
3000{
3001 QVariant input = v;
3002 if ( !input.isValid() )
3003 {
3004 if ( !defaultValue().isValid() )
3005 return mFlags & FlagOptional;
3006
3007 input = defaultValue();
3008 }
3009
3010 if ( input.userType() == QMetaType::type( "QgsCoordinateReferenceSystem" ) )
3011 {
3012 return true;
3013 }
3014 else if ( input.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
3015 {
3016 return true;
3017 }
3018 else if ( input.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
3019 {
3020 return true;
3021 }
3022
3023 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
3024 {
3025 return true;
3026 }
3027
3028 // direct map layer value
3029 if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
3030 return true;
3031
3032 if ( input.type() != QVariant::String || input.toString().isEmpty() )
3033 return mFlags & FlagOptional;
3034
3035 return true;
3036}
3037
3038QString QgsProcessingParameterCrs::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
3039{
3040 if ( !value.isValid() )
3041 return QStringLiteral( "None" );
3042
3043 if ( value.userType() == QMetaType::type( "QgsCoordinateReferenceSystem" ) )
3044 {
3045 if ( !value.value< QgsCoordinateReferenceSystem >().isValid() )
3046 return QStringLiteral( "QgsCoordinateReferenceSystem()" );
3047 else
3048 return QStringLiteral( "QgsCoordinateReferenceSystem('%1')" ).arg( value.value< QgsCoordinateReferenceSystem >().authid() );
3049 }
3050
3051 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
3052 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3053
3054 QVariantMap p;
3055 p.insert( name(), value );
3056 QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
3057 if ( layer )
3059
3061}
3062
3063QString QgsProcessingParameterCrs::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
3064{
3066}
3067
3068QVariant QgsProcessingParameterCrs::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
3069{
3071}
3072
3073QgsProcessingParameterCrs *QgsProcessingParameterCrs::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3074{
3075 return new QgsProcessingParameterCrs( name, description, definition.compare( QLatin1String( "none" ), Qt::CaseInsensitive ) == 0 ? QVariant() : definition, isOptional );
3076}
3077
3078QgsProcessingParameterMapLayer::QgsProcessingParameterMapLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, const QList<int> &types )
3079 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3081{
3082
3083}
3084
3089
3091{
3092 QVariant input = v;
3093
3094 if ( !input.isValid() )
3095 {
3096 if ( !defaultValue().isValid() )
3097 return mFlags & FlagOptional;
3098
3099 input = defaultValue();
3100 }
3101
3102 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
3103 {
3104 return true;
3105 }
3106
3107 if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
3108 {
3109 return true;
3110 }
3111
3112 if ( input.type() != QVariant::String || input.toString().isEmpty() )
3113 return mFlags & FlagOptional;
3114
3115 if ( !context )
3116 {
3117 // that's as far as we can get without a context
3118 return true;
3119 }
3120
3121 // try to load as layer
3122 if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context ) )
3123 return true;
3124
3125 return false;
3126}
3127
3129{
3130 if ( !val.isValid() )
3131 return QStringLiteral( "None" );
3132
3133 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
3134 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
3135
3136 QVariantMap p;
3137 p.insert( name(), val );
3138 QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
3141}
3142
3143QString QgsProcessingParameterMapLayer::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
3144{
3146}
3147
3148QVariant QgsProcessingParameterMapLayer::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
3149{
3151}
3152
3154{
3155 QStringList vectors = QgsProviderRegistry::instance()->fileVectorFilters().split( QStringLiteral( ";;" ) );
3156 const QStringList rasters = QgsProviderRegistry::instance()->fileRasterFilters().split( QStringLiteral( ";;" ) );
3157 for ( const QString &raster : rasters )
3158 {
3159 if ( !vectors.contains( raster ) )
3160 vectors << raster;
3161 }
3162 const QStringList meshFilters = QgsProviderRegistry::instance()->fileMeshFilters().split( QStringLiteral( ";;" ) );
3163 for ( const QString &mesh : meshFilters )
3164 {
3165 if ( !vectors.contains( mesh ) )
3166 vectors << mesh;
3167 }
3168 const QStringList pointCloudFilters = QgsProviderRegistry::instance()->filePointCloudFilters().split( QStringLiteral( ";;" ) );
3169 for ( const QString &pointCloud : pointCloudFilters )
3170 {
3171 if ( !vectors.contains( pointCloud ) )
3172 vectors << pointCloud;
3173 }
3174 vectors.removeAll( QObject::tr( "All files (*.*)" ) );
3175 std::sort( vectors.begin(), vectors.end() );
3176
3177 return QObject::tr( "All files (*.*)" ) + QStringLiteral( ";;" ) + vectors.join( QLatin1String( ";;" ) );
3178}
3179
3184
3186{
3187 QString code = QStringLiteral( "##%1=" ).arg( mName );
3188 if ( mFlags & FlagOptional )
3189 code += QLatin1String( "optional " );
3190 code += QLatin1String( "layer " );
3191
3192 for ( const int type : mDataTypes )
3193 {
3194 switch ( type )
3195 {
3197 code += QLatin1String( "hasgeometry " );
3198 break;
3199
3201 code += QLatin1String( "point " );
3202 break;
3203
3205 code += QLatin1String( "line " );
3206 break;
3207
3209 code += QLatin1String( "polygon " );
3210 break;
3211
3213 code += QLatin1String( "raster " );
3214 break;
3215
3217 code += QLatin1String( "mesh " );
3218 break;
3219
3221 code += QLatin1String( "plugin " );
3222 break;
3223
3225 code += QLatin1String( "pointcloud " );
3226 break;
3227
3229 code += QLatin1String( "annotation " );
3230 break;
3231 }
3232 }
3233
3234 code += mDefault.toString();
3235 return code.trimmed();
3236}
3237
3238QgsProcessingParameterMapLayer *QgsProcessingParameterMapLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3239{
3240 QList< int > types;
3241 QString def = definition;
3242 while ( true )
3243 {
3244 if ( def.startsWith( QLatin1String( "hasgeometry" ), Qt::CaseInsensitive ) )
3245 {
3247 def = def.mid( 12 );
3248 continue;
3249 }
3250 else if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
3251 {
3253 def = def.mid( 6 );
3254 continue;
3255 }
3256 else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
3257 {
3259 def = def.mid( 5 );
3260 continue;
3261 }
3262 else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
3263 {
3265 def = def.mid( 8 );
3266 continue;
3267 }
3268 else if ( def.startsWith( QLatin1String( "raster" ), Qt::CaseInsensitive ) )
3269 {
3271 def = def.mid( 7 );
3272 continue;
3273 }
3274 else if ( def.startsWith( QLatin1String( "mesh" ), Qt::CaseInsensitive ) )
3275 {
3276 types << QgsProcessing::TypeMesh;
3277 def = def.mid( 5 );
3278 continue;
3279 }
3280 else if ( def.startsWith( QLatin1String( "plugin" ), Qt::CaseInsensitive ) )
3281 {
3283 def = def.mid( 7 );
3284 continue;
3285 }
3286 else if ( def.startsWith( QLatin1String( "pointcloud" ), Qt::CaseInsensitive ) )
3287 {
3289 def = def.mid( 11 );
3290 continue;
3291 }
3292 else if ( def.startsWith( QLatin1String( "annotation" ), Qt::CaseInsensitive ) )
3293 {
3295 def = def.mid( 11 );
3296 continue;
3297 }
3298 break;
3299 }
3300
3301 return new QgsProcessingParameterMapLayer( name, description, def.isEmpty() ? QVariant() : def, isOptional, types );
3302}
3303
3305{
3306 switch ( outputType )
3307 {
3309 {
3310 QString code = QStringLiteral( "QgsProcessingParameterMapLayer('%1', %2" )
3312 if ( mFlags & FlagOptional )
3313 code += QLatin1String( ", optional=True" );
3314
3316 code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );
3317
3318 if ( !mDataTypes.empty() )
3319 {
3320 QStringList options;
3321 options.reserve( mDataTypes.size() );
3322 for ( const int t : mDataTypes )
3323 options << QStringLiteral( "QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( static_cast< QgsProcessing::SourceType >( t ) ) );
3324 code += QStringLiteral( ", types=[%1])" ).arg( options.join( ',' ) );
3325 }
3326 else
3327 {
3328 code += QLatin1Char( ')' );
3329 }
3330
3331 return code;
3332 }
3333 }
3334 return QString();
3335}
3336
3338{
3340 QVariantList types;
3341 for ( const int type : mDataTypes )
3342 {
3343 types << type;
3344 }
3345 map.insert( QStringLiteral( "data_types" ), types );
3346 return map;
3347}
3348
3350{
3352 mDataTypes.clear();
3353 const QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
3354 for ( const QVariant &val : values )
3355 {
3356 mDataTypes << val.toInt();
3357 }
3358 return true;
3359}
3360
3361QgsProcessingParameterExtent::QgsProcessingParameterExtent( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
3362 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3363{
3364
3365}
3366
3371
3373{
3374 QVariant input = v;
3375 if ( !input.isValid() )
3376 {
3377 if ( !defaultValue().isValid() )
3378 return mFlags & FlagOptional;
3379
3380 input = defaultValue();
3381 }
3382
3383 if ( input.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
3384 {
3385 return true;
3386 }
3387 else if ( input.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
3388 {
3389 return true;
3390 }
3391
3392 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
3393 {
3394 return true;
3395 }
3396
3397 if ( input.userType() == QMetaType::type( "QgsRectangle" ) )
3398 {
3399 const QgsRectangle r = input.value<QgsRectangle>();
3400 return !r.isNull();
3401 }
3402 if ( input.userType() == QMetaType::type( "QgsGeometry" ) )
3403 {
3404 return true;
3405 }
3406 if ( input.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
3407 {
3408 const QgsReferencedRectangle r = input.value<QgsReferencedRectangle>();
3409 return !r.isNull();
3410 }
3411
3412 // direct map layer value
3413 if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
3414 return true;
3415
3416 if ( input.type() != QVariant::String || input.toString().isEmpty() )
3417 return mFlags & FlagOptional;
3418
3419 if ( !context )
3420 {
3421 // that's as far as we can get without a context
3422 return true;
3423 }
3424
3425 const QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
3426 const QRegularExpressionMatch match = rx.match( input.toString() );
3427 if ( match.hasMatch() )
3428 {
3429 bool xMinOk = false;
3430 ( void )match.captured( 1 ).toDouble( &xMinOk );
3431 bool xMaxOk = false;
3432 ( void )match.captured( 2 ).toDouble( &xMaxOk );
3433 bool yMinOk = false;
3434 ( void )match.captured( 3 ).toDouble( &yMinOk );
3435 bool yMaxOk = false;
3436 ( void )match.captured( 4 ).toDouble( &yMaxOk );
3437 if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
3438 return true;
3439 }
3440
3441 // try as layer extent
3442 return QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
3443}
3444
3445QString QgsProcessingParameterExtent::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
3446{
3447 if ( !value.isValid() )
3448 return QStringLiteral( "None" );
3449
3450 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
3451 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3452
3453 if ( value.userType() == QMetaType::type( "QgsRectangle" ) )
3454 {
3455 const QgsRectangle r = value.value<QgsRectangle>();
3456 return QStringLiteral( "'%1, %3, %2, %4'" ).arg( qgsDoubleToString( r.xMinimum() ),
3459 qgsDoubleToString( r.yMaximum() ) );
3460 }
3461 else if ( value.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
3462 {
3463 const QgsReferencedRectangle r = value.value<QgsReferencedRectangle>();
3464 return QStringLiteral( "'%1, %3, %2, %4 [%5]'" ).arg( qgsDoubleToString( r.xMinimum() ),
3467 qgsDoubleToString( r.yMaximum() ), r.crs().authid() );
3468 }
3469 else if ( value.userType() == QMetaType::type( "QgsGeometry" ) )
3470 {
3471 const QgsGeometry g = value.value<QgsGeometry>();
3472 if ( !g.isNull() )
3473 {
3474 const QString wkt = g.asWkt();
3475 return QStringLiteral( "QgsGeometry.fromWkt('%1')" ).arg( wkt );
3476 }
3477 }
3478
3479 QVariantMap p;
3480 p.insert( name(), value );
3481 QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
3482 if ( layer )
3484
3486}
3487
3488QString QgsProcessingParameterExtent::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
3489{
3491}
3492
3493QVariant QgsProcessingParameterExtent::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
3494{
3496}
3497
3498QgsProcessingParameterExtent *QgsProcessingParameterExtent::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3499{
3500 return new QgsProcessingParameterExtent( name, description, definition, isOptional );
3501}
3502
3503QgsProcessingParameterPoint::QgsProcessingParameterPoint( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
3504 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3505{
3506
3507}
3508
3513
3515{
3516 QVariant input = v;
3517 if ( !input.isValid() )
3518 {
3519 if ( !defaultValue().isValid() )
3520 return mFlags & FlagOptional;
3521
3522 input = defaultValue();
3523 }
3524
3525 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
3526 {
3527 return true;
3528 }
3529
3530 if ( input.userType() == QMetaType::type( "QgsPointXY" ) )
3531 {
3532 return true;
3533 }
3534 if ( input.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
3535 {
3536 return true;
3537 }
3538 if ( input.userType() == QMetaType::type( "QgsGeometry" ) )
3539 {
3540 return true;
3541 }
3542
3543 if ( input.type() == QVariant::String )
3544 {
3545 if ( input.toString().isEmpty() )
3546 return mFlags & FlagOptional;
3547 }
3548
3549 const QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
3550
3551 const QRegularExpressionMatch match = rx.match( input.toString() );
3552 if ( match.hasMatch() )
3553 {
3554 bool xOk = false;
3555 ( void )match.captured( 1 ).toDouble( &xOk );
3556 bool yOk = false;
3557 ( void )match.captured( 2 ).toDouble( &yOk );
3558 return xOk && yOk;
3559 }
3560 else
3561 return false;
3562}
3563
3564QString QgsProcessingParameterPoint::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
3565{
3566 if ( !value.isValid() )
3567 return QStringLiteral( "None" );
3568
3569 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
3570 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3571
3572 if ( value.userType() == QMetaType::type( "QgsPointXY" ) )
3573 {
3574 const QgsPointXY r = value.value<QgsPointXY>();
3575 return QStringLiteral( "'%1,%2'" ).arg( qgsDoubleToString( r.x() ),
3576 qgsDoubleToString( r.y() ) );
3577 }
3578 else if ( value.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
3579 {
3580 const QgsReferencedPointXY r = value.value<QgsReferencedPointXY>();
3581 return QStringLiteral( "'%1,%2 [%3]'" ).arg( qgsDoubleToString( r.x() ),
3582 qgsDoubleToString( r.y() ),
3583 r.crs().authid() );
3584 }
3585 else if ( value.userType() == QMetaType::type( "QgsGeometry" ) )
3586 {
3587 const QgsGeometry g = value.value<QgsGeometry>();
3588 if ( !g.isNull() )
3589 {
3590 const QString wkt = g.asWkt();
3591 return QStringLiteral( "QgsGeometry.fromWkt('%1')" ).arg( wkt );
3592 }
3593 }
3594
3596}
3597
3598QgsProcessingParameterPoint *QgsProcessingParameterPoint::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3599{
3600 return new QgsProcessingParameterPoint( name, description, definition, isOptional );
3601}
3602
3603QgsProcessingParameterGeometry::QgsProcessingParameterGeometry( const QString &name, const QString &description,
3604 const QVariant &defaultValue, bool optional, const QList<int> &geometryTypes, bool allowMultipart )
3605 : QgsProcessingParameterDefinition( name, description, defaultValue, optional ),
3606 mGeomTypes( geometryTypes ),
3607 mAllowMultipart( allowMultipart )
3608{
3609
3610}
3611
3616
3618{
3619 QVariant input = v;
3620 if ( !input.isValid() )
3621 {
3622 if ( !defaultValue().isValid() )
3623 return mFlags & FlagOptional;
3624
3625 input = defaultValue();
3626 }
3627
3628 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
3629 {
3630 return true;
3631 }
3632
3633 const bool anyTypeAllowed = mGeomTypes.isEmpty() || mGeomTypes.contains( QgsWkbTypes::UnknownGeometry );
3634
3635 if ( input.userType() == QMetaType::type( "QgsGeometry" ) )
3636 {
3637 return ( anyTypeAllowed || mGeomTypes.contains( input.value<QgsGeometry>().type() ) ) &&
3638 ( mAllowMultipart || !input.value<QgsGeometry>().isMultipart() );
3639 }
3640
3641 if ( input.userType() == QMetaType::type( "QgsReferencedGeometry" ) )
3642 {
3643 return ( anyTypeAllowed || mGeomTypes.contains( input.value<QgsReferencedGeometry>().type() ) ) &&
3644 ( mAllowMultipart || !input.value<QgsReferencedGeometry>().isMultipart() );
3645 }
3646
3647 if ( input.userType() == QMetaType::type( "QgsPointXY" ) )
3648 {
3649 return anyTypeAllowed || mGeomTypes.contains( QgsWkbTypes::PointGeometry );
3650 }
3651
3652 if ( input.userType() == QMetaType::type( "QgsRectangle" ) )
3653 {
3654 return anyTypeAllowed || mGeomTypes.contains( QgsWkbTypes::PolygonGeometry );
3655 }
3656
3657 if ( input.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
3658 {
3659 return anyTypeAllowed || mGeomTypes.contains( QgsWkbTypes::PointGeometry );
3660 }
3661
3662 if ( input.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
3663 {
3664 return anyTypeAllowed || mGeomTypes.contains( QgsWkbTypes::PolygonGeometry );
3665 }
3666
3667 if ( input.type() == QVariant::String )
3668 {
3669 if ( input.toString().isEmpty() )
3670 return mFlags & FlagOptional;
3671 }
3672
3673 // Match against EWKT
3674 const QRegularExpression rx( QStringLiteral( "^\\s*(?:CRS=(.*);)?(.*?)$" ) );
3675
3676 const QRegularExpressionMatch match = rx.match( input.toString() );
3677 if ( match.hasMatch() )
3678 {
3679 const QgsGeometry g = QgsGeometry::fromWkt( match.captured( 2 ) );
3680 if ( ! g.isNull() )
3681 {
3682 return ( anyTypeAllowed || mGeomTypes.contains( g.type() ) ) && ( mAllowMultipart || !g.isMultipart() );
3683 }
3684 else
3685 {
3686 QgsMessageLog::logMessage( QObject::tr( "Error creating geometry: \"%1\"" ).arg( g.lastError() ), QObject::tr( "Processing" ) );
3687 }
3688 }
3689 return false;
3690}
3691
3693{
3695 {
3696 if ( !crs.isValid() )
3698 else
3699 return QgsProcessingUtils::stringToPythonLiteral( QStringLiteral( "CRS=%1;%2" ).arg( crs.authid().isEmpty() ? crs.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) : crs.authid(), g.asWkt() ) );
3700 };
3701
3702 if ( !value.isValid() )
3703 return QStringLiteral( "None" );
3704
3705 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
3706 return QStringLiteral( "QgsProperty.fromExpression(%1)" ).arg( QgsProcessingUtils::stringToPythonLiteral( value.value< QgsProperty >().asExpression() ) );
3707
3708 if ( value.userType() == QMetaType::type( "QgsGeometry" ) )
3709 {
3710 const QgsGeometry g = value.value<QgsGeometry>();
3711 if ( !g.isNull() )
3712 return asPythonString( g );
3713 }
3714
3715 if ( value.userType() == QMetaType::type( "QgsReferencedGeometry" ) )
3716 {
3717 const QgsReferencedGeometry g = value.value<QgsReferencedGeometry>();
3718 if ( !g.isNull() )
3719 return asPythonString( g, g.crs() );
3720 }
3721
3722 if ( value.userType() == QMetaType::type( "QgsPointXY" ) )
3723 {
3724 const QgsGeometry g = QgsGeometry::fromPointXY( value.value<QgsPointXY>() );
3725 if ( !g.isNull() )
3726 return asPythonString( g );
3727 }
3728
3729 if ( value.userType() == QMetaType::type( "QgsReferencedPointXY" ) )
3730 {
3732 if ( !g.isNull() )
3733 return asPythonString( g, g.crs() );
3734 }
3735
3736 if ( value.userType() == QMetaType::type( "QgsRectangle" ) )
3737 {
3738 const QgsGeometry g = QgsGeometry::fromRect( value.value<QgsRectangle>() );
3739 if ( !g.isNull() )
3740 return asPythonString( g );
3741 }
3742
3743 if ( value.userType() == QMetaType::type( "QgsReferencedRectangle" ) )
3744 {
3746 if ( !g.isNull() )
3747 return asPythonString( g, g.crs() );
3748 }
3749
3751}
3752
3754{
3755 QString code = QStringLiteral( "##%1=" ).arg( mName );
3756 if ( mFlags & FlagOptional )
3757 code += QLatin1String( "optional " );
3758 code += type() + ' ';
3759
3760 for ( const int type : mGeomTypes )
3761 {
3762 switch ( static_cast<QgsWkbTypes::GeometryType>( type ) )
3763 {
3765 code += QLatin1String( "point " );
3766 break;
3767
3769 code += QLatin1String( "line " );
3770 break;
3771
3773 code += QLatin1String( "polygon " );
3774 break;
3775
3776 default:
3777 code += QLatin1String( "unknown " );
3778 break;
3779 }
3780 }
3781
3782 code += mDefault.toString();
3783 return code.trimmed();
3784}
3785
3787{
3788 switch ( outputType )
3789 {
3791 {
3792 QString code = QStringLiteral( "QgsProcessingParameterGeometry('%1', %2" )
3794 if ( mFlags & FlagOptional )
3795 code += QLatin1String( ", optional=True" );
3796
3797 if ( !mGeomTypes.empty() )
3798 {
3799 auto geomTypeToString = []( QgsWkbTypes::GeometryType t ) -> QString
3800 {
3801 switch ( t )
3802 {
3804 return QStringLiteral( "PointGeometry" );
3805
3807 return QStringLiteral( "LineGeometry" );
3808
3810 return QStringLiteral( "PolygonGeometry" );
3811
3813 return QStringLiteral( "UnknownGeometry" );
3814
3816 return QStringLiteral( "NullGeometry" );
3817 }
3818 return QString();
3819 };
3820
3821 QStringList options;
3822 options.reserve( mGeomTypes.size() );
3823 for ( const int type : mGeomTypes )
3824 {
3825 options << QStringLiteral( " QgsWkbTypes.%1" ).arg( geomTypeToString( static_cast<QgsWkbTypes::GeometryType>( type ) ) );
3826 }
3827 code += QStringLiteral( ", geometryTypes=[%1 ]" ).arg( options.join( ',' ) );
3828 }
3829
3830 if ( ! mAllowMultipart )
3831 {
3832 code += QLatin1String( ", allowMultipart=False" );
3833 }
3834
3836 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
3837 return code;
3838 }
3839 }
3840 return QString();
3841}
3842
3844{
3846 QVariantList types;
3847 for ( const int type : mGeomTypes )
3848 {
3849 types << type;
3850 }
3851 map.insert( QStringLiteral( "geometrytypes" ), types );
3852 map.insert( QStringLiteral( "multipart" ), mAllowMultipart );
3853 return map;
3854}
3855
3857{
3859 mGeomTypes.clear();
3860 const QVariantList values = map.value( QStringLiteral( "geometrytypes" ) ).toList();
3861 for ( const QVariant &val : values )
3862 {
3863 mGeomTypes << val.toInt();
3864 }
3865 mAllowMultipart = map.value( QStringLiteral( "multipart" ) ).toBool();
3866 return true;
3867}
3868
3869QgsProcessingParameterGeometry *QgsProcessingParameterGeometry::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3870{
3871 return new QgsProcessingParameterGeometry( name, description, definition, isOptional );
3872}
3873
3874QgsProcessingParameterFile::QgsProcessingParameterFile( const QString &name, const QString &description, Behavior behavior, const QString &extension, const QVariant &defaultValue, bool optional, const QString &fileFilter )
3875 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3876 , mBehavior( behavior )
3877 , mExtension( fileFilter.isEmpty() ? extension : QString() )
3878 , mFileFilter( fileFilter.isEmpty() && extension.isEmpty() ? QObject::tr( "All files (*.*)" ) : fileFilter )
3879{
3880
3881}
3882
3887
3889{
3890 QVariant input = v;
3891 if ( !input.isValid() )
3892 {
3893 if ( !defaultValue().isValid() )
3894 return mFlags & FlagOptional;
3895
3896 input = defaultValue();
3897 }
3898
3899 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
3900 {
3901 return true;
3902 }
3903
3904 const QString string = input.toString().trimmed();
3905
3906 if ( input.type() != QVariant::String || string.isEmpty() )
3907 return mFlags & FlagOptional;
3908
3909 switch ( mBehavior )
3910 {
3911 case File:
3912 {
3913 if ( !mExtension.isEmpty() )
3914 {
3915 return string.endsWith( mExtension, Qt::CaseInsensitive );
3916 }
3917 else if ( !mFileFilter.isEmpty() )
3918 {
3919 return QgsFileUtils::fileMatchesFilter( string, mFileFilter );
3920 }
3921 else
3922 {
3923 return true;
3924 }
3925 }
3926
3927 case Folder:
3928 return true;
3929 }
3930 return true;
3931}
3932
3934{
3935 QString code = QStringLiteral( "##%1=" ).arg( mName );
3936 if ( mFlags & FlagOptional )
3937 code += QLatin1String( "optional " );
3938 code += ( mBehavior == File ? QStringLiteral( "file" ) : QStringLiteral( "folder" ) ) + ' ';
3939 code += mDefault.toString();
3940 return code.trimmed();
3941}
3942
3944{
3945 switch ( outputType )
3946 {
3948 {
3949
3950 QString code = QStringLiteral( "QgsProcessingParameterFile('%1', %2" )
3952 if ( mFlags & FlagOptional )
3953 code += QLatin1String( ", optional=True" );
3954 code += QStringLiteral( ", behavior=%1" ).arg( mBehavior == File ? QStringLiteral( "QgsProcessingParameterFile.File" ) : QStringLiteral( "QgsProcessingParameterFile.Folder" ) );
3955 if ( !mExtension.isEmpty() )
3956 code += QStringLiteral( ", extension='%1'" ).arg( mExtension );
3957 if ( !mFileFilter.isEmpty() )
3958 code += QStringLiteral( ", fileFilter='%1'" ).arg( mFileFilter );
3960 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
3961 return code;
3962 }
3963 }
3964 return QString();
3965}
3966
3968{
3969 switch ( mBehavior )
3970 {
3971 case File:
3972 {
3973 if ( !mFileFilter.isEmpty() )
3974 return mFileFilter != QObject::tr( "All files (*.*)" ) ? mFileFilter + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" ) : mFileFilter;
3975 else if ( !mExtension.isEmpty() )
3976 return QObject::tr( "%1 files" ).arg( mExtension.toUpper() ) + QStringLiteral( " (*." ) + mExtension.toLower() + QStringLiteral( ");;" ) + QObject::tr( "All files (*.*)" );
3977 else
3978 return QObject::tr( "All files (*.*)" );
3979 }
3980
3981 case Folder:
3982 return QString();
3983 }
3984 return QString();
3985}
3986
3987void QgsProcessingParameterFile::setExtension( const QString &extension )
3988{
3989 mExtension = extension;
3990 mFileFilter.clear();
3991}
3992
3994{
3995 return mFileFilter;
3996}
3997
3999{
4000 mFileFilter = filter;
4001 mExtension.clear();
4002}
4003
4005{
4007 map.insert( QStringLiteral( "behavior" ), mBehavior );
4008 map.insert( QStringLiteral( "extension" ), mExtension );
4009 map.insert( QStringLiteral( "filefilter" ), mFileFilter );
4010 return map;
4011}
4012
4014{
4016 mBehavior = static_cast< Behavior >( map.value( QStringLiteral( "behavior" ) ).toInt() );
4017 mExtension = map.value( QStringLiteral( "extension" ) ).toString();
4018 mFileFilter = map.value( QStringLiteral( "filefilter" ) ).toString();
4019 return true;
4020}
4021
4022QgsProcessingParameterFile *QgsProcessingParameterFile::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition, QgsProcessingParameterFile::Behavior behavior )
4023{
4024 return new QgsProcessingParameterFile( name, description, behavior, QString(), definition, isOptional );
4025}
4026
4027QgsProcessingParameterMatrix::QgsProcessingParameterMatrix( const QString &name, const QString &description, int numberRows, bool fixedNumberRows, const QStringList &headers, const QVariant &defaultValue, bool optional )
4028 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4029 , mHeaders( headers )
4030 , mNumberRows( numberRows )
4031 , mFixedNumberRows( fixedNumberRows )
4032{
4033
4034}
4035
4040
4042{
4043 QVariant input = v;
4044 if ( !input.isValid() )
4045 {
4046 if ( !defaultValue().isValid() )
4047 return mFlags & FlagOptional;
4048
4049 input = defaultValue();
4050 }
4051
4052 if ( input.type() == QVariant::String )
4053 {
4054 if ( input.toString().isEmpty() )
4055 return mFlags & FlagOptional;
4056 return true;
4057 }
4058 else if ( input.type() == QVariant::List )
4059 {
4060 if ( input.toList().isEmpty() )
4061 return mFlags & FlagOptional;
4062 return true;
4063 }
4064 else if ( input.type() == QVariant::Double || input.type() == QVariant::Int )
4065 {
4066 return true;
4067 }
4068
4069 return false;
4070}
4071
4072QString QgsProcessingParameterMatrix::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
4073{
4074 if ( !value.isValid() )
4075 return QStringLiteral( "None" );
4076
4077 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
4078 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4079
4080 QVariantMap p;
4081 p.insert( name(), value );
4082 const QVariantList list = QgsProcessingParameters::parameterAsMatrix( this, p, context );
4083
4085}
4086
4088{
4089 switch ( outputType )
4090 {
4092 {
4093 QString code = QStringLiteral( "QgsProcessingParameterMatrix('%1', %2" )
4095 if ( mFlags & FlagOptional )
4096 code += QLatin1String( ", optional=True" );
4097 code += QStringLiteral( ", numberRows=%1" ).arg( mNumberRows );
4098 code += QStringLiteral( ", hasFixedNumberRows=%1" ).arg( mFixedNumberRows ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
4099
4100 QStringList headers;
4101 headers.reserve( mHeaders.size() );
4102 for ( const QString &h : mHeaders )
4104 code += QStringLiteral( ", headers=[%1]" ).arg( headers.join( ',' ) );
4105
4107 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
4108 return code;
4109 }
4110 }
4111 return QString();
4112}
4113
4115{
4116 return mHeaders;
4117}
4118
4119void QgsProcessingParameterMatrix::setHeaders( const QStringList &headers )
4120{
4121 mHeaders = headers;
4122}
4123
4125{
4126 return mNumberRows;
4127}
4128
4130{
4131 mNumberRows = numberRows;
4132}
4133
4135{
4136 return mFixedNumberRows;
4137}
4138
4140{
4141 mFixedNumberRows = fixedNumberRows;
4142}
4143
4145{
4147 map.insert( QStringLiteral( "headers" ), mHeaders );
4148 map.insert( QStringLiteral( "rows" ), mNumberRows );
4149 map.insert( QStringLiteral( "fixed_number_rows" ), mFixedNumberRows );
4150 return map;
4151}
4152
4154{
4156 mHeaders = map.value( QStringLiteral( "headers" ) ).toStringList();
4157 mNumberRows = map.value( QStringLiteral( "rows" ) ).toInt();
4158 mFixedNumberRows = map.value( QStringLiteral( "fixed_number_rows" ) ).toBool();
4159 return true;
4160}
4161
4162QgsProcessingParameterMatrix *QgsProcessingParameterMatrix::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4163{
4164 return new QgsProcessingParameterMatrix( name, description, 0, false, QStringList(), definition.isEmpty() ? QVariant() : definition, isOptional );
4165}
4166
4167QgsProcessingParameterMultipleLayers::QgsProcessingParameterMultipleLayers( const QString &name, const QString &description, QgsProcessing::SourceType layerType, const QVariant &defaultValue, bool optional )
4168 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4169 , mLayerType( layerType )
4170{
4171
4172}
4173
4178
4180{
4181 QVariant input = v;
4182 if ( !input.isValid() )
4183 {
4184 if ( !defaultValue().isValid() )
4185 return mFlags & FlagOptional;
4186
4187 input = defaultValue();
4188 }
4189
4190 if ( mLayerType != QgsProcessing::TypeFile )
4191 {
4192 if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
4193 {
4194 return true;
4195 }
4196 }
4197
4198 if ( input.type() == QVariant::String )
4199 {
4200 if ( input.toString().isEmpty() )
4201 return mFlags & FlagOptional;
4202
4203 if ( mMinimumNumberInputs > 1 )
4204 return false;
4205
4206 if ( !context )
4207 return true;
4208
4209 if ( mLayerType != QgsProcessing::TypeFile )
4210 return QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
4211 else
4212 return true;
4213 }
4214 else if ( input.type() == QVariant::List )
4215 {
4216 if ( input.toList().count() < mMinimumNumberInputs )
4217 return mFlags & FlagOptional;
4218
4219 if ( mMinimumNumberInputs > input.toList().count() )
4220 return false;
4221
4222 if ( !context )
4223 return true;
4224
4225 if ( mLayerType != QgsProcessing::TypeFile )
4226 {
4227 const auto constToList = input.toList();
4228 for ( const QVariant &v : constToList )
4229 {
4230 if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( v ) ) )
4231 continue;
4232
4233 if ( !QgsProcessingUtils::mapLayerFromString( v.toString(), *context ) )
4234 return false;
4235 }
4236 }
4237 return true;
4238 }
4239 else if ( input.type() == QVariant::StringList )
4240 {
4241 if ( input.toStringList().count() < mMinimumNumberInputs )
4242 return mFlags & FlagOptional;
4243
4244 if ( mMinimumNumberInputs > input.toStringList().count() )
4245 return false;
4246
4247 if ( !context )
4248 return true;
4249
4250 if ( mLayerType != QgsProcessing::TypeFile )
4251 {
4252 const auto constToStringList = input.toStringList();
4253 for ( const QString &v : constToStringList )
4254 {
4255 if ( !QgsProcessingUtils::mapLayerFromString( v, *context ) )
4256 return false;
4257 }
4258 }
4259 return true;
4260 }
4261 return false;
4262}
4263
4265{
4266 if ( !value.isValid() )
4267 return QStringLiteral( "None" );
4268
4269 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
4270 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4271
4272 if ( mLayerType == QgsProcessing::TypeFile )
4273 {
4274 QStringList parts;
4275 if ( value.type() == QVariant::StringList )
4276 {
4277 const QStringList list = value.toStringList();
4278 parts.reserve( list.count() );
4279 for ( const QString &v : list )
4281 }
4282 else if ( value.type() == QVariant::List )
4283 {
4284 const QVariantList list = value.toList();
4285 parts.reserve( list.count() );
4286 for ( const QVariant &v : list )
4287 parts << QgsProcessingUtils::stringToPythonLiteral( v.toString() );
4288 }
4289 if ( !parts.isEmpty() )
4290 return parts.join( ',' ).prepend( '[' ).append( ']' );
4291 }
4292 else
4293 {
4294 QVariantMap p;
4295 p.insert( name(), value );
4296 const QList<QgsMapLayer *> list = QgsProcessingParameters::parameterAsLayerList( this, p, context );
4297 if ( !list.isEmpty() )
4298 {
4299 QStringList parts;
4300 parts.reserve( list.count() );
4301 for ( const QgsMapLayer *layer : list )
4302 {
4304 }
4305 return parts.join( ',' ).prepend( '[' ).append( ']' );
4306 }
4307 }
4308
4310}
4311
4312QString QgsProcessingParameterMultipleLayers::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
4313{
4315}
4316
4318{
4320}
4321
4323{
4324 QString code = QStringLiteral( "##%1=" ).arg( mName );
4325 if ( mFlags & FlagOptional )
4326 code += QLatin1String( "optional " );
4327 switch ( mLayerType )
4328 {
4330 code += QLatin1String( "multiple raster" );
4331 break;
4332
4334 code += QLatin1String( "multiple file" );
4335 break;
4336
4337 default:
4338 code += QLatin1String( "multiple vector" );
4339 break;
4340 }
4341 code += ' ';
4342 if ( mDefault.type() == QVariant::List )
4343 {
4344 QStringList parts;
4345 const auto constToList = mDefault.toList();
4346 for ( const QVariant &var : constToList )
4347 {
4348 parts << var.toString();
4349 }
4350 code += parts.join( ',' );
4351 }
4352 else if ( mDefault.type() == QVariant::StringList )
4353 {
4354 code += mDefault.toStringList().join( ',' );
4355 }
4356 else
4357 {
4358 code += mDefault.toString();
4359 }
4360 return code.trimmed();
4361}
4362
4364{
4365 switch ( outputType )
4366 {
4368 {
4369 QString code = QStringLiteral( "QgsProcessingParameterMultipleLayers('%1', %2" )
4371 if ( mFlags & FlagOptional )
4372 code += QLatin1String( ", optional=True" );
4373
4374 const QString layerType = QStringLiteral( "QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( mLayerType ) );
4375
4376 code += QStringLiteral( ", layerType=%1" ).arg( layerType );
4378 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
4379 return code;
4380 }
4381 }
4382 return QString();
4383}
4384
4386{
4387 const QStringList exts;
4388 switch ( mLayerType )
4389 {
4391 return QObject::tr( "All files (*.*)" );
4392
4394 return QgsProviderRegistry::instance()->fileRasterFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
4395
4401 return QgsProviderRegistry::instance()->fileVectorFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
4402
4404 return QgsProviderRegistry::instance()->fileMeshFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
4405
4407 return QgsProviderRegistry::instance()->filePointCloudFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
4408
4413 }
4414 return QString();
4415}
4416
4421
4426
4428{
4429 return mMinimumNumberInputs;
4430}
4431
4433{
4434 if ( mMinimumNumberInputs >= 1 || !( flags() & QgsProcessingParameterDefinition::FlagOptional ) )
4435 mMinimumNumberInputs = minimumNumberInputs;
4436}
4437
4439{
4441 map.insert( QStringLiteral( "layer_type" ), mLayerType );
4442 map.insert( QStringLiteral( "min_inputs" ), mMinimumNumberInputs );
4443 return map;
4444}
4445
4447{
4449 mLayerType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "layer_type" ) ).toInt() );
4450 mMinimumNumberInputs = map.value( QStringLiteral( "min_inputs" ) ).toInt();
4451 return true;
4452}
4453
4454QgsProcessingParameterMultipleLayers *QgsProcessingParameterMultipleLayers::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4455{
4456 QString type = definition;
4457 QString defaultVal;
4458 const QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)" ) );
4459 const QRegularExpressionMatch m = re.match( definition );
4460 if ( m.hasMatch() )
4461 {
4462 type = m.captured( 1 ).toLower().trimmed();
4463 defaultVal = m.captured( 2 );
4464 }
4466 if ( type == QLatin1String( "vector" ) )
4468 else if ( type == QLatin1String( "raster" ) )
4470 else if ( type == QLatin1String( "file" ) )
4472 return new QgsProcessingParameterMultipleLayers( name, description, layerType, defaultVal.isEmpty() ? QVariant() : defaultVal, isOptional );
4473}
4474
4475QgsProcessingParameterNumber::QgsProcessingParameterNumber( const QString &name, const QString &description, Type type, const QVariant &defaultValue, bool optional, double minValue, double maxValue )
4476 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4477 , mMin( minValue )
4478 , mMax( maxValue )
4479 , mDataType( type )
4480{
4481 if ( mMin >= mMax )
4482 {
4483 QgsMessageLog::logMessage( QObject::tr( "Invalid number parameter \"%1\": min value %2 is >= max value %3!" ).arg( name ).arg( mMin ).arg( mMax ), QObject::tr( "Processing" ) );
4484 }
4485}
4486
4491
4493{
4494 QVariant input = value;
4495 if ( !input.isValid() )
4496 {
4497 if ( !defaultValue().isValid() )
4498 return mFlags & FlagOptional;
4499
4500 input = defaultValue();
4501 }
4502
4503 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
4504 {
4505 return true;
4506 }
4507
4508 bool ok = false;
4509 const double res = input.toDouble( &ok );
4510 if ( !ok )
4511 return mFlags & FlagOptional;
4512
4513 return !( res < mMin || res > mMax );
4514}
4515
4517{
4518 if ( !value.isValid() )
4519 return QStringLiteral( "None" );
4520
4521 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
4522 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4523
4524 return value.toString();
4525}
4526
4528{
4530 QStringList parts;
4531 if ( mMin > std::numeric_limits<double>::lowest() + 1 )
4532 parts << QObject::tr( "Minimum value: %1" ).arg( mMin );
4533 if ( mMax < std::numeric_limits<double>::max() )
4534 parts << QObject::tr( "Maximum value: %1" ).arg( mMax );
4535 if ( mDefault.isValid() )
4536 parts << QObject::tr( "Default value: %1" ).arg( mDataType == Integer ? mDefault.toInt() : mDefault.toDouble() );
4537 const QString extra = parts.join( QLatin1String( "<br />" ) );
4538 if ( !extra.isEmpty() )
4539 text += QStringLiteral( "<p>%1</p>" ).arg( extra );
4540 return text;
4541}
4542
4544{
4545 switch ( outputType )
4546 {
4548 {
4549 QString code = QStringLiteral( "QgsProcessingParameterNumber('%1', %2" )
4551 if ( mFlags & FlagOptional )
4552 code += QLatin1String( ", optional=True" );
4553
4554 code += QStringLiteral( ", type=%1" ).arg( mDataType == Integer ? QStringLiteral( "QgsProcessingParameterNumber.Integer" ) : QStringLiteral( "QgsProcessingParameterNumber.Double" ) );
4555
4556 if ( mMin != std::numeric_limits<double>::lowest() + 1 )
4557 code += QStringLiteral( ", minValue=%1" ).arg( mMin );
4558 if ( mMax != std::numeric_limits<double>::max() )
4559 code += QStringLiteral( ", maxValue=%1" ).arg( mMax );
4561 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
4562 return code;
4563 }
4564 }
4565 return QString();
4566}
4567
4569{
4570 return mMin;
4571}
4572
4574{
4575 mMin = min;
4576}
4577
4579{
4580 return mMax;
4581}
4582
4584{
4585 mMax = max;
4586}
4587
4592
4594{
4595 mDataType = dataType;
4596}
4597
4599{
4601 map.insert( QStringLiteral( "min" ), mMin );
4602 map.insert( QStringLiteral( "max" ), mMax );
4603 map.insert( QStringLiteral( "data_type" ), mDataType );
4604 return map;
4605}
4606
4608{
4610 mMin = map.value( QStringLiteral( "min" ) ).toDouble();
4611 mMax = map.value( QStringLiteral( "max" ) ).toDouble();
4612 mDataType = static_cast< Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
4613 return true;
4614}
4615
4616QgsProcessingParameterNumber *QgsProcessingParameterNumber::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4617{
4618 return new QgsProcessingParameterNumber( name, description, Double, definition.isEmpty() ? QVariant()
4619 : ( definition.toLower().trimmed() == QLatin1String( "none" ) ? QVariant() : definition ), isOptional );
4620}
4621
4622QgsProcessingParameterRange::QgsProcessingParameterRange( const QString &name, const QString &description, QgsProcessingParameterNumber::Type type, const QVariant &defaultValue, bool optional )
4623 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4624 , mDataType( type )
4625{
4626
4627}
4628
4633
4635{
4636 QVariant input = v;
4637 if ( !input.isValid() )
4638 {
4639 if ( !defaultValue().isValid() )
4640 return mFlags & FlagOptional;
4641
4642 input = defaultValue();
4643 }
4644
4645 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
4646 {
4647 return true;
4648 }
4649
4650 if ( input.type() == QVariant::String )
4651 {
4652 const QStringList list = input.toString().split( ',' );
4653 if ( list.count() != 2 )
4654 return mFlags & FlagOptional;
4655 bool ok = false;
4656 list.at( 0 ).toDouble( &ok );
4657 bool ok2 = false;
4658 list.at( 1 ).toDouble( &ok2 );
4659 if ( !ok || !ok2 )
4660 return mFlags & FlagOptional;
4661 return true;
4662 }
4663 else if ( input.type() == QVariant::List )
4664 {
4665 if ( input.toList().count() != 2 )
4666 return mFlags & FlagOptional;
4667
4668 bool ok = false;
4669 input.toList().at( 0 ).toDouble( &ok );
4670 bool ok2 = false;
4671 input.toList().at( 1 ).toDouble( &ok2 );
4672 if ( !ok || !ok2 )
4673 return mFlags & FlagOptional;
4674 return true;
4675 }
4676
4677 return false;
4678}
4679
4680QString QgsProcessingParameterRange::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
4681{
4682 if ( !value.isValid() )
4683 return QStringLiteral( "None" );
4684
4685 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
4686 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4687
4688 QVariantMap p;
4689 p.insert( name(), value );
4690 const QList< double > parts = QgsProcessingParameters::parameterAsRange( this, p, context );
4691
4692 QStringList stringParts;
4693 const auto constParts = parts;
4694 for ( const double v : constParts )
4695 {
4696 stringParts << QString::number( v );
4697 }
4698 return stringParts.join( ',' ).prepend( '[' ).append( ']' );
4699}
4700
4702{
4703 switch ( outputType )
4704 {
4706 {
4707 QString code = QStringLiteral( "QgsProcessingParameterRange('%1', %2" )
4709 if ( mFlags & FlagOptional )
4710 code += QLatin1String( ", optional=True" );
4711
4712 code += QStringLiteral( ", type=%1" ).arg( mDataType == QgsProcessingParameterNumber::Integer ? QStringLiteral( "QgsProcessingParameterNumber.Integer" ) : QStringLiteral( "QgsProcessingParameterNumber.Double" ) );
4713
4715 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
4716 return code;
4717 }
4718 }
4719 return QString();
4720}
4721
4726
4731
4733{
4735 map.insert( QStringLiteral( "data_type" ), mDataType );
4736 return map;
4737}
4738
4740{
4742 mDataType = static_cast< QgsProcessingParameterNumber::Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
4743 return true;
4744}
4745
4746QgsProcessingParameterRange *QgsProcessingParameterRange::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4747{
4748 return new QgsProcessingParameterRange( name, description, QgsProcessingParameterNumber::Double, definition.isEmpty() ? QVariant()
4749 : ( definition.toLower().trimmed() == QLatin1String( "none" ) ? QVariant() : definition ), isOptional );
4750}
4751
4752QgsProcessingParameterRasterLayer::QgsProcessingParameterRasterLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
4753 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4754{
4755
4756}
4757
4762
4764{
4765 QVariant input = v;
4766 if ( !input.isValid() )
4767 {
4768 if ( !defaultValue().isValid() )
4769 return mFlags & FlagOptional;
4770
4771 input = defaultValue();
4772 }
4773
4774 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
4775 {
4776 return true;
4777 }
4778
4779 if ( qobject_cast< QgsRasterLayer * >( qvariant_cast<QObject *>( input ) ) )
4780 return true;
4781
4782 if ( input.type() != QVariant::String || input.toString().isEmpty() )
4783 return mFlags & FlagOptional;
4784
4785 if ( !context )
4786 {
4787 // that's as far as we can get without a context
4788 return true;
4789 }
4790
4791 // try to load as layer
4792 if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context, true, QgsProcessingUtils::LayerHint::Raster ) )
4793 return true;
4794
4795 return false;
4796}
4797
4799{
4800 if ( !val.isValid() )
4801 return QStringLiteral( "None" );
4802
4803 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
4804 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
4805
4806 QVariantMap p;
4807 p.insert( name(), val );
4811}
4812
4813QString QgsProcessingParameterRasterLayer::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
4814{
4816}
4817
4819{
4821}
4822
4824{
4825 return QgsProviderRegistry::instance()->fileRasterFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
4826}
4827
4828QgsProcessingParameterRasterLayer *QgsProcessingParameterRasterLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4829{
4830 return new QgsProcessingParameterRasterLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
4831}
4832
4833QgsProcessingParameterEnum::QgsProcessingParameterEnum( const QString &name, const QString &description, const QStringList &options, bool allowMultiple, const QVariant &defaultValue, bool optional, bool usesStaticStrings )
4834 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4835 , mOptions( options )
4836 , mAllowMultiple( allowMultiple )
4837 , mUsesStaticStrings( usesStaticStrings )
4838{
4839
4840}
4841
4846
4848{
4849 QVariant input = value;
4850 if ( !input.isValid() )
4851 {
4852 if ( !defaultValue().isValid() )
4853 return mFlags & FlagOptional;
4854
4855 input = defaultValue();
4856 }
4857
4858 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
4859 {
4860 return true;
4861 }
4862
4863 if ( mUsesStaticStrings )
4864 {
4865 if ( input.type() == QVariant::List )
4866 {
4867 if ( !mAllowMultiple )
4868 return false;
4869
4870 const QVariantList values = input.toList();
4871 if ( values.empty() && !( mFlags & FlagOptional ) )
4872 return false;
4873
4874 for ( const QVariant &val : values )
4875 {
4876 if ( !mOptions.contains( val.toString() ) )
4877 return false;
4878 }
4879
4880 return true;
4881 }
4882 else if ( input.type() == QVariant::StringList )
4883 {
4884 if ( !mAllowMultiple )
4885 return false;
4886
4887 const QStringList values = input.toStringList();
4888
4889 if ( values.empty() && !( mFlags & FlagOptional ) )
4890 return false;
4891
4892 if ( values.count() > 1 && !mAllowMultiple )
4893 return false;
4894
4895 for ( const QString &val : values )
4896 {
4897 if ( !mOptions.contains( val ) )
4898 return false;
4899 }
4900 return true;
4901 }
4902 else if ( input.type() == QVariant::String )
4903 {
4904 const QStringList parts = input.toString().split( ',' );
4905 if ( parts.count() > 1 && !mAllowMultiple )
4906 return false;
4907
4908 const auto constParts = parts;
4909 for ( const QString &part : constParts )
4910 {
4911 if ( !mOptions.contains( part ) )
4912 return false;
4913 }
4914 return true;
4915 }
4916 }
4917 else
4918 {
4919 if ( input.type() == QVariant::List )
4920 {
4921 if ( !mAllowMultiple )
4922 return false;
4923
4924 const QVariantList values = input.toList();
4925 if ( values.empty() && !( mFlags & FlagOptional ) )
4926 return false;
4927
4928 for ( const QVariant &val : values )
4929 {
4930 bool ok = false;
4931 const int res = val.toInt( &ok );
4932 if ( !ok )
4933 return false;
4934 else if ( res < 0 || res >= mOptions.count() )
4935 return false;
4936 }
4937
4938 return true;
4939 }
4940 else if ( input.type() == QVariant::String )
4941 {
4942 const QStringList parts = input.toString().split( ',' );
4943 if ( parts.count() > 1 && !mAllowMultiple )
4944 return false;
4945
4946 const auto constParts = parts;
4947 for ( const QString &part : constParts )
4948 {
4949 bool ok = false;
4950 const int res = part.toInt( &ok );
4951 if ( !ok )
4952 return false;
4953 else if ( res < 0 || res >= mOptions.count() )
4954 return false;
4955 }
4956 return true;
4957 }
4958 else if ( input.type() == QVariant::Int || input.type() == QVariant::Double )
4959 {
4960 bool ok = false;
4961 const int res = input.toInt( &ok );
4962 if ( !ok )
4963 return false;
4964 else if ( res >= 0 && res < mOptions.count() )
4965 return true;
4966 }
4967 }
4968
4969 return false;
4970}
4971
4973{
4974 if ( !value.isValid() )
4975 return QStringLiteral( "None" );
4976
4977 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
4978 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4979
4980 if ( mUsesStaticStrings )
4981 {
4982 if ( value.type() == QVariant::List || value.type() == QVariant::StringList )
4983 {
4984 QStringList parts;
4985 const QStringList constList = value.toStringList();
4986 for ( const QString &val : constList )
4987 {
4989 }
4990 return parts.join( ',' ).prepend( '[' ).append( ']' );
4991 }
4992 else if ( value.type() == QVariant::String )
4993 {
4994 QStringList parts;
4995 const QStringList constList = value.toString().split( ',' );
4996 if ( constList.count() > 1 )
4997 {
4998 for ( const QString &val : constList )
4999 {
5001 }
5002 return parts.join( ',' ).prepend( '[' ).append( ']' );
5003 }
5004 }
5005
5006 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
5007 }
5008 else
5009 {
5010 if ( value.type() == QVariant::List )
5011 {
5012 QStringList parts;
5013 const auto constToList = value.toList();
5014 for ( const QVariant &val : constToList )
5015 {
5016 parts << QString::number( static_cast< int >( val.toDouble() ) );
5017 }
5018 return parts.join( ',' ).prepend( '[' ).append( ']' );
5019 }
5020 else if ( value.type() == QVariant::String )
5021 {
5022 const QStringList parts = value.toString().split( ',' );
5023 if ( parts.count() > 1 )
5024 {
5025 return parts.join( ',' ).prepend( '[' ).append( ']' );
5026 }
5027 }
5028
5029 return QString::number( static_cast< int >( value.toDouble() ) );
5030 }
5031}
5032
5034{
5035 if ( !value.isValid() )
5036 return QString();
5037
5038 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
5039 return QString();
5040
5041 if ( mUsesStaticStrings )
5042 {
5043 return QString();
5044 }
5045 else
5046 {
5047 if ( value.type() == QVariant::List )
5048 {
5049 QStringList parts;
5050 const QVariantList toList = value.toList();
5051 parts.reserve( toList.size() );
5052 for ( const QVariant &val : toList )
5053 {
5054 parts << mOptions.value( static_cast< int >( val.toDouble() ) );
5055 }
5056 return parts.join( ',' );
5057 }
5058 else if ( value.type() == QVariant::String )
5059 {
5060 const QStringList parts = value.toString().split( ',' );
5061 QStringList comments;
5062 if ( parts.count() > 1 )
5063 {
5064 for ( const QString &part : parts )
5065 {
5066 bool ok = false;
5067 const int val = part.toInt( &ok );
5068 if ( ok )
5069 comments << mOptions.value( val );
5070 }
5071 return comments.join( ',' );
5072 }
5073 }
5074
5075 return mOptions.value( static_cast< int >( value.toDouble() ) );
5076 }
5077}
5078
5080{
5081 QString code = QStringLiteral( "##%1=" ).arg( mName );
5082 if ( mFlags & FlagOptional )
5083 code += QLatin1String( "optional " );
5084 code += QLatin1String( "enum " );
5085
5086 if ( mAllowMultiple )
5087 code += QLatin1String( "multiple " );
5088
5089 if ( mUsesStaticStrings )
5090 code += QLatin1String( "static " );
5091
5092 code += mOptions.join( ';' ) + ' ';
5093
5094 code += mDefault.toString();
5095 return code.trimmed();
5096}
5097
5099{
5100 switch ( outputType )
5101 {
5103 {
5104 QString code = QStringLiteral( "QgsProcessingParameterEnum('%1', %2" )
5106 if ( mFlags & FlagOptional )
5107 code += QLatin1String( ", optional=True" );
5108
5109 QStringList options;
5110 options.reserve( mOptions.size() );
5111 for ( const QString &o : mOptions )
5113 code += QStringLiteral( ", options=[%1]" ).arg( options.join( ',' ) );
5114
5115 code += QStringLiteral( ", allowMultiple=%1" ).arg( mAllowMultiple ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
5116
5117 code += QStringLiteral( ", usesStaticStrings=%1" ).arg( mUsesStaticStrings ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
5118
5120 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
5121
5122 return code;
5123 }
5124 }
5125 return QString();
5126}
5127
5129{
5130 return mOptions;
5131}
5132
5133void QgsProcessingParameterEnum::setOptions( const QStringList &options )
5134{
5135 mOptions = options;
5136}
5137
5139{
5140 return mAllowMultiple;
5141}
5142
5144{
5145 mAllowMultiple = allowMultiple;
5146}
5147
5149{
5150 return mUsesStaticStrings;
5151}
5152
5154{
5155 mUsesStaticStrings = usesStaticStrings;
5156}
5157
5159{
5161 map.insert( QStringLiteral( "options" ), mOptions );
5162 map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
5163 map.insert( QStringLiteral( "uses_static_strings" ), mUsesStaticStrings );
5164 return map;
5165}
5166
5168{
5170 mOptions = map.value( QStringLiteral( "options" ) ).toStringList();
5171 mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
5172 mUsesStaticStrings = map.value( QStringLiteral( "uses_static_strings" ) ).toBool();
5173 return true;
5174}
5175
5176QgsProcessingParameterEnum *QgsProcessingParameterEnum::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5177{
5178 QString defaultVal;
5179 QString def = definition;
5180
5181 bool multiple = false;
5182 if ( def.startsWith( QLatin1String( "multiple" ), Qt::CaseInsensitive ) )
5183 {
5184 multiple = true;
5185 def = def.mid( 9 );
5186 }
5187
5188 bool staticStrings = false;
5189 if ( def.startsWith( QLatin1String( "static" ), Qt::CaseInsensitive ) )
5190 {
5191 staticStrings = true;
5192 def = def.mid( 7 );
5193 }
5194
5195 const QRegularExpression re( QStringLiteral( "(.*)\\s+(.*?)$" ) );
5196 const QRegularExpressionMatch m = re.match( def );
5197 QString values = def;
5198 if ( m.hasMatch() )
5199 {
5200 values = m.captured( 1 ).trimmed();
5201 defaultVal = m.captured( 2 );
5202 }
5203
5204 return new QgsProcessingParameterEnum( name, description, values.split( ';' ), multiple, defaultVal.isEmpty() ? QVariant() : defaultVal, isOptional, staticStrings );
5205}
5206
5207QgsProcessingParameterString::QgsProcessingParameterString( const QString &name, const QString &description, const QVariant &defaultValue, bool multiLine, bool optional )
5208 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5209 , mMultiLine( multiLine )
5210{
5211
5212}
5213
5218
5220{
5221 if ( QgsVariantUtils::isNull( value ) )
5222 return QStringLiteral( "None" );
5223
5224 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
5225 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
5226
5227 const QString s = value.toString();
5229}
5230
5232{
5233 QString code = QStringLiteral( "##%1=" ).arg( mName );
5234 if ( mFlags & FlagOptional )
5235 code += QLatin1String( "optional " );
5236 code += QLatin1String( "string " );
5237
5238 if ( mMultiLine )
5239 code += QLatin1String( "long " );
5240
5241 code += mDefault.toString();
5242 return code.trimmed();
5243}
5244
5246{
5247 switch ( outputType )
5248 {
5250 {
5251 QString code = QStringLiteral( "QgsProcessingParameterString('%1', %2" )
5253 if ( mFlags & FlagOptional )
5254 code += QLatin1String( ", optional=True" );
5255 code += QStringLiteral( ", multiLine=%1" ).arg( mMultiLine ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
5256
5258 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
5259 return code;
5260 }
5261 }
5262 return QString();
5263}
5264
5266{
5267 return mMultiLine;
5268}
5269
5271{
5272 mMultiLine = multiLine;
5273}
5274
5276{
5278 map.insert( QStringLiteral( "multiline" ), mMultiLine );
5279 return map;
5280}
5281
5283{
5285 mMultiLine = map.value( QStringLiteral( "multiline" ) ).toBool();
5286 return true;
5287}
5288
5289QgsProcessingParameterString *QgsProcessingParameterString::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5290{
5291 QString def = definition;
5292 bool multiLine = false;
5293 if ( def.startsWith( QLatin1String( "long" ), Qt::CaseInsensitive ) )
5294 {
5295 multiLine = true;
5296 def = def.mid( 5 );
5297 }
5298
5299 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
5300 def = def.mid( 1 );
5301 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
5302 def.chop( 1 );
5303
5304 QVariant defaultValue = def;
5305 if ( def == QLatin1String( "None" ) )
5306 defaultValue = QVariant();
5307
5309}
5310
5311//
5312// QgsProcessingParameterAuthConfig
5313//
5314
5315QgsProcessingParameterAuthConfig::QgsProcessingParameterAuthConfig( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
5316 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5317{
5318
5319}
5320
5325
5327{
5328 if ( !value.isValid() )
5329 return QStringLiteral( "None" );
5330
5331 const QString s = value.toString();
5333}
5334
5336{
5337 QString code = QStringLiteral( "##%1=" ).arg( mName );
5338 if ( mFlags & FlagOptional )
5339 code += QLatin1String( "optional " );
5340 code += QLatin1String( "authcfg " );
5341
5342 code += mDefault.toString();
5343 return code.trimmed();
5344}
5345
5346QgsProcessingParameterAuthConfig *QgsProcessingParameterAuthConfig::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5347{
5348 QString def = definition;
5349
5350 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
5351 def = def.mid( 1 );
5352 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
5353 def.chop( 1 );
5354
5355 QVariant defaultValue = def;
5356 if ( def == QLatin1String( "None" ) )
5357 defaultValue = QVariant();
5358
5360}
5361
5362
5363//
5364// QgsProcessingParameterExpression
5365//
5366
5367QgsProcessingParameterExpression::QgsProcessingParameterExpression( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional )
5368 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5369 , mParentLayerParameterName( parentLayerParameterName )
5370{
5371
5372}
5373
5378
5380{
5381 if ( !value.isValid() )
5382 return QStringLiteral( "None" );
5383
5384 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
5385 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
5386
5387 const QString s = value.toString();
5389}
5390
5392{
5393 QStringList depends;
5394 if ( !mParentLayerParameterName.isEmpty() )
5395 depends << mParentLayerParameterName;
5396 return depends;
5397}
5398
5400{
5401 switch ( outputType )
5402 {
5404 {
5405 QString code = QStringLiteral( "QgsProcessingParameterExpression('%1', %2" )
5407 if ( mFlags & FlagOptional )
5408 code += QLatin1String( ", optional=True" );
5409
5410 code += QStringLiteral( ", parentLayerParameterName='%1'" ).arg( mParentLayerParameterName );
5411
5413 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
5414 return code;
5415 }
5416 }
5417 return QString();
5418}
5419
5421{
5422 return mParentLayerParameterName;
5423}
5424
5425void QgsProcessingParameterExpression::setParentLayerParameterName( const QString &parentLayerParameterName )
5426{
5427 mParentLayerParameterName = parentLayerParameterName;
5428}
5429
5431{
5433 map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
5434 return map;
5435}
5436
5438{
5440 mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
5441 return true;
5442}
5443
5444QgsProcessingParameterExpression *QgsProcessingParameterExpression::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5445{
5446 return new QgsProcessingParameterExpression( name, description, definition, QString(), isOptional );
5447}
5448
5449QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
5450 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5452{
5453
5454}
5455
5460
5462{
5463 QVariant var = v;
5464 if ( !var.isValid() )
5465 {
5466 if ( !defaultValue().isValid() )
5467 return mFlags & FlagOptional;
5468
5469 var = defaultValue();
5470 }
5471
5472 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
5473 {
5474 const QgsProperty p = var.value< QgsProperty >();
5476 {
5477 var = p.staticValue();
5478 }
5479 else
5480 {
5481 return true;
5482 }
5483 }
5484
5485 if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( var ) ) )
5486 return true;
5487
5488 if ( var.type() != QVariant::String || var.toString().isEmpty() )
5489 return mFlags & FlagOptional;
5490
5491 if ( !context )
5492 {
5493 // that's as far as we can get without a context
5494 return true;
5495 }
5496
5497 // try to load as layer
5499 return true;
5500
5501 return false;
5502}
5503
5505{
5506 if ( !val.isValid() )
5507 return QStringLiteral( "None" );
5508
5509 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
5510 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
5511
5512 QVariantMap p;
5513 p.insert( name(), val );
5517}
5518
5519QString QgsProcessingParameterVectorLayer::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
5520{
5522}
5523
5525{
5527}
5528
5530{
5531 switch ( outputType )
5532 {
5534 {
5535 QString code = QStringLiteral( "QgsProcessingParameterVectorLayer('%1', %2" )
5537 if ( mFlags & FlagOptional )
5538 code += QLatin1String( ", optional=True" );
5539
5540 if ( !mDataTypes.empty() )
5541 {
5542 QStringList options;
5543 for ( const int t : mDataTypes )
5544 options << QStringLiteral( "QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( static_cast< QgsProcessing::SourceType >( t ) ) );
5545 code += QStringLiteral( ", types=[%1]" ).arg( options.join( ',' ) );
5546 }
5547
5549 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
5550 return code;
5551 }
5552 }
5553 return QString();
5554}
5555
5557{
5558 return QgsProviderRegistry::instance()->fileVectorFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
5559}
5560
5562{
5563 return mDataTypes;
5564}
5565
5567{
5568 mDataTypes = types;
5569}
5570
5572{
5574 QVariantList types;
5575 for ( const int type : mDataTypes )
5576 {
5577 types << type;
5578 }
5579 map.insert( QStringLiteral( "data_types" ), types );
5580 return map;
5581}
5582
5584{
5586 mDataTypes.clear();
5587 const QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
5588 for ( const QVariant &val : values )
5589 {
5590 mDataTypes << val.toInt();
5591 }
5592 return true;
5593}
5594
5595QgsProcessingParameterVectorLayer *QgsProcessingParameterVectorLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5596{
5597 return new QgsProcessingParameterVectorLayer( name, description, QList< int>(), definition.isEmpty() ? QVariant() : definition, isOptional );
5598}
5599
5600QgsProcessingParameterMeshLayer::QgsProcessingParameterMeshLayer( const QString &name, const QString &description,
5601 const QVariant &defaultValue, bool optional )
5602 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5603{
5604
5605}
5606
5611
5613{
5614 QVariant var = v;
5615
5616 if ( !var.isValid() )
5617 {
5618 if ( !defaultValue().isValid() )
5619 return mFlags & FlagOptional;
5620
5621 var = defaultValue();
5622 }
5623
5624 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
5625 {
5626 const QgsProperty p = var.value< QgsProperty >();
5628 {
5629 var = p.staticValue();
5630 }
5631 else
5632 {
5633 return true;
5634 }
5635 }
5636
5637 if ( qobject_cast< QgsMeshLayer * >( qvariant_cast<QObject *>( var ) ) )
5638 return true;
5639
5640 if ( var.type() != QVariant::String || var.toString().isEmpty() )
5641 return mFlags & FlagOptional;
5642
5643 if ( !context )
5644 {
5645 // that's as far as we can get without a context
5646 return true;
5647 }
5648
5649 // try to load as layer
5650 if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::LayerHint::Mesh ) )
5651 return true;
5652
5653 return false;
5654}
5655
5657{
5658 if ( !val.isValid() )
5659 return QStringLiteral( "None" );
5660
5661 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
5662 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
5663
5664 QVariantMap p;
5665 p.insert( name(), val );
5669}
5670
5671QString QgsProcessingParameterMeshLayer::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
5672{
5674}
5675
5676QVariant QgsProcessingParameterMeshLayer::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
5677{
5679}
5680
5682{
5683 return QgsProviderRegistry::instance()->fileMeshFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
5684}
5685
5686QgsProcessingParameterMeshLayer *QgsProcessingParameterMeshLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5687{
5688 return new QgsProcessingParameterMeshLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
5689}
5690
5691QgsProcessingParameterField::QgsProcessingParameterField( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, DataType type, bool allowMultiple, bool optional, bool defaultToAllFields )
5692 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5693 , mParentLayerParameterName( parentLayerParameterName )
5694 , mDataType( type )
5695 , mAllowMultiple( allowMultiple )
5696 , mDefaultToAllFields( defaultToAllFields )
5697{
5698
5699}
5700
5701
5706
5708{
5709 QVariant input = v;
5710 if ( !input.isValid() )
5711 {
5712 if ( !defaultValue().isValid() )
5713 return mFlags & FlagOptional;
5714
5715 input = defaultValue();
5716 }
5717
5718 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
5719 {
5720 return true;
5721 }
5722
5723 if ( input.type() == QVariant::List || input.type() == QVariant::StringList )
5724 {
5725 if ( !mAllowMultiple )
5726 return false;
5727
5728 if ( input.toList().isEmpty() && !( mFlags & FlagOptional ) )
5729 return false;
5730 }
5731 else if ( input.type() == QVariant::String )
5732 {
5733 if ( input.toString().isEmpty() )
5734 return mFlags & FlagOptional;
5735
5736 const QStringList parts = input.toString().split( ';' );
5737 if ( parts.count() > 1 && !mAllowMultiple )
5738 return false;
5739 }
5740 else
5741 {
5742 if ( input.toString().isEmpty() )
5743 return mFlags & FlagOptional;
5744 }
5745 return true;
5746}
5747
5749{
5750 if ( !value.isValid() )
5751 return QStringLiteral( "None" );
5752
5753 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
5754 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
5755
5756 if ( value.type() == QVariant::List )
5757 {
5758 QStringList parts;
5759 const auto constToList = value.toList();
5760 for ( const QVariant &val : constToList )
5761 {
5762 parts << QgsProcessingUtils::stringToPythonLiteral( val.toString() );
5763 }
5764 return parts.join( ',' ).prepend( '[' ).append( ']' );
5765 }
5766 else if ( value.type() == QVariant::StringList )
5767 {
5768 QStringList parts;
5769 const auto constToStringList = value.toStringList();
5770 for ( const QString &s : constToStringList )
5771 {
5773 }
5774 return parts.join( ',' ).prepend( '[' ).append( ']' );
5775 }
5776
5777 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
5778}
5779
5781{
5782 QString code = QStringLiteral( "##%1=" ).arg( mName );
5783 if ( mFlags & FlagOptional )
5784 code += QLatin1String( "optional " );
5785 code += QLatin1String( "field " );
5786
5787 switch ( mDataType )
5788 {
5789 case Numeric:
5790 code += QLatin1String( "numeric " );
5791 break;
5792
5793 case String:
5794 code += QLatin1String( "string " );
5795 break;
5796
5797 case DateTime:
5798 code += QLatin1String( "datetime " );
5799 break;
5800
5801 case Any:
5802 break;
5803 }
5804
5805 if ( mAllowMultiple )
5806 code += QLatin1String( "multiple " );
5807
5808 if ( mDefaultToAllFields )
5809 code += QLatin1String( "default_to_all_fields " );
5810
5811 code += mParentLayerParameterName + ' ';
5812
5813 code += mDefault.toString();
5814 return code.trimmed();
5815}
5816
5818{
5819 switch ( outputType )
5820 {
5822 {
5823 QString code = QStringLiteral( "QgsProcessingParameterField('%1', %2" )
5825 if ( mFlags & FlagOptional )
5826 code += QLatin1String( ", optional=True" );
5827
5828 QString dataType;
5829 switch ( mDataType )
5830 {
5831 case Any:
5832 dataType = QStringLiteral( "QgsProcessingParameterField.Any" );
5833 break;
5834
5835 case Numeric:
5836 dataType = QStringLiteral( "QgsProcessingParameterField.Numeric" );
5837 break;
5838
5839 case String:
5840 dataType = QStringLiteral( "QgsProcessingParameterField.String" );
5841 break;
5842
5843 case DateTime:
5844 dataType = QStringLiteral( "QgsProcessingParameterField.DateTime" );
5845 break;
5846 }
5847 code += QStringLiteral( ", type=%1" ).arg( dataType );
5848
5849 code += QStringLiteral( ", parentLayerParameterName='%1'" ).arg( mParentLayerParameterName );
5850 code += QStringLiteral( ", allowMultiple=%1" ).arg( mAllowMultiple ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
5852 code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );
5853
5854 if ( mDefaultToAllFields )
5855 code += QLatin1String( ", defaultToAllFields=True" );
5856
5857 code += ')';
5858
5859 return code;
5860 }
5861 }
5862 return QString();
5863}
5864
5866{
5867 QStringList depends;
5868 if ( !mParentLayerParameterName.isEmpty() )
5869 depends << mParentLayerParameterName;
5870 return depends;
5871}
5872
5874{
5875 return mParentLayerParameterName;
5876}
5877
5878void QgsProcessingParameterField::setParentLayerParameterName( const QString &parentLayerParameterName )
5879{
5880 mParentLayerParameterName = parentLayerParameterName;
5881}
5882
5887
5889{
5890 mDataType = dataType;
5891}
5892
5894{
5895 return mAllowMultiple;
5896}
5897
5899{
5900 mAllowMultiple = allowMultiple;
5901}
5902
5904{
5905 return mDefaultToAllFields;
5906}
5907
5909{
5910 mDefaultToAllFields = enabled;
5911}
5912
5914{
5916 map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
5917 map.insert( QStringLiteral( "data_type" ), mDataType );
5918 map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
5919 map.insert( QStringLiteral( "default_to_all_fields" ), mDefaultToAllFields );
5920 return map;
5921}
5922
5924{
5926 mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
5927 mDataType = static_cast< DataType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
5928 mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
5929 mDefaultToAllFields = map.value( QStringLiteral( "default_to_all_fields" ) ).toBool();
5930 return true;
5931}
5932
5933QgsProcessingParameterField *QgsProcessingParameterField::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
5934{
5935 QString parent;
5936 DataType type = Any;
5937 bool allowMultiple = false;
5938 bool defaultToAllFields = false;
5939 QString def = definition;
5940
5941 if ( def.startsWith( QLatin1String( "numeric " ), Qt::CaseInsensitive ) )
5942 {
5943 type = Numeric;
5944 def = def.mid( 8 );
5945 }
5946 else if ( def.startsWith( QLatin1String( "string " ), Qt::CaseInsensitive ) )
5947 {
5948 type = String;
5949 def = def.mid( 7 );
5950 }
5951 else if ( def.startsWith( QLatin1String( "datetime " ), Qt::CaseInsensitive ) )
5952 {
5953 type = DateTime;
5954 def = def.mid( 9 );
5955 }
5956
5957 if ( def.startsWith( QLatin1String( "multiple" ), Qt::CaseInsensitive ) )
5958 {
5959 allowMultiple = true;
5960 def = def.mid( 8 ).trimmed();
5961 }
5962
5963 if ( def.startsWith( QLatin1String( "default_to_all_fields" ), Qt::CaseInsensitive ) )
5964 {
5965 defaultToAllFields = true;
5966 def = def.mid( 21 ).trimmed();
5967 }
5968
5969 const QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
5970 const QRegularExpressionMatch m = re.match( def );
5971 if ( m.hasMatch() )
5972 {
5973 parent = m.captured( 1 ).trimmed();
5974 def = m.captured( 2 );
5975 }
5976 else
5977 {
5978 parent = def;
5979 def.clear();
5980 }
5981
5982 return new QgsProcessingParameterField( name, description, def.isEmpty() ? QVariant() : def, parent, type, allowMultiple, isOptional, defaultToAllFields );
5983}
5984
5985QgsProcessingParameterFeatureSource::QgsProcessingParameterFeatureSource( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
5986 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
5988{
5989
5990}
5991
5996
5998{
5999 QVariant var = input;
6000 if ( !var.isValid() )
6001 {
6002 if ( !defaultValue().isValid() )
6003 return mFlags & FlagOptional;
6004
6005 var = defaultValue();
6006 }
6007
6008 if ( var.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
6009 {
6010 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( var );
6011 var = fromVar.source;
6012 }
6013 else if ( var.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6014 {
6015 // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
6016 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
6017 var = fromVar.sink;
6018 }
6019
6020 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
6021 {
6022 const QgsProperty p = var.value< QgsProperty >();
6024 {
6025 var = p.staticValue();
6026 }
6027 else
6028 {
6029 return true;
6030 }
6031 }
6032 if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( input ) ) )
6033 {
6034 return true;
6035 }
6036
6037 if ( var.type() != QVariant::String || var.toString().isEmpty() )
6038 return mFlags & FlagOptional;
6039
6040 if ( !context )
6041 {
6042 // that's as far as we can get without a context
6043 return true;
6044 }
6045
6046 // try to load as layer
6048 return true;
6049
6050 return false;
6051}
6052
6054{
6055 if ( !value.isValid() )
6056 return QStringLiteral( "None" );
6057
6058 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
6059 return QStringLiteral( "QgsProperty.fromExpression(%1)" ).arg( QgsProcessingUtils::stringToPythonLiteral( value.value< QgsProperty >().asExpression() ) );
6060
6061 if ( value.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
6062 {
6063 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( value );
6064 QString geometryCheckString;
6065 switch ( fromVar.geometryCheck )
6066 {
6068 geometryCheckString = QStringLiteral( "QgsFeatureRequest.GeometryNoCheck" );
6069 break;
6070
6072 geometryCheckString = QStringLiteral( "QgsFeatureRequest.GeometrySkipInvalid" );
6073 break;
6074
6076 geometryCheckString = QStringLiteral( "QgsFeatureRequest.GeometryAbortOnInvalid" );
6077 break;
6078 }
6079
6080 QStringList flags;
6081 QString flagString;
6083 flags << QStringLiteral( "QgsProcessingFeatureSourceDefinition.FlagOverrideDefaultGeometryCheck" );
6085 flags << QStringLiteral( "QgsProcessingFeatureSourceDefinition.FlagCreateIndividualOutputPerInputFeature" );
6086 if ( !flags.empty() )
6087 flagString = flags.join( QLatin1String( " | " ) );
6088
6090 {
6091 QString layerString = fromVar.source.staticValue().toString();
6092 // prefer to use layer source instead of id if possible (since it's persistent)
6093 if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context, true, QgsProcessingUtils::LayerHint::Vector ) ) )
6094 layerString = layer->source();
6095
6096 if ( fromVar.selectedFeaturesOnly || fromVar.featureLimit != -1 || fromVar.flags )
6097 {
6098 return QStringLiteral( "QgsProcessingFeatureSourceDefinition(%1, selectedFeaturesOnly=%2, featureLimit=%3%4, geometryCheck=%5)" ).arg( QgsProcessingUtils::stringToPythonLiteral( layerString ),
6099 fromVar.selectedFeaturesOnly ? QStringLiteral( "True" ) : QStringLiteral( "False" ),
6100 QString::number( fromVar.featureLimit ),
6101 flagString.isEmpty() ? QString() : ( QStringLiteral( ", flags=%1" ).arg( flagString ) ),
6102 geometryCheckString );
6103 }
6104 else
6105 {
6106 return QgsProcessingUtils::stringToPythonLiteral( layerString );
6107 }
6108 }
6109 else
6110 {
6111 if ( fromVar.selectedFeaturesOnly || fromVar.featureLimit != -1 || fromVar.flags )
6112 {
6113 return QStringLiteral( "QgsProcessingFeatureSourceDefinition(QgsProperty.fromExpression(%1), selectedFeaturesOnly=%2, featureLimit=%3%4, geometryCheck=%5)" )
6115 fromVar.selectedFeaturesOnly ? QStringLiteral( "True" ) : QStringLiteral( "False" ),
6116 QString::number( fromVar.featureLimit ),
6117 flagString.isEmpty() ? QString() : ( QStringLiteral( ", flags=%1" ).arg( flagString ) ),
6118 geometryCheckString );
6119 }
6120 else
6121 {
6122 return QStringLiteral( "QgsProperty.fromExpression(%1)" ).arg( QgsProcessingUtils::stringToPythonLiteral( fromVar.source.asExpression() ) );
6123 }
6124 }
6125 }
6126 else if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( value ) ) )
6127 {
6128 return QgsProcessingUtils::stringToPythonLiteral( layer->source() );
6129 }
6130
6131 QString layerString = value.toString();
6132
6133 // prefer to use layer source if possible (since it's persistent)
6134 if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context, true, QgsProcessingUtils::LayerHint::Vector ) ) )
6135 layerString = layer->providerType() != QLatin1String( "ogr" ) && layer->providerType() != QLatin1String( "gdal" ) && layer->providerType() != QLatin1String( "mdal" ) ? QgsProcessingUtils::encodeProviderKeyAndUri( layer->providerType(), layer->source() ) : layer->source();
6136
6137 return QgsProcessingUtils::stringToPythonLiteral( layerString );
6138}
6139
6140QString QgsProcessingParameterFeatureSource::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
6141{
6143}
6144
6146{
6148}
6149
6151{
6152 QString code = QStringLiteral( "##%1=" ).arg( mName );
6153 if ( mFlags & FlagOptional )
6154 code += QLatin1String( "optional " );
6155 code += QLatin1String( "source " );
6156
6157 for ( const int type : mDataTypes )
6158 {
6159 switch ( type )
6160 {
6162 code += QLatin1String( "point " );
6163 break;
6164
6166 code += QLatin1String( "line " );
6167 break;
6168
6170 code += QLatin1String( "polygon " );
6171 break;
6172
6173 }
6174 }
6175
6176 code += mDefault.toString();
6177 return code.trimmed();
6178}
6179
6181{
6182 switch ( outputType )
6183 {
6185 {
6186 QString code = QStringLiteral( "QgsProcessingParameterFeatureSource('%1', %2" )
6188 if ( mFlags & FlagOptional )
6189 code += QLatin1String( ", optional=True" );
6190
6191 if ( !mDataTypes.empty() )
6192 {
6193 QStringList options;
6194 options.reserve( mDataTypes.size() );
6195 for ( const int t : mDataTypes )
6196 options << QStringLiteral( "QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( static_cast< QgsProcessing::SourceType >( t ) ) );
6197 code += QStringLiteral( ", types=[%1]" ).arg( options.join( ',' ) );
6198 }
6199
6201 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6202 return code;
6203 }
6204 }
6205 return QString();
6206}
6207
6209{
6210 return QgsProviderRegistry::instance()->fileVectorFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
6211}
6212
6214 : mDataTypes( types )
6215{
6216
6217}
6218
6220{
6222 QVariantList types;
6223 for ( const int type : mDataTypes )
6224 {
6225 types << type;
6226 }
6227 map.insert( QStringLiteral( "data_types" ), types );
6228 return map;
6229}
6230
6232{
6234 mDataTypes.clear();
6235 const QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
6236 for ( const QVariant &val : values )
6237 {
6238 mDataTypes << val.toInt();
6239 }
6240 return true;
6241}
6242
6243QgsProcessingParameterFeatureSource *QgsProcessingParameterFeatureSource::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6244{
6245 QList< int > types;
6246 QString def = definition;
6247 while ( true )
6248 {
6249 if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
6250 {
6252 def = def.mid( 6 );
6253 continue;
6254 }
6255 else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
6256 {
6258 def = def.mid( 5 );
6259 continue;
6260 }
6261 else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
6262 {
6264 def = def.mid( 8 );
6265 continue;
6266 }
6267 break;
6268 }
6269
6270 return new QgsProcessingParameterFeatureSource( name, description, types, def.isEmpty() ? QVariant() : def, isOptional );
6271}
6272
6273QgsProcessingParameterFeatureSink::QgsProcessingParameterFeatureSink( const QString &name, const QString &description, QgsProcessing::SourceType type, const QVariant &defaultValue, bool optional, bool createByDefault, bool supportsAppend )
6274 : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
6275 , mDataType( type )
6276 , mSupportsAppend( supportsAppend )
6277{
6278}
6279
6284
6286{
6287 QVariant var = input;
6288 if ( !var.isValid() )
6289 {
6290 if ( !defaultValue().isValid() )
6291 return mFlags & FlagOptional;
6292
6293 var = defaultValue();
6294 }
6295
6296 if ( var.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6297 {
6298 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
6299 var = fromVar.sink;
6300 }
6301
6302 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
6303 {
6304 const QgsProperty p = var.value< QgsProperty >();
6306 {
6307 var = p.staticValue();
6308 }
6309 else
6310 {
6311 return true;
6312 }
6313 }
6314
6315 if ( var.type() != QVariant::String )
6316 return false;
6317
6318 if ( var.toString().isEmpty() )
6319 return mFlags & FlagOptional;
6320
6321 return true;
6322}
6323
6325{
6326 if ( !value.isValid() )
6327 return QStringLiteral( "None" );
6328
6329 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
6330 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
6331
6332 if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6333 {
6334 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
6336 {
6337 return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
6338 }
6339 else
6340 {
6341 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
6342 }
6343 }
6344
6345 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
6346}
6347
6349{
6350 QString code = QStringLiteral( "##%1=" ).arg( mName );
6351 if ( mFlags & FlagOptional )
6352 code += QLatin1String( "optional " );
6353 code += QLatin1String( "sink " );
6354
6355 switch ( mDataType )
6356 {
6358 code += QLatin1String( "point " );
6359 break;
6360
6362 code += QLatin1String( "line " );
6363 break;
6364
6366 code += QLatin1String( "polygon " );
6367 break;
6368
6370 code += QLatin1String( "table " );
6371 break;
6372
6373 default:
6374 break;
6375 }
6376
6377 code += mDefault.toString();
6378 return code.trimmed();
6379}
6380
6385
6387{
6388 if ( auto *lOriginalProvider = originalProvider() )
6389 {
6390 return lOriginalProvider->defaultVectorFileExtension( hasGeometry() );
6391 }
6392 else if ( QgsProcessingProvider *p = provider() )
6393 {
6394 return p->defaultVectorFileExtension( hasGeometry() );
6395 }
6396 else
6397 {
6398 if ( hasGeometry() )
6399 {
6401 }
6402 else
6403 {
6404 return QStringLiteral( "dbf" );
6405 }
6406 }
6407}
6408
6410{
6411 switch ( outputType )
6412 {
6414 {
6415 QString code = QStringLiteral( "QgsProcessingParameterFeatureSink('%1', %2" )
6417 if ( mFlags & FlagOptional )
6418 code += QLatin1String( ", optional=True" );
6419
6420 code += QStringLiteral( ", type=QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( mDataType ) );
6421
6422 code += QStringLiteral( ", createByDefault=%1" ).arg( createByDefault() ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
6423 if ( mSupportsAppend )
6424 code += QLatin1String( ", supportsAppend=True" );
6425
6427 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6428 return code;
6429 }
6430 }
6431 return QString();
6432}
6433
6435{
6436 const QStringList exts = supportedOutputVectorLayerExtensions();
6437 QStringList filters;
6438 for ( const QString &ext : exts )
6439 {
6440 filters << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
6441 }
6442 return filters.join( QLatin1String( ";;" ) ) + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
6443
6444}
6445
6447{
6448 if ( auto *lOriginalProvider = originalProvider() )
6449 {
6450 if ( hasGeometry() )
6451 return lOriginalProvider->supportedOutputVectorLayerExtensions();
6452 else
6453 return lOriginalProvider->supportedOutputTableExtensions();
6454 }
6455 else if ( QgsProcessingProvider *p = provider() )
6456 {
6457 if ( hasGeometry() )
6458 return p->supportedOutputVectorLayerExtensions();
6459 else
6460 return p->supportedOutputTableExtensions();
6461 }
6462 else
6463 {
6465 }
6466}
6467
6472
6474{
6475 switch ( mDataType )
6476 {
6482 return true;
6483
6491 return false;
6492 }
6493 return true;
6494}
6495
6500
6502{
6504 map.insert( QStringLiteral( "data_type" ), mDataType );
6505 map.insert( QStringLiteral( "supports_append" ), mSupportsAppend );
6506 return map;
6507}
6508
6510{
6512 mDataType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
6513 mSupportsAppend = map.value( QStringLiteral( "supports_append" ), false ).toBool();
6514 return true;
6515}
6516
6518{
6520 return QStringLiteral( "memory:%1" ).arg( description() );
6521 else
6523}
6524
6525QgsProcessingParameterFeatureSink *QgsProcessingParameterFeatureSink::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6526{
6528 QString def = definition;
6529 if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
6530 {
6532 def = def.mid( 6 );
6533 }
6534 else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
6535 {
6537 def = def.mid( 5 );
6538 }
6539 else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
6540 {
6542 def = def.mid( 8 );
6543 }
6544 else if ( def.startsWith( QLatin1String( "table" ), Qt::CaseInsensitive ) )
6545 {
6547 def = def.mid( 6 );
6548 }
6549
6550 return new QgsProcessingParameterFeatureSink( name, description, type, definition.trimmed().isEmpty() ? QVariant() : definition, isOptional );
6551}
6552
6554{
6555 return mSupportsAppend;
6556}
6557
6559{
6560 mSupportsAppend = supportsAppend;
6561}
6562
6563QgsProcessingParameterRasterDestination::QgsProcessingParameterRasterDestination( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, bool createByDefault )
6564 : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
6565{
6566}
6567
6572
6574{
6575 QVariant var = input;
6576 if ( !var.isValid() )
6577 {
6578 if ( !defaultValue().isValid() )
6579 return mFlags & FlagOptional;
6580
6581 var = defaultValue();
6582 }
6583
6584 if ( var.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6585 {
6586 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
6587 var = fromVar.sink;
6588 }
6589
6590 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
6591 {
6592 const QgsProperty p = var.value< QgsProperty >();
6594 {
6595 var = p.staticValue();
6596 }
6597 else
6598 {
6599 return true;
6600 }
6601 }
6602
6603 if ( var.type() != QVariant::String )
6604 return false;
6605
6606 if ( var.toString().isEmpty() )
6607 return mFlags & FlagOptional;
6608
6609 return true;
6610}
6611
6613{
6614 if ( !value.isValid() )
6615 return QStringLiteral( "None" );
6616
6617 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
6618 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
6619
6620 if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6621 {
6622 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
6624 {
6625 return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
6626 }
6627 else
6628 {
6629 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
6630 }
6631 }
6632
6633 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
6634}
6635
6640
6642{
6643 if ( auto *lOriginalProvider = originalProvider() )
6644 {
6645 return lOriginalProvider->defaultRasterFileExtension();
6646 }
6647 else if ( QgsProcessingProvider *p = provider() )
6648 {
6649 return p->defaultRasterFileExtension();
6650 }
6651 else
6652 {
6654 }
6655}
6656
6658{
6659 const QStringList exts = supportedOutputRasterLayerExtensions();
6660 QStringList filters;
6661 for ( const QString &ext : exts )
6662 {
6663 filters << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
6664 }
6665 return filters.join( QLatin1String( ";;" ) ) + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
6666}
6667
6669{
6670 if ( auto *lOriginalProvider = originalProvider() )
6671 {
6672 return lOriginalProvider->supportedOutputRasterLayerExtensions();
6673 }
6674 else if ( QgsProcessingProvider *p = provider() )
6675 {
6676 return p->supportedOutputRasterLayerExtensions();
6677 }
6678 else
6679 {
6681 }
6682}
6683
6684QgsProcessingParameterRasterDestination *QgsProcessingParameterRasterDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6685{
6686 return new QgsProcessingParameterRasterDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
6687}
6688
6689
6690QgsProcessingParameterFileDestination::QgsProcessingParameterFileDestination( const QString &name, const QString &description, const QString &fileFilter, const QVariant &defaultValue, bool optional, bool createByDefault )
6691 : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
6692 , mFileFilter( fileFilter.isEmpty() ? QObject::tr( "All files (*.*)" ) : fileFilter )
6693{
6694
6695}
6696
6701
6703{
6704 QVariant var = input;
6705 if ( !var.isValid() )
6706 {
6707 if ( !defaultValue().isValid() )
6708 return mFlags & FlagOptional;
6709
6710 var = defaultValue();
6711 }
6712
6713 if ( var.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6714 {
6715 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
6716 var = fromVar.sink;
6717 }
6718
6719 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
6720 {
6721 const QgsProperty p = var.value< QgsProperty >();
6723 {
6724 var = p.staticValue();
6725 }
6726 else
6727 {
6728 return true;
6729 }
6730 }
6731
6732 if ( var.type() != QVariant::String )
6733 return false;
6734
6735 if ( var.toString().isEmpty() )
6736 return mFlags & FlagOptional;
6737
6738 // possible enhancement - check that value is compatible with file filter?
6739
6740 return true;
6741}
6742
6744{
6745 if ( !value.isValid() )
6746 return QStringLiteral( "None" );
6747
6748 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
6749 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
6750
6751 if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
6752 {
6753 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
6755 {
6756 return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
6757 }
6758 else
6759 {
6760 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
6761 }
6762 }
6763
6764 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
6765}
6766
6768{
6769 if ( !mFileFilter.isEmpty() && mFileFilter.contains( QStringLiteral( "htm" ), Qt::CaseInsensitive ) )
6770 {
6771 return new QgsProcessingOutputHtml( name(), description() );
6772 }
6773 else
6774 {
6775 return new QgsProcessingOutputFile( name(), description() );
6776 }
6777}
6778
6780{
6781 if ( mFileFilter.isEmpty() || mFileFilter == QObject::tr( "All files (*.*)" ) )
6782 return QStringLiteral( "file" );
6783
6784 // get first extension from filter
6785 const QRegularExpression rx( QStringLiteral( ".*?\\(\\*\\.([a-zA-Z0-9._]+).*" ) );
6786 const QRegularExpressionMatch match = rx.match( mFileFilter );
6787 if ( !match.hasMatch() )
6788 return QStringLiteral( "file" );
6789
6790 return match.captured( 1 );
6791}
6792
6794{
6795 switch ( outputType )
6796 {
6798 {
6799 QString code = QStringLiteral( "QgsProcessingParameterFileDestination('%1', %2" )
6801 if ( mFlags & FlagOptional )
6802 code += QLatin1String( ", optional=True" );
6803
6804 code += QStringLiteral( ", fileFilter=%1" ).arg( QgsProcessingUtils::stringToPythonLiteral( mFileFilter ) );
6805
6806 code += QStringLiteral( ", createByDefault=%1" ).arg( createByDefault() ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
6807
6809 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6810 return code;
6811 }
6812 }
6813 return QString();
6814}
6815
6817{
6818 return ( fileFilter().isEmpty() ? QString() : fileFilter() + QStringLiteral( ";;" ) ) + QObject::tr( "All files (*.*)" );
6819}
6820
6822{
6823 return mFileFilter;
6824}
6825
6827{
6828 mFileFilter = fileFilter;
6829}
6830
6832{
6834 map.insert( QStringLiteral( "file_filter" ), mFileFilter );
6835 return map;
6836}
6837
6839{
6841 mFileFilter = map.value( QStringLiteral( "file_filter" ) ).toString();
6842 return true;
6843
6844}
6845
6846QgsProcessingParameterFileDestination *QgsProcessingParameterFileDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6847{
6848 return new QgsProcessingParameterFileDestination( name, description, QString(), definition.isEmpty() ? QVariant() : definition, isOptional );
6849}
6850
6851QgsProcessingParameterFolderDestination::QgsProcessingParameterFolderDestination( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, bool createByDefault )
6852 : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
6853{}
6854
6859
6861{
6862 QVariant var = input;
6863 if ( !var.isValid() )
6864 {
6865 if ( !defaultValue().isValid() )
6866 return mFlags & FlagOptional;
6867
6868 var = defaultValue();
6869 }
6870
6871 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
6872 {
6873 const QgsProperty p = var.value< QgsProperty >();
6875 {
6876 var = p.staticValue();
6877 }
6878 else
6879 {
6880 return true;
6881 }
6882 }
6883
6884 if ( var.type() != QVariant::String )
6885 return false;
6886
6887 if ( var.toString().isEmpty() )
6888 return mFlags & FlagOptional;
6889
6890 return true;
6891}
6892
6897
6899{
6900 return QString();
6901}
6902
6903QgsProcessingParameterFolderDestination *QgsProcessingParameterFolderDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
6904{
6905 return new QgsProcessingParameterFolderDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
6906}
6907
6908QgsProcessingDestinationParameter::QgsProcessingDestinationParameter( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, bool createByDefault )
6909 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
6910 , mCreateByDefault( createByDefault )
6911{
6912
6913}
6914
6916{
6918 map.insert( QStringLiteral( "supports_non_file_outputs" ), mSupportsNonFileBasedOutputs );
6919 map.insert( QStringLiteral( "create_by_default" ), mCreateByDefault );
6920 return map;
6921}
6922
6924{
6926 mSupportsNonFileBasedOutputs = map.value( QStringLiteral( "supports_non_file_outputs" ) ).toBool();
6927 mCreateByDefault = map.value( QStringLiteral( "create_by_default" ), QStringLiteral( "1" ) ).toBool();
6928 return true;
6929}
6930
6932{
6933 switch ( outputType )
6934 {
6936 {
6937 // base class method is probably not much use
6939 {
6940 QString code = t->className() + QStringLiteral( "('%1', %2" )
6942 if ( mFlags & FlagOptional )
6943 code += QLatin1String( ", optional=True" );
6944
6945 code += QStringLiteral( ", createByDefault=%1" ).arg( mCreateByDefault ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
6946
6948 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
6949 return code;
6950 }
6951 break;
6952 }
6953 }
6954 // oh well, we tried
6955 return QString();
6956}
6957
6959{
6960 return QObject::tr( "Default extension" ) + QStringLiteral( " (*." ) + defaultFileExtension() + ')';
6961}
6962
6964{
6965 // sanitize name to avoid multiple . in the filename. E.g. when name() contain
6966 // backend command name having a "." inside as in case of grass commands
6967 const QRegularExpression rx( QStringLiteral( "[.]" ) );
6968 QString sanitizedName = name();
6969 sanitizedName.replace( rx, QStringLiteral( "_" ) );
6970
6971 if ( defaultFileExtension().isEmpty() )
6972 {
6973 return QgsProcessingUtils::generateTempFilename( sanitizedName );
6974 }
6975 else
6976 {
6977 return QgsProcessingUtils::generateTempFilename( sanitizedName + '.' + defaultFileExtension() );
6978 }
6979}
6980
6981bool QgsProcessingDestinationParameter::isSupportedOutputValue( const QVariant &value, QgsProcessingContext &context, QString &error ) const
6982{
6983 if ( auto *lOriginalProvider = originalProvider() )
6984 return lOriginalProvider->isSupportedOutputValue( value, this, context, error );
6985 else if ( provider() )
6986 return provider()->isSupportedOutputValue( value, this, context, error );
6987
6988 return true;
6989}
6990
6992{
6993 return mCreateByDefault;
6994}
6995
6997{
6998 mCreateByDefault = createByDefault;
6999}
7000
7001QgsProcessingParameterVectorDestination::QgsProcessingParameterVectorDestination( const QString &name, const QString &description, QgsProcessing::SourceType type, const QVariant &defaultValue, bool optional, bool createByDefault )
7002 : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
7003 , mDataType( type )
7004{
7005
7006}
7007
7012
7014{
7015 QVariant var = input;
7016 if ( !var.isValid() )
7017 {
7018 if ( !defaultValue().isValid() )
7019 return mFlags & FlagOptional;
7020
7021 var = defaultValue();
7022 }
7023
7024 if ( var.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
7025 {
7026 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
7027 var = fromVar.sink;
7028 }
7029
7030 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
7031 {
7032 const QgsProperty p = var.value< QgsProperty >();
7034 {
7035 var = p.staticValue();
7036 }
7037 else
7038 {
7039 return true;
7040 }
7041 }
7042
7043 if ( var.type() != QVariant::String )
7044 return false;
7045
7046 if ( var.toString().isEmpty() )
7047 return mFlags & FlagOptional;
7048
7049 return true;
7050}
7051
7053{
7054 if ( !value.isValid() )
7055 return QStringLiteral( "None" );
7056
7057 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
7058 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7059
7060 if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
7061 {
7062 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
7064 {
7065 return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
7066 }
7067 else
7068 {
7069 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
7070 }
7071 }
7072
7073 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
7074}
7075
7077{
7078 QString code = QStringLiteral( "##%1=" ).arg( mName );
7079 if ( mFlags & FlagOptional )
7080 code += QLatin1String( "optional " );
7081 code += QLatin1String( "vectorDestination " );
7082
7083 switch ( mDataType )
7084 {
7086 code += QLatin1String( "point " );
7087 break;
7088
7090 code += QLatin1String( "line " );
7091 break;
7092
7094 code += QLatin1String( "polygon " );
7095 break;
7096
7097 default:
7098 break;
7099 }
7100
7101 code += mDefault.toString();
7102 return code.trimmed();
7103}
7104
7109
7111{
7112 if ( auto *lOriginalProvider = originalProvider() )
7113 {
7114 return lOriginalProvider->defaultVectorFileExtension( hasGeometry() );
7115 }
7116 else if ( QgsProcessingProvider *p = provider() )
7117 {
7118 return p->defaultVectorFileExtension( hasGeometry() );
7119 }
7120 else
7121 {
7122 if ( hasGeometry() )
7123 {
7125 }
7126 else
7127 {
7128 return QStringLiteral( "dbf" );
7129 }
7130 }
7131}
7132
7134{
7135 switch ( outputType )
7136 {
7138 {
7139 QString code = QStringLiteral( "QgsProcessingParameterVectorDestination('%1', %2" )
7141 if ( mFlags & FlagOptional )
7142 code += QLatin1String( ", optional=True" );
7143
7144 code += QStringLiteral( ", type=QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( mDataType ) );
7145
7146 code += QStringLiteral( ", createByDefault=%1" ).arg( createByDefault() ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
7147
7149 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7150 return code;
7151 }
7152 }
7153 return QString();
7154}
7155
7157{
7158 const QStringList exts = supportedOutputVectorLayerExtensions();
7159 QStringList filters;
7160 for ( const QString &ext : exts )
7161 {
7162 filters << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
7163 }
7164 return filters.join( QLatin1String( ";;" ) ) + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
7165}
7166
7168{
7169 if ( auto *lOriginalProvider = originalProvider() )
7170 {
7171 if ( hasGeometry() )
7172 return lOriginalProvider->supportedOutputVectorLayerExtensions();
7173 else
7174 return lOriginalProvider->supportedOutputTableExtensions();
7175 }
7176 else if ( QgsProcessingProvider *p = provider() )
7177 {
7178 if ( hasGeometry() )
7179 return p->supportedOutputVectorLayerExtensions();
7180 else
7181 return p->supportedOutputTableExtensions();
7182 }
7183 else
7184 {
7186 }
7187}
7188
7193
7195{
7196 switch ( mDataType )
7197 {
7203 return true;
7204
7212 return false;
7213 }
7214 return true;
7215}
7216
7221
7223{
7225 map.insert( QStringLiteral( "data_type" ), mDataType );
7226 return map;
7227}
7228
7230{
7232 mDataType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
7233 return true;
7234}
7235
7236QgsProcessingParameterVectorDestination *QgsProcessingParameterVectorDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7237{
7239 QString def = definition;
7240 if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
7241 {
7243 def = def.mid( 6 );
7244 }
7245 else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
7246 {
7248 def = def.mid( 5 );
7249 }
7250 else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
7251 {
7253 def = def.mid( 8 );
7254 }
7255
7256 return new QgsProcessingParameterVectorDestination( name, description, type, definition.isEmpty() ? QVariant() : definition, isOptional );
7257}
7258
7259QgsProcessingParameterBand::QgsProcessingParameterBand( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional, bool allowMultiple )
7260 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7261 , mParentLayerParameterName( parentLayerParameterName )
7262 , mAllowMultiple( allowMultiple )
7263{
7264
7265}
7266
7271
7273{
7274 QVariant input = value;
7275 if ( !input.isValid() )
7276 {
7277 if ( !defaultValue().isValid() )
7278 return mFlags & FlagOptional;
7279
7280 input = defaultValue();
7281 }
7282
7283 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
7284 {
7285 return true;
7286 }
7287
7288 if ( input.type() == QVariant::List || input.type() == QVariant::StringList )
7289 {
7290 if ( !mAllowMultiple )
7291 return false;
7292
7293 if ( input.toList().isEmpty() && !( mFlags & FlagOptional ) )
7294 return false;
7295 }
7296 else
7297 {
7298 bool ok = false;
7299 const double res = input.toInt( &ok );
7300 Q_UNUSED( res )
7301 if ( !ok )
7302 return mFlags & FlagOptional;
7303 }
7304 return true;
7305}
7306
7308{
7309 return mAllowMultiple;
7310}
7311
7313{
7314 mAllowMultiple = allowMultiple;
7315}
7316
7318{
7319 if ( !value.isValid() )
7320 return QStringLiteral( "None" );
7321
7322 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
7323 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7324
7325 if ( value.type() == QVariant::List )
7326 {
7327 QStringList parts;
7328 const QVariantList values = value.toList();
7329 for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
7330 {
7331 parts << QString::number( static_cast< int >( it->toDouble() ) );
7332 }
7333 return parts.join( ',' ).prepend( '[' ).append( ']' );
7334 }
7335 else if ( value.type() == QVariant::StringList )
7336 {
7337 QStringList parts;
7338 const QStringList values = value.toStringList();
7339 for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
7340 {
7341 parts << QString::number( static_cast< int >( it->toDouble() ) );
7342 }
7343 return parts.join( ',' ).prepend( '[' ).append( ']' );
7344 }
7345
7346 return value.toString();
7347}
7348
7350{
7351 QString code = QStringLiteral( "##%1=" ).arg( mName );
7352 if ( mFlags & FlagOptional )
7353 code += QLatin1String( "optional " );
7354 code += QLatin1String( "band " );
7355
7356 if ( mAllowMultiple )
7357 code += QLatin1String( "multiple " );
7358
7359 code += mParentLayerParameterName + ' ';
7360
7361 code += mDefault.toString();
7362 return code.trimmed();
7363}
7364
7366{
7367 QStringList depends;
7368 if ( !mParentLayerParameterName.isEmpty() )
7369 depends << mParentLayerParameterName;
7370 return depends;
7371}
7372
7374{
7375 switch ( outputType )
7376 {
7378 {
7379 QString code = QStringLiteral( "QgsProcessingParameterBand('%1', %2" )
7381 if ( mFlags & FlagOptional )
7382 code += QLatin1String( ", optional=True" );
7383
7384 code += QStringLiteral( ", parentLayerParameterName='%1'" ).arg( mParentLayerParameterName );
7385 code += QStringLiteral( ", allowMultiple=%1" ).arg( mAllowMultiple ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
7386
7388 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7389 return code;
7390 }
7391 }
7392 return QString();
7393}
7394
7396{
7397 return mParentLayerParameterName;
7398}
7399
7400void QgsProcessingParameterBand::setParentLayerParameterName( const QString &parentLayerParameterName )
7401{
7402 mParentLayerParameterName = parentLayerParameterName;
7403}
7404
7406{
7408 map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
7409 map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
7410 return map;
7411}
7412
7414{
7416 mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
7417 mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
7418 return true;
7419}
7420
7421QgsProcessingParameterBand *QgsProcessingParameterBand::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7422{
7423 QString parent;
7424 QString def = definition;
7425 bool allowMultiple = false;
7426
7427 if ( def.startsWith( QLatin1String( "multiple" ), Qt::CaseInsensitive ) )
7428 {
7429 allowMultiple = true;
7430 def = def.mid( 8 ).trimmed();
7431 }
7432
7433 const QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
7434 const QRegularExpressionMatch m = re.match( def );
7435 if ( m.hasMatch() )
7436 {
7437 parent = m.captured( 1 ).trimmed();
7438 def = m.captured( 2 );
7439 }
7440 else
7441 {
7442 parent = def;
7443 def.clear();
7444 }
7445
7446 return new QgsProcessingParameterBand( name, description, def.isEmpty() ? QVariant() : def, parent, isOptional, allowMultiple );
7447}
7448
7449//
7450// QgsProcessingParameterDistance
7451//
7452
7453QgsProcessingParameterDistance::QgsProcessingParameterDistance( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentParameterName, bool optional, double minValue, double maxValue )
7454 : QgsProcessingParameterNumber( name, description, Double, defaultValue, optional, minValue, maxValue )
7455 , mParentParameterName( parentParameterName )
7456{
7457
7458}
7459
7464
7466{
7467 return typeName();
7468}
7469
7471{
7472 QStringList depends;
7473 if ( !mParentParameterName.isEmpty() )
7474 depends << mParentParameterName;
7475 return depends;
7476}
7477
7479{
7480 switch ( outputType )
7481 {
7483 {
7484 QString code = QStringLiteral( "QgsProcessingParameterDistance('%1', %2" )
7486 if ( mFlags & FlagOptional )
7487 code += QLatin1String( ", optional=True" );
7488
7489 code += QStringLiteral( ", parentParameterName='%1'" ).arg( mParentParameterName );
7490
7491 if ( minimum() != std::numeric_limits<double>::lowest() + 1 )
7492 code += QStringLiteral( ", minValue=%1" ).arg( minimum() );
7493 if ( maximum() != std::numeric_limits<double>::max() )
7494 code += QStringLiteral( ", maxValue=%1" ).arg( maximum() );
7496 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7497 return code;
7498 }
7499 }
7500 return QString();
7501}
7502
7504{
7505 return mParentParameterName;
7506}
7507
7508void QgsProcessingParameterDistance::setParentParameterName( const QString &parentParameterName )
7509{
7510 mParentParameterName = parentParameterName;
7511}
7512
7514{
7516 map.insert( QStringLiteral( "parent" ), mParentParameterName );
7517 map.insert( QStringLiteral( "default_unit" ), static_cast< int >( mDefaultUnit ) );
7518 return map;
7519}
7520
7522{
7524 mParentParameterName = map.value( QStringLiteral( "parent" ) ).toString();
7525 mDefaultUnit = static_cast< QgsUnitTypes::DistanceUnit>( map.value( QStringLiteral( "default_unit" ), QgsUnitTypes::DistanceUnknownUnit ).toInt() );
7526 return true;
7527}
7528
7529
7530//
7531// QgsProcessingParameterDuration
7532//
7533
7534QgsProcessingParameterDuration::QgsProcessingParameterDuration( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, double minValue, double maxValue )
7535 : QgsProcessingParameterNumber( name, description, Double, defaultValue, optional, minValue, maxValue )
7536{
7537}
7538
7543
7545{
7546 return typeName();
7547}
7548
7550{
7551 switch ( outputType )
7552 {
7554 {
7555 QString code = QStringLiteral( "QgsProcessingParameterDuration('%1', %2" )
7557 if ( mFlags & FlagOptional )
7558 code += QLatin1String( ", optional=True" );
7559
7560 if ( minimum() != std::numeric_limits<double>::lowest() + 1 )
7561 code += QStringLiteral( ", minValue=%1" ).arg( minimum() );
7562 if ( maximum() != std::numeric_limits<double>::max() )
7563 code += QStringLiteral( ", maxValue=%1" ).arg( maximum() );
7565 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7566 return code;
7567 }
7568 }
7569 return QString();
7570}
7571
7573{
7575 map.insert( QStringLiteral( "default_unit" ), static_cast< int >( mDefaultUnit ) );
7576 return map;
7577}
7578
7580{
7582 mDefaultUnit = static_cast< QgsUnitTypes::TemporalUnit>( map.value( QStringLiteral( "default_unit" ), QgsUnitTypes::TemporalDays ).toInt() );
7583 return true;
7584}
7585
7586
7587//
7588// QgsProcessingParameterScale
7589//
7590
7591QgsProcessingParameterScale::QgsProcessingParameterScale( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
7592 : QgsProcessingParameterNumber( name, description, Double, defaultValue, optional )
7593{
7594
7595}
7596
7601
7603{
7604 return typeName();
7605}
7606
7608{
7609 switch ( outputType )
7610 {
7612 {
7613 QString code = QStringLiteral( "QgsProcessingParameterScale('%1', %2" )
7615 if ( mFlags & FlagOptional )
7616 code += QLatin1String( ", optional=True" );
7618 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7619 return code;
7620 }
7621 }
7622 return QString();
7623}
7624
7625QgsProcessingParameterScale *QgsProcessingParameterScale::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7626{
7627 return new QgsProcessingParameterScale( name, description, definition.isEmpty() ? QVariant()
7628 : ( definition.toLower().trimmed() == QLatin1String( "none" ) ? QVariant() : definition ), isOptional );
7629}
7630
7631
7632//
7633// QgsProcessingParameterLayout
7634//
7635
7636QgsProcessingParameterLayout::QgsProcessingParameterLayout( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
7637 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7638{}
7639
7644
7646{
7647 if ( QgsVariantUtils::isNull( value ) )
7648 return QStringLiteral( "None" );
7649
7650 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
7651 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7652
7653 const QString s = value.toString();
7655}
7656
7658{
7659 QString code = QStringLiteral( "##%1=" ).arg( mName );
7660 if ( mFlags & FlagOptional )
7661 code += QLatin1String( "optional " );
7662 code += QLatin1String( "layout " );
7663
7664 code += mDefault.toString();
7665 return code.trimmed();
7666}
7667
7669{
7670 switch ( outputType )
7671 {
7673 {
7674 QString code = QStringLiteral( "QgsProcessingParameterLayout('%1', %2" )
7676 if ( mFlags & FlagOptional )
7677 code += QLatin1String( ", optional=True" );
7679 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7680 return code;
7681 }
7682 }
7683 return QString();
7684}
7685
7686QgsProcessingParameterLayout *QgsProcessingParameterLayout::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7687{
7688 QString def = definition;
7689
7690 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
7691 def = def.mid( 1 );
7692 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
7693 def.chop( 1 );
7694
7695 QVariant defaultValue = def;
7696 if ( def == QLatin1String( "None" ) )
7697 defaultValue = QVariant();
7698
7699 return new QgsProcessingParameterLayout( name, description, defaultValue, isOptional );
7700}
7701
7702
7703//
7704// QString mParentLayerParameterName;
7705//
7706
7707QgsProcessingParameterLayoutItem::QgsProcessingParameterLayoutItem( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayoutParameterName, int itemType, bool optional )
7708 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7709 , mParentLayoutParameterName( parentLayoutParameterName )
7710 , mItemType( itemType )
7711{
7712
7713}
7714
7719
7721{
7722 if ( QgsVariantUtils::isNull( value ) )
7723 return QStringLiteral( "None" );
7724
7725 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
7726 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7727
7728 const QString s = value.toString();
7730}
7731
7733{
7734 QString code = QStringLiteral( "##%1=" ).arg( mName );
7735 if ( mFlags & FlagOptional )
7736 code += QLatin1String( "optional " );
7737 code += QLatin1String( "layoutitem " );
7738 if ( mItemType >= 0 )
7739 code += QString::number( mItemType ) + ' ';
7740
7741 code += mParentLayoutParameterName + ' ';
7742
7743 code += mDefault.toString();
7744 return code.trimmed();
7745}
7746
7748{
7749 switch ( outputType )
7750 {
7752 {
7753 QString code = QStringLiteral( "QgsProcessingParameterLayoutItem('%1', %2" )
7755 if ( mFlags & FlagOptional )
7756 code += QLatin1String( ", optional=True" );
7757
7758 if ( mItemType >= 0 )
7759 code += QStringLiteral( ", itemType=%1" ).arg( mItemType );
7760
7761 code += QStringLiteral( ", parentLayoutParameterName='%1'" ).arg( mParentLayoutParameterName );
7762
7764 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7765 return code;
7766 }
7767 }
7768 return QString();
7769}
7770
7772{
7774 map.insert( QStringLiteral( "parent_layout" ), mParentLayoutParameterName );
7775 map.insert( QStringLiteral( "item_type" ), mItemType );
7776 return map;
7777}
7778
7780{
7782 mParentLayoutParameterName = map.value( QStringLiteral( "parent_layout" ) ).toString();
7783 mItemType = map.value( QStringLiteral( "item_type" ) ).toInt();
7784 return true;
7785}
7786
7788{
7789 QStringList depends;
7790 if ( !mParentLayoutParameterName.isEmpty() )
7791 depends << mParentLayoutParameterName;
7792 return depends;
7793}
7794
7795QgsProcessingParameterLayoutItem *QgsProcessingParameterLayoutItem::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7796{
7797 QString parent;
7798 QString def = definition;
7799 int itemType = -1;
7800 const QRegularExpression re( QStringLiteral( "(\\d+)?\\s*(.*?)\\s+(.*)$" ) );
7801 const QRegularExpressionMatch m = re.match( def );
7802 if ( m.hasMatch() )
7803 {
7804 itemType = m.captured( 1 ).trimmed().isEmpty() ? -1 : m.captured( 1 ).trimmed().toInt();
7805 parent = m.captured( 2 ).trimmed().isEmpty() ? m.captured( 3 ).trimmed() : m.captured( 2 ).trimmed();
7806 def = !m.captured( 2 ).trimmed().isEmpty() ? m.captured( 3 ) : QString();
7807 }
7808 else
7809 {
7810 parent = def;
7811 def.clear();
7812 }
7813
7814 return new QgsProcessingParameterLayoutItem( name, description, def.isEmpty() ? QVariant() : def, parent, itemType, isOptional );
7815}
7816
7818{
7819 return mParentLayoutParameterName;
7820}
7821
7823{
7824 mParentLayoutParameterName = name;
7825}
7826
7828{
7829 return mItemType;
7830}
7831
7833{
7834 mItemType = type;
7835}
7836
7837//
7838// QgsProcessingParameterColor
7839//
7840
7841QgsProcessingParameterColor::QgsProcessingParameterColor( const QString &name, const QString &description, const QVariant &defaultValue, bool opacityEnabled, bool optional )
7842 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7843 , mAllowOpacity( opacityEnabled )
7844{
7845
7846}
7847
7852
7854{
7855 if ( QgsVariantUtils::isNull( value ) )
7856 return QStringLiteral( "None" );
7857
7858 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
7859 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
7860
7861 if ( value.canConvert< QColor >() && !value.value< QColor >().isValid() )
7862 return QStringLiteral( "QColor()" );
7863
7864 if ( value.canConvert< QColor >() )
7865 {
7866 const QColor c = value.value< QColor >();
7867 if ( !mAllowOpacity || c.alpha() == 255 )
7868 return QStringLiteral( "QColor(%1, %2, %3)" ).arg( c.red() ).arg( c.green() ).arg( c.blue() );
7869 else
7870 return QStringLiteral( "QColor(%1, %2, %3, %4)" ).arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
7871 }
7872
7873 const QString s = value.toString();
7875}
7876
7878{
7879 QString code = QStringLiteral( "##%1=" ).arg( mName );
7880 if ( mFlags & FlagOptional )
7881 code += QLatin1String( "optional " );
7882 code += QLatin1String( "color " );
7883
7884 if ( mAllowOpacity )
7885 code += QLatin1String( "withopacity " );
7886
7887 code += mDefault.toString();
7888 return code.trimmed();
7889}
7890
7892{
7893 switch ( outputType )
7894 {
7896 {
7897 QString code = QStringLiteral( "QgsProcessingParameterColor('%1', %2" )
7899 if ( mFlags & FlagOptional )
7900 code += QLatin1String( ", optional=True" );
7901
7902 code += QStringLiteral( ", opacityEnabled=%1" ).arg( mAllowOpacity ? QStringLiteral( "True" ) : QStringLiteral( "False" ) );
7903
7905 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
7906 return code;
7907 }
7908 }
7909 return QString();
7910}
7911
7913{
7914 if ( !input.isValid() && ( mDefault.isValid() && ( !mDefault.toString().isEmpty() || mDefault.value< QColor >().isValid() ) ) )
7915 return true;
7916
7917 if ( !input.isValid() )
7918 return mFlags & FlagOptional;
7919
7920 if ( input.type() == QVariant::Color )
7921 {
7922 return true;
7923 }
7924 else if ( input.userType() == QMetaType::type( "QgsProperty" ) )
7925 {
7926 return true;
7927 }
7928
7929 if ( input.type() != QVariant::String || input.toString().isEmpty() )
7930 return mFlags & FlagOptional;
7931
7932 bool containsAlpha = false;
7933 return QgsSymbolLayerUtils::parseColorWithAlpha( input.toString(), containsAlpha ).isValid();
7934}
7935
7937{
7939 map.insert( QStringLiteral( "opacityEnabled" ), mAllowOpacity );
7940 return map;
7941}
7942
7944{
7946 mAllowOpacity = map.value( QStringLiteral( "opacityEnabled" ) ).toBool();
7947 return true;
7948}
7949
7951{
7952 return mAllowOpacity;
7953}
7954
7956{
7957 mAllowOpacity = enabled;
7958}
7959
7960QgsProcessingParameterColor *QgsProcessingParameterColor::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
7961{
7962 QString def = definition;
7963
7964 bool allowOpacity = false;
7965 if ( def.startsWith( QLatin1String( "withopacity" ), Qt::CaseInsensitive ) )
7966 {
7967 allowOpacity = true;
7968 def = def.mid( 12 );
7969 }
7970
7971 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
7972 def = def.mid( 1 );
7973 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
7974 def.chop( 1 );
7975
7976 QVariant defaultValue = def;
7977 if ( def == QLatin1String( "None" ) || def.isEmpty() )
7978 defaultValue = QVariant();
7979
7980 return new QgsProcessingParameterColor( name, description, defaultValue, allowOpacity, isOptional );
7981}
7982
7983//
7984// QgsProcessingParameterCoordinateOperation
7985//
7986QgsProcessingParameterCoordinateOperation::QgsProcessingParameterCoordinateOperation( const QString &name, const QString &description, const QVariant &defaultValue, const QString &sourceCrsParameterName, const QString &destinationCrsParameterName, const QVariant &staticSourceCrs, const QVariant &staticDestinationCrs, bool optional )
7987 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
7988 , mSourceParameterName( sourceCrsParameterName )
7989 , mDestParameterName( destinationCrsParameterName )
7990 , mSourceCrs( staticSourceCrs )
7991 , mDestCrs( staticDestinationCrs )
7992{
7993
7994}
7995
8000
8002{
8003 if ( QgsVariantUtils::isNull( value ) )
8004 return QStringLiteral( "None" );
8005
8006 if ( value.userType() == QMetaType::type( "QgsCoordinateReferenceSystem" ) )
8007 {
8008 if ( !value.value< QgsCoordinateReferenceSystem >().isValid() )
8009 return QStringLiteral( "QgsCoordinateReferenceSystem()" );
8010 else
8011 return QStringLiteral( "QgsCoordinateReferenceSystem('%1')" ).arg( value.value< QgsCoordinateReferenceSystem >().authid() );
8012 }
8013
8014 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
8015 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
8016
8017 QVariantMap p;
8018 p.insert( name(), value );
8019 QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
8020 if ( layer )
8022
8023 const QString s = value.toString();
8025}
8026
8028{
8029 QString code = QStringLiteral( "##%1=" ).arg( mName );
8030 if ( mFlags & FlagOptional )
8031 code += QLatin1String( "optional " );
8032 code += QLatin1String( "coordinateoperation " );
8033
8034 code += mDefault.toString();
8035 return code.trimmed();
8036}
8037
8039{
8040 switch ( outputType )
8041 {
8043 {
8045 QString code = QStringLiteral( "QgsProcessingParameterCoordinateOperation('%1', %2" )
8047 if ( mFlags & FlagOptional )
8048 code += QLatin1String( ", optional=True" );
8049 if ( !mSourceParameterName.isEmpty() )
8050 code += QStringLiteral( ", sourceCrsParameterName=%1" ).arg( valueAsPythonString( mSourceParameterName, c ) );
8051 if ( !mDestParameterName.isEmpty() )
8052 code += QStringLiteral( ", destinationCrsParameterName=%1" ).arg( valueAsPythonString( mDestParameterName, c ) );
8053
8054 if ( mSourceCrs.isValid() )
8055 code += QStringLiteral( ", staticSourceCrs=%1" ).arg( valueAsPythonString( mSourceCrs, c ) );
8056 if ( mDestCrs.isValid() )
8057 code += QStringLiteral( ", staticDestinationCrs=%1" ).arg( valueAsPythonString( mDestCrs, c ) );
8058
8059 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
8060 return code;
8061 }
8062 }
8063 return QString();
8064}
8065
8067{
8068 QStringList res;
8069 if ( !mSourceParameterName.isEmpty() )
8070 res << mSourceParameterName;
8071 if ( !mDestParameterName.isEmpty() )
8072 res << mDestParameterName;
8073 return res;
8074}
8075
8077{
8079 map.insert( QStringLiteral( "source_crs_parameter_name" ), mSourceParameterName );
8080 map.insert( QStringLiteral( "dest_crs_parameter_name" ), mDestParameterName );
8081 map.insert( QStringLiteral( "static_source_crs" ), mSourceCrs );
8082 map.insert( QStringLiteral( "static_dest_crs" ), mDestCrs );
8083 return map;
8084}
8085
8087{
8089 mSourceParameterName = map.value( QStringLiteral( "source_crs_parameter_name" ) ).toString();
8090 mDestParameterName = map.value( QStringLiteral( "dest_crs_parameter_name" ) ).toString();
8091 mSourceCrs = map.value( QStringLiteral( "static_source_crs" ) );
8092 mDestCrs = map.value( QStringLiteral( "static_dest_crs" ) );
8093 return true;
8094}
8095
8096QgsProcessingParameterCoordinateOperation *QgsProcessingParameterCoordinateOperation::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8097{
8098 QString def = definition;
8099
8100 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
8101 def = def.mid( 1 );
8102 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
8103 def.chop( 1 );
8104
8105 QVariant defaultValue = def;
8106 if ( def == QLatin1String( "None" ) )
8107 defaultValue = QVariant();
8108
8109 return new QgsProcessingParameterCoordinateOperation( name, description, defaultValue, QString(), QString(), QVariant(), QVariant(), isOptional );
8110}
8111
8112
8113//
8114// QgsProcessingParameterMapTheme
8115//
8116
8117QgsProcessingParameterMapTheme::QgsProcessingParameterMapTheme( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
8118 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8119{
8120
8121}
8122
8123
8128
8130{
8131 if ( !input.isValid() && !mDefault.isValid() )
8132 return mFlags & FlagOptional;
8133
8134 if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
8135 || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
8136 return mFlags & FlagOptional;
8137
8138 return true;
8139}
8140
8142{
8143 if ( !value.isValid() )
8144 return QStringLiteral( "None" );
8145
8146 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
8147 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
8148
8149 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
8150}
8151
8153{
8154 QString code = QStringLiteral( "##%1=" ).arg( mName );
8155 if ( mFlags & FlagOptional )
8156 code += QLatin1String( "optional " );
8157 code += QLatin1String( "maptheme " );
8158
8159 code += mDefault.toString();
8160 return code.trimmed();
8161}
8162
8164{
8165 switch ( outputType )
8166 {
8168 {
8169 QString code = QStringLiteral( "QgsProcessingParameterMapTheme('%1', %2" )
8171 if ( mFlags & FlagOptional )
8172 code += QLatin1String( ", optional=True" );
8173
8175 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
8176
8177 return code;
8178 }
8179 }
8180 return QString();
8181}
8182
8184{
8186 return map;
8187}
8188
8190{
8192 return true;
8193}
8194
8195QgsProcessingParameterMapTheme *QgsProcessingParameterMapTheme::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8196{
8197 QString def = definition;
8198 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
8199 def = def.mid( 1 );
8200 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
8201 def.chop( 1 );
8202
8203 QVariant defaultValue = def;
8204
8205 if ( defaultValue == QLatin1String( "None" ) || defaultValue.toString().isEmpty() )
8206 defaultValue = QVariant();
8207
8208 return new QgsProcessingParameterMapTheme( name, description, defaultValue, isOptional );
8209}
8210
8211
8212//
8213// QgsProcessingParameterDateTime
8214//
8215
8216QgsProcessingParameterDateTime::QgsProcessingParameterDateTime( const QString &name, const QString &description, Type type, const QVariant &defaultValue, bool optional, const QDateTime &minValue, const QDateTime &maxValue )
8217 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8218 , mMin( minValue )
8219 , mMax( maxValue )
8220 , mDataType( type )
8221{
8222 if ( mMin.isValid() && mMax.isValid() && mMin >= mMax )
8223 {
8224 QgsMessageLog::logMessage( QObject::tr( "Invalid datetime parameter \"%1\": min value %2 is >= max value %3!" ).arg( name, mMin.toString(), mMax.toString() ), QObject::tr( "Processing" ) );
8225 }
8226}
8227
8232
8234{
8235 QVariant input = value;
8236 if ( !input.isValid() )
8237 {
8238 if ( !defaultValue().isValid() )
8239 return mFlags & FlagOptional;
8240
8241 input = defaultValue();
8242 }
8243
8244 if ( input.userType() == QMetaType::type( "QgsProperty" ) )
8245 {
8246 return true;
8247 }
8248
8249 if ( input.type() != QVariant::DateTime && input.type() != QVariant::Date && input.type() != QVariant::Time && input.type() != QVariant::String )
8250 return false;
8251
8252 if ( ( input.type() == QVariant::DateTime || input.type() == QVariant::Date ) && mDataType == Time )
8253 return false;
8254
8255 if ( input.type() == QVariant::String )
8256 {
8257 const QString s = input.toString();
8258 if ( s.isEmpty() )
8259 return mFlags & FlagOptional;
8260
8261 input = QDateTime::fromString( s, Qt::ISODate );
8262 if ( mDataType == Time )
8263 {
8264 if ( !input.toDateTime().isValid() )
8265 input = QTime::fromString( s );
8266 else
8267 input = input.toDateTime().time();
8268 }
8269 }
8270
8271 if ( mDataType != Time )
8272 {
8273 const QDateTime res = input.toDateTime();
8274 return res.isValid() && ( res >= mMin || !mMin.isValid() ) && ( res <= mMax || !mMax.isValid() );
8275 }
8276 else
8277 {
8278 const QTime res = input.toTime();
8279 return res.isValid() && ( res >= mMin.time() || !mMin.isValid() ) && ( res <= mMax.time() || !mMax.isValid() );
8280 }
8281}
8282
8284{
8285 if ( !value.isValid() )
8286 return QStringLiteral( "None" );
8287
8288 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
8289 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
8290
8291 if ( value.type() == QVariant::DateTime )
8292 {
8293 const QDateTime dt = value.toDateTime();
8294 if ( !dt.isValid() )
8295 return QStringLiteral( "QDateTime()" );
8296 else
8297 return QStringLiteral( "QDateTime(QDate(%1, %2, %3), QTime(%4, %5, %6))" ).arg( dt.date().year() )
8298 .arg( dt.date().month() )
8299 .arg( dt.date().day() )
8300 .arg( dt.time().hour() )
8301 .arg( dt.time().minute() )
8302 .arg( dt.time().second() );
8303 }
8304 else if ( value.type() == QVariant::Date )
8305 {
8306 const QDate dt = value.toDate();
8307 if ( !dt.isValid() )
8308 return QStringLiteral( "QDate()" );
8309 else
8310 return QStringLiteral( "QDate(%1, %2, %3)" ).arg( dt.year() )
8311 .arg( dt.month() )
8312 .arg( dt.day() );
8313 }
8314 else if ( value.type() == QVariant::Time )
8315 {
8316 const QTime dt = value.toTime();
8317 if ( !dt.isValid() )
8318 return QStringLiteral( "QTime()" );
8319 else
8320 return QStringLiteral( "QTime(%4, %5, %6)" )
8321 .arg( dt.hour() )
8322 .arg( dt.minute() )
8323 .arg( dt.second() );
8324 }
8325 return value.toString();
8326}
8327
8329{
8331 QStringList parts;
8332 if ( mMin.isValid() )
8333 parts << QObject::tr( "Minimum value: %1" ).arg( mMin.toString( Qt::ISODate ) );
8334 if ( mMax.isValid() )
8335 parts << QObject::tr( "Maximum value: %1" ).arg( mMax.toString( Qt::ISODate ) );
8336 if ( mDefault.isValid() )
8337 parts << QObject::tr( "Default value: %1" ).arg( mDataType == DateTime ? mDefault.toDateTime().toString( Qt::ISODate ) :
8338 ( mDataType == Date ? mDefault.toDate().toString( Qt::ISODate ) : mDefault.toTime( ).toString() ) );
8339 const QString extra = parts.join( QLatin1String( "<br />" ) );
8340 if ( !extra.isEmpty() )
8341 text += QStringLiteral( "<p>%1</p>" ).arg( extra );
8342 return text;
8343}
8344
8346{
8347 switch ( outputType )
8348 {
8350 {
8351 QString code = QStringLiteral( "QgsProcessingParameterDateTime('%1', %2" )
8353 if ( mFlags & FlagOptional )
8354 code += QLatin1String( ", optional=True" );
8355
8356 code += QStringLiteral( ", type=%1" ).arg( mDataType == DateTime ? QStringLiteral( "QgsProcessingParameterDateTime.DateTime" )
8357 : mDataType == Date ? QStringLiteral( "QgsProcessingParameterDateTime.Date" )
8358 : QStringLiteral( "QgsProcessingParameterDateTime.Time" ) );
8359
8361 if ( mMin.isValid() )
8362 code += QStringLiteral( ", minValue=%1" ).arg( valueAsPythonString( mMin, c ) );
8363 if ( mMax.isValid() )
8364 code += QStringLiteral( ", maxValue=%1" ).arg( valueAsPythonString( mMax, c ) );
8365 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
8366 return code;
8367 }
8368 }
8369 return QString();
8370}
8371
8373{
8374 return mMin;
8375}
8376
8378{
8379 mMin = min;
8380}
8381
8383{
8384 return mMax;
8385}
8386
8388{
8389 mMax = max;
8390}
8391
8396
8398{
8399 mDataType = dataType;
8400}
8401
8403{
8405 map.insert( QStringLiteral( "min" ), mMin );
8406 map.insert( QStringLiteral( "max" ), mMax );
8407 map.insert( QStringLiteral( "data_type" ), mDataType );
8408 return map;
8409}
8410
8412{
8414 mMin = map.value( QStringLiteral( "min" ) ).toDateTime();
8415 mMax = map.value( QStringLiteral( "max" ) ).toDateTime();
8416 mDataType = static_cast< Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
8417 return true;
8418}
8419
8420QgsProcessingParameterDateTime *QgsProcessingParameterDateTime::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8421{
8422 return new QgsProcessingParameterDateTime( name, description, DateTime, definition.isEmpty() ? QVariant()
8423 : ( definition.toLower().trimmed() == QLatin1String( "none" ) ? QVariant() : definition ), isOptional );
8424}
8425
8426
8427
8428//
8429// QgsProcessingParameterProviderConnection
8430//
8431
8432QgsProcessingParameterProviderConnection::QgsProcessingParameterProviderConnection( const QString &name, const QString &description, const QString &provider, const QVariant &defaultValue, bool optional )
8433 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8434 , mProviderId( provider )
8435{
8436
8437}
8438
8439
8444
8446{
8447 if ( !input.isValid() && !mDefault.isValid() )
8448 return mFlags & FlagOptional;
8449
8450 if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
8451 || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
8452 return mFlags & FlagOptional;
8453
8454 return true;
8455}
8456
8458{
8459 if ( !value.isValid() )
8460 return QStringLiteral( "None" );
8461
8462 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
8463 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
8464
8465 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
8466}
8467
8469{
8470 QString code = QStringLiteral( "##%1=" ).arg( mName );
8471 if ( mFlags & FlagOptional )
8472 code += QLatin1String( "optional " );
8473 code += QLatin1String( "providerconnection " );
8474 code += mProviderId + ' ';
8475
8476 code += mDefault.toString();
8477 return code.trimmed();
8478}
8479
8481{
8482 switch ( outputType )
8483 {
8485 {
8486 QString code = QStringLiteral( "QgsProcessingParameterProviderConnection('%1', %2, '%3'" )
8487 .arg( name(), QgsProcessingUtils::stringToPythonLiteral( description() ), mProviderId );
8488 if ( mFlags & FlagOptional )
8489 code += QLatin1String( ", optional=True" );
8490
8492 code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
8493
8494 return code;
8495 }
8496 }
8497 return QString();
8498}
8499
8501{
8503 map.insert( QStringLiteral( "provider" ), mProviderId );
8504 return map;
8505}
8506
8508{
8510 mProviderId = map.value( QStringLiteral( "provider" ) ).toString();
8511 return true;
8512}
8513
8514QgsProcessingParameterProviderConnection *QgsProcessingParameterProviderConnection::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8515{
8516 QString def = definition;
8517 QString provider;
8518 if ( def.contains( ' ' ) )
8519 {
8520 provider = def.left( def.indexOf( ' ' ) );
8521 def = def.mid( def.indexOf( ' ' ) + 1 );
8522 }
8523 else
8524 {
8525 provider = def;
8526 def.clear();
8527 }
8528
8529 if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
8530 def = def.mid( 1 );
8531 if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
8532 def.chop( 1 );
8533
8534 QVariant defaultValue = def;
8535
8536 if ( defaultValue == QLatin1String( "None" ) || defaultValue.toString().isEmpty() )
8537 defaultValue = QVariant();
8538
8540}
8541
8542
8543//
8544// QgsProcessingParameterDatabaseSchema
8545//
8546
8547QgsProcessingParameterDatabaseSchema::QgsProcessingParameterDatabaseSchema( const QString &name, const QString &description, const QString &parentLayerParameterName, const QVariant &defaultValue, bool optional )
8548 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8549 , mParentConnectionParameterName( parentLayerParameterName )
8550{
8551
8552}
8553
8554
8559
8561{
8562 if ( !input.isValid() && !mDefault.isValid() )
8563 return mFlags & FlagOptional;
8564
8565 if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
8566 || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
8567 return mFlags & FlagOptional;
8568
8569 return true;
8570}
8571
8573{
8574 if ( !value.isValid() )
8575 return QStringLiteral( "None" );
8576
8577 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
8578 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
8579
8580 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
8581}
8582
8584{
8585 QString code = QStringLiteral( "##%1=" ).arg( mName );
8586 if ( mFlags & FlagOptional )
8587 code += QLatin1String( "optional " );
8588 code += QLatin1String( "databaseschema " );
8589
8590 code += mParentConnectionParameterName + ' ';
8591
8592 code += mDefault.toString();
8593 return code.trimmed();
8594}
8595
8597{
8598 switch ( outputType )
8599 {
8601 {
8602 QString code = QStringLiteral( "QgsProcessingParameterDatabaseSchema('%1', %2" )
8604 if ( mFlags & FlagOptional )
8605 code += QLatin1String( ", optional=True" );
8606
8607 code += QStringLiteral( ", connectionParameterName='%1'" ).arg( mParentConnectionParameterName );
8609 code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );
8610
8611 code += ')';
8612
8613 return code;
8614 }
8615 }
8616 return QString();
8617}
8618
8620{
8621 QStringList depends;
8622 if ( !mParentConnectionParameterName.isEmpty() )
8623 depends << mParentConnectionParameterName;
8624 return depends;
8625}
8626
8628{
8629 return mParentConnectionParameterName;
8630}
8631
8633{
8634 mParentConnectionParameterName = name;
8635}
8636
8638{
8640 map.insert( QStringLiteral( "mParentConnectionParameterName" ), mParentConnectionParameterName );
8641 return map;
8642}
8643
8645{
8647 mParentConnectionParameterName = map.value( QStringLiteral( "mParentConnectionParameterName" ) ).toString();
8648 return true;
8649}
8650
8651QgsProcessingParameterDatabaseSchema *QgsProcessingParameterDatabaseSchema::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8652{
8653 QString parent;
8654 QString def = definition;
8655
8656 const QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
8657 const QRegularExpressionMatch m = re.match( def );
8658 if ( m.hasMatch() )
8659 {
8660 parent = m.captured( 1 ).trimmed();
8661 def = m.captured( 2 );
8662 }
8663 else
8664 {
8665 parent = def;
8666 def.clear();
8667 }
8668
8669 return new QgsProcessingParameterDatabaseSchema( name, description, parent, def.isEmpty() ? QVariant() : def, isOptional );
8670}
8671
8672//
8673// QgsProcessingParameterDatabaseTable
8674//
8675
8677 const QString &connectionParameterName,
8678 const QString &schemaParameterName,
8679 const QVariant &defaultValue, bool optional, bool allowNewTableNames )
8680 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8681 , mParentConnectionParameterName( connectionParameterName )
8682 , mParentSchemaParameterName( schemaParameterName )
8683 , mAllowNewTableNames( allowNewTableNames )
8684{
8685
8686}
8687
8688
8693
8695{
8696 if ( !input.isValid() && !mDefault.isValid() )
8697 return mFlags & FlagOptional;
8698
8699 if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
8700 || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
8701 return mFlags & FlagOptional;
8702
8703 return true;
8704}
8705
8707{
8708 if ( !value.isValid() )
8709 return QStringLiteral( "None" );
8710
8711 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
8712 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
8713
8714 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
8715}
8716
8718{
8719 QString code = QStringLiteral( "##%1=" ).arg( mName );
8720 if ( mFlags & FlagOptional )
8721 code += QLatin1String( "optional " );
8722 code += QLatin1String( "databasetable " );
8723
8724 code += ( mParentConnectionParameterName.isEmpty() ? QStringLiteral( "none" ) : mParentConnectionParameterName ) + ' ';
8725 code += ( mParentSchemaParameterName.isEmpty() ? QStringLiteral( "none" ) : mParentSchemaParameterName ) + ' ';
8726
8727 code += mDefault.toString();
8728 return code.trimmed();
8729}
8730
8732{
8733 switch ( outputType )
8734 {
8736 {
8737 QString code = QStringLiteral( "QgsProcessingParameterDatabaseTable('%1', %2" )
8739 if ( mFlags & FlagOptional )
8740 code += QLatin1String( ", optional=True" );
8741
8742 if ( mAllowNewTableNames )
8743 code += QLatin1String( ", allowNewTableNames=True" );
8744
8745 code += QStringLiteral( ", connectionParameterName='%1'" ).arg( mParentConnectionParameterName );
8746 code += QStringLiteral( ", schemaParameterName='%1'" ).arg( mParentSchemaParameterName );
8748 code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );
8749
8750 code += ')';
8751
8752 return code;
8753 }
8754 }
8755 return QString();
8756}
8757
8759{
8760 QStringList depends;
8761 if ( !mParentConnectionParameterName.isEmpty() )
8762 depends << mParentConnectionParameterName;
8763 if ( !mParentSchemaParameterName.isEmpty() )
8764 depends << mParentSchemaParameterName;
8765 return depends;
8766}
8767
8769{
8770 return mParentConnectionParameterName;
8771}
8772
8774{
8775 mParentConnectionParameterName = name;
8776}
8777
8779{
8780 return mParentSchemaParameterName;
8781}
8782
8784{
8785 mParentSchemaParameterName = name;
8786}
8787
8789{
8791 map.insert( QStringLiteral( "mParentConnectionParameterName" ), mParentConnectionParameterName );
8792 map.insert( QStringLiteral( "mParentSchemaParameterName" ), mParentSchemaParameterName );
8793 map.insert( QStringLiteral( "mAllowNewTableNames" ), mAllowNewTableNames );
8794 return map;
8795}
8796
8798{
8800 mParentConnectionParameterName = map.value( QStringLiteral( "mParentConnectionParameterName" ) ).toString();
8801 mParentSchemaParameterName = map.value( QStringLiteral( "mParentSchemaParameterName" ) ).toString();
8802 mAllowNewTableNames = map.value( QStringLiteral( "mAllowNewTableNames" ), false ).toBool();
8803 return true;
8804}
8805
8806QgsProcessingParameterDatabaseTable *QgsProcessingParameterDatabaseTable::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8807{
8808 QString connection;
8809 QString schema;
8810 QString def = definition;
8811
8812 const QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*+)\\b\\s*(.*)$" ) );
8813 const QRegularExpressionMatch m = re.match( def );
8814 if ( m.hasMatch() )
8815 {
8816 connection = m.captured( 1 ).trimmed();
8817 if ( connection == QLatin1String( "none" ) )
8818 connection.clear();
8819 schema = m.captured( 2 ).trimmed();
8820 if ( schema == QLatin1String( "none" ) )
8821 schema.clear();
8822 def = m.captured( 3 );
8823 }
8824
8825 return new QgsProcessingParameterDatabaseTable( name, description, connection, schema, def.isEmpty() ? QVariant() : def, isOptional );
8826}
8827
8829{
8830 return mAllowNewTableNames;
8831}
8832
8834{
8835 mAllowNewTableNames = allowNewTableNames;
8836}
8837
8838//
8839// QgsProcessingParameterPointCloudLayer
8840//
8841
8843 const QVariant &defaultValue, bool optional )
8844 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8845{
8846}
8847
8852
8854{
8855 QVariant var = v;
8856
8857 if ( !var.isValid() )
8858 {
8859 if ( !defaultValue().isValid() )
8860 return mFlags & FlagOptional;
8861
8862 var = defaultValue();
8863 }
8864
8865 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
8866 {
8867 const QgsProperty p = var.value< QgsProperty >();
8869 {
8870 var = p.staticValue();
8871 }
8872 else
8873 {
8874 return true;
8875 }
8876 }
8877
8878 if ( qobject_cast< QgsPointCloudLayer * >( qvariant_cast<QObject *>( var ) ) )
8879 return true;
8880
8881 if ( var.type() != QVariant::String || var.toString().isEmpty() )
8882 return mFlags & FlagOptional;
8883
8884 if ( !context )
8885 {
8886 // that's as far as we can get without a context
8887 return true;
8888 }
8889
8890 // try to load as layer
8892 return true;
8893
8894 return false;
8895}
8896
8898{
8899 if ( !val.isValid() )
8900 return QStringLiteral( "None" );
8901
8902 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
8903 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
8904
8905 QVariantMap p;
8906 p.insert( name(), val );
8910}
8911
8912QString QgsProcessingParameterPointCloudLayer::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
8913{
8915}
8916
8918{
8920}
8921
8923{
8924 return QgsProviderRegistry::instance()->filePointCloudFilters() + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
8925}
8926
8927QgsProcessingParameterPointCloudLayer *QgsProcessingParameterPointCloudLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
8928{
8929 return new QgsProcessingParameterPointCloudLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
8930}
8931
8932//
8933// QgsProcessingParameterAnnotationLayer
8934//
8935
8937 const QVariant &defaultValue, bool optional )
8938 : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
8939{
8940}
8941
8946
8948{
8949 QVariant var = v;
8950 if ( !var.isValid() )
8951 {
8952 if ( !defaultValue().isValid() )
8953 return mFlags & FlagOptional;
8954
8955 var = defaultValue();
8956 }
8957
8958 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
8959 {
8960 const QgsProperty p = var.value< QgsProperty >();
8962 {
8963 var = p.staticValue();
8964 }
8965 else
8966 {
8967 return true;
8968 }
8969 }
8970
8971 if ( qobject_cast< QgsAnnotationLayer * >( qvariant_cast<QObject *>( var ) ) )
8972 return true;
8973
8974 if ( var.type() != QVariant::String || var.toString().isEmpty() )
8975 return mFlags & FlagOptional;
8976
8977 if ( !context )
8978 {
8979 // that's as far as we can get without a context
8980 return true;
8981 }
8982
8983 // try to load as layer
8985 return true;
8986
8987 return false;
8988}
8989
8991{
8992 if ( !val.isValid() )
8993 return QStringLiteral( "None" );
8994
8995 if ( val.userType() == QMetaType::type( "QgsProperty" ) )
8996 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
8997
8998 QVariantMap p;
8999 p.insert( name(), val );
9001 return layer ? QgsProcessingUtils::stringToPythonLiteral( layer == context.project()->mainAnnotationLayer() ? QStringLiteral( "main" ) : layer->id() )
9003}
9004
9005QString QgsProcessingParameterAnnotationLayer::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
9006{
9008}
9009
9011{
9013}
9014
9015QgsProcessingParameterAnnotationLayer *QgsProcessingParameterAnnotationLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
9016{
9017 return new QgsProcessingParameterAnnotationLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
9018}
9019
9020QgsProcessingParameterPointCloudDestination::QgsProcessingParameterPointCloudDestination( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, bool createByDefault )
9021 : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
9022{
9023}
9024
9029
9031{
9032 QVariant var = input;
9033 if ( !var.isValid() )
9034 {
9035 if ( !defaultValue().isValid() )
9036 return mFlags & FlagOptional;
9037
9038 var = defaultValue();
9039 }
9040
9041 if ( var.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
9042 {
9043 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
9044 var = fromVar.sink;
9045 }
9046
9047 if ( var.userType() == QMetaType::type( "QgsProperty" ) )
9048 {
9049 const QgsProperty p = var.value< QgsProperty >();
9051 {
9052 var = p.staticValue();
9053 }
9054 else
9055 {
9056 return true;
9057 }
9058 }
9059
9060 if ( var.type() != QVariant::String )
9061 return false;
9062
9063 if ( var.toString().isEmpty() )
9064 return mFlags & FlagOptional;
9065
9066 return true;
9067}
9068
9070{
9071 if ( !value.isValid() )
9072 return QStringLiteral( "None" );
9073
9074 if ( value.userType() == QMetaType::type( "QgsProperty" ) )
9075 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
9076
9077 if ( value.userType() == QMetaType::type( "QgsProcessingOutputLayerDefinition" ) )
9078 {
9079 const QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
9081 {
9082 return QgsProcessingUtils::stringToPythonLiteral( fromVar.sink.staticValue().toString() );
9083 }
9084 else
9085 {
9086 return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
9087 }
9088 }
9089
9090 return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
9091}
9092
9097
9099{
9100 if ( auto *lOriginalProvider = originalProvider() )
9101 {
9102 return lOriginalProvider->defaultPointCloudFileExtension();
9103 }
9104 else if ( QgsProcessingProvider *p = provider() )
9105 {
9106 return p->defaultPointCloudFileExtension();
9107 }
9108 else
9109 {
9111 }
9112}
9113
9115{
9116 const QStringList exts = supportedOutputPointCloudLayerExtensions();
9117 QStringList filters;
9118 for ( const QString &ext : exts )
9119 {
9120 filters << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
9121 }
9122 return filters.join( QLatin1String( ";;" ) ) + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" );
9123}
9124
9126{
9127 if ( auto *lOriginalProvider = originalProvider() )
9128 {
9129 return lOriginalProvider->supportedOutputPointCloudLayerExtensions();
9130 }
9131 else if ( QgsProcessingProvider *p = provider() )
9132 {
9133 return p->supportedOutputPointCloudLayerExtensions();
9134 }
9135 else
9136 {
9138 return QStringList() << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
9139 }
9140}
9141
9142QgsProcessingParameterPointCloudDestination *QgsProcessingParameterPointCloudDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
9143{
9144 return new QgsProcessingParameterPointCloudDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
9145}
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.
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transform the point from the source CRS to the destination CRS.
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const
Transforms a rectangle from the source CRS to the destination CRS.
Custom exception class for Coordinate Reference System related exceptions.
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.
QgsGeometry densifyByCount(int extraNodesPerSegment) const
Returns a copy of the geometry which has been densified by adding the specified number of extra nodes...
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
QString lastError() const
Returns an error string referring to the last error encountered either when this geometry was created...
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
QgsWkbTypes::GeometryType type
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
QgsGeometry centroid() const
Returns the center of mass of a geometry.
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.
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...
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.
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
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.
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...
@ FlagCreateIndividualOutputPerInputFeature
If set, every feature processed from this source will be placed into its own individually created out...
@ FlagOverrideDefaultGeometryCheck
If set, the default geometry check method (as dictated by QgsProcessingContext) will be overridden fo...
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.
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 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 QgsFeatureSink * parameterAsSink(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context, QString &destinationIdentifier, QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), const QVariantMap &createOptions=QVariantMap(), const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList())
Evaluates the parameter with matching definition to a feature sink.
static bool parameterAsBoolean(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static boolean value.
static QgsCoordinateReferenceSystem parameterAsPointCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the coordinate reference system associated with an point parameter value.
static QgsLayoutItem * parameterAsLayoutItem(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsPrintLayout *layout)
Evaluates the parameter with matching definition to a print layout item, taken from the specified lay...
static bool parameterAsBool(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static boolean value.
static QString parameterAsCompatibleSourceLayerPathAndLayerName(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat=QString("shp"), QgsProcessingFeedback *feedback=nullptr, QString *layerName=nullptr)
Evaluates the parameter with matching definition to a source vector layer file path and layer name of...
static QgsMeshLayer * parameterAsMeshLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition and value to a mesh layer.
static QString parameterAsCompatibleSourceLayerPath(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat=QString("shp"), QgsProcessingFeedback *feedback=nullptr)
Evaluates the parameter with matching definition to a source vector layer file path of compatible for...
static QgsProcessingParameterDefinition * parameterFromScriptCode(const QString &code)
Creates a new QgsProcessingParameterDefinition using the configuration from a supplied script code st...
static QColor parameterAsColor(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the color associated with an point parameter value, or an invalid color if the parameter was ...
static QgsVectorLayer * parameterAsVectorLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a vector layer.
static 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 QgsFeatureSink * createFeatureSink(QString &destination, QgsProcessingContext &context, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &createOptions=QVariantMap(), const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), QgsRemappingSinkDefinition *remappingDefinition=nullptr)
Creates a feature sink ready for adding features.
static QString encodeProviderKeyAndUri(const QString &providerKey, const QString &uri)
Encodes a provider key and layer uri to a single string, for use with decodeProviderKeyAndUri()
LayerHint
Layer type hints.
@ 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 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.
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
SourceType
Data source types enum.
@ TypePlugin
Plugin layers.
@ TypeVectorLine
Vector line layers.
@ TypeMapLayer
Any map layer type (raster, vector, mesh, point cloud, annotation or plugin layer)
@ TypeVectorPolygon
Vector polygon layers.
@ TypeFile
Files (i.e. non map layer sources, such as text files)
@ TypeAnnotation
Annotation layers.
@ TypePointCloud
Point cloud layers.
@ TypeMesh
Mesh layers.
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ TypeRaster
Raster layers.
@ TypeVectorPoint
Vector point layers.
@ TypeVectorAnyGeometry
Any vector layer with geometry.
static QString sourceTypeToString(SourceType type)
Converts a source type to a string representation.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:104
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.
@ ExpressionBasedProperty
Expression based property (QgsExpressionBasedProperty)
@ StaticProperty
Static property (QgsStaticProperty)
@ FieldBasedProperty
Field based property (QgsFieldBasedProperty)
@ InvalidProperty
Invalid (not set) property.
QString asExpression() const
Returns an expression string representing the state of the property, or an empty string if the proper...
QString valueAsString(const QgsExpressionContext &context, const QString &defaultString=QString(), bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a string.
QVariant value(const QgsExpressionContext &context, const QVariant &defaultValue=QVariant(), bool *ok=nullptr) const
Calculates the current value of the property, including any transforms which are set for the property...
QVariant toVariant() const
Saves this property to a QVariantMap, wrapped in a QVariant.
bool loadVariant(const QVariant &property)
Loads this property from a QVariantMap, wrapped in a QVariant.
QVariant staticValue() const
Returns the current static value for the property.
Type propertyType() const
Returns the property type.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QString fileVectorFilters() const
Returns a file filter string for supported vector files.
QString fileRasterFilters() const
Returns a file filter string for supported raster files.
QString fileMeshFilters() const
Returns a file filter string for supported mesh files.
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.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
A QgsGeometry with associated coordinate reference system.
static QgsReferencedGeometry fromReferencedPointXY(const QgsReferencedPointXY &point)
Construct a new QgsReferencedGeometry from referenced point.
static QgsReferencedGeometry fromReferencedRect(const QgsReferencedRectangle &rectangle)
Construct a new QgsReferencedGeometry from referenced rectangle.
A QgsPointXY with associated coordinate reference system.
A QgsRectangle with associated coordinate reference system.
Defines the parameters used to remap features when creating a QgsRemappingProxyFeatureSink.
static QColor parseColorWithAlpha(const QString &colorStr, bool &containsAlpha, bool strictEval=false)
Attempts to parse a string as a color using a variety of common formats, including hex codes,...
DistanceUnit
Units of distance.
@ DistanceUnknownUnit
Unknown distance unit.
TemporalUnit
Temporal units.
@ TemporalDays
Days.
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.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Type
The WKB type describes the number of dimensions a geometry has.
Definition qgswkbtypes.h:70
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:2466
#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