QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
59  Q_ENUM( ColumnDataIndex );
60 
64  struct Field
65  {
67  QString originalName;
69  QgsField field;
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 
169  private:
170 
171  const QgsExpressionContextGenerator *mBaseGenerator = nullptr;
172 
173  const QgsFields *mSourceFields;
174 
175  };
176 
177 
178  QgsFieldConstraints::Constraints fieldConstraints( const QgsField &field ) const;
179 
180  bool moveUpOrDown( const QModelIndex &index, bool up = true );
181 
191  QString findExpressionForDestinationField( const QgsFieldMappingModel::Field &field, QStringList &excludedFieldNames );
192 
193  QList<Field> mMapping;
194  bool mDestinationEditable = false;
195  QgsFields mSourceFields;
196  std::unique_ptr<ExpressionContextGenerator> mExpressionContextGenerator;
197 
198  friend class QgsAggregateMappingModel;
199 
200 };
201 
202 
203 
204 #endif // QGSFIELDMAPPINGMODEL_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
qgsfields.h
QgsFields
Definition: qgsfields.h:44
QgsFieldMappingModel::ColumnDataIndex
ColumnDataIndex
The ColumnDataIndex enum represents the column index for the view.
Definition: qgsfieldmappingmodel.h:63
QgsFieldMappingModel
Definition: qgsfieldmappingmodel.h:39
qgsexpressioncontextgenerator.h
QgsFieldMappingModel::Field
The Field struct holds information about a mapped field.
Definition: qgsfieldmappingmodel.h:78
qgsproperty.h
QgsAggregateMappingModel
Definition: qgsprocessingaggregatewidgets.h:40
qgsfieldconstraints.h
QgsExpressionContextGenerator
Definition: qgsexpressioncontextgenerator.h:36
QgsField
Definition: qgsfield.h:49