35 : QAbstractTableModel( parent )
36 , mLayerCache( layerCache )
48 if ( !
layer()->hasGeometryType() )
84 beginRemoveRows( QModelIndex(), row, row );
95 for (
int i = row; i < row + count; i++ )
103 for (
int i = row + count; i < n; i++ )
114 for ( QHash<QgsFeatureId, int>::iterator it =
mIdRowMap.begin(); it !=
mIdRowMap.end(); ++it )
117 QHash<QgsFeatureId, int>::iterator idit;
120 for ( QHash<int, QgsFeatureId>::iterator it =
mRowIdMap.begin(); it !=
mRowIdMap.end(); ++it )
134 beginInsertRows( QModelIndex(), n, n );
164 beginRemoveRows( QModelIndex(), 0,
rowCount() - 1 );
168 const QMap<QString, QVariant> *item;
196 bool ins =
false, rm =
false;
200 for (
int idx = 0; idx < fields.
count(); ++idx )
208 mValueMaps.insert( idx,
new QMap< QString, QVariant >(
layer()->valueMap( idx ) ) );
230 if ( ki >= 0 && vi >= 0 )
232 QSet<int> attributes;
233 attributes << ki << vi;
251 QMap< QString, QVariant > *map =
new QMap< QString, QVariant >();
255 while ( fit.nextFeature( f ) )
257 if ( e && !e->
evaluate( &f ).toBool() )
278 beginInsertColumns( QModelIndex(),
mFieldCount, attributes.size() - 1 );
283 beginRemoveColumns( QModelIndex(), attributes.size(),
mFieldCount - 1 );
303 beginRemoveRows( QModelIndex(), 0,
rowCount() - 1 );
319 if ( t.elapsed() > 1000 )
363 QgsDebugMsg( QString(
"idToRow: id %1 not in the map" ).arg(
id ) );
372 return index(
idToRow(
id ), 0 );
377 QModelIndexList indexes;
380 for (
int column = 0; column <
columnCount(); ++column )
382 indexes.append( index( row, column ) );
392 QgsDebugMsg( QString(
"rowToId: row %1 not in the map" ).arg( row ) );
427 if ( role == Qt::DisplayRole )
429 if ( orientation == Qt::Vertical )
431 return QVariant( section );
436 if ( attributeName.isEmpty() )
439 attributeName = field.
name();
441 return QVariant( attributeName );
445 return tr(
"feature id" );
456 if ( !index.isValid() ||
457 ( role != Qt::TextAlignmentRole
458 && role != Qt::DisplayRole
459 && role != Qt::EditRole
473 return role == Qt::DisplayRole ? rowId : QVariant();
482 QVariant::Type fldType = field.
type();
483 bool fldNumeric = ( fldType == QVariant::Int || fldType == QVariant::Double );
485 if ( role == Qt::TextAlignmentRole )
488 return QVariant( Qt::AlignRight );
490 return QVariant( Qt::AlignLeft );
505 return QVariant(
"ERROR" );
508 return QVariant(
"ERROR" );
523 if ( role == Qt::EditRole )
525 return QVariant( fldType );
530 return settings.value(
"qgis/nullValue",
"NULL" );
534 if ( role == Qt::DisplayRole )
538 return mValueMaps[ fieldId ]->key( val.toString(), QString(
"(%1)" ).arg( val.toString() ) );
543 return val.toDate().toString(
layer()->dateFormat( fieldId ) );
561 emit dataChanged( index, index );
568 if ( !index.isValid() )
569 return Qt::ItemIsEnabled;
572 return Qt::NoItemFlags;
574 Qt::ItemFlags
flags = QAbstractItemModel::flags( index );
576 if (
layer()->isEditable() &&
578 flags |= Qt::ItemIsEditable;
585 for (
int row = index1.row(); row <= index2.row(); row++ )
592 emit dataChanged( index1, index2 );
631 QStringList fldNames;
632 fldNames << fields[ fieldId ].name();
QgsFeatureId id() const
Get the feature id for this feature.
Class for parsing and evaluation of expressions (formerly called "search strings").
const QString & name() const
Gets the name of the field.
Wrapper for iterator of features from vector data provider or vector layer.
QgsAttributeList mAttributes
virtual void loadLayer()
Loads the layer into the model Preferably to be called, before basing any other models on this model...
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
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.
QVariant evaluate(const QgsFeature *f=NULL)
Evaluate the feature and return the result.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of rows.
bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
QString displayString(const QVariant &v) const
Formats string for display.
The attribute value should not be changed in the attribute form.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Remove rows.
QHash< QgsFeatureId, QVariant > mFieldCache
Allows caching one specific column (used for sorting)
virtual void layerDeleted()
Launched when layer has been deleted.
bool removeCachedFeature(QgsFeatureId fid)
Removes the feature identified by fid from the cache if present.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
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.
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 bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Updates data on given index.
void setFeatureId(QgsFeatureId id)
Set the feature id for this feature.
QMap< int, const QMap< QString, QVariant > * > mValueMaps
#define QgsDebugMsgLevel(str, level)
The attribute value should not be shown in the attribute form.
EditType editType(int idx)
get edit type
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest)
Query this VectorLayerCache for features.
QStringList referencedColumns()
Get list of columns referenced by the expression.
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
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
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.
ValueRelationData & valueRelation(int idx)
access relations
void executeAction(int action, const QModelIndex &idx) const
Execute an action.
virtual void featureAdded(QgsFeatureId fid, bool inOperation=true)
Launched when a feature has been added.
virtual void attributeDeleted(int idx)
Called whenever a column is removed;.
QgsFeature feature(const QModelIndex &idx) const
Return the feature attributes at given model index.
This class caches features of a given QgsVectorLayer.
bool needsGeometry()
Returns true if the expression uses feature geometry for some computation.
void progress(int i, bool &cancel)
void setValid(bool validity)
Set the validity of the feature.
void modelChanged()
Model has been changed.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Returns header data.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
virtual ~QgsAttributeTableModel()
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.
QString mFilterExpression
virtual void loadAttributes()
Gets mFieldCount, mAttributes and mValueMaps.
int idToRow(QgsFeatureId id) const
Maps feature id to table row.
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
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)
virtual bool isEditable() const
Returns true if the provider is in editing mode.
Represents a vector layer which manages a vector based data sets.
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
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.
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.