QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsfieldmappingwidget.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsfieldmappingwidget.h - 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#ifndef QGSFIELDMAPPINGWIDGET_H
17#define QGSFIELDMAPPINGWIDGET_H
18
19#include "qgis_gui.h"
21#include "qgspanelwidget.h"
22
23#include <QAbstractTableModel>
24#include <QPointer>
25#include <QStyledItemDelegate>
26#include <QWidget>
27
28class QTableView;
29class QItemSelectionModel;
30class QgsVectorLayer;
31class QgsFieldMappingTypeDelegate;
32
39class GUI_EXPORT QgsFieldMappingWidget : public QgsPanelWidget
40{
41 Q_OBJECT
42
43 public:
44
57 explicit QgsFieldMappingWidget(
58 QWidget *parent = nullptr,
59 const QgsFields &sourceFields = QgsFields(),
60 const QgsFields &destinationFields = QgsFields(),
61 const QMap<QString, QString> &expressions = QMap<QString, QString>(),
62 const QList< QgsVectorDataProvider::NativeType > &nativeTypes = QList< QgsVectorDataProvider::NativeType >()
63 );
64
66 void setDestinationEditable( bool editable );
67
69 bool destinationEditable() const;
70
73
75 QList<QgsFieldMappingModel::Field> mapping() const;
76
83 QMap<QString, QgsProperty> fieldPropertyMap() const;
84
90 void setFieldPropertyMap( const QMap<QString, QgsProperty> &map );
91
93 QItemSelectionModel *selectionModel();
94
96 void setSourceFields( const QgsFields &sourceFields );
97
103 void setSourceLayer( QgsVectorLayer *layer );
104
113
120 void setDestinationFields( const QgsFields &destinationFields, const QMap<QString, QString> &expressions = QMap<QString, QString>() );
121
131 void setNativeTypes( const QList< QgsVectorDataProvider::NativeType > &nativeTypes );
132
136 void scrollTo( const QModelIndex &index ) const;
137
143
144 signals:
145
149 void changed();
150
151 public slots:
152
154 void appendField( const QgsField &field, const QString &expression = QString() );
155
158
161
164
170 void invertSelection();
171
172 private:
173 QTableView *mTableView = nullptr;
174 QgsFieldMappingModel *mModel = nullptr;
175
176 QgsFieldMappingTypeDelegate *mTypeDelegate = nullptr;
177
178 QPointer<QgsVectorLayer> mSourceLayer;
179 void updateColumns();
181 std::list<int> selectedRows();
182
184};
185
187
188#ifndef SIP_RUN
189
190class QgsFieldMappingExpressionDelegate : public QStyledItemDelegate
191{
192 Q_OBJECT
193
194 public:
195 QgsFieldMappingExpressionDelegate( QObject *parent = nullptr );
196
197 // QAbstractItemDelegate interface
198 QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
199 void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
200 void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
201};
202
203class QgsFieldMappingTypeDelegate : public QStyledItemDelegate
204{
205 Q_OBJECT
206
207 public:
208 QgsFieldMappingTypeDelegate( const QList< QgsVectorDataProvider::NativeType > &nativeTypes, QObject *parent = nullptr );
209
210 // QAbstractItemDelegate interface
211 QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
212 void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
213 void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
214
224 void setNativeTypes( const QList< QgsVectorDataProvider::NativeType > &nativeTypes );
225
226 private:
227 QList< QgsVectorDataProvider::NativeType > mNativeTypes;
228};
229
230#endif
231
233
234#endif // QGSFIELDMAPPINGWIDGET_H
Abstract interface for generating an expression context.
Holds mapping information for mapping from one set of QgsFields to another.
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:54
Container of fields for a vector layer.
Definition qgsfields.h:46
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.