QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
84 const QgsFields &sourceFields = QgsFields(), const QgsFields &destinationFields = QgsFields(), const QMap<QString, QString> &expressions = QMap<QString, QString>(), QObject *parent = nullptr
85 );
86
96 void setNativeTypes( const QList< QgsVectorDataProvider::NativeType > &nativeTypes );
97
99 bool destinationEditable() const;
100
102 void setDestinationEditable( bool editable );
103
108 Q_DECL_DEPRECATED static const QMap<QMetaType::Type, QString> dataTypes();
109
114 static const QList<QgsVectorDataProvider::NativeType> supportedDataTypes();
115
117 QgsFields sourceFields() const;
118
120 QList<QgsFieldMappingModel::Field> mapping() const;
121
128 QMap<QString, QgsProperty> fieldPropertyMap() const;
129
135 void setFieldPropertyMap( const QMap<QString, QgsProperty> &map );
136
138 void appendField( const QgsField &field, const QString &expression = QString() );
139
141 bool removeField( const QModelIndex &index );
142
144 bool moveUp( const QModelIndex &index );
145
147 bool moveDown( const QModelIndex &index );
148
151
154
160
166 void setDestinationFields( const QgsFields &destinationFields, const QMap<QString, QString> &expressions = QMap<QString, QString>() );
167
168
169 // QAbstractItemModel interface
170 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
171 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
172 QVariant data( const QModelIndex &index, int role ) const override;
173 QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
174 Qt::ItemFlags flags( const QModelIndex &index ) const override;
175 bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
176
177 private:
178 class ExpressionContextGenerator : public QgsExpressionContextGenerator
179 {
180 public:
181 ExpressionContextGenerator( const QgsFields &sourceFields );
182
183 // QgsExpressionContextGenerator interface
184 QgsExpressionContext createExpressionContext() const override;
186 void setSourceFields( const QgsFields &fields );
187
188 private:
189 const QgsExpressionContextGenerator *mBaseGenerator = nullptr;
190
191 QgsFields mSourceFields;
192 };
193
194
195 QgsFieldConstraints::Constraints fieldConstraints( const QgsField &field ) const;
196
201 QString qgsFieldToTypeName( const QgsField &field ) const;
202
207 void setFieldTypeFromName( QgsField &field, const QString &name ) const;
208
209 bool moveUpOrDown( const QModelIndex &index, bool up = true );
210
220 QString findExpressionForDestinationField( const QgsFieldMappingModel::Field &field, QStringList &excludedFieldNames );
221
222 QList< QgsVectorDataProvider::NativeType > mNativeTypes;
223
224 QList<Field> mMapping;
225 bool mDestinationEditable = false;
226 QgsFields mSourceFields;
227 std::unique_ptr<ExpressionContextGenerator> mExpressionContextGenerator;
228
230};
231
232
233#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:56
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.