QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Public Types | Signals | Public Member Functions | Protected Slots | Protected Member Functions | Protected Attributes | Private Slots | Private Member Functions | Private Attributes | List of all members
QgsAttributeTableModel Class Reference

A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a QAbstractItemView. More...

#include <qgsattributetablemodel.h>

Collaboration diagram for QgsAttributeTableModel:
Collaboration graph
[legend]

Public Types

enum  Role { SortRole = Qt::UserRole + 1, FeatureIdRole = Qt::UserRole + 2, FieldIndexRole = Qt::UserRole + 3 }

Signals

void modelChanged ()
 Model has been changed.
void progress (int i, bool &cancel)
void finished ()

Public Member Functions

 QgsAttributeTableModel (QgsVectorLayerCache *layerCache, QObject *parent=0)
 Constructor.
virtual ~QgsAttributeTableModel ()
virtual void loadLayer ()
 Loads the layer into the model Preferably to be called, before basing any other models on this model.
virtual int rowCount (const QModelIndex &parent=QModelIndex()) const
 Returns the number of rows.
int columnCount (const QModelIndex &parent=QModelIndex()) const
 Returns the number of columns.
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
 Returns header data.
virtual QVariant data (const QModelIndex &index, int role) const
 Returns data on the given index.
virtual bool setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
 Updates data on given index.
Qt::ItemFlags flags (const QModelIndex &index) const
 Returns item flags for the index.
void reload (const QModelIndex &index1, const QModelIndex &index2)
 Reloads the model data between indices.
bool removeRows (int row, int count, const QModelIndex &parent=QModelIndex())
 Remove rows.
void resetModel ()
 Resets the model.
int idToRow (QgsFeatureId id) const
 Maps feature id to table row.
QModelIndex idToIndex (QgsFeatureId id) const
QModelIndexList idToIndexList (QgsFeatureId id) const
int fieldIdx (int col) const
 get field index from column
int fieldCol (int idx) const
 get column from field index
QgsFeatureId rowToId (int row) const
 Maps row to feature id.
void swapRows (QgsFeatureId a, QgsFeatureId b)
 Swaps two rows.
QgsVectorLayerlayer () const
 Returns the layer this model uses as backend.
QgsVectorLayerCachelayerCache () const
 Returns the layer cache this model uses as backend.
void executeAction (int action, const QModelIndex &idx) const
 Execute an action.
QgsFeature feature (const QModelIndex &idx) const
 Return the feature attributes at given model index.
void prefetchColumnData (int column)
 Caches the entire data for one column.

Protected Slots

virtual void attributeValueChanged (QgsFeatureId fid, int idx, const QVariant &value)
 Launched when attribute value has been changed.
virtual void featureDeleted (QgsFeatureId fid)
 Launched when a feature has been deleted.
virtual void featureAdded (QgsFeatureId fid, bool inOperation=true)
 Launched when a feature has been added.
virtual void layerDeleted ()
 Launched when layer has been deleted.

Protected Member Functions

virtual void loadAttributes ()
 Gets mFieldCount, mAttributes and mValueMaps.

Protected Attributes

QgsVectorLayerCachemLayerCache
int mFieldCount
QgsFeature mFeat
QgsAttributeList mAttributes
QMap< int, const QMap< QString,
QVariant > * > 
mValueMaps
QHash< QgsFeatureId, int > mIdRowMap
QHash< int, QgsFeatureIdmRowIdMap

Private Slots

virtual void updatedFields ()
 Launched whenever the number of fields has changed.
virtual void attributeDeleted (int idx)
 Called whenever a column is removed;.

Private Member Functions

virtual bool loadFeatureAtId (QgsFeatureId fid) const
 Load feature fid into local cache (mFeat)

Private Attributes

QgsFeatureRequest mFeatureRequest
int mCachedField
 The currently cached column.
QHash< QgsFeatureId, QVariant > mFieldCache
 Allows to cache one specific column (used for sorting)

Detailed Description

A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a QAbstractItemView.

