21 #include "qgseditorwidgetregistry.h"
36 : QAbstractTableModel( parent )
37 , mLayerCache( layerCache )
50 if ( !
layer()->hasGeometryType() )
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 )
144 beginInsertRows( QModelIndex(), n, n );
182 beginRemoveRows( QModelIndex(), 0,
rowCount() - 1 );
239 bool ins =
false, rm =
false;
248 for (
int idx = 0; idx < fields.
count(); ++idx )
251 QgsEditorWidgetFactory* widgetFactory = QgsEditorWidgetRegistry::instance()->factory( widgetType );
252 if ( widgetFactory && widgetType !=
"Hidden" )
265 beginInsertColumns( QModelIndex(),
mFieldCount, attributes.size() - 1 );
270 beginRemoveColumns( QModelIndex(), attributes.size(),
mFieldCount - 1 );
292 beginRemoveRows( QModelIndex(), 0,
rowCount() - 1 );
309 if ( t.elapsed() > 1000 )
354 QgsDebugMsg( QString(
"idToRow: id %1 not in the map" ).arg(
id ) );
368 QModelIndexList indexes;
371 for (
int column = 0; column <
columnCount(); ++column )
373 indexes.append(
index( row, column ) );
383 QgsDebugMsg( QString(
"rowToId: row %1 not in the map" ).arg( row ) );
418 if ( role == Qt::DisplayRole )
420 if ( orientation == Qt::Vertical )
422 return QVariant( section );
427 if ( attributeName.isEmpty() )
430 attributeName = field.
name();
432 return QVariant( attributeName );
436 return tr(
"feature id" );
447 if ( !index.isValid() ||
448 ( role != Qt::TextAlignmentRole
449 && role != Qt::DisplayRole
450 && role != Qt::EditRole
464 return role == Qt::DisplayRole ? rowId : QVariant();
473 QVariant::Type fldType = field.
type();
474 bool fldNumeric = ( fldType == QVariant::Int || fldType == QVariant::Double || fldType == QVariant::LongLong );
476 if ( role == Qt::TextAlignmentRole )
479 return QVariant( Qt::AlignRight );
481 return QVariant( Qt::AlignLeft );
496 return QVariant(
"ERROR" );
499 return QVariant(
"ERROR" );
505 if ( role == Qt::DisplayRole )
552 if ( !index.isValid() )
553 return Qt::ItemIsEnabled;
556 return Qt::NoItemFlags;
558 Qt::ItemFlags
flags = QAbstractItemModel::flags( index );
560 if (
layer()->isEditable() &&
562 flags |= Qt::ItemIsEditable;
570 emit dataChanged( index1, index2 );
618 QStringList fldNames;
619 fldNames << fields[ fieldId ].name();
QgsFeatureId id() const
Get the feature id for this feature.
void setRequest(const QgsFeatureRequest &request)
const QString & name() const
Gets the name of the field.
QRect mChangedCellBounds
Holds the bounds of changed cells while an update operation is running top = min row left = min colum...
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()
QgsFeatureRequest mFeatureRequest
int columnCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of columns.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of rows.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Remove rows.
QHash< QgsFeatureId, QVariant > mFieldCache
Allows caching of one specific column (used for sorting)
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)
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.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
void resetModel()
Resets the model.
void triggerForFeature(QgsMapLayer *layer, QgsFeature *feature)
Triggers the action with the specified layer and feature.
QgsVectorLayerCache * mLayerCache
virtual void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Launched when attribute value has been changed.
int mCachedField
The currently cached column.
virtual void editCommandEnded()
Gets called when an edit command ends This will synchronize all fields which have been changed since ...
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Updates data on given index.
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)
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.
void doAction(int index, QgsFeature &feat, int defaultValueIndex=0)
Qt::ItemFlags flags(const QModelIndex &index) const
Returns item flags for the index.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QList< int > QgsAttributeList
QVector< QgsEditorWidgetConfig > mWidgetConfigs
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 attributeDeleted(int idx)
Called whenever a column is removed;.
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 headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Returns header data.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
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 QVariant data(const QModelIndex &index, int role) const
Returns data on the given index.
virtual void loadAttributes()
Gets mFieldCount, mAttributes and mValueMaps.
int idToRow(QgsFeatureId id) const
Maps feature id to table row.
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
double ANALYSIS_EXPORT min(double x, double y)
returns the minimum of two doubles or the first argument if both are equal
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.
virtual bool isEditable() const
Returns true if the provider is in editing mode.
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 loadFeatureAtId(QgsFeatureId fid) const
Load feature fid into local cache (mFeat)
QgsAttributeTableModel(QgsVectorLayerCache *layerCache, QObject *parent=0)
Constructor.
An action which can run on map layers.
void prefetchColumnData(int column)
Caches the entire data for one column.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
virtual void updatedFields()
Launched whenever the number of fields has changed.