28 : QAbstractItemModel( parent )
34 QString fldName( fieldName );
42 if ( !fieldNameWithAlias.isNull() )
43 fldName = fieldNameWithAlias;
55 QModelIndex idx =
index( r, 0 );
85 disconnect(
mLayer, &QObject::destroyed,
this, &QgsFieldModel::layerDeleted );
93 connect(
mLayer, &QObject::destroyed,
this, &QgsFieldModel::layerDeleted );
99 void QgsFieldModel::layerDeleted()
144 for (
int i = 0; i < newFields.
count(); ++i )
154 beginRemoveRows( QModelIndex(), i + offset, i + offset );
189 beginRemoveRows( QModelIndex(), start, end );
202 beginInsertRows( QModelIndex(), 0, 0 );
208 beginRemoveRows( QModelIndex(), 0, 0 );
226 if ( !expression.isEmpty() )
240 if ( hasIndex( row, column, parent ) )
242 return createIndex( row, column, row );
245 return QModelIndex();
251 return QModelIndex();
256 if ( parent.isValid() )
272 if ( !index.isValid() )
285 if ( isEmpty || exprIdx >= 0 )
312 if ( isEmpty || exprIdx >= 0 )
316 return index.row() - fieldOffset;
333 exp.prepare( &context );
334 return !exp.hasParserError();
341 if ( exprIdx < 0 && !isEmpty )
344 return static_cast< int >( field.
type() );
351 if ( exprIdx < 0 && !isEmpty )
365 if ( exprIdx < 0 && !isEmpty )
367 return mFields.
at( index.row() - fieldOffset ).editorWidgetSetup().
type();
374 if ( exprIdx < 0 && !isEmpty )
390 case Qt::DisplayRole:
392 case Qt::ToolTipRole:
398 else if ( exprIdx >= 0 )
402 else if ( role == Qt::EditRole )
404 return mFields.
at( index.row() - fieldOffset ).name();
406 else if ( role == Qt::ToolTipRole )
418 case Qt::ForegroundRole:
420 if ( !isEmpty && exprIdx >= 0 )
428 exp.prepare( &context );
429 if ( exp.hasParserError() )
431 return QBrush( QColor( Qt::red ) );
439 if ( !isEmpty && exprIdx >= 0 )
442 QFont font = QFont();
443 font.setItalic(
true );
449 case Qt::DecorationRole:
451 if ( !isEmpty && exprIdx < 0 )
466 if ( !field.
alias().isEmpty() )
468 toolTip = QStringLiteral(
"<b>%1</b> (%2)" ).arg( field.
alias(), field.
name() );
472 toolTip = QStringLiteral(
"<b>%1</b>" ).arg( field.
name() );
479 typeString = QStringLiteral(
"%1 (%2, %3)" ).arg( field.
typeName() ).arg( field.
length() ).arg( field.
precision() );
483 typeString = QStringLiteral(
"%1 (%2)" ).arg( field.
typeName() ).arg( field.
length() );
490 toolTip += QStringLiteral(
"<p>%1</p>" ).arg( typeString );
int lookupField(const QString &fieldName) const
Looks 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.
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
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.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is present in the model.
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...
Defines left outer join from our vector layer to some other vector layer.
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.
true if a joined field is editable (returns QVariant if not a joined field)
void remove(int fieldIdx)
Removes 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.
QgsVectorLayerJoinBuffer * joinBuffer()
Returns the join buffer object.
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.
const QgsVectorLayerJoinInfo * joinForFieldIndex(int index, const QgsFields &fields, int &sourceFieldIndex) const
Finds the vector join for a layer field index.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QList< QString > mExpression
bool isEditable() const
Returns whether joined fields may be edited through the form of the target layer. ...
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()
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.