Is able to generate editor widgets for its QModelIndexes as well. Is mostly referred to as "master model" within this doc and the source.

See Also
Qt Model View Programming

Definition at line 42 of file qgsattributetablemodel.h.

Member Enumeration Documentation

Enumerator:
SortRole 
FeatureIdRole 
FieldIndexRole 

Definition at line 47 of file qgsattributetablemodel.h.

Constructor & Destructor Documentation

QgsAttributeTableModel::QgsAttributeTableModel ( QgsVectorLayerCache layerCache,
QObject *  parent = 0 
)
QgsAttributeTableModel::~QgsAttributeTableModel ( )
virtual

Definition at line 61 of file qgsattributetablemodel.cpp.

References mValueMaps.

Member Function Documentation

void QgsAttributeTableModel::attributeDeleted ( int  idx)
privatevirtualslot

Called whenever a column is removed;.

Definition at line 152 of file qgsattributetablemodel.cpp.

References mCachedField, and prefetchColumnData().

Referenced by QgsAttributeTableModel().

void QgsAttributeTableModel::attributeValueChanged ( QgsFeatureId  fid,
int  idx,
const QVariant &  value 
)
protectedvirtualslot

Launched when attribute value has been changed.

Parameters
fidfeature id
idxattribute index
valuenew value

Definition at line 177 of file qgsattributetablemodel.cpp.

References fieldCol(), QgsFeature::id(), idToRow(), mCachedField, mFeat, mFieldCache, setData(), and QgsFeature::setValid().

Referenced by QgsAttributeTableModel().

int QgsAttributeTableModel::columnCount ( const QModelIndex &  parent = QModelIndex()) const

Returns the number of columns.

Parameters
parentparent index

Definition at line 416 of file qgsattributetablemodel.cpp.

References mFieldCount.

Referenced by featureAdded(), and idToIndexList().

QVariant QgsAttributeTableModel::data ( const QModelIndex &  index,
int  role 
) const
virtual
void QgsAttributeTableModel::executeAction ( int  action,
const QModelIndex &  idx 
) const
QgsFeature QgsAttributeTableModel::feature ( const QModelIndex &  idx) const

Return the feature attributes at given model index.

Returns
feature attributes at given model index

Definition at line 606 of file qgsattributetablemodel.cpp.

References data(), QgsFeature::initAttributes(), mAttributes, rowToId(), QgsFeature::setAttribute(), and QgsFeature::setFeatureId().

Referenced by executeAction().

void QgsAttributeTableModel::featureAdded ( QgsFeatureId  fid,
bool  inOperation = true 
)
protectedvirtualslot

Launched when a feature has been added.

Parameters
fidfeature id
inOperationguard insertion with beginInsertRows() / endInsertRows()

Definition at line 129 of file qgsattributetablemodel.cpp.

References columnCount(), mIdRowMap, mRowIdMap, prefetchColumnData(), reload(), and rowCount().

Referenced by loadLayer(), and QgsAttributeTableModel().

void QgsAttributeTableModel::featureDeleted ( QgsFeatureId  fid)
protectedvirtualslot

Launched when a feature has been deleted.

Parameters
fidfeature id

Definition at line 78 of file qgsattributetablemodel.cpp.

References idToRow(), and prefetchColumnData().

Referenced by QgsAttributeTableModel().

int QgsAttributeTableModel::fieldCol ( int  idx) const

get column from field index

Definition at line 405 of file qgsattributetablemodel.cpp.

References mAttributes.

Referenced by attributeValueChanged().

int QgsAttributeTableModel::fieldIdx ( int  col) const

get field index from column

Definition at line 400 of file qgsattributetablemodel.cpp.

References mAttributes.

Referenced by executeAction().

void QgsAttributeTableModel::finished ( )
signal

Referenced by loadLayer().

Qt::ItemFlags QgsAttributeTableModel::flags ( const QModelIndex &  index) const

Returns item flags for the index.

Parameters
indexmodel index

Definition at line 566 of file qgsattributetablemodel.cpp.

References QgsVectorLayer::Immutable, layer(), mAttributes, and mFieldCount.

