QGIS API Documentation 4.3.0-Master (6ece89243d2)
Loading...
Searching...
No Matches
qgsattributetablemodel.h
Go to the documentation of this file.
1/***************************************************************************
2 QgsAttributeTableModel.h - Models for attribute table
3 -------------------
4 date : Feb 2009
5 copyright : (C) 2009 by Vita Cizek
6 email : weetya (at) gmail.com
7
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
17#ifndef QGSATTRIBUTETABLEMODEL_H
18#define QGSATTRIBUTETABLEMODEL_H
19
20#include "qgis_gui.h"
21#include "qgis_sip.h"
23#include "qgsconditionalstyle.h"
25
26#include <QAbstractTableModel>
27#include <QHash>
28#include <QMap>
29#include <QModelIndex>
30#include <QObject>
31#include <QQueue>
32
33class QgsMapCanvas;
37
49class GUI_EXPORT QgsAttributeTableModel : public QAbstractTableModel
50{
51 Q_OBJECT
52
53 public:
54 // *INDENT-OFF*
55
63 {
64 FeatureId SIP_MONKEYPATCH_COMPAT_NAME( FeatureIdRole ) = Qt::UserRole,
65 FieldIndex SIP_MONKEYPATCH_COMPAT_NAME( FieldIndexRole ),
66 ColumnType,
67 User SIP_MONKEYPATCH_COMPAT_NAME( UserRole ),
68 // Insert new values here, SortRole needs to be the last one
69 Sort SIP_MONKEYPATCH_COMPAT_NAME( SortRole ) = User + 1000,
70 };
71 Q_ENUM( CustomRole )
72
73
80 {
81 Field SIP_MONKEYPATCH_COMPAT_NAME( ColumnTypeField ),
82 ActionButton SIP_MONKEYPATCH_COMPAT_NAME( ColumnTypeActionButton )
83 };
84 Q_ENUM( ColumnTypeValue )
85
86 // *INDENT-ON*
87
88 public:
94 QgsAttributeTableModel( QgsVectorLayerCache *layerCache, QObject *parent = nullptr );
95
100 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
101
106 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
107
114 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
115
121 QVariant data( const QModelIndex &index, int role ) const override;
122
129 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
130
135 Qt::ItemFlags flags( const QModelIndex &index ) const override;
136
142 void reload( const QModelIndex &index1, const QModelIndex &index2 );
143
147 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
148
154 inline void resetModel() { loadLayer(); }
155
160 int idToRow( QgsFeatureId id ) const;
161
162 QModelIndex idToIndex( QgsFeatureId id ) const;
163
164 QModelIndexList idToIndexList( QgsFeatureId id ) const;
165
169 int fieldIdx( int col ) const;
170
174 int fieldCol( int idx ) const;
175
180 QgsFeatureId rowToId( int row ) const;
181
187 void swapRows( QgsFeatureId a, QgsFeatureId b );
188
192 inline QgsVectorLayer *layer() const { return mLayer; }
193
197 inline QgsVectorLayerCache *layerCache() const { return mLayerCache; }
198
202 void executeAction( QUuid action, const QModelIndex &idx ) const;
203
207 void executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx, const QgsMapLayerActionContext &context = QgsMapLayerActionContext() ) const;
208
213 QgsFeature feature( const QModelIndex &idx ) const;
214
222 void prefetchColumnData( int column );
223
230 void prefetchSortData( const QString &expression, unsigned long cacheIndex = 0 );
231
237 QString sortCacheExpression( unsigned long cacheIndex = 0 ) const;
238
246 void setRequest( const QgsFeatureRequest &request );
247
248 // TODO QGIS 5: return copy instead of reference
249
253 const QgsFeatureRequest &request() const;
254
261 void setEditorContext( const QgsAttributeEditorContext &context ) { mEditorContext = context; }
262
269 const QgsAttributeEditorContext &editorContext() const { return mEditorContext; }
270
277 Q_DECL_DEPRECATED int extraColumns() const SIP_DEPRECATED;
278
285 Q_DECL_DEPRECATED void setExtraColumns( int extraColumns ) SIP_DEPRECATED;
286
292 bool showValidityState() const { return mShowValidityState; }
293
299 void setShowValidityState( bool show ) { mShowValidityState = show; }
300
301 public slots:
302
307 virtual void loadLayer();
308
313 void fieldConditionalStyleChanged( const QString &fieldName );
314
315 signals:
316
321
323 void progress( int i, bool &cancel ) SIP_SKIP;
324
328 void finished();
329
330 private slots:
331
335 virtual void updatedFields();
336
342 virtual void editCommandEnded();
343
347 virtual void attributeDeleted( int idx );
348
355 virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
356
361 virtual void featuresDeleted( const QgsFeatureIds &fids );
362
367 virtual void featureAdded( QgsFeatureId fid );
368
372 virtual void layerDeleted();
373
374 virtual void fieldFormatterRemoved( QgsFieldFormatter *fieldFormatter );
375
376 private:
377 QgsVectorLayer *mLayer = nullptr;
378 QgsVectorLayerCache *mLayerCache = nullptr;
379 int mFieldCount = 0;
380
381 mutable QgsFeature mFeat;
382
383 QgsFields mFields;
384 QgsAttributeList mAttributes;
385
386 struct WidgetData
387 {
388 QgsFieldFormatter *fieldFormatter = nullptr;
389 QVariant cache;
390 QVariantMap config;
391 bool loaded = false;
392 };
393 mutable QVector<WidgetData> mWidgetDatas;
394
395 QHash<QgsFeatureId, int> mIdRowMap;
396 QHash<int, QgsFeatureId> mRowIdMap;
397 mutable QHash<QgsFeatureId, QList<QgsConditionalStyle>> mRowStylesMap;
398 mutable QHash<QgsFeatureId, QHash<int, QgsConditionalStyle>> mConstraintStylesMap;
399
400 mutable QgsExpressionContext mExpressionContext;
401
405 const WidgetData &getWidgetData( int column ) const;
406
410 void loadAttributes();
411
419 virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
420
431 virtual bool loadFeatureAtId( QgsFeatureId fid, int fieldIdx ) const;
432
433 QgsFeatureRequest mFeatureRequest;
434
435 struct SortCache
436 {
438 int sortFieldIndex;
440 QgsExpression sortCacheExpression;
441 QgsAttributeList sortCacheAttributes;
443 QHash<QgsFeatureId, QVariant> sortCache;
444 };
445
446 std::vector<SortCache> mSortCaches;
447
448 QgsAttributeEditorContext mEditorContext;
449
451 bool mBulkEditCommandRunning = false;
452
454 bool mResettingModel = false;
455
457 void bulkEditCommandStarted();
458
460 void bulkEditCommandEnded();
461
463 QMap<QPair<QgsFeatureId, int>, QVariant> mAttributeValueChanges;
464
466 QList<QgsFeatureId> mInsertedRowsChanges;
467
469 bool mIsCleaningUpAfterRollback = false;
470
471 bool mShowValidityState = false;
472
474};
475
476
477#endif
Contains context information for attribute editor widgets.
A proxy model for filtering an attribute table model.
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
const QgsAttributeEditorContext & editorContext() const
Returns the context in which this table is shown.
void resetModel()
Resets the model.
QgsAttributeTableModel(QgsVectorLayerCache *layerCache, QObject *parent=nullptr)
Constructor.
void modelChanged()
Emitted when the model has been changed.
void progress(int i, bool &cancel)
void setEditorContext(const QgsAttributeEditorContext &context)
Sets the context in which this table is shown.
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
QgsVectorLayerCache * layerCache() const
Returns the layer cache this model uses as backend.
void finished()
Emitted when the model has completely loaded all features.
void setShowValidityState(bool show)
Sets whether the attribute table will add a visual feedback to cells when an attribute constraint is ...
ColumnTypeValue
The type of a column.
virtual void loadLayer()
Loads the layer into the model Preferably to be called, before using this model as source for any oth...
bool showValidityState() const
Returns whether the attribute table will add a visual feedback to cells when an attribute constraint ...
Every attribute editor widget needs a factory, which inherits this class.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Handles parsing and evaluation of expressions (formerly called "search strings").
Wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
A field formatter helps to handle and display values for a field.
Container of fields for a vector layer.
Definition qgsfields.h:45
Map canvas is a class for displaying all GIS data types on a canvas.
Encapsulates the context in which a QgsMapLayerAction action is executed.
An action which can run on map layers.
Caches features for a given QgsVectorLayer.
Represents a vector layer which manages a vector based dataset.
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_SKIP
Definition qgis_sip.h:138
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:270
#define SIP_MONKEYPATCH_COMPAT_NAME(FORMERNAME)
Definition qgis_sip.h:272
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
QList< int > QgsAttributeList
Definition qgsfield.h:30