QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsfieldmappingwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfieldmappingwidget.cpp - QgsFieldMappingWidget
3
4 ---------------------
5 begin : 16.3.2020
6 copyright : (C) 2020 by Alessandro Pasotti
7 email : elpaso at itopen dot it
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18
19#include "qgsexpression.h"
23#include "qgsvectorlayer.h"
24
25#include <QItemSelectionModel>
26#include <QTableView>
27#include <QVBoxLayout>
28
29#include "moc_qgsfieldmappingwidget.cpp"
30
31#ifdef ENABLE_MODELTEST
32#include "modeltest.h"
33#endif
34
36 QWidget *parent, const QgsFields &sourceFields, const QgsFields &destinationFields, const QMap<QString, QString> &expressions, const QList< QgsVectorDataProvider::NativeType > &nativeTypes
37)
38 : QgsPanelWidget( parent )
39{
40 QVBoxLayout *verticalLayout = new QVBoxLayout();
41 verticalLayout->setContentsMargins( 0, 0, 0, 0 );
42 mTableView = new QTableView();
43 verticalLayout->addWidget( mTableView );
44 setLayout( verticalLayout );
45
46 mModel = new QgsFieldMappingModel( sourceFields, destinationFields, expressions, this );
47 mModel->setNativeTypes( nativeTypes );
48
49#ifdef ENABLE_MODELTEST
50 new ModelTest( mModel, this );
51#endif
52
53 mTableView->setModel( mModel );
54 mTableView->setItemDelegateForColumn( static_cast<int>( QgsFieldMappingModel::ColumnDataIndex::SourceExpression ), new QgsFieldMappingExpressionDelegate( this ) );
55 mTypeDelegate = new QgsFieldMappingTypeDelegate( nativeTypes, mTableView );
56 mTableView->setItemDelegateForColumn( static_cast<int>( QgsFieldMappingModel::ColumnDataIndex::DestinationType ), mTypeDelegate );
57 updateColumns();
58 // Make sure columns are updated when rows are added
59 connect( mModel, &QgsFieldMappingModel::rowsInserted, this, [this] { updateColumns(); } );
60 connect( mModel, &QgsFieldMappingModel::modelReset, this, [this] { updateColumns(); } );
61 connect( mModel, &QgsFieldMappingModel::dataChanged, this, &QgsFieldMappingWidget::changed );
62 connect( mModel, &QgsFieldMappingModel::rowsInserted, this, &QgsFieldMappingWidget::changed );
63 connect( mModel, &QgsFieldMappingModel::rowsRemoved, this, &QgsFieldMappingWidget::changed );
64 connect( mModel, &QgsFieldMappingModel::modelReset, this, &QgsFieldMappingWidget::changed );
65}
66
68{
69 qobject_cast<QgsFieldMappingModel *>( mModel )->setDestinationEditable( editable );
70 updateColumns();
71}
72
74{
75 return qobject_cast<QgsFieldMappingModel *>( mModel )->destinationEditable();
76}
77
79{
80 return qobject_cast<QgsFieldMappingModel *>( mModel );
81}
82
83QList<QgsFieldMappingModel::Field> QgsFieldMappingWidget::mapping() const
84{
85 return model()->mapping();
86}
87
88QMap<QString, QgsProperty> QgsFieldMappingWidget::fieldPropertyMap() const
89{
90 return model()->fieldPropertyMap();
91}
92
93void QgsFieldMappingWidget::setFieldPropertyMap( const QMap<QString, QgsProperty> &map )
94{
95 model()->setFieldPropertyMap( map );
96}
97
99{
100 return mTableView->selectionModel();
101}
102
104{
105 model()->setSourceFields( sourceFields );
106}
107
109{
110 mSourceLayer = layer;
111}
112
114{
115 return mSourceLayer;
116}
117
118void QgsFieldMappingWidget::setDestinationFields( const QgsFields &destinationFields, const QMap<QString, QString> &expressions )
119{
120 model()->setDestinationFields( destinationFields, expressions );
121}
122
123void QgsFieldMappingWidget::setNativeTypes( const QList<QgsVectorDataProvider::NativeType> &nativeTypes )
124{
125 mTypeDelegate->setNativeTypes( nativeTypes );
126 mModel->setNativeTypes( nativeTypes );
127}
128
129void QgsFieldMappingWidget::scrollTo( const QModelIndex &index ) const
130{
131 mTableView->scrollTo( index );
132}
133
138
139void QgsFieldMappingWidget::appendField( const QgsField &field, const QString &expression )
140{
141 model()->appendField( field, expression );
142}
143
145{
146 if ( !mTableView->selectionModel()->hasSelection() )
147 return false;
148
149 std::list<int> rowsToRemove { selectedRows() };
150 rowsToRemove.reverse();
151 for ( const int row : rowsToRemove )
152 {
153 if ( !model()->removeField( model()->index( row, 0, QModelIndex() ) ) )
154 {
155 return false;
156 }
157 }
158 return true;
159}
160
162{
163 for ( int i = 0; i < mTableView->model()->rowCount(); ++i )
164 {
165 for ( int j = 0; j < mTableView->model()->columnCount(); j++ )
166 {
167 QModelIndex index = mTableView->model()->index( i, j );
168 mTableView->selectionModel()->select( index, QItemSelectionModel::Toggle );
169 }
170 }
171}
172
174{
175 if ( !mTableView->selectionModel()->hasSelection() )
176 return false;
177
178 const std::list<int> rowsToMoveUp { selectedRows() };
179 for ( const int row : rowsToMoveUp )
180 {
181 if ( !model()->moveUp( model()->index( row, 0, QModelIndex() ) ) )
182 {
183 return false;
184 }
185 }
186 return true;
187}
188
190{
191 if ( !mTableView->selectionModel()->hasSelection() )
192 return false;
193
194 std::list<int> rowsToMoveDown { selectedRows() };
195 rowsToMoveDown.reverse();
196 for ( const int row : rowsToMoveDown )
197 {
198 if ( !model()->moveDown( model()->index( row, 0, QModelIndex() ) ) )
199 {
200 return false;
201 }
202 }
203 return true;
204}
205
206void QgsFieldMappingWidget::updateColumns()
207{
208 for ( int i = 0; i < mModel->rowCount(); ++i )
209 {
210 mTableView->openPersistentEditor( mModel->index( i, static_cast<int>( QgsFieldMappingModel::ColumnDataIndex::SourceExpression ) ) );
211 mTableView->openPersistentEditor( mModel->index( i, static_cast<int>( QgsFieldMappingModel::ColumnDataIndex::DestinationType ) ) );
212 }
213
214 for ( int i = 0; i < mModel->columnCount(); ++i )
215 {
216 mTableView->resizeColumnToContents( i );
217 }
218}
219
220std::list<int> QgsFieldMappingWidget::selectedRows()
221{
222 std::list<int> rows;
223 if ( mTableView->selectionModel()->hasSelection() )
224 {
225 const QModelIndexList constSelection { mTableView->selectionModel()->selectedIndexes() };
226 for ( const QModelIndex &index : constSelection )
227 {
228 rows.push_back( index.row() );
229 }
230 rows.sort();
231 rows.unique();
232 }
233 return rows;
234}
235
237
238//
239// QgsFieldMappingExpressionDelegate
240//
241
242QgsFieldMappingExpressionDelegate::QgsFieldMappingExpressionDelegate( QObject *parent )
243 : QStyledItemDelegate( parent )
244{}
245
246void QgsFieldMappingExpressionDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
247{
248 QgsFieldExpressionWidget *editorWidget { qobject_cast<QgsFieldExpressionWidget *>( editor ) };
249 if ( !editorWidget )
250 return;
251
252 bool isExpression;
253 bool isValid;
254 const QString currentValue { editorWidget->currentField( &isExpression, &isValid ) };
255 if ( isExpression )
256 {
257 model->setData( index, currentValue, Qt::EditRole );
258 }
259 else
260 {
261 model->setData( index, QgsExpression::quotedColumnRef( currentValue ), Qt::EditRole );
262 }
263}
264
265void QgsFieldMappingExpressionDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
266{
267 QgsFieldExpressionWidget *editorWidget { qobject_cast<QgsFieldExpressionWidget *>( editor ) };
268 if ( !editorWidget )
269 return;
270
271 const QVariant value = index.model()->data( index, Qt::EditRole );
272 editorWidget->setField( value.toString() );
273}
274
275QWidget *QgsFieldMappingExpressionDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
276{
277 Q_UNUSED( option )
279 editor->setAutoFillBackground( true );
280 editor->setAllowEvalErrors( false );
281 if ( const QgsFieldMappingModel *model = qobject_cast<const QgsFieldMappingModel *>( index.model() ) )
282 {
283 editor->registerExpressionContextGenerator( model->contextGenerator() );
284 editor->setFields( model->sourceFields() );
285 }
286 else if ( const QgsAggregateMappingModel *model = qobject_cast<const QgsAggregateMappingModel *>( index.model() ) )
287 {
288 editor->registerExpressionContextGenerator( model->contextGenerator() );
289 editor->setFields( model->sourceFields() );
290 }
291 else
292 {
293 Q_ASSERT( false );
294 }
295
296 if ( QgsFieldMappingWidget *mappingWidget = qobject_cast<QgsFieldMappingWidget *>( QgsFieldMappingExpressionDelegate::parent() ) )
297 {
298 if ( mappingWidget->sourceLayer() )
299 editor->setLayer( mappingWidget->sourceLayer() );
300 }
301 else if ( QgsAggregateMappingWidget *aggregateWidget = qobject_cast<QgsAggregateMappingWidget *>( QgsFieldMappingExpressionDelegate::parent() ) )
302 {
303 if ( aggregateWidget->sourceLayer() )
304 editor->setLayer( aggregateWidget->sourceLayer() );
305 }
306
307 editor->setField( index.model()->data( index, Qt::DisplayRole ).toString() );
308 connect( editor, qOverload<const QString &>( &QgsFieldExpressionWidget::fieldChanged ), this, [this, editor]( const QString &fieldName ) {
309 Q_UNUSED( fieldName )
310 const_cast<QgsFieldMappingExpressionDelegate *>( this )->emit commitData( editor );
311 } );
312 return editor;
313}
314
315
316//
317// QgsFieldMappingTypeDelegate
318//
319
320QgsFieldMappingTypeDelegate::QgsFieldMappingTypeDelegate( const QList< QgsVectorDataProvider::NativeType > &nativeTypes, QObject *parent )
321 : QStyledItemDelegate( parent )
322 , mNativeTypes( nativeTypes.isEmpty() ? QgsFieldMappingModel::supportedDataTypes() : nativeTypes )
323{}
324
325QWidget *QgsFieldMappingTypeDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
326{
327 Q_UNUSED( option )
328 QComboBox *editor = new QComboBox( parent );
329
330 int i = 0;
331 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
332 {
333 editor->addItem( QgsFields::iconForFieldType( type.mType, type.mSubType, type.mTypeName ), type.mTypeDesc );
334 editor->setItemData( i, type.mTypeName, Qt::UserRole );
335 i++;
336 }
337
338 const QgsFieldMappingModel *model { qobject_cast<const QgsFieldMappingModel *>( index.model() ) };
339
340 if ( model && !model->destinationEditable() )
341 {
342 editor->setEnabled( false );
343 }
344 else
345 {
346 connect( editor, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this, editor]( int currentIndex ) {
347 Q_UNUSED( currentIndex )
348 const_cast<QgsFieldMappingTypeDelegate *>( this )->emit commitData( editor );
349 } );
350 }
351 return editor;
352}
353
354void QgsFieldMappingTypeDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
355{
356 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
357 if ( !editorWidget )
358 return;
359
360 const QVariant value = index.model()->data( index, Qt::EditRole );
361 editorWidget->setCurrentIndex( editorWidget->findData( value ) );
362}
363
364void QgsFieldMappingTypeDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
365{
366 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
367 if ( !editorWidget )
368 return;
369
370 const QVariant currentValue = editorWidget->currentData();
371 model->setData( index, currentValue, Qt::EditRole );
372}
373
374void QgsFieldMappingTypeDelegate::setNativeTypes( const QList<QgsVectorDataProvider::NativeType> &nativeTypes )
375{
376 mNativeTypes = nativeTypes.isEmpty() ? QgsFieldMappingModel::supportedDataTypes() : nativeTypes;
377}
378
Holds mapping information for defining sets of aggregates of fields from a QgsFields object.
A widget which creates a mapping for defining sets of aggregates of fields from a QgsFields object.
Abstract interface for generating an expression context.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes).
A widget for selection of layer fields or expression creation.
void setField(const QString &fieldName)
sets the current field or expression in the widget
void setFields(const QgsFields &fields)
Sets the fields used in the widget to fields, this allows the widget to work without a layer.
void setLayer(QgsMapLayer *layer)
Sets the layer used to display the fields and expression.
void registerExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
void setAllowEvalErrors(bool allowEvalErrors)
Allow accepting expressions with evaluation errors.
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
QString currentField(bool *isExpression=nullptr, bool *isValid=nullptr) const
currentField returns the currently selected field or expression if allowed
Holds mapping information for mapping from one set of QgsFields to another.
@ DestinationType
Destination field type string.
void appendField(const QgsField &field, const QString &expression=QString())
Appends a new field to the model, with an optional expression.
QMap< QString, QgsProperty > fieldPropertyMap() const
Returns a map of destination field name to QgsProperty definition for field value,...
QList< QgsFieldMappingModel::Field > mapping() const
Returns a list of Field objects representing the current status of the model.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setBaseExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Sets the base expression context generator, which will generate the expression contexts for expressio...
static const QList< QgsVectorDataProvider::NativeType > supportedDataTypes()
Returns a static list of supported data types.
void setDestinationFields(const QgsFields &destinationFields, const QMap< QString, QString > &expressions=QMap< QString, QString >())
Set destination fields to destinationFields, initial values for the expressions can be optionally spe...
void setSourceFields(const QgsFields &sourceFields)
Set source fields to sourceFields.
bool destinationEditable() const
Returns true if the destination fields are editable.
void setFieldPropertyMap(const QMap< QString, QgsProperty > &map)
Sets a map of destination field name to QgsProperty definition for field value.
Creates a mapping from one set of QgsFields to another, for each set of "destination" fields an expre...
bool removeSelectedFields()
Removes the currently selected field from the model.
void setSourceLayer(QgsVectorLayer *layer)
Sets a source layer to use when generating expression previews in the widget.
QgsFieldMappingModel * model() const
Returns the underlying mapping model.
void registerExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
bool moveSelectedFieldsDown()
Moves down the currently selected field.
QgsFieldMappingWidget(QWidget *parent=nullptr, const QgsFields &sourceFields=QgsFields(), const QgsFields &destinationFields=QgsFields(), const QMap< QString, QString > &expressions=QMap< QString, QString >(), const QList< QgsVectorDataProvider::NativeType > &nativeTypes=QList< QgsVectorDataProvider::NativeType >())
Constructs a QgsFieldMappingWidget from a set of sourceFields and destinationFields,...
QItemSelectionModel * selectionModel()
Returns the selection model.
void setSourceFields(const QgsFields &sourceFields)
Set source fields of the underlying mapping model to sourceFields.
QMap< QString, QgsProperty > fieldPropertyMap() const
Returns a map of destination field name to QgsProperty definition for field value,...
bool moveSelectedFieldsUp()
Moves up currently selected field.
void invertSelection()
Invert the field selection state.
QList< QgsFieldMappingModel::Field > mapping() const
Returns a list of Field objects representing the current status of the underlying mapping model.
void setFieldPropertyMap(const QMap< QString, QgsProperty > &map)
Sets a map of destination field name to QgsProperty definition for field value.
QgsVectorLayer * sourceLayer()
Returns the source layer for use when generating expression previews.
void setDestinationFields(const QgsFields &destinationFields, const QMap< QString, QString > &expressions=QMap< QString, QString >())
Set destination fields to destinationFields in the underlying model, initial values for the expressio...
void setDestinationEditable(bool editable)
Sets the destination fields editable state to editable.
void appendField(const QgsField &field, const QString &expression=QString())
Appends a new field to the model, with an optional expression.
void scrollTo(const QModelIndex &index) const
Scroll the fields view to index.
void changed()
Emitted when the fields defined in the widget are changed.
void setNativeTypes(const QList< QgsVectorDataProvider::NativeType > &nativeTypes)
Sets the list of nativeTypes supported by a data provider.
bool destinationEditable() const
Returns true if the destination fields are editable in the model.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
static QIcon iconForFieldType(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType, const QString &typeString=QString())
Returns an icon corresponding to a field type.
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
Represents a vector layer which manages a vector based dataset.