Referenced by loadAttributes().

QVariant QgsAttributeTableModel::headerData ( int  section,
Qt::Orientation  orientation,
int  role = Qt::DisplayRole 
) const

Returns header data.

Parameters
sectionrequired section
orientationhorizontal or vertical orientation
roledata role

Definition at line 422 of file qgsattributetablemodel.cpp.

References QgsVectorLayer::attributeAlias(), layer(), mAttributes, mFieldCount, QgsField::name(), QgsVectorLayer::pendingFields(), and tr.

QModelIndex QgsAttributeTableModel::idToIndex ( QgsFeatureId  id) const

Definition at line 370 of file qgsattributetablemodel.cpp.

References idToRow().

Referenced by QgsFeatureListModel::fidToIdx().

QModelIndexList QgsAttributeTableModel::idToIndexList ( QgsFeatureId  id) const

Definition at line 375 of file qgsattributetablemodel.cpp.

References columnCount(), and idToRow().

int QgsAttributeTableModel::idToRow ( QgsFeatureId  id) const

Maps feature id to table row.

Parameters
idfeature id

Definition at line 359 of file qgsattributetablemodel.cpp.

References mIdRowMap, and QgsDebugMsg.

Referenced by attributeValueChanged(), featureDeleted(), idToIndex(), idToIndexList(), and swapRows().

QgsVectorLayer* QgsAttributeTableModel::layer ( ) const
inline

Returns the layer this model uses as backend.

Retrieved from the layer cache.

Definition at line 164 of file qgsattributetablemodel.h.

Referenced by data(), executeAction(), flags(), headerData(), QgsAttributeTableDelegate::layer(), loadAttributes(), prefetchColumnData(), QgsAttributeTableModel(), and setData().

QgsVectorLayerCache* QgsAttributeTableModel::layerCache ( ) const
inline

Returns the layer cache this model uses as backend.

Definition at line 169 of file qgsattributetablemodel.h.

Referenced by QgsAttributeTableFilterModel::generateListOfVisibleFeatures().

void QgsAttributeTableModel::layerDeleted ( )
protectedvirtualslot

Launched when layer has been deleted.

Definition at line 160 of file qgsattributetablemodel.cpp.

References mValueMaps, QgsDebugMsg, removeRows(), and rowCount().

Referenced by QgsAttributeTableModel().

void QgsAttributeTableModel::loadAttributes ( )
protectedvirtual
bool QgsAttributeTableModel::loadFeatureAtId ( QgsFeatureId  fid) const
privatevirtual

Load feature fid into local cache (mFeat)

Parameters
fidfeature id
Returns
feature exists

Definition at line 66 of file qgsattributetablemodel.cpp.

References QgsVectorLayerCache::featureAtId(), mFeat, MathUtils::min(), mLayerCache, and QgsDebugMsgLevel.

Referenced by data().

void QgsAttributeTableModel::loadLayer ( )
virtual
void QgsAttributeTableModel::modelChanged ( )
signal

Model has been changed.

Referenced by updatedFields().

void QgsAttributeTableModel::prefetchColumnData ( int  column)

Caches the entire data for one column.

This should be called prior to sorting, so the data does not have to be fetched for every single comparison. Specify -1 as column to invalidate the cache

Parameters
columnThe column index of the field to catch

Definition at line 619 of file qgsattributetablemodel.cpp.

References QgsFeature::attribute(), QgsVectorLayerCache::getFeatures(), QgsFeature::id(), layer(), mAttributes, mCachedField, mFieldCache, mLayerCache, QgsFeatureIterator::nextFeature(), QgsFeatureRequest::NoGeometry, and QgsVectorLayer::pendingFields().

Referenced by attributeDeleted(), featureAdded(), featureDeleted(), and QgsAttributeTableFilterModel::sort().

void QgsAttributeTableModel::progress ( int  i,
bool &  cancel 
)
signal
Note
not available in python bindings

Referenced by loadLayer().

void QgsAttributeTableModel::reload ( const QModelIndex &  index1,
const QModelIndex &  index2 
)

