QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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"
26 #include "qgsvectordataprovider.h"
27 #include "qgis_gui.h"
28 
29 
40 class 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  };
59 
61 
65  struct Field
66  {
68  QString originalName;
72  QString expression;
73  };
74 
82  QgsFieldMappingModel( const QgsFields &sourceFields = QgsFields(),
83  const QgsFields &destinationFields = QgsFields(),
84  const QMap<QString, QString> &expressions = QMap<QString, QString>(),
85  QObject *parent = nullptr );
86 
88  bool destinationEditable() const;
89 
91  void setDestinationEditable( bool editable );
92 
97  Q_DECL_DEPRECATED static const QMap<QVariant::Type, QString> dataTypes();
98 
103  static const QList<QgsVectorDataProvider::NativeType> supportedDataTypes();
104 
106  QgsFields sourceFields() const;
107 
109  QList<QgsFieldMappingModel::Field> mapping() const;
110 
117  QMap< QString, QgsProperty > fieldPropertyMap() const;
118 
124  void setFieldPropertyMap( const QMap< QString, QgsProperty > &map );
125 
127  void appendField( const QgsField &field, const QString &expression = QString() );
128 
130  bool removeField( const QModelIndex &index );
131 
133  bool moveUp( const QModelIndex &index );
134 
136  bool moveDown( const QModelIndex &index );
137 
139  void setSourceFields( const QgsFields &sourceFields );
140 
142  QgsExpressionContextGenerator *contextGenerator() const;
143 
148  void setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
149 
155  void setDestinationFields( const QgsFields &destinationFields,
156  const QMap<QString, QString> &expressions = QMap<QString, QString>() );
157 
158 
159  // QAbstractItemModel interface
160  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
161  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
162  QVariant data( const QModelIndex &index, int role ) const override;
163  QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
164  Qt::ItemFlags flags( const QModelIndex &index ) const override;
165  bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
166 
167  private:
168 
169  class ExpressionContextGenerator: public QgsExpressionContextGenerator
170  {
171 
172  public:
173 
174  ExpressionContextGenerator( const QgsFields &sourceFields );
175 
176  // QgsExpressionContextGenerator interface
177  QgsExpressionContext createExpressionContext() const override;
178  void setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
179  void setSourceFields( const QgsFields &fields );
180 
181  private:
182 
183  const QgsExpressionContextGenerator *mBaseGenerator = nullptr;
184 
185  QgsFields mSourceFields;
186 
187  };
188 
189 
190  QgsFieldConstraints::Constraints fieldConstraints( const QgsField &field ) const;
191 
196  static const QString qgsFieldToTypeName( const QgsField &field );
197 
202  static void setFieldTypeFromName( QgsField &field, const QString &name );
203 
204  bool moveUpOrDown( const QModelIndex &index, bool up = true );
205 
215  QString findExpressionForDestinationField( const QgsFieldMappingModel::Field &field, QStringList &excludedFieldNames );
216 
217  QList<Field> mMapping;
218  bool mDestinationEditable = false;
219  QgsFields mSourceFields;
220  std::unique_ptr<ExpressionContextGenerator> mExpressionContextGenerator;
221 
223 
224 };
225 
226 
227 
228 #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...
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:51
Container of fields for a vector layer.
Definition: qgsfields.h:45
const QgsField & field
Definition: qgsfield.h:463
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.