27 : QAbstractItemModel( parent )
33 QString fldName( fieldName );
41 if ( !fieldNameWithAlias.isNull() )
42 fldName = fieldNameWithAlias;
54 QModelIndex idx =
index( r, 0 );
84 disconnect(
mLayer, &QObject::destroyed,
this, &QgsFieldModel::layerDeleted );
92 connect(
mLayer, &QObject::destroyed,
this, &QgsFieldModel::layerDeleted );
98 void QgsFieldModel::layerDeleted()
143 for (
int i = 0; i < newFields.
count(); ++i )
153 beginRemoveRows( QModelIndex(), i + offset, i + offset );
188 beginRemoveRows( QModelIndex(), start, end );
201 beginInsertRows( QModelIndex(), 0, 0 );
207 beginRemoveRows( QModelIndex(), 0, 0 );
225 if ( !expression.isEmpty() )
239 if ( hasIndex( row, column, parent ) )
241 return createIndex( row, column, row );
244 return QModelIndex();
250 return QModelIndex();
255 if ( parent.isValid() )
271 if ( !index.isValid() )
284 if ( isEmpty || exprIdx >= 0 )
311 if ( isEmpty || exprIdx >= 0 )
315 return index.row() - fieldOffset;
332 exp.prepare( &context );
333 return !exp.hasParserError();
340 if ( exprIdx < 0 && !isEmpty )
343 return static_cast< int >( field.
type() );
350 if ( exprIdx < 0 && !isEmpty )
362 case Qt::DisplayRole:
364 case Qt::ToolTipRole:
370 else if ( exprIdx >= 0 )
374 else if ( role == Qt::EditRole )
376 return mFields.
at( index.row() - fieldOffset ).name();
378 else if ( role == Qt::ToolTipRole )
390 case Qt::ForegroundRole:
392 if ( !isEmpty && exprIdx >= 0 )
400 exp.prepare( &context );
401 if ( exp.hasParserError() )
403 return QBrush( QColor( Qt::red ) );
411 if ( !isEmpty && exprIdx >= 0 )
414 QFont font = QFont();
415 font.setItalic(
true );
421 case Qt::DecorationRole:
423 if ( !isEmpty && exprIdx < 0 )
438 if ( !field.
alias().isEmpty() )
440 toolTip = QStringLiteral(
"<b>%1</b> (%2)" ).arg( field.
alias(), field.
name() );
444 toolTip = QStringLiteral(
"<b>%1</b>" ).arg( field.
name() );
451 typeString = QStringLiteral(
"%1 (%2, %3)" ).arg( field.
typeName() ).arg( field.
length() ).arg( field.
precision() );
455 typeString = QStringLiteral(
"%1 (%2)" ).arg( field.
typeName() ).arg( field.
length() );
462 toolTip += QStringLiteral(
"<p>%1</p>" ).arg( typeString );
int lookupField(const QString &fieldName) const
Look up field's index from the field name.
Class for parsing and evaluation of expressions (formerly called "search strings").
QgsStringMap attributeAliases() const
Returns a map of field name to attribute alias.
FieldOrigin fieldOrigin(int fieldIdx) const
Gets field's origin (value from an enumeration)
QIcon iconForField(int fieldIdx) const
Returns an icon corresponding to a field index, based on the field's type and source.
static QString fieldToolTip(const QgsField &field)
Returns a HTML formatted tooltip string for a field, containing details like the field name...
void setExpression(const QString &expression)
Sets a single expression to be added after the fields at the end of the model.
Return field index if index corresponds to a field.
Container of fields for a vector layer.
int count() const
Returns number of items.
QgsVectorLayer * layer()
Returns the layer associated with the model.
Return field name if index corresponds to a field.
Return the field origin (if a field, returns QVariant if expression)
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
QVariant data(const QModelIndex &index, int role) const override
QModelIndex parent(const QModelIndex &child) const override
QgsFieldModel(QObject *parent=nullptr)
Constructor for QgsFieldModel - creates a model to display the fields of a given layer.
int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is present in the model.
QgsFields fields() const override
Returns the list of fields of this layer.
void removeExpression()
Removes any custom expression from the model.
QString typeName() const
Gets the field type.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setLayer(QgsVectorLayer *layer)
Set the layer from which fields are displayed.
Return if expression is valid or not.
void setAllowExpression(bool allowExpression)
Sets whether custom expressions are accepted and displayed in the model.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Return the field type (if a field, return QVariant if expression)
Encapsulate a field in an attribute table or data source.
void remove(int fieldIdx)
Remove a field with the given index.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
Return field name or expression.
int columnCount(const QModelIndex &parent) const override
Return if index corresponds to an expression.
Return if the index corresponds to the empty value.
bool isField(const QString &expression) const
Returns true if a string represents a field reference, or false if it is an expression consisting of ...
virtual void updateModel()
Called when the model must be updated.
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QList< QString > mExpression
QModelIndex indexFromName(const QString &fieldName)
Returns the index corresponding to a given fieldName.
Represents a vector layer which manages a vector based data sets.
void updatedFields()
Is emitted, whenever the fields available from this layer have been changed.
QString attributeDisplayName(int index) const
Convenience function that returns the attribute alias if defined or the field name else...
bool allowExpression()
Returns true if the model allows custom expressions to be created and displayed.