QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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:
56 explicit QgsFieldMappingWidget(
57 QWidget *parent = nullptr,
58 const QgsFields &sourceFields = QgsFields(),
59 const QgsFields &destinationFields = QgsFields(),
60 const QMap<QString, QString> &expressions = QMap<QString, QString>(),
61 const QList< QgsVectorDataProvider::NativeType > &nativeTypes = QList< QgsVectorDataProvider::NativeType >()
62 );
63
65 void setDestinationEditable( bool editable );
66
68 bool destinationEditable() const;
69
72
74 QList<QgsFieldMappingModel::Field> mapping() const;
75
82 QMap<QString, QgsProperty> fieldPropertyMap() const;
83
89 void setFieldPropertyMap( const QMap<QString, QgsProperty> &map );
90
92 QItemSelectionModel *selectionModel();
93
95 void setSourceFields( const QgsFields &sourceFields );
96
102 void setSourceLayer( QgsVectorLayer *layer );
103
112
119 void setDestinationFields( const QgsFields &destinationFields, const QMap<QString, QString> &expressions = QMap<QString, QString>() );
120
130 void setNativeTypes( const QList< QgsVectorDataProvider::NativeType > &nativeTypes );
131
135 void scrollTo( const QModelIndex &index ) const;
136
142
143 signals:
144
148 void changed();
149
150 public slots:
151
153 void appendField( const QgsField &field, const QString &expression = QString() );
154
157
160
163
169 void invertSelection();
170
171 private:
172 QTableView *mTableView = nullptr;
173 QgsFieldMappingModel *mModel = nullptr;
174
175 QgsFieldMappingTypeDelegate *mTypeDelegate = nullptr;
176
177 QPointer<QgsVectorLayer> mSourceLayer;
178 void updateColumns();
180 std::list<int> selectedRows();
181
183};
184
186
187#ifndef SIP_RUN
188
189class QgsFieldMappingExpressionDelegate : public QStyledItemDelegate
190{
191 Q_OBJECT
192
193 public:
194 QgsFieldMappingExpressionDelegate( QObject *parent = nullptr );
195
196 // QAbstractItemDelegate interface
197 QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
198 void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
199 void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
200};
201
202class QgsFieldMappingTypeDelegate : public QStyledItemDelegate
203{
204 Q_OBJECT
205
206 public:
207 QgsFieldMappingTypeDelegate( const QList< QgsVectorDataProvider::NativeType > &nativeTypes, QObject *parent = nullptr );
208
209 // QAbstractItemDelegate interface
210 QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
211 void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
212 void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
213
223 void setNativeTypes( const QList< QgsVectorDataProvider::NativeType > &nativeTypes );
224
225 private:
226 QList< QgsVectorDataProvider::NativeType > mNativeTypes;
227};
228
229#endif
230
232
233#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.