QGIS API Documentation 3.41.0-Master (3440c17df1d)
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 <QAbstractTableModel>
21#include "qgis_sip.h"
22#include <QModelIndex>
23#include <QObject>
24#include <QHash>
25#include <QQueue>
26#include <QMap>
27
28#include "qgsconditionalstyle.h"
31#include "qgis_gui.h"
32
33class QgsMapCanvas;
37
49class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
50{
51 Q_OBJECT
52
53 public:
54
55 // *INDENT-OFF*
56
64 {
65 FeatureId SIP_MONKEYPATCH_COMPAT_NAME(FeatureIdRole)= Qt::UserRole,
66 FieldIndex SIP_MONKEYPATCH_COMPAT_NAME(FieldIndexRole),
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),
70 };
71 Q_ENUM( CustomRole )
72 // *INDENT-ON*
73
74 public:
75
81 QgsAttributeTableModel( QgsVectorLayerCache *layerCache, QObject *parent = nullptr );
82
87 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
88
93 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
94
101 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
102
108 QVariant data( const QModelIndex &index, int role ) const override;
109
116 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
117
122 Qt::ItemFlags flags( const QModelIndex &index ) const override;
123
129 void reload( const QModelIndex &index1, const QModelIndex &index2 );
130
134 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
135
141 inline void resetModel() { loadLayer(); }
142
147 int idToRow( QgsFeatureId id ) const;
148
149 QModelIndex idToIndex( QgsFeatureId id ) const;
150
151 QModelIndexList idToIndexList( QgsFeatureId id ) const;
152
156 int fieldIdx( int col ) const;
157
161 int fieldCol( int idx ) const;
162
167 QgsFeatureId rowToId( int row ) const;
168
174 void swapRows( QgsFeatureId a, QgsFeatureId b );
175
179 inline QgsVectorLayer *layer() const { return mLayer; }
180
184 inline QgsVectorLayerCache *layerCache() const { return mLayerCache; }
185
189 void executeAction( QUuid action, const QModelIndex &idx ) const;
190
194 void executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx, const QgsMapLayerActionContext &context = QgsMapLayerActionContext() ) const;
195
200 QgsFeature feature( const QModelIndex &idx ) const;
201
209 void prefetchColumnData( int column );
210
217 void prefetchSortData( const QString &expression, unsigned long cacheIndex = 0 );
218
224 QString sortCacheExpression( unsigned long cacheIndex = 0 ) const;
225
233 void setRequest( const QgsFeatureRequest &request );
234
235 // TODO QGIS 4: return copy instead of reference
236
240 const QgsFeatureRequest &request() const;
241
248 void setEditorContext( const QgsAttributeEditorContext &context ) { mEditorContext = context; }
249
256 const QgsAttributeEditorContext &editorContext() const { return mEditorContext; }
257
262 int extraColumns() const;
263
268 void setExtraColumns( int extraColumns );
269
275 bool showValidityState() const { return mShowValidityState; }
276
282 void setShowValidityState( bool show ) { mShowValidityState = show; }
283
284 public slots:
285
290 virtual void loadLayer();
291
296 void fieldConditionalStyleChanged( const QString &fieldName );
297
298 signals:
299
304
306 void progress( int i, bool &cancel ) SIP_SKIP;
307
311 void finished();
312
313 private slots:
314
318 virtual void updatedFields();
319
325 virtual void editCommandEnded();
326
330 virtual void attributeDeleted( int idx );
331
338 virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
339
344 virtual void featuresDeleted( const QgsFeatureIds &fids );
345
350 virtual void featureAdded( QgsFeatureId fid );
351
355 virtual void layerDeleted();
356
357 virtual void fieldFormatterRemoved( QgsFieldFormatter *fieldFormatter );
358
359 private:
360 QgsVectorLayer *mLayer = nullptr;
361 QgsVectorLayerCache *mLayerCache = nullptr;
362 int mFieldCount = 0;
363
364 mutable QgsFeature mFeat;
365
366 QgsFields mFields;
367 QgsAttributeList mAttributes;
368
369 struct WidgetData
370 {
371 QgsFieldFormatter *fieldFormatter = nullptr;
372 QVariant cache;
373 QVariantMap config;
374 bool loaded = false;
375 };
376 mutable QVector<WidgetData> mWidgetDatas;
377
378 QHash<QgsFeatureId, int> mIdRowMap;
379 QHash<int, QgsFeatureId> mRowIdMap;
380 mutable QHash<QgsFeatureId, QList<QgsConditionalStyle> > mRowStylesMap;
381 mutable QHash<QgsFeatureId, QHash<int, QgsConditionalStyle> > mConstraintStylesMap;
382
383 mutable QgsExpressionContext mExpressionContext;
384
388 const WidgetData &getWidgetData( int column ) const;
389
393 void loadAttributes();
394
402 virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
403
414 virtual bool loadFeatureAtId( QgsFeatureId fid, int fieldIdx ) const;
415
416 QgsFeatureRequest mFeatureRequest;
417
418 struct SortCache
419 {
421 int sortFieldIndex;
423 QgsExpression sortCacheExpression;
424 QgsAttributeList sortCacheAttributes;
426 QHash<QgsFeatureId, QVariant> sortCache;
427 };
428
429 std::vector<SortCache> mSortCaches;
430
431 QgsAttributeEditorContext mEditorContext;
432
433 int mExtraColumns = 0;
434
436 bool mBulkEditCommandRunning = false;
437
439 bool mResettingModel = false;
440
442 void bulkEditCommandStarted();
443
445 void bulkEditCommandEnded();
446
448 QMap<QPair<QgsFeatureId, int>, QVariant> mAttributeValueChanges;
449
451 QList<QgsFeatureId> mInsertedRowsChanges;
452
454 bool mIsCleaningUpAfterRollback = false;
455
456 bool mShowValidityState = false;
457
458 friend class TestQgsAttributeTable;
459
460};
461
462
463#endif
This class 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.
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 ...
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...
Class for parsing and evaluation of expressions (formerly called "search strings").
This class 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 The class can be used in two manners:
This class caches features of a given QgsVectorLayer.
Represents a vector layer which manages a vector based data sets.
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:271
#define SIP_MONKEYPATCH_COMPAT_NAME(FORMERNAME)
Definition qgis_sip.h:273
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
QList< int > QgsAttributeList
Definition qgsfield.h:27