27#include "moc_qgsprocessingparameterswidget.cpp"
29using namespace Qt::StringLiterals;
37 , mActiveLayer( activeLayer )
38 , mMessageBar( messageBar )
40 Q_ASSERT( mAlgorithm );
47 scrollAreaWidgetContents->setContentsMargins( 4, 4, 4, 4 );
50QgsProcessingParametersWidget::~QgsProcessingParametersWidget()
52 qDeleteAll( mWrappers );
62 return mContextGenerator ? mContextGenerator->processingContext() :
nullptr;
65QVariantMap QgsProcessingParametersWidget::createProcessingParameters( Flags flags )
67 QList< QgsProcessingParametersGenerator::ParameterValidationResult > validationResults;
68 return createAndValidateParameters( flags, validationResults );
73 mContextGenerator = generator;
76QList<QgsProcessingParametersGenerator::ParameterValidationResult> QgsProcessingParametersWidget::validate()
const
78 QList< QgsProcessingParametersGenerator::ParameterValidationResult > validationResults;
80 return validationResults;
83QVariantMap QgsProcessingParametersWidget::createAndValidateParameters(
89 const bool includeDefault = !flags.testFlag( Flag::SkipDefaultValueParameters );
90 const bool validate = !flags.testFlag( Flag::SkipValidation );
92 QVariantMap parameters = mExtraParameters;
101 if ( !definition->isDestination() )
115 if ( definition->defaultValue() != value || includeDefault )
117 parameters[definition->name()] = value;
120 if ( validate && !definition->checkValueIsAcceptable( value ) )
125 validationResults.append( result );
130 if ( mInPlace && definition->name() ==
"OUTPUT"_L1 )
132 parameters[definition->name()] = u
"memory:"_s;
143 if ( wrapper->
customProperties().value( u
"OPEN_AFTER_RUNNING"_s ).toBool() )
148 if ( value.userType() == qMetaTypeId<QgsProcessingOutputLayerDefinition>() )
152 value = QVariant::fromValue( outputDefinition );
157 parameters[definition->name()] = value;
161 if ( destinationParam )
164 const bool ok = destinationParam->isSupportedOutputValue( value, *context, error );
171 validationResults.append( result );
182void QgsProcessingParametersWidget::setParameters(
const QVariantMap ¶meters )
186 mExtraParameters.clear();
190 auto paramIt = parameters.constFind( definition->name() );
193 if ( paramIt != parameters.constEnd() )
195 mExtraParameters.insert( definition->name(), paramIt.value() );
200 if ( paramIt == parameters.constEnd() )
205 const QVariant value = paramIt.value();
207 auto wrapperIt = mWrappers.constFind( definition->name() );
208 if ( wrapperIt != mWrappers.constEnd() )
210 wrapperIt.value()->setParameterValue( value, *context );
217 return mWrappers.value( name );
220QList<QgsAbstractProcessingParameterWidgetWrapper *> QgsProcessingParametersWidget::wrappers()
const
222 return mWrappers.values();
225void QgsProcessingParametersWidget::parameterChanged()
227 auto wrapper = qobject_cast< QgsAbstractProcessingParameterWidgetWrapper * >( sender() );
231 const QVariantMap defaultValues
234 if ( !defaultValues.empty() )
236 setParameters( defaultValues );
240void QgsProcessingParametersWidget::initWidgets()
263 if (
auto modelAlgorithm =
dynamic_cast< const QgsProcessingModelAlgorithm *
>( mAlgorithm ) )
265 widgetContext.
setModel(
const_cast< QgsProcessingModelAlgorithm *
>( modelAlgorithm ) );
268 QString inPlaceInputParameterName = u
"INPUT"_s;
271 inPlaceInputParameterName = featureAlgorithm->inputParameterName();
274 bool hasNonInputParams =
false;
277 if ( definition->name() != inPlaceInputParameterName && definition->name() !=
"OUTPUT"_L1 )
279 hasNonInputParams =
true;
285 if ( mInPlace && !hasNonInputParams )
287 auto widget =
new QWidget(
this );
288 auto layout =
new QVBoxLayout( widget );
289 auto label =
new QLabel( widget );
290 label->setWordWrap(
true );
292 const QString layerName = mActiveLayer ? mActiveLayer->name() : QString();
293 const QString infoText = tr(
"<i>No additional parameters are required. This algorithm will activate edit mode and modify the features on layer <b>%1</b> in place.</i>" ).arg( layerName );
294 label->setText( infoText );
296 layout->addWidget( label );
297 layout->addStretch();
298 addExtraWidget( widget );
309 if ( definition->isDestination() )
314 if ( mInPlace && ( definition->name() == inPlaceInputParameterName || definition->name() ==
"OUTPUT"_L1 ) )
321 wrapper->setLinkedVectorLayer( qobject_cast< QgsVectorLayer * >( mActiveLayer.get() ) );
322 mWrappers.insert( definition->name(), wrapper );
336 mWrappers.insert( definition->name(), wrapper );
345 addParameterLabel( definition, label );
347 addParameterWidget( definition, widget, wrapper->
stretch() );
359 if ( mInPlace && ( output->name() == inPlaceInputParameterName || output->name() ==
"OUTPUT"_L1 ) )
373 mWrappers.insert( output->name(), wrapper );
377 addOutputLabel( label );
381 addOutputWidget( widget, wrapper->
stretch() );
384 const QList< QgsAbstractProcessingParameterWidgetWrapper * > wrapperList = mWrappers.values();
385 for (
auto it = mWrappers.constBegin(); it != mWrappers.constEnd(); ++it )
387 it.value()->postInitialize( wrapperList );
394 mAdvancedGroupLayout->addWidget( widget, stretch );
396 mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 3, widget, stretch );
402 mAdvancedGroupLayout->addWidget( label );
404 mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 3, label );
407void QgsProcessingParametersWidget::addOutputLabel( QWidget *label )
410 mOutputGroupLayout->addWidget( label );
413void QgsProcessingParametersWidget::addOutputWidget( QWidget *widget,
int stretch )
416 mOutputGroupLayout->addWidget( widget, stretch );
419void QgsProcessingParametersWidget::addExtraWidget( QWidget *widget )
421 mScrollAreaLayout->addWidget( widget );
@ Standard
Standard (single-run) algorithm mode.
@ Hidden
Parameter is hidden and should not be shown to users.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
static QgsProcessingGuiRegistry * processingGuiRegistry()
Returns the global processing gui registry, used for registering the GUI behavior of processing algor...
Base class for all map layer types.
A bar for displaying non-blocking messages to the user.
Abstract base class for processing algorithms.
QgsProcessingParameterDefinitions destinationParameterDefinitions() const
Returns a list of destination parameters definitions utilized by the algorithm.
An interface for objects which can create Processing contexts.
Contains information about the context in which a processing algorithm is executed.
Base class for all parameter definitions which represent file or layer destinations,...
An abstract QgsProcessingAlgorithm base class for processing algorithms which operate "feature-by-fea...
QgsAbstractProcessingParameterWidgetWrapper * createParameterWidgetWrapper(const QgsProcessingParameterDefinition *parameter, Qgis::ProcessingMode type)
Creates a new parameter widget wrapper for the given parameter.
QgsProcessingParameterWidgetContext createWidgetContext() override
Register a Processing widget context.
Encapsulates settings relating to a feature sink or output raster layer for a processing algorithm.
QgsProject * destinationProject
Destination project.
Base class for the definition of processing parameters.
QString name() const
Returns the name of the parameter.
Qgis::ProcessingParameterFlags flags() const
Returns any flags associated with the parameter.
Contains settings which reflect the context in which a Processing parameter widget is shown.
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
void setModel(QgsProcessingModelAlgorithm *model)
Sets the model which the parameter widget is associated with.
Encapsulates the result of validating parameters shown in the generator.
QString message
Validation message.
QgsProcessingParametersGenerator::ValidationResult result
Validation result.
QString parameterName
Associated parameter name.
@ InvalidValue
Generic result for invalid parameter values.
@ InvalidOutputExtension
Output parameter extension is invalid.
@ SkipValidation
Skip validation of parameters.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
static QgsProject * instance()
Returns the QgsProject singleton instance.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
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 allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
QList< const QgsProcessingParameterDefinition * > QgsProcessingParameterDefinitions
List of processing parameters.