QGIS API Documentation 3.41.0-Master (3440c17df1d)
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 <QAbstractTableModel>
20#include <QStyledItemDelegate>
21
22#include "qgsfields.h"
24#include "qgsfieldconstraints.h"
25#include "qgsproperty.h"
27#include "qgis_gui.h"
28
29
40class GUI_EXPORT QgsFieldMappingModel: public QAbstractTableModel
41{
42
43 Q_OBJECT
44
45 public:
46
50 enum class ColumnDataIndex : int
51 {
52 SourceExpression,
53 DestinationName,
54 DestinationType,
55 DestinationLength,
56 DestinationPrecision,
57 DestinationConstraints,
58 DestinationAlias,
59 DestinationComment,
60 };
61
63
67 struct Field
68 {
70 QString originalName;
74 QString expression;
75 };
76
84 QgsFieldMappingModel( const QgsFields &sourceFields = QgsFields(),
85 const QgsFields &destinationFields = QgsFields(),
86 const QMap<QString, QString> &expressions = QMap<QString, QString>(),
87 QObject *parent = nullptr );
88
90 bool destinationEditable() const;
91
93 void setDestinationEditable( bool editable );
94
99 Q_DECL_DEPRECATED static const QMap<QMetaType::Type, QString> dataTypes();
100
105 static const QList<QgsVectorDataProvider::NativeType> supportedDataTypes();
106
108 QgsFields sourceFields() const;
109
111 QList<QgsFieldMappingModel::Field> mapping() const;
112
119 QMap< QString, QgsProperty > fieldPropertyMap() const;
120
126 void setFieldPropertyMap( const QMap< QString, QgsProperty > &map );
127
129 void appendField( const QgsField &field, const QString &expression = QString() );
130
132 bool removeField( const QModelIndex &index );
133
135 bool moveUp( const QModelIndex &index );
136
138 bool moveDown( const QModelIndex &index );
139
141 void setSourceFields( const QgsFields &sourceFields );
142
144 QgsExpressionContextGenerator *contextGenerator() const;
145
150 void setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
151
157 void setDestinationFields( const QgsFields &destinationFields,
158 const QMap<QString, QString> &expressions = QMap<QString, QString>() );
159
160
161 // QAbstractItemModel interface
162 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
163 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
164 QVariant data( const QModelIndex &index, int role ) const override;
165 QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
166 Qt::ItemFlags flags( const QModelIndex &index ) const override;
167 bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
168
169 private:
170
171 class ExpressionContextGenerator: public QgsExpressionContextGenerator
172 {
173
174 public:
175
176 ExpressionContextGenerator( const QgsFields &sourceFields );
177
178 // QgsExpressionContextGenerator interface
179 QgsExpressionContext createExpressionContext() const override;
180 void setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
181 void setSourceFields( const QgsFields &fields );
182
183 private:
184
185 const QgsExpressionContextGenerator *mBaseGenerator = nullptr;
186
187 QgsFields mSourceFields;
188
189 };
190
191
192 QgsFieldConstraints::Constraints fieldConstraints( const QgsField &field ) const;
193
198 static const QString qgsFieldToTypeName( const QgsField &field );
199
204 static void setFieldTypeFromName( QgsField &field, const QString &name );
205
206 bool moveUpOrDown( const QModelIndex &index, bool up = true );
207
217 QString findExpressionForDestinationField( const QgsFieldMappingModel::Field &field, QStringList &excludedFieldNames );
218
219 QList<Field> mMapping;
220 bool mDestinationEditable = false;
221 QgsFields mSourceFields;
222 std::unique_ptr<ExpressionContextGenerator> mExpressionContextGenerator;
223
225
226};
227
228
229
230#endif // QGSFIELDMAPPINGMODEL_H
The QgsAggregateMappingModel holds mapping information for defining sets of aggregates of fields from...
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
The QgsFieldMappingModel holds mapping information for mapping from one set of QgsFields to another,...
ColumnDataIndex
The ColumnDataIndex enum represents the column index for the view.
Q_ENUM(ColumnDataIndex)
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
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.