17#include "moc_qgsprocessingfieldmapwidgetwrapper.cpp"
23#include <QStandardItemModel>
25#include <QItemSelectionModel>
41QgsProcessingFieldMapPanelWidget::QgsProcessingFieldMapPanelWidget( QWidget *parent )
46 mModel = mFieldsView->model();
47 mFieldsView->setDestinationEditable(
true );
49 mLayerCombo->setAllowEmptyLayer(
true );
52 connect( mResetButton, &QPushButton::clicked,
this, &QgsProcessingFieldMapPanelWidget::loadFieldsFromLayer );
53 connect( mAddButton, &QPushButton::clicked,
this, &QgsProcessingFieldMapPanelWidget::addField );
58 connect( mLoadLayerFieldsButton, &QPushButton::clicked,
this, &QgsProcessingFieldMapPanelWidget::loadLayerFields );
63 if ( !mBlockChangedSignal )
70void QgsProcessingFieldMapPanelWidget::setLayer(
QgsVectorLayer *layer )
72 if ( layer == mLayer )
76 mFieldsView->setSourceLayer( mLayer );
77 if ( mModel->rowCount() == 0 )
79 loadFieldsFromLayer();
83 QMessageBox dlg(
this );
84 dlg.setText( tr(
"Do you want to reset the field mapping?" ) );
85 dlg.setStandardButtons(
86 QMessageBox::StandardButtons( QMessageBox::Yes |
88 dlg.setDefaultButton( QMessageBox::No );
89 if ( dlg.exec() == QMessageBox::Yes )
91 loadFieldsFromLayer();
100QVariant QgsProcessingFieldMapPanelWidget::value()
const
102 const QList<QgsFieldMappingModel::Field> mapping = mFieldsView->mapping();
104 QVariantList results;
105 results.reserve( mapping.size() );
109 def.insert( QStringLiteral(
"name" ), field.field.name() );
110 def.insert( QStringLiteral(
"type" ),
static_cast< int >( field.field.type() ) );
111 def.insert( QStringLiteral(
"type_name" ), field.field.typeName() );
112 def.insert( QStringLiteral(
"length" ), field.field.length() );
113 def.insert( QStringLiteral(
"precision" ), field.field.precision() );
114 def.insert( QStringLiteral(
"sub_type" ),
static_cast< int >( field.field.subType() ) );
115 def.insert( QStringLiteral(
"expression" ), field.expression );
116 def.insert( QStringLiteral(
"alias" ), field.field.alias() );
117 def.insert( QStringLiteral(
"comment" ), field.field.comment() );
118 results.append( def );
123void QgsProcessingFieldMapPanelWidget::setValue(
const QVariant &value )
125 if ( value.userType() != QMetaType::Type::QVariantList )
129 QMap<QString, QString> expressions;
132 const QVariantList fields = value.toList();
133 for (
const QVariant &field : fields )
135 const QVariantMap map = field.toMap();
136 QgsField f( map.value( QStringLiteral(
"name" ) ).toString(),
137 static_cast< QMetaType::Type
>( map.value( QStringLiteral(
"type" ),
static_cast< int >( QMetaType::Type::UnknownType ) ).toInt() ),
138 map.value( QStringLiteral(
"type_name" ), QVariant::typeToName(
static_cast< QMetaType::Type
>( map.value( QStringLiteral(
"type" ),
static_cast< int >( QMetaType::Type::UnknownType ) ).toInt() ) ) ).toString(),
139 map.value( QStringLiteral(
"length" ), 0 ).toInt(),
140 map.value( QStringLiteral(
"precision" ), 0 ).toInt(),
143 f.setAlias( map.value( QStringLiteral(
"alias" ) ).toString() );
144 f.setComment( map.value( QStringLiteral(
"comment" ) ).toString() );
146 if ( map.contains( QStringLiteral(
"constraints" ) ) )
158 f.setConstraints( fieldConstraints );
161 if ( !map.value( QStringLiteral(
"expression" ) ).toString().isEmpty() )
163 expressions.insert( f.name(), map.value( QStringLiteral(
"expression" ) ).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( QStringLiteral(
"new_field" ) ) );
195 const QModelIndex index = mModel->index( rowCount, 0 );
196 mFieldsView->selectionModel()->select(
198 QItemSelectionModel::SelectionFlags(
199 QItemSelectionModel::Clear |
200 QItemSelectionModel::Select |
201 QItemSelectionModel::Current |
202 QItemSelectionModel::Rows ) );
203 mFieldsView->scrollTo( index );
206void QgsProcessingFieldMapPanelWidget::loadLayerFields()
208 if (
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( mLayerCombo->currentLayer() ) )
210 mFieldsView->setSourceFields( vl->fields() );
211 mFieldsView->setDestinationFields( vl->fields() );
222 QVBoxLayout *vlayout =
new QVBoxLayout();
223 vlayout->setContentsMargins( 0, 0, 0, 0 );
225 vlayout->addWidget(
new QLabel( tr(
"Parent layer" ) ) );
227 mParentLayerComboBox =
new QComboBox();
228 mParentLayerComboBox->addItem( tr(
"None" ), QVariant() );
230 QString initialParent;
232 initialParent = mapParam->parentLayerParameterName();
234 if (
auto *lModel = widgetContext.
model() )
237 const QMap<QString, QgsProcessingModelParameter> components = lModel->parameterComponents();
238 for (
auto it = components.constBegin(); it != components.constEnd(); ++it )
242 mParentLayerComboBox-> addItem( definition->
description(), definition->
name() );
243 if ( !initialParent.isEmpty() && initialParent == definition->
name() )
245 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
250 mParentLayerComboBox-> addItem( definition->
description(), definition->
name() );
251 if ( !initialParent.isEmpty() && initialParent == definition->
name() )
253 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
259 if ( mParentLayerComboBox->count() == 1 && !initialParent.isEmpty() )
262 mParentLayerComboBox->addItem( initialParent, initialParent );
263 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
266 vlayout->addWidget( mParentLayerComboBox );
267 setLayout( vlayout );
272 auto param = std::make_unique< QgsProcessingParameterFieldMapping >( name, description, mParentLayerComboBox->currentData().toString() );
273 param->setFlags( flags );
274 return param.release();
286QString QgsProcessingFieldMapWidgetWrapper::parameterType()
const
293 return new QgsProcessingFieldMapWidgetWrapper( parameter, type );
296QWidget *QgsProcessingFieldMapWidgetWrapper::createWidget()
298 mPanel =
new QgsProcessingFieldMapPanelWidget(
nullptr );
299 mPanel->setToolTip( parameterDefinition()->toolTip() );
300 mPanel->registerExpressionContextGenerator(
this );
302 connect( mPanel, &QgsProcessingFieldMapPanelWidget::changed,
this, [ = ]
304 emit widgetValueHasChanged(
this );
312 return new QgsProcessingFieldMapParameterDefinitionWidget( context, widgetContext, definition,
algorithm );
315void QgsProcessingFieldMapWidgetWrapper::postInitialize(
const QList<QgsAbstractProcessingParameterWidgetWrapper *> &wrappers )
327 setParentLayerWrapperValue( wrapper );
330 setParentLayerWrapperValue( wrapper );
343int QgsProcessingFieldMapWidgetWrapper::stretch()
const
352 std::unique_ptr< QgsProcessingContext > tmpContext;
353 if ( mProcessingContextGenerator )
354 context = mProcessingContextGenerator->processingContext();
358 tmpContext = std::make_unique< QgsProcessingContext >();
359 context = tmpContext.get();
366 mPanel->setLayer(
nullptr );
372 std::unique_ptr< QgsMapLayer > ownedLayer( context->
takeResultLayer( layer->
id() ) );
375 mParentLayer.reset( qobject_cast< QgsVectorLayer * >( ownedLayer.release() ) );
376 layer = mParentLayer.get();
384 mPanel->setLayer( layer );
387void QgsProcessingFieldMapWidgetWrapper::setWidgetValue(
const QVariant &value,
QgsProcessingContext & )
390 mPanel->setValue( value );
393QVariant QgsProcessingFieldMapWidgetWrapper::widgetValue()
const
395 return mPanel ? mPanel->value() : QVariant();
398QStringList QgsProcessingFieldMapWidgetWrapper::compatibleParameterTypes()
const
404QStringList QgsProcessingFieldMapWidgetWrapper::compatibleOutputTypes()
const
406 return QStringList();
409QString QgsProcessingFieldMapWidgetWrapper::modelerExpressionFormatString()
const
411 return tr(
"an array of map items, each containing a 'name', 'type' and 'expression' values (and optional 'length' and 'precision' values)." );
414const QgsVectorLayer *QgsProcessingFieldMapWidgetWrapper::linkedVectorLayer()
const
416 if ( mPanel && mPanel->layer() )
417 return mPanel->layer();
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...
WidgetType
Types of dialogs which Processing widgets can be created for.
@ Standard
Standard algorithm dialog.
@ Batch
Batch processing dialog.
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 data sets.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into 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.