QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 "qgis_gui.h"
27 
28 
39 class GUI_EXPORT QgsFieldMappingModel: public QAbstractTableModel
40 {
41 
42  Q_OBJECT
43 
44  public:
45 
49  enum class ColumnDataIndex : int
50  {
51  SourceExpression,
52  DestinationName,
53  DestinationType,
54  DestinationLength,
55  DestinationPrecision,
56  DestinationConstraints,
57  };
58 
60 
64  struct Field
65  {
67  QString originalName;
71  QString expression;
72  };
73 
81  QgsFieldMappingModel( const QgsFields &sourceFields = QgsFields(),
82  const QgsFields &destinationFields = QgsFields(),
83  const QMap<QString, QString> &expressions = QMap<QString, QString>(),
84  QObject *parent = nullptr );
85 
87  bool destinationEditable() const;
88 
90  void setDestinationEditable( bool editable );
91 
93  static const QMap<QVariant::Type, QString> dataTypes();
94 
96  QgsFields sourceFields() const;
97 
99  QList<QgsFieldMappingModel::Field> mapping() const;
100 
107  QMap< QString, QgsProperty > fieldPropertyMap() const;
108 
114  void setFieldPropertyMap( const QMap< QString, QgsProperty > &map );
115 
117  void appendField( const QgsField &field, const QString &expression = QString() );
118 
120  bool removeField( const QModelIndex &index );
121 
123  bool moveUp( const QModelIndex &index );
124 
126  bool moveDown( const QModelIndex &index );
127 
129  void setSourceFields( const QgsFields &sourceFields );
130 
132  QgsExpressionContextGenerator *contextGenerator() const;
133 
138  void setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
139 
145  void setDestinationFields( const QgsFields &destinationFields,
146  const QMap<QString, QString> &expressions = QMap<QString, QString>() );
147 
148  // QAbstractItemModel interface
149  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
150  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
151  QVariant data( const QModelIndex &index, int role ) const override;
152  QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
153  Qt::ItemFlags flags( const QModelIndex &index ) const override;
154  bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
155 
156  private:
157 
158  class ExpressionContextGenerator: public QgsExpressionContextGenerator
159  {
160 
161  public:
162 
163  ExpressionContextGenerator( const QgsFields &sourceFields );
164 
165  // QgsExpressionContextGenerator interface
166  QgsExpressionContext createExpressionContext() const override;
167  void setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
168  void setSourceFields( const QgsFields &fields );
169 
170  private:
171 
172  const QgsExpressionContextGenerator *mBaseGenerator = nullptr;
173 
174  QgsFields mSourceFields;
175 
176  };
177 
178 
179  QgsFieldConstraints::Constraints fieldConstraints( const QgsField &field ) const;
180 
181  bool moveUpOrDown( const QModelIndex &index, bool up = true );
182 
192  QString findExpressionForDestinationField( const QgsFieldMappingModel::Field &field, QStringList &excludedFieldNames );
193 
194  QList<Field> mMapping;
195  bool mDestinationEditable = false;
196  QgsFields mSourceFields;
197  std::unique_ptr<ExpressionContextGenerator> mExpressionContextGenerator;
198 
200 
201 };
202 
203 
204 
205 #endif // QGSFIELDMAPPINGMODEL_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
qgsfields.h
QgsFieldMappingModel::Q_ENUM
Q_ENUM(ColumnDataIndex)
QgsFieldMappingModel::Field::originalName
QString originalName
The original name of the field.
Definition: qgsfieldmappingmodel.h:67
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
QgsFieldMappingModel::ColumnDataIndex
ColumnDataIndex
The ColumnDataIndex enum represents the column index for the view.
Definition: qgsfieldmappingmodel.h:50
field
const QgsField & field
Definition: qgsfield.h:456
QgsFieldMappingModel
The QgsFieldMappingModel holds mapping information for mapping from one set of QgsFields to another,...
Definition: qgsfieldmappingmodel.h:40
QgsFieldMappingModel::Field::field
QgsField field
The field in its current status (it might have been renamed)
Definition: qgsfieldmappingmodel.h:69
qgsexpressioncontextgenerator.h
QgsFieldMappingModel::Field
The Field struct holds information about a mapped field.
Definition: qgsfieldmappingmodel.h:65
qgsproperty.h
QgsAggregateMappingModel
The QgsAggregateMappingModel holds mapping information for defining sets of aggregates of fields from...
Definition: qgsprocessingaggregatewidgets.h:42
qgsfieldconstraints.h
QgsFieldMappingModel::Field::expression
QString expression
The expression for the mapped field from the source fields.
Definition: qgsfieldmappingmodel.h:71
QgsExpressionContextGenerator
Abstract interface for generating an expression context.
Definition: qgsexpressioncontextgenerator.h:37
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50