QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsfieldmappingmodel.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsfieldmappingmodel.h - QgsFieldMappingModel
3
4 ---------------------
5 begin : 17.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 QGSFIELDMAPPINGMODEL_H
17#define QGSFIELDMAPPINGMODEL_H
18
19#include "qgis_gui.h"
21#include "qgsfieldconstraints.h"
22#include "qgsfields.h"
23#include "qgsproperty.h"
25
26#include <QAbstractTableModel>
27#include <QStyledItemDelegate>
28
41class GUI_EXPORT QgsFieldMappingModel : public QAbstractTableModel
42{
43 Q_OBJECT
44
45 public:
49 enum class ColumnDataIndex : int
50 {
51 SourceExpression,
52 DestinationName,
53 DestinationType,
54 DestinationLength,
55 DestinationPrecision,
56 DestinationConstraints,
57 DestinationAlias,
58 DestinationComment,
59 };
60
62
66 struct Field
67 {
69 QString originalName;
73 QString expression;
74 };
75
83 QgsFieldMappingModel( const QgsFields &sourceFields = QgsFields(), const QgsFields &destinationFields = QgsFields(), const QMap<QString, QString> &expressions = QMap<QString, QString>(), QObject *parent = nullptr );
84
94 void setNativeTypes( const QList< QgsVectorDataProvider::NativeType > &nativeTypes );
95
97 bool destinationEditable() const;
98
100 void setDestinationEditable( bool editable );
101
106 Q_DECL_DEPRECATED static const QMap<QMetaType::Type, QString> dataTypes();
107
112 static const QList<QgsVectorDataProvider::NativeType> supportedDataTypes();
113
115 QgsFields sourceFields() const;
116
118 QList<QgsFieldMappingModel::Field> mapping() const;
119
126 QMap<QString, QgsProperty> fieldPropertyMap() const;
127
133 void setFieldPropertyMap( const QMap<QString, QgsProperty> &map );
134
136 void appendField( const QgsField &field, const QString &expression = QString() );
137
139 bool removeField( const QModelIndex &index );
140
142 bool moveUp( const QModelIndex &index );
143
145 bool moveDown( const QModelIndex &index );
146
149
152
158
164 void setDestinationFields( const QgsFields &destinationFields, const QMap<QString, QString> &expressions = QMap<QString, QString>() );
165
166
167 // QAbstractItemModel interface
168 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
169 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
170 QVariant data( const QModelIndex &index, int role ) const override;
171 QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
172 Qt::ItemFlags flags( const QModelIndex &index ) const override;
173 bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
174
175 private:
176 class ExpressionContextGenerator : public QgsExpressionContextGenerator
177 {
178 public:
179 ExpressionContextGenerator( const QgsFields &sourceFields );
180
181 // QgsExpressionContextGenerator interface
182 QgsExpressionContext createExpressionContext() const override;
184 void setSourceFields( const QgsFields &fields );
185
186 private:
187 const QgsExpressionContextGenerator *mBaseGenerator = nullptr;
188
189 QgsFields mSourceFields;
190 };
191
192
193 QgsFieldConstraints::Constraints fieldConstraints( const QgsField &field ) const;
194
199 QString qgsFieldToTypeName( const QgsField &field ) const;
200
205 void setFieldTypeFromName( QgsField &field, const QString &name ) const;
206
207 bool moveUpOrDown( const QModelIndex &index, bool up = true );
208
218 QString findExpressionForDestinationField( const QgsFieldMappingModel::Field &field, QStringList &excludedFieldNames );
219
220 QList< QgsVectorDataProvider::NativeType > mNativeTypes;
221
222 QList<Field> mMapping;
223 bool mDestinationEditable = false;
224 QgsFields mSourceFields;
225 std::unique_ptr<ExpressionContextGenerator> mExpressionContextGenerator;
226
228};
229
230
231#endif // QGSFIELDMAPPINGMODEL_H
Abstract interface for generating an expression context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QFlags< Constraint > Constraints
void setDestinationEditable(bool editable)
Sets the destination fields editable state to editable.
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
ColumnDataIndex
The ColumnDataIndex enum represents the column index for the view.
Qt::ItemFlags flags(const QModelIndex &index) const override
QgsFields sourceFields() const
Returns a list of source fields.
void setNativeTypes(const QList< QgsVectorDataProvider::NativeType > &nativeTypes)
Sets the list of nativeTypes supported by a data provider.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
bool removeField(const QModelIndex &index)
Removes the field at index from the model, returns true on success.
void appendField(const QgsField &field, const QString &expression=QString())
Appends a new field to the model, with an optional expression.
friend class QgsAggregateMappingModel
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.
QgsFieldMappingModel(const QgsFields &sourceFields=QgsFields(), const QgsFields &destinationFields=QgsFields(), const QMap< QString, QString > &expressions=QMap< QString, QString >(), QObject *parent=nullptr)
Constructs a QgsFieldMappingModel from a set of sourceFields and destinationFields,...
bool moveDown(const QModelIndex &index)
Moves up the field at index.
bool moveUp(const QModelIndex &index)
Moves down the field at index.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role) const override
static Q_DECL_DEPRECATED const QMap< QMetaType::Type, QString > dataTypes()
Returns a static map of supported data types.
void setBaseExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Sets the base expression context generator, which will generate the expression contexts for expressio...
Q_ENUM(ColumnDataIndex)
static const QList< QgsVectorDataProvider::NativeType > supportedDataTypes()
Returns a static list of supported data types.
QgsExpressionContextGenerator * contextGenerator() const
Returns the context generator with the source fields.
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 setData(const QModelIndex &index, const QVariant &value, int role) override
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.
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
The Field struct holds information about a mapped field.
QgsField field
The field in its current status (it might have been renamed).
QString expression
The expression for the mapped field from the source fields.
QString originalName
The original name of the field.