QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsfieldmodel.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfieldmodel.h
3  --------------------------------------
4  Date : 01.04.2014
5  Copyright : (C) 2014 Denis Rouzaud
6  Email : [email protected]
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15 
16 #ifndef QGSFIELDMODEL_H
17 #define QGSFIELDMODEL_H
18 
19 #include <QAbstractItemModel>
20 #include <QComboBox>
21 #include <QItemSelectionModel>
22 
23 #include "qgsfields.h"
24 #include "qgis_core.h"
25 
26 #include "qgis.h"
27 
28 class QgsVectorLayer;
29 
37 class CORE_EXPORT QgsFieldModel : public QAbstractItemModel
38 {
39  Q_OBJECT
40 
41  Q_PROPERTY( bool allowExpression READ allowExpression WRITE setAllowExpression )
42  Q_PROPERTY( bool allowEmptyFieldName READ allowEmptyFieldName WRITE setAllowEmptyFieldName )
43  Q_PROPERTY( QgsVectorLayer *layer READ layer WRITE setLayer )
44 
45  public:
46 
49  {
50  FieldNameRole = Qt::UserRole + 1,
51  FieldIndexRole = Qt::UserRole + 2,
52  ExpressionRole = Qt::UserRole + 3,
53  IsExpressionRole = Qt::UserRole + 4,
54  ExpressionValidityRole = Qt::UserRole + 5,
55  FieldTypeRole = Qt::UserRole + 6,
56  FieldOriginRole = Qt::UserRole + 7,
57  IsEmptyRole = Qt::UserRole + 8,
58  };
59 
63  explicit QgsFieldModel( QObject *parent SIP_TRANSFERTHIS = nullptr );
64 
68  QModelIndex indexFromName( const QString &fieldName );
69 
75  void setAllowExpression( bool allowExpression );
76 
81  bool allowExpression() { return mAllowExpression; }
82 
88  void setAllowEmptyFieldName( bool allowEmpty );
89 
95  bool allowEmptyFieldName() const { return mAllowEmpty; }
96 
101  bool isField( const QString &expression ) const;
102 
109  void setExpression( const QString &expression );
110 
116  void removeExpression();
117 
122  QgsVectorLayer *layer() { return mLayer; }
123 
124  // QAbstractItemModel interface
125  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
126  QModelIndex parent( const QModelIndex &child ) const override;
127  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
128  int columnCount( const QModelIndex &parent ) const override;
129  QVariant data( const QModelIndex &index, int role ) const override;
130 
136  static QString fieldToolTip( const QgsField &field );
137 
138  public slots:
139 
144  void setLayer( QgsVectorLayer *layer );
145 
146  protected slots:
147 
151  virtual void updateModel();
152 
153  private slots:
154  void layerDeleted();
155 
156  protected:
158  QList<QString> mExpression;
159 
160  QgsVectorLayer *mLayer = nullptr;
161  bool mAllowExpression = false;
162  bool mAllowEmpty = false;
163 
164  private:
165  void fetchFeature();
166 };
167 
168 #endif // QGSFIELDMODEL_H
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
Container of fields for a vector layer.
Definition: qgsfields.h:42
The QgsFieldModel class is a model to display the list of fields of a layer in widgets.
Definition: qgsfieldmodel.h:37
QgsVectorLayer * layer()
Returns the layer associated with the model.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
FieldRoles
Roles utilized by the model.
Definition: qgsfieldmodel.h:48
QgsFields mFields
QList< QString > mExpression
bool allowEmptyFieldName() const
Returns true if the model allows the empty field ("not set") choice.
Definition: qgsfieldmodel.h:95
Represents a vector layer which manages a vector based data sets.
bool allowExpression()
Returns true if the model allows custom expressions to be created and displayed.
Definition: qgsfieldmodel.h:81