32
32
QgsAttributeTableModel::QgsAttributeTableModel ( QgsVectorLayer *theLayer, QObject *parent )
33
33
: QAbstractTableModel( parent )
34
34
{
35
- mLastRowId = -1 ;
36
- mLastRow = NULL ;
37
35
mLayer = theLayer;
38
36
mFeatureCount = mLayer ->pendingFeatureCount ();
39
37
loadAttributes ();
40
38
41
39
42
40
connect ( mLayer , SIGNAL ( layerModified ( bool ) ), this , SLOT ( layerModified ( bool ) ) );
43
- // connect(mLayer, SIGNAL(attributeAdded(int)), this, SLOT( attributeAdded(int)));
44
- // connect(mLayer, SIGNAL(attributeDeleted(int)), this, SLOT( attributeDeleted(int)));
45
41
// connect(mLayer, SIGNAL(attributeValueChanged(int, int, const QVariant&)), this, SLOT( attributeValueChanged(int, int, const QVariant&)));
46
42
// connect(mLayer, SIGNAL(featureDeleted(int)), this, SLOT( featureDeleted(int)));
47
43
// connect(mLayer, SIGNAL(featureAdded(int)), this, SLOT( featureAdded(int)));
48
44
49
45
loadLayer ();
50
46
}
51
47
52
- bool QgsAttributeTableModel::featureAtId ( int fid )
48
+ bool QgsAttributeTableModel::featureAtId ( int fid ) const
53
49
{
54
50
return mLayer ->featureAtId ( fid, mFeat , false , true );
55
51
}
@@ -201,14 +197,12 @@ void QgsAttributeTableModel::loadLayer()
201
197
QgsDebugMsg ( " ins" );
202
198
ins = true ;
203
199
beginInsertRows ( QModelIndex (), mFeatureCount , pendingFeatureCount - 1 );
204
- // QgsDebugMsg(QString("%1, %2").arg(mFeatureCount).arg(mLayer->pendingFeatureCount() - 1));
205
200
}
206
201
else if ( mFeatureCount > pendingFeatureCount )
207
202
{
208
203
QgsDebugMsg ( " rm" );
209
204
rm = true ;
210
205
beginRemoveRows ( QModelIndex (), pendingFeatureCount, mFeatureCount - 1 );
211
- // QgsDebugMsg(QString("%1, %2").arg(mFeatureCount).arg(mLayer->pendingFeatureCount() -1));
212
206
}
213
207
214
208
mLayer ->select ( mAttributes , QgsRectangle (), false );
@@ -368,16 +362,13 @@ void QgsAttributeTableModel::sort( int column, Qt::SortOrder order )
368
362
}
369
363
370
364
QVariant QgsAttributeTableModel::data ( const QModelIndex &index, int role ) const
371
- {
372
- return (( QgsAttributeTableModel * ) this )->data ( index , role );
373
- }
374
-
375
- QVariant QgsAttributeTableModel::data ( const QModelIndex &index, int role )
376
365
{
377
366
if ( !index .isValid () || ( role != Qt::TextAlignmentRole && role != Qt::DisplayRole && role != Qt::EditRole ) )
378
367
return QVariant ();
379
368
380
- QVariant::Type fldType = mLayer ->pendingFields ()[ mAttributes [index .column ()] ].type ();
369
+ int fieldId = mAttributes [ index .column ()];
370
+
371
+ QVariant::Type fldType = mLayer ->pendingFields ()[ fieldId ].type ();
381
372
bool fldNumeric = ( fldType == QVariant::Int || fldType == QVariant::Double );
382
373
383
374
if ( role == Qt::TextAlignmentRole )
@@ -389,21 +380,17 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role )
389
380
}
390
381
391
382
// if we don't have the row in current cache, load it from layer first
392
- if ( mLastRowId != rowToId ( index .row () ) )
383
+ int rowId = rowToId ( index .row () );
384
+ if ( mFeat .id () != rowId )
393
385
{
394
- bool res = featureAtId ( rowToId ( index .row () ) );
395
-
396
- if ( !res )
386
+ if ( !featureAtId ( rowId ) )
397
387
return QVariant ( " ERROR" );
398
-
399
- mLastRowId = rowToId ( index .row () );
400
- mLastRow = ( QgsAttributeMap * ) & mFeat .attributeMap ();
401
388
}
402
389
403
- if ( ! mLastRow )
390
+ if ( mFeat . id () != rowId )
404
391
return QVariant ( " ERROR" );
405
392
406
- const QVariant &val = ( * mLastRow )[ mAttributes [ index . column ()] ];
393
+ const QVariant &val = mFeat . attributeMap ( )[ fieldId ];
407
394
408
395
if ( val.isNull () )
409
396
{
@@ -428,26 +415,21 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role )
428
415
429
416
bool QgsAttributeTableModel::setData ( const QModelIndex &index, const QVariant &value, int role )
430
417
{
431
- if ( !index .isValid () || role != Qt::EditRole )
432
- return false ;
433
-
434
- if ( !mLayer ->isEditable () )
418
+ if ( !index .isValid () || role != Qt::EditRole || !mLayer ->isEditable () )
435
419
return false ;
436
420
437
- bool res = featureAtId ( rowToId ( index .row () ) );
438
-
439
- if ( res )
421
+ int rowId = rowToId ( index .row () );
422
+ if ( mFeat .id () == rowId || featureAtId ( rowId ) )
440
423
{
441
- mLastRowId = rowToId ( index .row () );
442
- mLastRow = ( QgsAttributeMap * ) & mFeat .attributeMap ();
424
+ int fieldId = mAttributes [ index .column ()];
443
425
444
426
disconnect ( mLayer , SIGNAL ( layerModified ( bool ) ), this , SLOT ( layerModified ( bool ) ) );
445
427
446
428
mLayer ->beginEditCommand ( tr ( " Attribute changed" ) );
447
- mLayer ->changeAttributeValue ( rowToId ( index . row () ), mAttributes [ index . column ()] , value, true );
429
+ mLayer ->changeAttributeValue ( rowId, fieldId , value, true );
448
430
mLayer ->endEditCommand ();
449
431
450
- ( * mLastRow )[ mAttributes [ index . column ()] ] = value;
432
+ mFeat . changeAttribute ( fieldId, value ) ;
451
433
452
434
connect ( mLayer , SIGNAL ( layerModified ( bool ) ), this , SLOT ( layerModified ( bool ) ) );
453
435
}
@@ -456,6 +438,7 @@ bool QgsAttributeTableModel::setData( const QModelIndex &index, const QVariant &
456
438
return false ;
457
439
458
440
emit dataChanged ( index , index );
441
+
459
442
return true ;
460
443
}
461
444
@@ -466,7 +449,8 @@ Qt::ItemFlags QgsAttributeTableModel::flags( const QModelIndex &index ) const
466
449
467
450
Qt::ItemFlags flags = QAbstractItemModel::flags ( index );
468
451
469
- if ( mLayer ->isEditable () )
452
+ if ( mLayer ->isEditable () &&
453
+ mLayer ->editType ( mAttributes [ index .column ()] ) != QgsVectorLayer::Immutable )
470
454
flags |= Qt::ItemIsEditable;
471
455
472
456
return flags;
0 commit comments