/
qgsattributetablemodel.cpp
439 lines (355 loc) · 11.8 KB
/
qgsattributetablemodel.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/***************************************************************************
QgsAttributeTableModel.cpp
--------------------------------------
Date : Feb 2009
Copyright : (C) 2009 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. *
* *
***************************************************************************/
#include "qgsattributetablemodel.h"
#include "qgsattributetablefiltermodel.h"
#include "qgsfield.h"
#include "qgsvectorlayer.h"
#include "qgslogger.h"
#include <QtGui>
#include <QVariant>
////////////////////////////
// QgsAttributeTableModel //
////////////////////////////
QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayer *theLayer, QObject *parent )
: QAbstractTableModel( parent )
{
mLastRowId = -1;
mLastRow = NULL;
mLayer = theLayer;
mFeatureCount = mLayer->pendingFeatureCount();
mFieldCount = mLayer->pendingFields().size();
mAttributes = mLayer->pendingAllAttributesList();
connect( mLayer, SIGNAL( layerModified( bool ) ), this, SLOT( layerModified( bool ) ) );
//connect(mLayer, SIGNAL(attributeAdded(int)), this, SLOT( attributeAdded(int)));
//connect(mLayer, SIGNAL(attributeDeleted(int)), this, SLOT( attributeDeleted(int)));
//connect(mLayer, SIGNAL(attributeValueChanged(int, int, const QVariant&)), this, SLOT( attributeValueChanged(int, int, const QVariant&)));
//connect(mLayer, SIGNAL(featureDeleted(int)), this, SLOT( featureDeleted(int)));
//connect(mLayer, SIGNAL(featureAdded(int)), this, SLOT( featureAdded(int)));
loadLayer();
}
void QgsAttributeTableModel::featureDeleted( int fid )
{
QgsDebugMsg( "entered." );
#ifdef QGISDEBUG
int idx = mIdRowMap[fid];
QgsDebugMsg( idx );
QgsDebugMsg( fid );
#endif
#if 0
--mFeatureCount;
mIdRowMap.remove( fid );
mRowIdMap.remove( idx );
// fill the hole in the view
if ( idx != mFeatureCount )
{
QgsDebugMsg( "jo" );
//mRowIdMap[idx] = mRowIdMap[mFeatureCount];
//mIdRowMap[mRowIdMap[idx]] = idx;
int movedId = mRowIdMap[mFeatureCount];
mRowIdMap.remove( mFeatureCount );
mRowIdMap.insert( idx, movedId );
mIdRowMap[movedId] = idx;
//mIdRowMap.remove(mRowIdMap[idx]);
//mIdRowMap.insert(mRowIdMap[idx], idx);
}
QgsDebugMsg( QString( "map sizes:%1, %2" ).arg( mRowIdMap.size() ).arg( mIdRowMap.size() ) );
emit layoutChanged();
//reload(index(0,0), index(rowCount(), columnCount()));
#endif
QgsDebugMsg( "id->row" );
QMap<int, int>::iterator it;
for ( it = mIdRowMap.begin(); it != mIdRowMap.end(); ++it )
QgsDebugMsg( QString( "%1->%2" ).arg( it.key() ).arg( *it ) );
QgsDebugMsg( "row->id" );
for ( it = mRowIdMap.begin(); it != mRowIdMap.end(); ++it )
QgsDebugMsg( QString( "%1->%2" ).arg( it.key() ).arg( *it ) );
}
void QgsAttributeTableModel::featureAdded( int fid )
{
QgsDebugMsg( "BM feature added" );
++mFeatureCount;
mIdRowMap.insert( fid, mFeatureCount - 1 );
mRowIdMap.insert( mFeatureCount - 1, fid );
QgsDebugMsg( QString( "map sizes:%1, %2" ).arg( mRowIdMap.size() ).arg( mIdRowMap.size() ) );
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
}
void QgsAttributeTableModel::attributeAdded( int idx )
{
QgsDebugMsg( "BM attribute added" );
loadLayer();
QgsDebugMsg( QString( "map sizes:%1, %2" ).arg( mRowIdMap.size() ).arg( mIdRowMap.size() ) );
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
emit modelChanged();
}
void QgsAttributeTableModel::attributeDeleted( int idx )
{
QgsDebugMsg( "BM attribute deleted" );
loadLayer();
QgsDebugMsg( QString( "map sizes:%1, %2" ).arg( mRowIdMap.size() ).arg( mIdRowMap.size() ) );
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
emit modelChanged();
}
void QgsAttributeTableModel::layerDeleted()
{
QgsDebugMsg( "entered." );
mIdRowMap.clear();
mRowIdMap.clear();
QgsDebugMsg( QString( "map sizes:%1, %2" ).arg( mRowIdMap.size() ).arg( mIdRowMap.size() ) );
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
}
//TODO: check whether caching in data()/setData() doesn't cache old value
void QgsAttributeTableModel::attributeValueChanged( int fid, int idx, const QVariant &value )
{
QgsDebugMsg( "entered." );
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
}
void QgsAttributeTableModel::layerModified( bool onlyGeometry )
{
if ( onlyGeometry )
return;
loadLayer();
emit modelChanged();
}
void QgsAttributeTableModel::loadLayer()
{
QgsDebugMsg( "entered." );
QgsFeature f;
bool ins = false, rm = false;
mRowIdMap.clear();
mIdRowMap.clear();
if ( mFeatureCount < mLayer->pendingFeatureCount() )
{
QgsDebugMsg( "ins" );
ins = true;
beginInsertRows( QModelIndex(), mFeatureCount, mLayer->pendingFeatureCount() - 1 );
// QgsDebugMsg(QString("%1, %2").arg(mFeatureCount).arg(mLayer->pendingFeatureCount() - 1));
}
else if ( mFeatureCount > mLayer->pendingFeatureCount() )
{
QgsDebugMsg( "rm" );
rm = true;
beginRemoveRows( QModelIndex(), mLayer->pendingFeatureCount(), mFeatureCount - 1 );
// QgsDebugMsg(QString("%1, %2").arg(mFeatureCount).arg(mLayer->pendingFeatureCount() -1));
}
mLayer->select( QgsAttributeList(), QgsRectangle(), false );
for ( int i = 0; mLayer->nextFeature( f ); ++i )
{
mRowIdMap.insert( i, f.id() );
mIdRowMap.insert( f.id(), i );
}
// not needed when we have featureAdded signal
mFeatureCount = mLayer->pendingFeatureCount();
mFieldCount = mLayer->pendingFields().size();
if ( ins )
{
endInsertRows();
QgsDebugMsg( "end ins" );
}
else if ( rm )
{
endRemoveRows();
QgsDebugMsg( "end rm" );
}
#if 0
QgsDebugMsg( "id->row" );
QMap<int, int>::iterator it;
for ( it = mIdRowMap.begin(); it != mIdRowMap.end(); ++it )
QgsDebugMsg( QString( "%1->%2" ).arg( it.key() ).arg( *it ) );
QgsDebugMsg( "row->id" );
for ( it = mRowIdMap.begin(); it != mRowIdMap.end(); ++it )
QgsDebugMsg( QString( "%1->%2" ).arg( it.key() ).arg( *it ) );
#endif
}
void QgsAttributeTableModel::swapRows( int a, int b )
{
if ( a == b )
return;
int rowA = idToRow( a );
int rowB = idToRow( b );
//emit layoutAboutToBeChanged();
mRowIdMap.remove( rowA );
mRowIdMap.remove( rowB );
mRowIdMap.insert( rowA, b );
mRowIdMap.insert( rowB, a );
mIdRowMap.remove( a );
mIdRowMap.remove( b );
mIdRowMap.insert( a, rowB );
mIdRowMap.insert( b, rowA );
//emit layoutChanged();
}
int QgsAttributeTableModel::idToRow( const int id ) const
{
if ( !mIdRowMap.contains( id ) )
{
QgsDebugMsg( QString( "idToRow: id %1 not in map" ).arg( id ) );
return -1;
}
return mIdRowMap[id];
}
int QgsAttributeTableModel::rowToId( const int id ) const
{
if ( !mRowIdMap.contains( id ) )
{
QgsDebugMsg( QString( "rowToId: row %1 not in map" ).arg( id ) );
return -1;
}
return mRowIdMap[id];
}
int QgsAttributeTableModel::rowCount( const QModelIndex &parent ) const
{
return mFeatureCount;
}
int QgsAttributeTableModel::columnCount( const QModelIndex &parent ) const
{
return mFieldCount;
}
QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
if ( role == Qt::DisplayRole )
{
if ( orientation == Qt::Vertical ) //row
{
return QVariant( section );
}
else
{
QgsField field = mLayer->pendingFields()[ mAttributes[section] ]; //column
return QVariant( field.name() );
}
}
else return QVariant();
}
void QgsAttributeTableModel::sort( int column, Qt::SortOrder order )
{
QgsAttributeMap row;
QgsAttributeTableIdColumnPair pair;
QgsAttributeList attrs;
QgsFeature f;
attrs.append( mAttributes[column] );
emit layoutAboutToBeChanged();
// QgsDebugMsg("SORTing");
mSortList.clear();
mLayer->select( attrs, QgsRectangle(), false );
while ( mLayer->nextFeature( f ) )
{
row = f.attributeMap();
pair.id = f.id();
pair.columnItem = row[ mAttributes[column] ];
mSortList.append( pair );
}
if ( order == Qt::AscendingOrder )
qStableSort( mSortList.begin(), mSortList.end() );
else
qStableSort( mSortList.begin(), mSortList.end(), qGreater<QgsAttributeTableIdColumnPair>() );
// recalculate id<->row maps
mRowIdMap.clear();
mIdRowMap.clear();
int i = 0;
QList<QgsAttributeTableIdColumnPair>::Iterator it;
for ( it = mSortList.begin(); it != mSortList.end(); ++it, ++i )
{
mRowIdMap.insert( i, it->id );
mIdRowMap.insert( it->id, i );
}
// restore selection
emit layoutChanged();
//reset();
emit modelChanged();
}
QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) const
{
if ( !index.isValid() || ( role != Qt::TextAlignmentRole && role != Qt::DisplayRole && role != Qt::EditRole ) )
return QVariant();
QVariant::Type fldType = mLayer->pendingFields()[ mAttributes[index.column()] ].type();
bool fldNumeric = ( fldType == QVariant::Int || fldType == QVariant::Double );
if ( role == Qt::TextAlignmentRole )
{
if ( fldNumeric )
return QVariant( Qt::AlignRight );
else
return QVariant( Qt::AlignLeft );
}
// if we don't have the row in current cache, load it from layer first
if ( mLastRowId != rowToId( index.row() ) )
{
bool res = mLayer->featureAtId( rowToId( index.row() ), mFeat, false, true );
if ( !res )
return QVariant( "ERROR" );
mLastRowId = rowToId( index.row() );
mLastRow = ( QgsAttributeMap * ) & mFeat.attributeMap();
}
if ( !mLastRow )
return QVariant( "ERROR" );
QVariant& val = ( *mLastRow )[ mAttributes[index.column()] ];
if ( val.isNull() )
{
// if the value is NULL, show that in table, but don't show "NULL" text in editor
if ( role == Qt::EditRole )
return QVariant();
else
return QVariant( "NULL" );
}
// force also numeric data for EditRole to be strings
// otherwise it creates spinboxes instead of line edits
// (probably not what we do want)
if ( fldNumeric && role == Qt::EditRole )
return val.toString();
// convert to QString from some other representation
// this prevents displaying greater numbers in exponential format
return val.toString();
}
bool QgsAttributeTableModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
if ( !index.isValid() || role != Qt::EditRole )
return false;
if ( !mLayer->isEditable() )
return false;
bool res = mLayer->featureAtId( rowToId( index.row() ), mFeat, false, true );
if ( res )
{
mLastRowId = rowToId( index.row() );
mLastRow = ( QgsAttributeMap * )( &( mFeat.attributeMap() ) );
mLayer->changeAttributeValue( rowToId( index.row() ), mAttributes[ index.column()], value, true );
}
if ( !mLayer->isModified() )
return false;
emit dataChanged( index, index );
return true;
}
Qt::ItemFlags QgsAttributeTableModel::flags( const QModelIndex &index ) const
{
if ( !index.isValid() )
return Qt::ItemIsEnabled;
Qt::ItemFlags flags = QAbstractItemModel::flags( index );
if ( mLayer->isEditable() )
flags |= Qt::ItemIsEditable;
return flags;
}
void QgsAttributeTableModel::reload( const QModelIndex &index1, const QModelIndex &index2 )
{
emit dataChanged( index1, index2 );
}
void QgsAttributeTableModel::resetModel()
{
reset();
}
void QgsAttributeTableModel::changeLayout()
{
emit layoutChanged();
}
void QgsAttributeTableModel::incomingChangeLayout()
{
emit layoutAboutToBeChanged();
}