40 , mLayerCache( layerCache )
42 , mSortFieldIndex( -1 )
56 if ( !
layer()->hasGeometryType() )
63 connect(
layer(), SIGNAL( attributeDeleted(
int ) ),
this, SLOT( attributeDeleted(
int ) ) );
64 connect(
layer(), SIGNAL( updatedFields() ),
this, SLOT( updatedFields() ) );
65 connect(
layer(), SIGNAL( editCommandEnded() ),
this, SLOT( editCommandEnded() ) );
70 bool QgsAttributeTableModel::loadFeatureAtId(
QgsFeatureId fid )
const 110 int currentRowCount = 0;
114 Q_FOREACH (
int row, rows )
117 qDebug() <<
"Row: " << row <<
", begin " << beginRow <<
", last " << lastRow <<
", current " << currentRowCount <<
", removed " << removedRows;
124 if ( row != lastRow + 1 && lastRow != -1 )
126 if ( rows.
count() > 100 && currentRowCount < 10 )
131 removeRows( beginRow - removedRows, currentRowCount );
134 removedRows += currentRowCount;
144 removeRows( beginRow - removedRows, currentRowCount );
151 if ( row < 0 || count < 1 )
161 for (
int i = row; i < row + count; i++ )
170 for (
int i = row + count; i < n; i++ )
205 featOk = loadFeatureAtId( fid );
210 if ( mSortFieldIndex >= 0 )
218 else if ( mSortCacheExpression.
isValid() )
228 if ( !resettingModel )
232 if ( !resettingModel )
239 void QgsAttributeTableModel::updatedFields()
245 void QgsAttributeTableModel::editCommandEnded()
249 mChangedCellBounds =
QRect();
252 void QgsAttributeTableModel::attributeDeleted(
int idx )
254 if ( mSortCacheAttributes.
contains( idx ) )
272 if ( mSortCacheAttributes.
contains( idx ) )
274 if ( mSortFieldIndex == -1 )
276 loadFeatureAtId( fid );
286 mSortCache.
insert( fid, sortValue );
292 if ( loadFeatureAtId( fid ) )
297 if ( loadFeatureAtId( fid ) )
332 bool ins =
false, rm =
false;
341 for (
int idx = 0; idx < fields.
count(); ++idx )
360 else if ( attributes.
size() + mExtraColumns <
mFieldCount + mExtraColumns )
370 mSortFieldIndex = -1;
437 if ( fieldIndex == -1 )
486 QModelIndexList indexes;
490 indexes.reserve( columns );
491 for (
int column = 0; column < columns; ++column )
493 indexes.append(
index( row, column ) );
538 if ( role == Qt::DisplayRole )
540 if ( orientation == Qt::Vertical )
551 return tr(
"extra column" );
554 else if ( role == Qt::ToolTipRole )
556 if ( orientation == Qt::Vertical )
559 return tr(
"Feature ID: %1" ).
arg(
rowToId( section ) );
576 ( role != Qt::TextAlignmentRole
577 && role != Qt::DisplayRole
578 && role != Qt::EditRole
582 && role != Qt::BackgroundColorRole
583 && role != Qt::TextColorRole
584 && role != Qt::DecorationRole
585 && role != Qt::FontRole
605 return mSortCache[rowId];
610 if ( role == Qt::TextAlignmentRole )
617 if ( !loadFeatureAtId( rowId ) )
628 case Qt::DisplayRole:
635 case Qt::BackgroundColorRole:
636 case Qt::TextColorRole:
637 case Qt::DecorationRole:
656 styles.
insert( 0, rowstyle );
665 if ( role == Qt::DecorationRole )
667 if ( role == Qt::FontRole )
688 if ( mChangedCellBounds.
isNull() )
694 if ( index.
column() < mChangedCellBounds.
left() )
698 if ( index.
row() < mChangedCellBounds.
top() )
700 mChangedCellBounds.
setTop( index.
row() );
706 if ( index.
row() > mChangedCellBounds.
bottom() )
718 return Qt::ItemIsEnabled;
721 return Qt::NoItemFlags;
725 if (
layer()->isEditable() &&
729 flags |= Qt::ItemIsEditable;
781 mSortCacheAttributes.
clear();
782 mSortFieldIndex = -1;
783 if ( !expressionString.
isEmpty() )
796 if ( mSortCacheExpression.
isField() )
802 if ( mSortFieldIndex == -1 )
813 mSortCacheAttributes.
append( mSortFieldIndex );
828 if ( mSortFieldIndex == -1 )
836 mSortCache.
insert( f.
id(), sortValue );
843 if ( mSortCacheExpression.
isValid() )
858 return mFeatureRequest;
bool isValid() const
Returns the validity of this feature.
void setRequest(const QgsFeatureRequest &request)
Set a request that will be used to fill this attribute table model.
Class for parsing and evaluation of expressions (formerly called "search strings").
QgsActionManager * actions()
Get all layer actions defined on this layer.
Wrapper for iterator of features from vector data provider or vector layer.
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
int extraColumns() const
Empty extra columns to announce from this model.
bool acceptFeature(const QgsFeature &feature)
Check if a feature is accepted by this requests filter.
virtual void featuresDeleted(const QgsFeatureIds &fids)
Launched when eatures have been deleted.
QgsAttributeList mAttributes
iterator insert(const Key &key, const T &value)
virtual void loadLayer()
Loads the layer into the model Preferably to be called, before using this model as source for any oth...
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
Q_DECL_DEPRECATED QVariant evaluate(const QgsFeature *f)
Evaluate the feature and return the result.
const Flags & flags() const
QHash< int, QgsFeatureId > mRowIdMap
Get the field index of this column.
void append(const T &value)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
Q_DECL_DEPRECATED bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QSet< QgsFeatureId > QgsFeatureIds
virtual void layerDeleted()
Launched when layer has been deleted.
QStringList referencedColumns() const
Get list of columns referenced by the expression.
QList< QgsConditionalStyle > fieldStyles(const QString &fieldName)
Returns the conditional styles set for the field UI properties.
const T & at(int i) const
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.
FilterType filterType() const
Return the filter type which is currently set on this request.
Container of fields for a vector layer.
bool setAttribute(int field, const QVariant &attr)
Set an attribute's value by field index.
bool validBackgroundColor() const
Check if the background color is valid for render.
QPixmap icon() const
The icon set for style generated from the set symbol.
void fieldConditionalStyleChanged(const QString &fieldName)
Handles updating the model when the conditional style for a field changes.
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...
virtual bool isEditable() const override
Returns true if the provider is in editing mode.
int count() const
Return number of items.
QString tr(const char *sourceText, const char *disambiguation, int n)
void resetModel()
Resets the model.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
QgsVectorLayerCache * mLayerCache
QModelIndex idToIndex(QgsFeatureId id) const
QgsEditFormConfig * editFormConfig() const
Get the configuration of the form used to represent this vector layer.
QgsFields fields() const
Returns the list of fields of this layer.
QgsConditionalLayerStyles * conditionalStyles() const
Return the conditional styles that are set for this layer.
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.
int indexOf(const T &value, int from) const
Get the feature id of the feature in this row.
const Node * rootNode() const
Returns root node of the expression. Root node is null is parsing has failed.
QgsFeature feature(const QModelIndex &idx) const
Return the feature attributes at given model index.
const char * name() const
int count(const T &value) const
QVariantMap QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
void append(const T &value)
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Returns header data.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
Conditional styling for a rule.
const_iterator constEnd() const
void setFeatureId(QgsFeatureId id)
Sets the feature ID for this feature.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
#define QgsDebugMsgLevel(str, level)
void setExtraColumns(int extraColumns)
Empty extra columns to announce from this model.
bool isValid() const
Checks if this expression is valid.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns.
QVector< QgsEditorWidgetFactory * > mWidgetFactories
QgsExpressionContext mExpressionContext
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
bool isValid() const
isValid Check if this rule is valid.
static QList< QgsConditionalStyle > matchingConditionalStyles(const QList< QgsConditionalStyle > &styles, const QVariant &value, QgsExpressionContext &context)
Find and return the matching styles for the value and feature.
void beginRemoveRows(const QModelIndex &parent, int first, int last)
This class wraps a request for features to a vector layer (or directly its vector data provider)...
void beginRemoveColumns(const QModelIndex &parent, int first, int last)
QColor backgroundColor() const
The background color for style.
QVector< QgsEditorWidgetConfig > mWidgetConfigs
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Remove rows.
QHash< QgsFeatureId, int > mIdRowMap
QgsFeatureRequest & setFlags(const QgsFeatureRequest::Flags &flags)
Set flags that affect how features will be fetched.
static QgsConditionalStyle compressStyles(const QList< QgsConditionalStyle > &styles)
Compress a list of styles into a single style.
QGis::GeometryType geometryType() const
Returns point, line or polygon.
const QgsFeatureRequest & request() const
Get the the feature request.
Encapsulate a field in an attribute table or data source.
int remove(const Key &key)
void executeAction(int action, const QModelIndex &idx) const
Execute an action.
QgsFeatureId id() const
Get the feature ID for this feature.
QModelIndexList idToIndexList(QgsFeatureId id) const
QString expression() const
Return the original, unmodified expression string.
This class caches features of a given QgsVectorLayer.
bool contains(const T &value) const
static int debugLevel()
Reads the environment variable QGIS_DEBUG and converts it to int.
void progress(int i, bool &cancel)
void beginInsertRows(const QModelIndex &parent, int first, int last)
virtual void featureAdded(QgsFeatureId fid, bool resettingModel=false)
Launched when a feature has been added.
void modelChanged()
Model has been changed.
QVector< QVariant > mAttributeWidgetCaches
const T & at(int i) const
const_iterator constBegin() const
QColor textColor() const
The text color set for style.
QgsAttributeTableModel(QgsVectorLayerCache *layerCache, QObject *parent=nullptr)
Constructor.
int fieldCol(int idx) const
get column from field index
int fieldIdx(int col) const
get field index from column
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows.
void insert(int i, const T &value)
bool featureAtId(QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
Gets the feature at the given feature id.
bool validTextColor() const
Check if the text color is valid for render.
bool isField() const
Checks whether an expression consists only of a single field reference.
virtual bool isModified() const
Returns true if the provider has been modified since the last commit.
void executeMapLayerAction(QgsMapLayerAction *action, const QModelIndex &idx) const
Execute a QgsMapLayerAction.
virtual void loadAttributes()
Gets mFieldCount, mAttributes and mValueMaps.
QgsFeatureId rowToId(int row) const
Maps row to feature id.
QString sortCacheExpression() const
The expression which was used to fill the sorting cache.
virtual Qt::ItemFlags flags(const QModelIndex &index) const
bool contains(const Key &key) const
static QgsExpressionContextScope * projectScope()
Creates a new scope which contains variables and functions relating to the current QGIS project...
QFont font() const
The font for the style.
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
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.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest=QgsFeatureRequest())
Query this VectorLayerCache for features.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
void beginInsertColumns(const QModelIndex &parent, int first, int last)
void doAction(int index, const QgsFeature &feat, int defaultValueIndex=0, const QgsExpressionContextScope &scope=QgsExpressionContextScope())
Does the given action.
int idToRow(QgsFeatureId id) const
Maps feature id to table row.
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.
Allows modification of attribute values.
An action which can run on map layers.
void prefetchColumnData(int column)
Caches the entire data for one column.
QHash< int, QList< QgsConditionalStyle > > mRowStylesMap
void prefetchSortData(const QString &expression)
Prefetches the entire data for one expression.
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns item flags for the index.