QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 User SIP_MONKEYPATCH_COMPAT_NAME( UserRole ),
67 // Insert new values here, SortRole needs to be the last one
68 Sort SIP_MONKEYPATCH_COMPAT_NAME( SortRole ),
69 };
70 Q_ENUM( CustomRole )
71 // *INDENT-ON*
72
73 public:
74
80 QgsAttributeTableModel( QgsVectorLayerCache *layerCache, QObject *parent = nullptr );
81
86 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
87
92 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
93
100 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
101
107 QVariant data( const QModelIndex &index, int role ) const override;
108
115 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
116
121 Qt::ItemFlags flags( const QModelIndex &index ) const override;
122
128 void reload( const QModelIndex &index1, const QModelIndex &index2 );
129
133 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
134
140 inline void resetModel() { loadLayer(); }
141
146 int idToRow( QgsFeatureId id ) const;
147
148 QModelIndex idToIndex( QgsFeatureId id ) const;
149
150 QModelIndexList idToIndexList( QgsFeatureId id ) const;
151
155 int fieldIdx( int col ) const;
156
160 int fieldCol( int idx ) const;
161
166 QgsFeatureId rowToId( int row ) const;
167
173 void swapRows( QgsFeatureId a, QgsFeatureId b );
174
178 inline QgsVectorLayer *layer() const { return mLayer; }
179
183 inline QgsVectorLayerCache *layerCache() const { return mLayerCache; }
184
188 void executeAction( QUuid action, const QModelIndex &idx ) const;
189
193 void executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx, const QgsMapLayerActionContext &context = QgsMapLayerActionContext() ) const;
194
199 QgsFeature feature( const QModelIndex &idx ) const;
200
208 void prefetchColumnData( int column );
209
216 void prefetchSortData( const QString &expression, unsigned long cacheIndex = 0 );
217
223 QString sortCacheExpression( unsigned long cacheIndex = 0 ) const;
224
232 void setRequest( const QgsFeatureRequest &request );
233
234 // TODO QGIS 4: return copy instead of reference
235
239 const QgsFeatureRequest &request() const;
240
247 void setEditorContext( const QgsAttributeEditorContext &context ) { mEditorContext = context; }
248
255 const QgsAttributeEditorContext &editorContext() const { return mEditorContext; }
256
261 int extraColumns() const;
262
267 void setExtraColumns( int extraColumns );
268
274 bool showValidityState() const { return mShowValidityState; }
275
281 void setShowValidityState( bool show ) { mShowValidityState = show; }
282
283 public slots:
284
289 virtual void loadLayer();
290
295 void fieldConditionalStyleChanged( const QString &fieldName );
296
297 signals:
298
303
305 void progress( int i, bool &cancel ) SIP_SKIP;
306
310 void finished();
311
312 private slots:
313
317 virtual void updatedFields();
318
324 virtual void editCommandEnded();
325
329 virtual void attributeDeleted( int idx );
330
337 virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
338
343 virtual void featuresDeleted( const QgsFeatureIds &fids );
344
349 virtual void featureAdded( QgsFeatureId fid );
350
354 virtual void layerDeleted();
355
356 virtual void fieldFormatterRemoved( QgsFieldFormatter *fieldFormatter );
357
358 private:
359 QgsVectorLayer *mLayer = nullptr;
360 QgsVectorLayerCache *mLayerCache = nullptr;
361 int mFieldCount = 0;
362
363 mutable QgsFeature mFeat;
364
365 QgsFields mFields;
366 QgsAttributeList mAttributes;
367
368 struct WidgetData
369 {
370 QgsFieldFormatter *fieldFormatter = nullptr;
371 QVariant cache;
372 QVariantMap config;
373 bool loaded = false;
374 };
375 mutable QVector<WidgetData> mWidgetDatas;
376
377 QHash<QgsFeatureId, int> mIdRowMap;
378 QHash<int, QgsFeatureId> mRowIdMap;
379 mutable QHash<QgsFeatureId, QList<QgsConditionalStyle>> mRowStylesMap;
380 mutable QHash<QgsFeatureId, QHash<int, QgsConditionalStyle>> mConstraintStylesMap;
381
382 mutable QgsExpressionContext mExpressionContext;
383
387 const WidgetData &getWidgetData( int column ) const;
388
392 void loadAttributes();
393
401 virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
402
413 virtual bool loadFeatureAtId( QgsFeatureId fid, int fieldIdx ) const;
414
415 QgsFeatureRequest mFeatureRequest;
416
417 struct SortCache
418 {
420 int sortFieldIndex;
422 QgsExpression sortCacheExpression;
423 QgsAttributeList sortCacheAttributes;
425 QHash<QgsFeatureId, QVariant> sortCache;
426 };
427
428 std::vector<SortCache> mSortCaches;
429
430 QgsAttributeEditorContext mEditorContext;
431
432 int mExtraColumns = 0;
433
435 bool mBulkEditCommandRunning = false;
436
438 bool mResettingModel = false;
439
441 void bulkEditCommandStarted();
442
444 void bulkEditCommandEnded();
445
447 QMap<QPair<QgsFeatureId, int>, QVariant> mAttributeValueChanges;
448
450 QList<QgsFeatureId> mInsertedRowsChanges;
451
453 bool mIsCleaningUpAfterRollback = false;
454
455 bool mShowValidityState = false;
456
458};
459
460
461#endif
Contains context information for attribute editor widgets.
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 ...
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:58
A field formatter helps to handle and display values for a field.
Container of fields for a vector layer.
Definition qgsfields.h:46
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_SKIP
Definition qgis_sip.h:134
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:268
#define SIP_MONKEYPATCH_COMPAT_NAME(FORMERNAME)
Definition qgis_sip.h:270
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
QList< int > QgsAttributeList
Definition qgsfield.h:28