24#include <QItemSelectionModel>
28#include <QStandardItemModel>
32#include "moc_qgsprocessingfieldmapwidgetwrapper.cpp"
34using namespace Qt::StringLiterals;
43QgsProcessingFieldMapPanelWidget::QgsProcessingFieldMapPanelWidget( QWidget *parent )
48 mModel = mFieldsView->model();
49 mFieldsView->setDestinationEditable(
true );
51 mLayerCombo->setAllowEmptyLayer(
true );
54 connect( mResetButton, &QPushButton::clicked,
this, &QgsProcessingFieldMapPanelWidget::loadFieldsFromLayer );
55 connect( mAddButton, &QPushButton::clicked,
this, &QgsProcessingFieldMapPanelWidget::addField );
60 connect( mLoadLayerFieldsButton, &QPushButton::clicked,
this, &QgsProcessingFieldMapPanelWidget::loadLayerFields );
64 if ( !mBlockChangedSignal )
71void QgsProcessingFieldMapPanelWidget::setLayer(
QgsVectorLayer *layer )
73 if ( layer == mLayer )
77 mFieldsView->setSourceLayer( mLayer );
78 if ( mModel->rowCount() == 0 )
80 loadFieldsFromLayer();
84 if ( mSkipConfirmDialog )
89 QMessageBox dlg(
this );
90 dlg.setText( tr(
"Do you want to reset the field mapping?" ) );
91 dlg.setStandardButtons(
92 QMessageBox::StandardButtons( QMessageBox::Yes | QMessageBox::No )
94 dlg.setDefaultButton( QMessageBox::No );
95 if ( dlg.exec() == QMessageBox::Yes )
97 loadFieldsFromLayer();
106QVariant QgsProcessingFieldMapPanelWidget::value()
const
108 const QList<QgsFieldMappingModel::Field> mapping = mFieldsView->mapping();
110 QVariantList results;
111 results.reserve( mapping.size() );
115 def.insert( u
"name"_s, field.field.name() );
116 def.insert( u
"type"_s,
static_cast<int>( field.field.type() ) );
117 def.insert( u
"type_name"_s, field.field.typeName() );
118 def.insert( u
"length"_s, field.field.length() );
119 def.insert( u
"precision"_s, field.field.precision() );
120 def.insert( u
"sub_type"_s,
static_cast<int>( field.field.subType() ) );
121 def.insert( u
"expression"_s, field.expression );
122 def.insert( u
"alias"_s, field.field.alias() );
123 def.insert( u
"comment"_s, field.field.comment() );
124 results.append( def );
129void QgsProcessingFieldMapPanelWidget::setValue(
const QVariant &value )
131 if ( value.userType() != QMetaType::Type::QVariantList )
135 QMap<QString, QString> expressions;
138 const QVariantList fields = value.toList();
139 for (
const QVariant &field : fields )
141 const QVariantMap map = field.toMap();
142 QgsField f( map.value( u
"name"_s ).toString(),
static_cast<QMetaType::Type
>( map.value( u
"type"_s,
static_cast<int>( QMetaType::Type::UnknownType ) ).toInt() ), map.value( u
"type_name"_s, QVariant::typeToName(
static_cast<QMetaType::Type
>( map.value( u
"type"_s,
static_cast<int>( QMetaType::Type::UnknownType ) ).toInt() ) ) ).toString(), map.value( u
"length"_s, 0 ).toInt(), map.value( u
"precision"_s, 0 ).toInt(), QString(),
static_cast<QMetaType::Type
>( map.value( u
"sub_type"_s,
QgsVariantUtils::createNullVariant( QMetaType::Type::UnknownType ) ).toInt() ) );
143 f.setAlias( map.value( u
"alias"_s ).toString() );
144 f.setComment( map.value( u
"comment"_s ).toString() );
146 if ( map.contains( u
"constraints"_s ) )
158 f.setConstraints( fieldConstraints );
161 if ( !map.value( u
"expression"_s ).toString().isEmpty() )
163 expressions.insert( f.name(), map.value( u
"expression"_s ).toString() );
166 destinationFields.
append( f );
169 mBlockChangedSignal =
true;
170 if ( destinationFields.
size() > 0 )
171 mFieldsView->setDestinationFields( destinationFields, expressions );
172 mBlockChangedSignal =
false;
179 mFieldsView->registerExpressionContextGenerator( generator );
182void QgsProcessingFieldMapPanelWidget::loadFieldsFromLayer()
186 mFieldsView->setSourceFields( mLayer->fields() );
187 mFieldsView->setDestinationFields( mLayer->fields() );
191void QgsProcessingFieldMapPanelWidget::addField()
193 const int rowCount = mModel->rowCount();
194 mModel->appendField(
QgsField( u
"new_field"_s ) );
195 const QModelIndex index = mModel->index( rowCount, 0 );
196 mFieldsView->selectionModel()->select(
198 QItemSelectionModel::SelectionFlags(
199 QItemSelectionModel::Clear | QItemSelectionModel::Select | QItemSelectionModel::Current | QItemSelectionModel::Rows
202 mFieldsView->scrollTo( index );
205void QgsProcessingFieldMapPanelWidget::loadLayerFields()
207 if (
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerCombo->currentLayer() ) )
209 mFieldsView->setDestinationFields( vl->fields() );
220 QVBoxLayout *vlayout =
new QVBoxLayout();
221 vlayout->setContentsMargins( 0, 0, 0, 0 );
223 vlayout->addWidget(
new QLabel( tr(
"Parent layer" ) ) );
225 mParentLayerComboBox =
new QComboBox();
226 mParentLayerComboBox->addItem( tr(
"None" ), QVariant() );
228 QString initialParent;
230 initialParent = mapParam->parentLayerParameterName();
232 if (
auto *lModel = widgetContext.
model() )
235 const QMap<QString, QgsProcessingModelParameter> components = lModel->parameterComponents();
236 for (
auto it = components.constBegin(); it != components.constEnd(); ++it )
240 mParentLayerComboBox->addItem( definition->
description(), definition->
name() );
241 if ( !initialParent.isEmpty() && initialParent == definition->
name() )
243 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
248 mParentLayerComboBox->addItem( definition->
description(), definition->
name() );
249 if ( !initialParent.isEmpty() && initialParent == definition->
name() )
251 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
257 if ( mParentLayerComboBox->count() == 1 && !initialParent.isEmpty() )
260 mParentLayerComboBox->addItem( initialParent, initialParent );
261 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
264 vlayout->addWidget( mParentLayerComboBox );
265 setLayout( vlayout );
270 auto param = std::make_unique<QgsProcessingParameterFieldMapping>( name, description, mParentLayerComboBox->currentData().toString() );
271 param->setFlags( flags );
272 return param.release();
284QString QgsProcessingFieldMapWidgetWrapper::parameterType()
const
291 return new QgsProcessingFieldMapWidgetWrapper( parameter, type );
294QWidget *QgsProcessingFieldMapWidgetWrapper::createWidget()
296 mPanel =
new QgsProcessingFieldMapPanelWidget(
nullptr );
297 mPanel->setToolTip( parameterDefinition()->toolTip() );
298 mPanel->registerExpressionContextGenerator(
this );
300 connect( mPanel, &QgsProcessingFieldMapPanelWidget::changed,
this, [
this] {
301 emit widgetValueHasChanged(
this );
309 return new QgsProcessingFieldMapParameterDefinitionWidget( context, widgetContext, definition,
algorithm );
312void QgsProcessingFieldMapWidgetWrapper::postInitialize(
const QList<QgsAbstractProcessingParameterWidgetWrapper *> &wrappers )
324 setParentLayerWrapperValue( wrapper );
326 setParentLayerWrapperValue( wrapper );
339int QgsProcessingFieldMapWidgetWrapper::stretch()
const
348 std::unique_ptr<QgsProcessingContext> tmpContext;
349 if ( mProcessingContextGenerator )
350 context = mProcessingContextGenerator->processingContext();
354 tmpContext = std::make_unique<QgsProcessingContext>();
355 context = tmpContext.get();
362 mPanel->setLayer(
nullptr );
368 std::unique_ptr<QgsMapLayer> ownedLayer( context->
takeResultLayer( layer->
id() ) );
371 mParentLayer.reset( qobject_cast<QgsVectorLayer *>( ownedLayer.release() ) );
372 layer = mParentLayer.get();
380 mPanel->setLayer( layer );
383void QgsProcessingFieldMapWidgetWrapper::setWidgetValue(
const QVariant &value,
QgsProcessingContext & )
386 mPanel->setValue( value );
389QVariant QgsProcessingFieldMapWidgetWrapper::widgetValue()
const
391 return mPanel ? mPanel->value() : QVariant();
394QString QgsProcessingFieldMapWidgetWrapper::modelerExpressionFormatString()
const
396 return tr(
"an array of map items, each containing a 'name', 'type' and 'expression' values (and optional 'length' and 'precision' values)." );
399const QgsVectorLayer *QgsProcessingFieldMapWidgetWrapper::linkedVectorLayer()
const
401 if ( mPanel && mPanel->layer() )
402 return mPanel->layer();
ProcessingMode
Types of modes which Processing widgets can be created for.
@ Batch
Batch processing mode.
@ Standard
Standard (single-run) algorithm mode.
QFlags< ProcessingParameterFlag > ProcessingParameterFlags
Flags which dictate the behavior of Processing parameters.
Abstract interface for generating an expression context.
Stores information about constraints which may be present on a field.
@ ConstraintNotNull
Field may not be null.
@ ConstraintUnique
Field must have a unique value.
@ ConstraintExpression
Field has an expression constraint set. See constraintExpression().
void setConstraint(Constraint constraint, ConstraintOrigin origin=ConstraintOriginLayer)
Sets a constraint on the field.
QFlags< Constraint > Constraints
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
int size() const
Returns number of items.
Abstract base class for processing algorithms.
Contains information about the context in which a processing algorithm is executed.
QgsMapLayer * takeResultLayer(const QString &id)
Takes the result map layer with matching id from the context and transfers ownership of it back to th...
Base class for the definition of processing parameters.
QString description() const
Returns the description for the parameter.
QString name() const
Returns the name of the parameter.
An input feature source (such as vector layers) parameter for processing algorithms.
A parameter for "field mapping" configurations, which consist of a definition of desired output field...
static QString typeName()
Returns the type name for the parameter class.
A vector layer (with or without geometry) parameter for processing algorithms.
Contains settings which reflect the context in which a Processing parameter widget is shown.
QgsProcessingModelAlgorithm * model() const
Returns the model which the parameter widget is associated with.
static QgsVectorLayer * parameterAsVectorLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a vector layer.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
Represents a vector layer which manages a vector based dataset.
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
The Field struct holds information about a mapped field.