36 : QAbstractTableModel( parent )
37 , mLayerCache( layerCache )
50 if ( !
layer()->hasGeometryType() )
57 connect(
layer(), SIGNAL( attributeDeleted(
int ) ),
this, SLOT( attributeDeleted(
int ) ) );
58 connect(
layer(), SIGNAL( updatedFields() ),
this, SLOT( updatedFields() ) );
59 connect(
layer(), SIGNAL( editCommandEnded() ),
this, SLOT( editCommandEnded() ) );
64 bool QgsAttributeTableModel::loadFeatureAtId(
QgsFeatureId fid )
const
79 mFieldCache.remove( fid );
85 beginRemoveRows( QModelIndex(), row, row );
97 for (
int i = row; i < row + count; i++ )
105 for (
int i = row + count; i < n; i++ )
116 for ( QHash<QgsFeatureId, int>::iterator it =
mIdRowMap.begin(); it !=
mIdRowMap.end(); ++it )
119 QHash<QgsFeatureId, int>::iterator idit;
122 for ( QHash<int, QgsFeatureId>::iterator it =
mRowIdMap.begin(); it !=
mRowIdMap.end(); ++it )
137 featOk = loadFeatureAtId( fid );
144 beginInsertRows( QModelIndex(), n, n );
155 void QgsAttributeTableModel::updatedFields()
162 void QgsAttributeTableModel::editCommandEnded()
164 reload( createIndex( mChangedCellBounds.top(), mChangedCellBounds.left() ),
165 createIndex( mChangedCellBounds.bottom(), mChangedCellBounds.right() ) );
167 mChangedCellBounds = QRect();
170 void QgsAttributeTableModel::attributeDeleted(
int idx )
172 if ( idx == mCachedField )
182 beginRemoveRows( QModelIndex(), 0,
rowCount() - 1 );
194 QgsDebugMsgLevel( QString(
"(%4) fid: %1, idx: %2, value: %3" ).arg( fid ).arg( idx ).arg( value.toString() ).arg( mFeatureRequest.
filterType() ), 3 );
196 if ( idx == mCachedField )
197 mFieldCache[ fid ] = value;
206 if ( loadFeatureAtId( fid ) )
241 bool ins =
false, rm =
false;
250 for (
int idx = 0; idx < fields.
count(); ++idx )
254 if ( widgetFactory && widgetType !=
"Hidden" )
267 beginInsertColumns( QModelIndex(),
mFieldCount, attributes.size() - 1 );
272 beginRemoveColumns( QModelIndex(), attributes.size(),
mFieldCount - 1 );
294 beginRemoveRows( QModelIndex(), 0,
rowCount() - 1 );
311 if ( t.elapsed() > 1000 )
356 QgsDebugMsg( QString(
"idToRow: id %1 not in the map" ).arg(
id ) );
370 QModelIndexList indexes;
373 for (
int column = 0; column <
columnCount(); ++column )
375 indexes.append(
index( row, column ) );
385 QgsDebugMsg( QString(
"rowToId: row %1 not in the map" ).arg( row ) );
420 if ( role == Qt::DisplayRole )
422 if ( orientation == Qt::Vertical )
424 return QVariant( section );
429 if ( attributeName.isEmpty() )
432 attributeName = field.
name();
434 return QVariant( attributeName );
438 return tr(
"feature id" );
449 if ( !index.isValid() ||
450 ( role != Qt::TextAlignmentRole
451 && role != Qt::DisplayRole
452 && role != Qt::EditRole
466 return role == Qt::DisplayRole ? rowId : QVariant();
475 QVariant::Type fldType = field.
type();
476 bool fldNumeric = ( fldType == QVariant::Int || fldType == QVariant::Double || fldType == QVariant::LongLong );
478 if ( role == Qt::TextAlignmentRole )
481 return QVariant( Qt::AlignRight );
483 return QVariant( Qt::AlignLeft );
489 if ( mCachedField == fieldId )
491 val = mFieldCache[ rowId ];
497 if ( !loadFeatureAtId( rowId ) )
498 return QVariant(
"ERROR" );
501 return QVariant(
"ERROR" );
507 if ( role == Qt::DisplayRole )
525 if ( mChangedCellBounds.isNull() )
527 mChangedCellBounds = QRect( index.column(), index.row(), 1, 1 );
531 if ( index.column() < mChangedCellBounds.left() )
533 mChangedCellBounds.setLeft( index.column() );
535 if ( index.row() < mChangedCellBounds.top() )
537 mChangedCellBounds.setTop( index.row() );
539 if ( index.column() > mChangedCellBounds.right() )
541 mChangedCellBounds.setRight( index.column() );
543 if ( index.row() > mChangedCellBounds.bottom() )
545 mChangedCellBounds.setBottom( index.row() );
554 if ( !index.isValid() )
555 return Qt::ItemIsEnabled;
558 return Qt::NoItemFlags;
562 if (
layer()->isEditable() &&
564 flags |= Qt::ItemIsEditable;
572 emit dataChanged( index1, index2 );
620 QStringList fldNames;
621 fldNames << fields[ fieldId ].name();
629 mFieldCache.insert( f.
id(), f.
attribute( fieldId ) );
632 mCachedField = fieldId;
645 return mFeatureRequest;