QGIS API Documentation  2.99.0-Master (19b062c)
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 : denis.rouzaud@gmail.com
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 
131  public slots:
132 
137  void setLayer( QgsVectorLayer *layer );
138 
139  protected slots:
140 
144  virtual void updateModel();
145 
146  private slots:
147  void layerDeleted();
148 
149  protected:
151  QList<QString> mExpression;
152 
153  QgsVectorLayer *mLayer = nullptr;
154  bool mAllowExpression = false;
155  bool mAllowEmpty = false;
156 
157  private:
158  void fetchFeature();
159 };
160 
161 #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.
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