27#include "moc_qgsprocessingparameterswidget.cpp"
29using namespace Qt::StringLiterals;
37 , mActiveLayer( activeLayer )
38 , mMessageBar( messageBar )
40 Q_ASSERT( mAlgorithm );
45 scrollAreaWidgetContents->setContentsMargins( 4, 4, 4, 4 );
48QgsProcessingParametersWidget::~QgsProcessingParametersWidget()
50 qDeleteAll( mWrappers );
60 return mContextGenerator ? mContextGenerator->processingContext() :
nullptr;
63QVariantMap QgsProcessingParametersWidget::createProcessingParameters( Flags flags )
65 QList< QgsProcessingParametersGenerator::ParameterValidationResult > validationResults;
66 return createAndValidateParameters( flags, validationResults );
71 mContextGenerator = generator;
74QList<QgsProcessingParametersGenerator::ParameterValidationResult> QgsProcessingParametersWidget::validate()
const
76 QList< QgsProcessingParametersGenerator::ParameterValidationResult > validationResults;
78 return validationResults;
81QVariantMap QgsProcessingParametersWidget::createAndValidateParameters(
87 const bool includeDefault = !flags.testFlag( Flag::SkipDefaultValueParameters );
88 const bool validate = !flags.testFlag( Flag::SkipValidation );
90 QVariantMap parameters = mExtraParameters;
99 if ( !definition->isDestination() )
113 if ( definition->defaultValue() != value || includeDefault )
115 parameters[definition->name()] = value;
118 if ( validate && !definition->checkValueIsAcceptable( value ) )
123 validationResults.append( result );
128 if ( mInPlace && definition->name() ==
"OUTPUT"_L1 )
130 parameters[definition->name()] = u
"memory:"_s;
141 if ( wrapper->
customProperties().value( u
"OPEN_AFTER_RUNNING"_s ).toBool() )
146 if ( value.userType() == qMetaTypeId<QgsProcessingOutputLayerDefinition>() )
150 value = QVariant::fromValue( outputDefinition );
155 parameters[definition->name()] = value;
159 if ( destinationParam )
162 const bool ok = destinationParam->isSupportedOutputValue( value, *context, error );
169 validationResults.append( result );
180void QgsProcessingParametersWidget::setParameters(
const QVariantMap ¶meters )
184 mExtraParameters.clear();
188 auto paramIt = parameters.constFind( definition->name() );
191 if ( paramIt != parameters.constEnd() )
193 mExtraParameters.insert( definition->name(), paramIt.value() );
198 if ( paramIt == parameters.constEnd() )
203 const QVariant value = paramIt.value();
205 auto wrapperIt = mWrappers.constFind( definition->name() );
206 if ( wrapperIt != mWrappers.constEnd() )
208 wrapperIt.value()->setParameterValue( value, *context );
215 return mWrappers.value( name );
218QList<QgsAbstractProcessingParameterWidgetWrapper *> QgsProcessingParametersWidget::wrappers()
const
220 return mWrappers.values();
223void QgsProcessingParametersWidget::parameterChanged()
225 auto wrapper = qobject_cast< QgsAbstractProcessingParameterWidgetWrapper * >( sender() );
229 const QVariantMap defaultValues
232 if ( !defaultValues.empty() )
234 setParameters( defaultValues );
238void QgsProcessingParametersWidget::initWidgets()
261 if (
auto modelAlgorithm =
dynamic_cast< const QgsProcessingModelAlgorithm *
>( mAlgorithm ) )
263 widgetContext.
setModel(
const_cast< QgsProcessingModelAlgorithm *
>( modelAlgorithm ) );
266 QString inPlaceInputParameterName = u
"INPUT"_s;
269 inPlaceInputParameterName = featureAlgorithm->inputParameterName();
272 bool hasNonInputParams =
false;
275 if ( definition->name() != inPlaceInputParameterName && definition->name() !=
"OUTPUT"_L1 )
277 hasNonInputParams =
true;
283 if ( mInPlace && !hasNonInputParams )
285 auto widget =
new QWidget(
this );
286 auto layout =
new QVBoxLayout( widget );
287 auto label =
new QLabel( widget );
288 label->setWordWrap(
true );
290 const QString layerName = mActiveLayer ? mActiveLayer->name() : QString();
291 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 );
292 label->setText( infoText );
294 layout->addWidget( label );
295 layout->addStretch();
296 addExtraWidget( widget );
307 if ( definition->isDestination() )
312 if ( mInPlace && ( definition->name() == inPlaceInputParameterName || definition->name() ==
"OUTPUT"_L1 ) )
319 wrapper->setLinkedVectorLayer( qobject_cast< QgsVectorLayer * >( mActiveLayer.get() ) );
320 mWrappers.insert( definition->name(), wrapper );
334 mWrappers.insert( definition->name(), wrapper );
343 addParameterLabel( definition, label );
345 addParameterWidget( definition, widget, wrapper->
stretch() );
357 if ( mInPlace && ( output->name() == inPlaceInputParameterName || output->name() ==
"OUTPUT"_L1 ) )
371 mWrappers.insert( output->name(), wrapper );
375 addOutputLabel( label );
379 addOutputWidget( widget, wrapper->
stretch() );
382 const QList< QgsAbstractProcessingParameterWidgetWrapper * > wrapperList = mWrappers.values();
383 for (
auto it = mWrappers.constBegin(); it != mWrappers.constEnd(); ++it )
385 it.value()->postInitialize( wrapperList );
392 mAdvancedGroupLayout->addWidget( widget, stretch );
394 mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 2, widget, stretch );
400 mAdvancedGroupLayout->addWidget( label );
402 mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 2, label );
405void QgsProcessingParametersWidget::addOutputLabel( QWidget *label )
407 mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 1, label );
410void QgsProcessingParametersWidget::addOutputWidget( QWidget *widget,
int stretch )
412 mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 1, widget, stretch );
415void QgsProcessingParametersWidget::addExtraWidget( QWidget *widget )
417 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.