24#include "moc_qgsfieldmodel.cpp"
27 : QAbstractItemModel(
parent )
33 QString fldName( fieldName );
38 if (
mLayer && !fldName.isEmpty() )
43 const QString fieldNameWithAlias =
mLayer->attributeAliases().key( fldName );
44 if ( !fieldNameWithAlias.isNull() )
45 fldName = fieldNameWithAlias;
51 int r =
mFields.lookupField( fldName );
57 QModelIndex idx =
index( r, 0 );
87 disconnect(
mLayer, &QObject::destroyed,
this, &QgsFieldModel::layerDeleted );
95 connect(
mLayer, &QObject::destroyed,
this, &QgsFieldModel::layerDeleted );
101void QgsFieldModel::layerDeleted()
126 beginInsertRows( QModelIndex(),
mFields.count() + offset,
mFields.count() + offset );
140 beginRemoveRows( QModelIndex(),
mFields.count() - 1 + offset,
mFields.count() - 1 + offset );
146 for (
int i = 0; i < newFields.
count(); ++i )
148 if (
mFields.at( i ) != newFields.
at( i ) )
156 beginRemoveRows( QModelIndex(), i + offset, i + offset );
189 const int start =
mFields.count();
191 beginRemoveRows( QModelIndex(), start, end );
204 beginInsertRows( QModelIndex(), 0, 0 );
210 beginRemoveRows( QModelIndex(), 0, 0 );
228 if ( !expression.isEmpty() )
242 if ( hasIndex( row, column,
parent ) )
244 return createIndex( row, column, row );
247 return QModelIndex();
253 return QModelIndex();
274 if ( !
index.isValid() )
287 if ( isEmpty || exprIdx >= 0 )
314 if ( isEmpty || exprIdx >= 0 )
318 return index.row() - fieldOffset;
343 if ( exprIdx < 0 && !isEmpty )
346 return static_cast< int >( field.
type() );
353 if ( exprIdx < 0 && !isEmpty )
355 return static_cast< int >(
mFields.fieldOrigin(
index.row() - fieldOffset ) );
367 if ( exprIdx < 0 && !isEmpty )
369 return mFields.at(
index.row() - fieldOffset ).editorWidgetSetup().type();
376 if ( exprIdx < 0 && !isEmpty )
394 return !(
mLayer->editFormConfig().readOnly(
index.row() - fieldOffset ) );
398 case Qt::DisplayRole:
400 case Qt::ToolTipRole:
406 else if ( exprIdx >= 0 )
410 else if ( role == Qt::EditRole )
414 else if ( role == Qt::ToolTipRole )
420 return mLayer->attributeDisplayName(
index.row() - fieldOffset );
424 return mFields.field(
index.row() - fieldOffset ).displayName();
430 case Qt::ForegroundRole:
432 if ( !isEmpty && exprIdx >= 0 )
443 return QBrush( QColor( Qt::red ) );
451 if ( !isEmpty && exprIdx >= 0 )
454 QFont font = QFont();
455 font.setItalic(
true );
461 case Qt::DecorationRole:
463 if ( !isEmpty && exprIdx < 0 )
465 return mFields.iconForField(
index.row() - fieldOffset );
478 if ( !field.
alias().isEmpty() )
480 toolTip = QStringLiteral(
"<b>%1</b> (%2)" ).arg( field.
alias(), field.
name() );
484 toolTip = QStringLiteral(
"<b>%1</b>" ).arg( field.
name() );
487 toolTip += QStringLiteral(
"<br><font style='font-family:monospace; white-space: nowrap;'>%3</font>" ).arg( field.
displayType(
true ) );
489 const QString comment = field.
comment();
491 if ( ! comment.isEmpty() )
493 toolTip += QStringLiteral(
"<br><em>%1</em>" ).arg( comment );
503 const int fieldIdx =
fields.indexOf( field.
name() );
509 ?
layer->expressionField( fieldIdx )
512 if ( !expressionString.isEmpty() )
514 toolTip += QStringLiteral(
"<br><font style='font-family:monospace;'>%3</font>" ).arg( expressionString );
@ Expression
Field is calculated from an expression.
@ Join
Field originates from a joined layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
Handles parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
static QString fieldToolTip(const QgsField &field)
Returns a HTML formatted tooltip string for a field, containing details like the field name,...
int columnCount(const QModelIndex &parent) const override
QVariant data(const QModelIndex &index, int role) const override
QModelIndex parent(const QModelIndex &child) const override
void removeExpression()
Removes any custom expression from the model.
bool isField(const QString &expression) const
Returns true if a string represents a field reference, or false if it is an expression consisting of ...
void setAllowExpression(bool allowExpression)
Sets whether custom expressions are accepted and displayed in the model.
@ FieldIsWidgetEditable
true if a is editable from the widget
@ FieldOrigin
Return the field origin (if a field, returns QVariant if expression).
@ Expression
Return field name or expression.
@ IsExpression
Return if index corresponds to an expression.
@ IsEmpty
Return if the index corresponds to the empty value.
@ ExpressionValidity
Return if expression is valid or not.
@ FieldName
Return field name if index corresponds to a field.
@ FieldIndex
Return field index if index corresponds to a field.
@ EditorWidgetType
Editor widget type.
@ FieldType
Return the field type (if a field, return QVariant if expression).
@ JoinedFieldIsEditable
true if a joined field is editable (returns QVariant if not a joined field)
void setLayer(QgsVectorLayer *layer)
Set the layer from which fields are displayed.
void setExpression(const QString &expression)
Sets a single expression to be added after the fields at the end of the model.
QgsFields fields() const
Returns the fields currently shown in the model.
void setFields(const QgsFields &fields)
Manually sets the fields to use for the model.
QgsFieldModel(QObject *parent=nullptr)
Constructor for QgsFieldModel - creates a model to display the fields of a given layer.
static QString fieldToolTipExtended(const QgsField &field, const QgsVectorLayer *layer)
Returns a HTML formatted tooltip string for a field, containing details like the field name,...
QList< QString > mExpression
QModelIndex indexFromName(const QString &fieldName)
Returns the index corresponding to a given fieldName.
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is present in the model.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
virtual void updateModel()
Called when the model must be updated.
Encapsulate a field in an attribute table or data source.
QString displayType(bool showConstraints=false) const
Returns the type to use when displaying this field, including the length and precision of the datatyp...
Container of fields for a vector layer.
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
void remove(int fieldIdx)
Removes the field with the given index.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Defines left outer join from our vector layer to some other vector layer.
bool isEditable() const
Returns whether joined fields may be edited through the form of the target layer.
Represents a vector layer which manages a vector based dataset.
void updatedFields()
Emitted whenever the fields available from this layer have been changed.