Reloads the model data between indices.

Parameters
index1start index
index2end index

Definition at line 583 of file qgsattributetablemodel.cpp.

References mFeat, MathUtils::min(), mLayerCache, QgsVectorLayerCache::removeCachedFeature(), rowToId(), and QgsFeature::setFeatureId().

Referenced by featureAdded().

bool QgsAttributeTableModel::removeRows ( int  row,
int  count,
const QModelIndex &  parent = QModelIndex() 
)

Remove rows.

Definition at line 89 of file qgsattributetablemodel.cpp.

References FID_TO_STRING, mIdRowMap, mRowIdMap, and QgsDebugMsgLevel.

Referenced by layerDeleted(), and loadLayer().

void QgsAttributeTableModel::resetModel ( )

Resets the model.

Definition at line 595 of file qgsattributetablemodel.cpp.

int QgsAttributeTableModel::rowCount ( const QModelIndex &  parent = QModelIndex()) const
virtual

Returns the number of rows.

Parameters
parentparent index

Definition at line 410 of file qgsattributetablemodel.cpp.

References mRowIdMap.

Referenced by featureAdded(), QgsDualView::featureCount(), layerDeleted(), and loadLayer().

QgsFeatureId QgsAttributeTableModel::rowToId ( int  row) const
bool QgsAttributeTableModel::setData ( const QModelIndex &  index,
const QVariant &  value,
int  role = Qt::EditRole 
)
virtual

Updates data on given index.

Parameters
indexmodel index
valuenew data value
roledata role

Definition at line 551 of file qgsattributetablemodel.cpp.

References QgsVectorLayer::isEditable(), QgsVectorLayer::isModified(), layer(), and mFieldCount.

Referenced by attributeValueChanged().

void QgsAttributeTableModel::swapRows ( QgsFeatureId  a,
QgsFeatureId  b 
)

Swaps two rows.

Parameters
afirst row
bsecond row

Definition at line 336 of file qgsattributetablemodel.cpp.

References idToRow(), mIdRowMap, and mRowIdMap.

void QgsAttributeTableModel::updatedFields ( )
privatevirtualslot

Launched whenever the number of fields has changed.

Definition at line 145 of file qgsattributetablemodel.cpp.

References loadAttributes(), modelChanged(), and QgsDebugMsg.

Referenced by QgsAttributeTableModel().

Member Data Documentation

QgsAttributeList QgsAttributeTableModel::mAttributes
protected
int QgsAttributeTableModel::mCachedField
private

The currently cached column.

Definition at line 267 of file qgsattributetablemodel.h.

Referenced by attributeDeleted(), attributeValueChanged(), data(), and prefetchColumnData().

QgsFeature QgsAttributeTableModel::mFeat
mutableprotected
QgsFeatureRequest QgsAttributeTableModel::mFeatureRequest
private

Definition at line 264 of file qgsattributetablemodel.h.

Referenced by loadLayer(), and QgsAttributeTableModel().

QHash<QgsFeatureId, QVariant> QgsAttributeTableModel::mFieldCache
private

Allows to cache one specific column (used for sorting)

Definition at line 269 of file qgsattributetablemodel.h.

Referenced by attributeValueChanged(), data(), and prefetchColumnData().

int QgsAttributeTableModel::mFieldCount
protected
QHash<QgsFeatureId, int> QgsAttributeTableModel::mIdRowMap
protected

Definition at line 246 of file qgsattributetablemodel.h.

Referenced by featureAdded(), idToRow(), removeRows(), and swapRows().

QgsVectorLayerCache* QgsAttributeTableModel::mLayerCache
protected
QHash<int, QgsFeatureId> QgsAttributeTableModel::mRowIdMap
protected

Definition at line 247 of file qgsattributetablemodel.h.

Referenced by featureAdded(), removeRows(), rowCount(), rowToId(), and swapRows().

QMap< int, const QMap<QString, QVariant> * > QgsAttributeTableModel::mValueMaps
protected

The documentation for this class was generated from the following files: