37 : QAbstractTableModel( parent )
38 , mLayerCache( layerCache )
51 if ( !
layer()->hasGeometryType() )
58 connect(
layer(), SIGNAL( attributeDeleted(
int ) ),
this, SLOT( attributeDeleted(
int ) ) );
59 connect(
layer(), SIGNAL( updatedFields() ),
this, SLOT( updatedFields() ) );
60 connect(
layer(), SIGNAL( editCommandEnded() ),
this, SLOT( editCommandEnded() ) );
65 bool QgsAttributeTableModel::loadFeatureAtId(
QgsFeatureId fid )
const 80 mFieldCache.remove( fid );
86 beginRemoveRows( QModelIndex(), row, row );
98 for (
int i = row; i < row + count; i++ )
106 for (
int i = row + count; i < n; i++ )
117 for ( QHash<QgsFeatureId, int>::iterator it =
mIdRowMap.begin(); it !=
mIdRowMap.end(); ++it )
120 QHash<QgsFeatureId, int>::iterator idit;
123 for ( QHash<int, QgsFeatureId>::iterator it =
mRowIdMap.begin(); it !=
mRowIdMap.end(); ++it )
138 featOk = loadFeatureAtId( fid );
145 beginInsertRows( QModelIndex(), n, n );
156 void QgsAttributeTableModel::updatedFields()
163 void QgsAttributeTableModel::editCommandEnded()
165 reload( createIndex( mChangedCellBounds.top(), mChangedCellBounds.left() ),
166 createIndex( mChangedCellBounds.bottom(), mChangedCellBounds.right() ) );
168 mChangedCellBounds = QRect();
171 void QgsAttributeTableModel::attributeDeleted(
int idx )
173 if ( idx == mCachedField )
183 beginRemoveRows( QModelIndex(), 0,
rowCount() - 1 );
195 QgsDebugMsgLevel( QString(
"(%4) fid: %1, idx: %2, value: %3" ).arg( fid ).arg( idx ).arg( value.toString() ).arg( mFeatureRequest.
filterType() ), 3 );
197 if ( idx == mCachedField )
198 mFieldCache[ fid ] = value;
207 if ( loadFeatureAtId( fid ) )
242 bool ins =
false, rm =
false;
251 for (
int idx = 0; idx < fields.
count(); ++idx )
255 if ( widgetFactory && widgetType !=
"Hidden" )
268 beginInsertColumns( QModelIndex(),
mFieldCount, attributes.size() - 1 );
273 beginRemoveColumns( QModelIndex(), attributes.size(),
mFieldCount - 1 );
295 beginRemoveRows( QModelIndex(), 0,
rowCount() - 1 );
312 if ( t.elapsed() > 1000 )
357 QgsDebugMsg( QString(
"idToRow: id %1 not in the map" ).arg(
id ) );
371 QModelIndexList indexes;
374 for (
int column = 0; column <
columnCount(); ++column )
376 indexes.append(
index( row, column ) );
386 QgsDebugMsg( QString(
"rowToId: row %1 not in the map" ).arg( row ) );
421 if ( role == Qt::DisplayRole )
423 if ( orientation == Qt::Vertical )
425 return QVariant( section );
430 if ( attributeName.isEmpty() )
433 attributeName = field.
name();
435 return QVariant( attributeName );
439 return tr(
"feature id" );
450 if ( !index.isValid() ||
451 ( role != Qt::TextAlignmentRole
452 && role != Qt::DisplayRole
453 && role != Qt::EditRole
467 return role == Qt::DisplayRole ? rowId : QVariant();
476 QVariant::Type fldType = field.
type();
477 bool fldNumeric = ( fldType == QVariant::Int || fldType == QVariant::Double || fldType == QVariant::LongLong );
479 if ( role == Qt::TextAlignmentRole )
482 return QVariant( Qt::AlignRight );
484 return QVariant( Qt::AlignLeft );
490 if ( mCachedField == fieldId )
492 val = mFieldCache[ rowId ];
498 if ( !loadFeatureAtId( rowId ) )
499 return QVariant(
"ERROR" );
502 return QVariant(
"ERROR" );
508 if ( role == Qt::DisplayRole )
526 if ( mChangedCellBounds.isNull() )
528 mChangedCellBounds = QRect( index.column(), index.row(), 1, 1 );
532 if ( index.column() < mChangedCellBounds.left() )
534 mChangedCellBounds.setLeft( index.column() );
536 if ( index.row() < mChangedCellBounds.top() )
538 mChangedCellBounds.setTop( index.row() );
540 if ( index.column() > mChangedCellBounds.right() )
542 mChangedCellBounds.setRight( index.column() );
544 if ( index.row() > mChangedCellBounds.bottom() )
546 mChangedCellBounds.setBottom( index.row() );
555 if ( !index.isValid() )
556 return Qt::ItemIsEnabled;
559 return Qt::NoItemFlags;
561 Qt::ItemFlags
flags = QAbstractItemModel::flags( index );
563 if (
layer()->isEditable() &&
567 flags |= Qt::ItemIsEditable;
575 emit dataChanged( index1, index2 );
623 QStringList fldNames;
624 fldNames << fields[ fieldId ].name();
632 mFieldCache.insert( f.
id(), f.
attribute( fieldId ) );
635 mCachedField = fieldId;
648 return mFeatureRequest;
QgsFeatureId id() const
Get the feature id for this feature.
void setRequest(const QgsFeatureRequest &request)
Set a request that will be used to fill this attribute table model.
const QString & name() const
Gets the name of the field.
Wrapper for iterator of features from vector data provider or vector layer.
bool acceptFeature(const QgsFeature &feature)
Check if a feature is accepted by this requests filter.
QgsAttributeList mAttributes
const Flags & flags() const
virtual void loadLayer()
Loads the layer into the model Preferably to be called, before basing any other models on this model...
QModelIndexList idToIndexList(QgsFeatureId id) const
QHash< int, QgsFeatureId > mRowIdMap
bool isValid() const
Return the validity of this feature.
QgsAttributeAction * actions()
virtual void layerDeleted()
Launched when layer has been deleted.
void reload(const QModelIndex &index1, const QModelIndex &index2)
Reloads the model data between indices.
#define FID_TO_STRING(fid)
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QModelIndex idToIndex(QgsFeatureId id) const
Container of fields for a vector layer.
bool setAttribute(int field, const QVariant &attr)
Set an attribute by id.
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
const QgsFeatureRequest & request() const
Get the the feature request.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
virtual bool isEditable() const override
Returns true if the provider is in editing mode.
void resetModel()
Resets the model.
QgsVectorLayerCache * mLayerCache
virtual void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Launched when attribute value has been changed.
virtual QVariant data(const QModelIndex &index, int role) const override
Returns data on the given index.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Returns header data.
void executeMapLayerAction(QgsMapLayerAction *action, const QModelIndex &idx) const
Execute a QgsMapLayerAction.
void setFeatureId(QgsFeatureId id)
Set the feature id for this feature.
#define QgsDebugMsgLevel(str, level)
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns.
QVector< QgsEditorWidgetFactory * > mWidgetFactories
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
const QString editorWidgetV2(int fieldIdx) const
Get the id for the editor widget used to represent the field at the given index.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QList< int > QgsAttributeList
QVector< QgsEditorWidgetConfig > mWidgetConfigs
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Remove rows.
int count() const
Return number of items.
virtual bool isModified() const
Returns true if the provider has been modified since the last commit.
QGis::GeometryType geometryType() const
Returns point, line or polygon.
QHash< QgsFeatureId, int > mIdRowMap
FilterType filterType() const
Encapsulate a field in an attribute table or data source.
QString attributeAlias(int attributeIndex) const
Returns the alias of an attribute name or an empty string if there is no alias.
void executeAction(int action, const QModelIndex &idx) const
Execute an action.
virtual void featureAdded(QgsFeatureId fid)
Launched when a feature has been added.
QgsFeature feature(const QModelIndex &idx) const
Return the feature attributes at given model index.
This class caches features of a given QgsVectorLayer.
void progress(int i, bool &cancel)
void modelChanged()
Model has been changed.
QVector< QVariant > mAttributeWidgetCaches
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows.
int fieldCol(int idx) const
get column from field index
virtual void featureDeleted(QgsFeatureId fid)
Launched when a feature has been deleted.
bool featureAtId(QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
Gets the feature at the given feature id.
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
virtual void loadAttributes()
Gets mFieldCount, mAttributes and mValueMaps.
int idToRow(QgsFeatureId id) const
Maps feature id to table row.
void doAction(int index, const QgsFeature &feat, int defaultValueIndex=0)
int fieldIdx(int col) const
get field index from column
QgsFeatureId rowToId(int row) const
Maps row to feature id.
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
void swapRows(QgsFeatureId a, QgsFeatureId b)
Swaps two rows.
bool nextFeature(QgsFeature &f)
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest=QgsFeatureRequest())
Query this VectorLayerCache for features.
QgsFeatureRequest & setFlags(Flags flags)
Set flags that affect how features will be fetched.
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Updates data on given index.
void triggerForFeature(QgsMapLayer *layer, const QgsFeature *feature)
Triggers the action with the specified layer and feature.
QgsAttributeTableModel(QgsVectorLayerCache *layerCache, QObject *parent=0)
Constructor.
allows modification of attribute values
An action which can run on map layers.
void prefetchColumnData(int column)
Caches the entire data for one column.
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns item flags for the index.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.