/
qgsattributetablemodel.h
222 lines (198 loc) · 5.89 KB
/
qgsattributetablemodel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/***************************************************************************
QgsAttributeTableModel.h - Models for attribute table
-------------------
date : Feb 2009
copyright : Vita Cizek
email : weetya (at) gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSATTRIBUTETABLEMODEL_H
#define QGSATTRIBUTETABLEMODEL_H
#include <QAbstractTableModel>
#include <QModelIndex>
#include <QObject>
#include <QHash>
#include "qgsfeature.h" // QgsAttributeMap
#include "qgsvectorlayer.h" // QgsAttributeList
#include "qgsattributetableidcolumnpair.h"
class QgsAttributeTableModel: public QAbstractTableModel
{
Q_OBJECT
public:
/**
* Constructor
* @param theLayer layer pointer
* @param parent parent pointer
*/
QgsAttributeTableModel( QgsVectorLayer *theLayer, QObject *parent = 0 );
/**
* Returns the number of rows
* @param parent parent index
*/
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
/**
* Returns the number of columns
* @param parent parent index
*/
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
/**
* Returns header data
* @param section required section
* @param orientation horizontal or vertical orientation
* @param role data role
*/
QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
/**
* Returns data on the given index
* @param index model index
* @param role data role
*/
virtual QVariant data( const QModelIndex &index, int role ) const;
/**
* Updates data on given index
* @param index model index
* @param value new data value
* @param role data role
*/
virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole );
/**
* Returns item flags for the index
* @param index model index
*/
Qt::ItemFlags flags( const QModelIndex &index ) const;
/**
* Reloads the model data between indices
* @param index1 start index
* @param index2 end index
*/
void reload( const QModelIndex &index1, const QModelIndex &index2 );
/**
* Resets the model
*/
void resetModel();
/**
* Layout has been changed
*/
void changeLayout();
/**
* Layout will be changed
*/
void incomingChangeLayout();
/**
* Maps feature id to table row
* @param id feature id
*/
int idToRow( const int id ) const;
/**
* get field index from column
*/
int fieldIdx( int col ) const;
/**
* Maps row to feature id
* @param id row id
*/
int rowToId( const int id ) const;
/**
* Sorts the model
* @param column column to sort by
* @param order sorting order
*/
virtual void sort( int column, Qt::SortOrder order = Qt::AscendingOrder );
/**
* Swaps two rows
* @param a first row
* @param b second row
*/
void swapRows( int a, int b );
/**
* Returns layer pointer
*/
QgsVectorLayer* layer() const { return mLayer; }
/** Execute an action */
void executeAction( int action, const QModelIndex &idx ) const;
signals:
/**
* Model has been changed
*/
void modelChanged();
/**
* Sets new number of rows
* @param oldNum old row number
* @param newNum new row number
*/
void setNumRows( int oldNum, int newNum );
private slots:
/**
* Launched when attribute has been added
* @param idx attribute index
*/
virtual void attributeAdded( int idx );
/**
* Launched when attribute has been deleted
* @param idx attribute index
*/
virtual void attributeDeleted( int idx );
/**
* Launched when attribute value has been changed
* @param fid feature id
* @param idx attribute index
* @param value new value
*/
virtual void attributeValueChanged( int fid, int idx, const QVariant &value );
/**
* Launched when layer has been modified
* Rebuilds the model
* @param onlyGeometry true if only geometry has changed
*/
virtual void layerModified( bool onlyGeometry );
protected slots:
/**
* Launched when a feature has been deleted
* @param fid feature id
*/
virtual void featureDeleted( int fid );
/**
* Launched when a feature has been added
* @param fid feature id
*/
virtual void featureAdded( int fid );
/**
* Launched when layer has been deleted
*/
virtual void layerDeleted();
protected:
QgsVectorLayer *mLayer;
int mFeatureCount;
int mFieldCount;
mutable QgsFeature mFeat;
QgsAttributeList mAttributes;
QMap< int, const QMap<QString, QVariant> * > mValueMaps;
QList<QgsAttributeTableIdColumnPair> mSortList;
QHash<int, int> mIdRowMap;
QHash<int, int> mRowIdMap;
/**
* Initializes id <-> row maps
*/
void initIdMaps();
/**
* Loads the layer into the model
*/
virtual void loadLayer();
/**
* Gets mFieldCount, mAttributes and mValueMaps
*/
virtual void loadAttributes();
/**
* load feature fid into mFeat
* @param fid feature id
* @return feature exists
*/
virtual bool featureAtId( int fid ) const;
};
#endif