QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsattributesformmodel.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsattributesformmodel.h
3 ---------------------
4 begin : March 2025
5 copyright : (C) 2025 by Germán Carrillo
6 email : german at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSATTRIBUTESFORMMODEL_H
17#define QGSATTRIBUTESFORMMODEL_H
18
19// We don't want to expose this in the public API
20
21#include "qgsaction.h"
22#include "qgsaddtaborgroup.h"
25#include "qgsvectorlayer.h"
26
27#include <QAbstractItemModel>
28#include <QPushButton>
29#include <QSortFilterProxyModel>
30
31#define SIP_NO_FILE
32
41class GUI_EXPORT QgsAttributesFormData
42{
43 public:
62
89
91 {
92 operator QVariant();
93
96 QVariant nmRelationId;
98 QString label;
99 };
100
102 {
103 QString qmlCode;
104 };
105
107 {
108 QString htmlCode;
109 };
110
112 {
113 QString text;
114 };
115
117 {
118 bool drawLine = false;
119 };
120
128 class GUI_EXPORT AttributeFormItemData
129 {
130 public:
132
134 operator QVariant() { return QVariant::fromValue<AttributeFormItemData>( *this ); }
135
137 int columnCount() const { return mColumnCount; }
138
140 void setColumnCount( int count ) { mColumnCount = count; }
141
148 Qgis::AttributeEditorContainerType containerType() const;
149
156 void setContainerType( Qgis::AttributeEditorContainerType type );
157
166 bool collapsed() const { return mCollapsed; };
167
175 void setCollapsed( bool collapsed ) { mCollapsed = collapsed; };
176
182 const QgsAttributeEditorElement::LabelStyle labelStyle() const;
183
189 void setLabelStyle( const QgsAttributeEditorElement::LabelStyle &labelStyle );
190
192 bool showLabel() const;
193
195 void setShowLabel( bool showLabel );
196
205 int horizontalStretch() const { return mHorizontalStretch; }
206
215 void setHorizontalStretch( int stretch ) { mHorizontalStretch = stretch; }
216
225 int verticalStretch() const { return mVerticalStretch; }
226
235 void setVerticalStretch( int stretch ) { mVerticalStretch = stretch; }
236
242 QgsOptionalExpression visibilityExpression() const;
243
250 void setVisibilityExpression( const QgsOptionalExpression &visibilityExpression );
251
260 QgsOptionalExpression collapsedExpression() const;
261
270 void setCollapsedExpression( const QgsOptionalExpression &collapsedExpression );
271
277 RelationEditorConfiguration relationEditorConfiguration() const;
278
284 void setRelationEditorConfiguration( const RelationEditorConfiguration &relationEditorConfiguration );
285
291 QmlElementEditorConfiguration qmlElementEditorConfiguration() const;
292
298 void setQmlElementEditorConfiguration( const QmlElementEditorConfiguration &qmlElementEditorConfiguration );
299
305 HtmlElementEditorConfiguration htmlElementEditorConfiguration() const;
306
312 void setHtmlElementEditorConfiguration( const HtmlElementEditorConfiguration &htmlElementEditorConfiguration );
313
318 SpacerElementEditorConfiguration spacerElementEditorConfiguration() const;
319
324 void setSpacerElementEditorConfiguration( SpacerElementEditorConfiguration spacerElementEditorConfiguration );
325
331 QColor backgroundColor() const;
332
338 void setBackgroundColor( const QColor &backgroundColor );
339
344 TextElementEditorConfiguration textElementEditorConfiguration() const;
345
350 void setTextElementEditorConfiguration( const TextElementEditorConfiguration &textElementEditorConfiguration );
351
352 private:
353 int mColumnCount = 1;
355 bool mShowLabel = true;
356 int mHorizontalStretch = 0;
357 int mVerticalStretch = 0;
358 QgsOptionalExpression mVisibilityExpression;
359 RelationEditorConfiguration mRelationEditorConfiguration;
360 QmlElementEditorConfiguration mQmlElementEditorConfiguration;
361 HtmlElementEditorConfiguration mHtmlElementEditorConfiguration;
362 TextElementEditorConfiguration mTextElementEditorConfiguration;
363 SpacerElementEditorConfiguration mSpacerElementEditorConfiguration;
364 QColor mBackgroundColor;
365 bool mCollapsed = false;
366 QgsOptionalExpression mCollapsedExpression;
368 };
369};
370
371
379class GUI_EXPORT QgsAttributesFormItem : public QObject
380{
381 Q_OBJECT
382
383 public:
385
396 explicit QgsAttributesFormItem( QgsAttributesFormData::AttributesFormItemType itemType, const QString &name, const QString &displayName = QString(), QgsAttributesFormItem *parent = nullptr );
397
408 explicit QgsAttributesFormItem(
411 const QString &name,
412 const QString &displayName = QString(),
414 );
415
422
428 QgsAttributesFormItem *firstTopChild( const QgsAttributesFormData::AttributesFormItemType itemType, const QString &itemId ) const;
429
435 QgsAttributesFormItem *firstChildRecursive( const QgsAttributesFormData::AttributesFormItemType &itemType, const QString &itemId ) const;
436
440 int childCount() const;
441
447 QgsAttributesFormItem *parent() { return mParent; }
448
452 int row() const;
453
457 QVariant data( int role ) const;
458
462 bool setData( int role, const QVariant &value );
463
467 void addChild( std::unique_ptr< QgsAttributesFormItem > &&child );
468
472 void insertChild( int position, std::unique_ptr< QgsAttributesFormItem > &&item );
473
477 void deleteChildAtIndex( int index );
478
482 void deleteChildren();
483
488
492 QString id() const { return mId; }
493
497 QString name() const { return mName; }
498
502 QString displayName() const { return mDisplayName; }
503
509 QIcon icon() const { return mIcon; }
510
516 void setIcon( const QIcon &icon ) { mIcon = icon; }
517
523 static bool isGroup( QgsAttributesFormItem *item );
524
525 signals:
526
532 void addedChildren( QgsAttributesFormItem *item, int indexFrom, int indexTo );
533
534 private:
535 QString mName = QString();
536 QString mDisplayName = QString();
537 QIcon mIcon;
539 QString mId = QString();
541 QgsAttributesFormData::FieldConfig mFieldConfigData;
542
543 std::vector< std::unique_ptr< QgsAttributesFormItem > > mChildren;
544 QgsAttributesFormItem *mParent = nullptr;
545
546 Q_DISABLE_COPY( QgsAttributesFormItem )
547};
548
549
557class GUI_EXPORT QgsAttributesFormModel : public QAbstractItemModel
558{
559 Q_OBJECT
560
561 public:
577
583 explicit QgsAttributesFormModel( QgsVectorLayer *layer, QgsProject *project, QObject *parent = nullptr );
584
586
587 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
588 QModelIndex parent( const QModelIndex &index ) const override;
589
590 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
591 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
592
593 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
594
600 QModelIndex firstTopMatchingModelIndex( const QgsAttributesFormData::AttributesFormItemType &itemType, const QString &itemId ) const;
601
607 QModelIndex firstRecursiveMatchingModelIndex( const QgsAttributesFormData::AttributesFormItemType &itemType, const QString &itemId ) const;
608
614 bool showAliases() const;
615
621 void setShowAliases( bool show );
622
628 QgsAttributesFormItem *itemForIndex( const QModelIndex &index ) const;
629
636
637 public slots:
638
642 virtual void populate() = 0;
643
644 signals:
645
652
653 protected:
665 bool indexLessThan( const QModelIndex &a, const QModelIndex &b ) const;
666
672 QVector<int> rootToLeafPath( QgsAttributesFormItem *item ) const;
673
674
683 void emitDataChangedRecursively( const QModelIndex &parent = QModelIndex(), const QVector<int> &roles = QVector<int>() );
684
685 std::unique_ptr< QgsAttributesFormItem > mRootItem;
688
689 bool mShowAliases = false;
690};
691
692
701{
702 Q_OBJECT
703
704 public:
710 explicit QgsAttributesAvailableWidgetsModel( QgsVectorLayer *layer, QgsProject *project, QObject *parent = nullptr );
711
712 Qt::ItemFlags flags( const QModelIndex &index ) const override;
713 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
714
715 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
716
717 Qt::DropActions supportedDragActions() const override;
718 QStringList mimeTypes() const override;
719 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
720
726 QModelIndex fieldContainer() const;
727
733 QModelIndex relationContainer() const;
734
740 QModelIndex actionContainer() const;
741
745 QModelIndex fieldModelIndex( const QString &fieldName ) const;
746
747 public slots:
748 void populate() override;
749
755 void populateLayerActions( const QList< QgsAction > actions );
756
757 private:
763 void populateActionItems( const QList<QgsAction> actions );
764};
765
766
775{
776 Q_OBJECT
777
778 public:
785 explicit QgsAttributesFormLayoutModel( QgsVectorLayer *layer, QgsProject *project, QObject *parent = nullptr );
786
787 Qt::ItemFlags flags( const QModelIndex &index ) const override;
788 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
789
790 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
791
795 bool removeRow( int row, const QModelIndex &parent = QModelIndex() );
796
797 Qt::DropActions supportedDragActions() const override;
798 Qt::DropActions supportedDropActions() const override;
799 QStringList mimeTypes() const override;
800 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
801 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
802 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
803
808
812 QList< QgsAddAttributeFormContainerDialog::ContainerPair > listOfContainers() const;
813
819 void addContainer( QModelIndex &parent, const QString &name, int columnCount, Qgis::AttributeEditorContainerType type );
820
829 void updateFieldConfigForFieldItems( const QString &fieldName, const QgsAttributesFormData::FieldConfig &config );
830
839 void updateAliasForFieldItems( const QString &fieldName, const QString &fieldAlias );
840
846 void insertChild( const QModelIndex &parent, int row, const QString &itemId, QgsAttributesFormData::AttributesFormItemType itemType, const QString &itemName );
847
848 public slots:
849 void populate() override;
850
851 signals:
853 void externalItemDropped( QModelIndex &index );
855 void internalItemDropped( QModelIndex &index );
856
857 private:
859 void updateFieldConfigForFieldItemsRecursive( QgsAttributesFormItem *parent, const QString &fieldName, const QgsAttributesFormData::FieldConfig &config );
861 void updateAliasForFieldItemsRecursive( QgsAttributesFormItem *parent, const QString &fieldName, const QString &fieldAlias );
862
863 QList< QgsAddAttributeFormContainerDialog::ContainerPair > recursiveListOfContainers( QgsAttributesFormItem *parent ) const;
864 void loadAttributeEditorElementItem( QgsAttributeEditorElement *const editorElement, QgsAttributesFormItem *parent, const int position = -1 );
865
874 QModelIndexList curateIndexesForMimeData( const QModelIndexList &indexes ) const;
875};
876
877
885class GUI_EXPORT QgsAttributesFormProxyModel : public QSortFilterProxyModel
886{
887 Q_OBJECT
888
889 public:
893 QgsAttributesFormProxyModel( QObject *parent = nullptr );
894
899
903 const QString filterText() const;
904
911
912 public slots:
914 void setFilterText( const QString &filterText = QString() );
915
916 protected:
917 bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
918
919 private:
920 QgsAttributesFormModel *mModel = nullptr;
921 QString mFilterText;
922};
923
924
928
929#endif // QGSATTRIBUTESFORMMODEL_H
AttributeEditorContainerType
Attribute editor container types.
Definition qgis.h:5826
AttributeFormReuseLastValuePolicy
Attribute form policy for reusing last entered values.
Definition qgis.h:5885
@ NotAllowed
Reuse of last values not allowed.
Definition qgis.h:5886
FieldDomainMergePolicy
Merge policy for field domains.
Definition qgis.h:4040
@ DefaultValue
Use default field value.
Definition qgis.h:4041
FieldDomainSplitPolicy
Split policy for field domains.
Definition qgis.h:4023
@ Duplicate
Duplicate original value.
Definition qgis.h:4025
FieldDuplicatePolicy
Duplicate policy for fields.
Definition qgis.h:4060
@ Duplicate
Duplicate original value.
Definition qgis.h:4062
An abstract base class for any elements of a drag and drop form.
QgsAttributesAvailableWidgetsModel(QgsVectorLayer *layer, QgsProject *project, QObject *parent=nullptr)
Constructor for QgsAttributesAvailableWidgetsModel, with the given parent.
Qt::DropActions supportedDragActions() const override
Qt::ItemFlags flags(const QModelIndex &index) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QMimeData * mimeData(const QModelIndexList &indexes) const override
QModelIndex fieldModelIndex(const QString &fieldName) const
Returns the model index that corresponds to the field with the given fieldName.
QModelIndex actionContainer() const
Returns the action container in this model, expected to be placed at the third top-level row.
void populateLayerActions(const QList< QgsAction > actions)
Refresh layer actions in the model to keep an updated action list.
QModelIndex fieldContainer() const
Returns the field container in this model, expected to be placed at the first top-level row.
QModelIndex relationContainer() const
Returns the relation container in this model, expected to be placed at the second top-level row.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Main class to store and transfer editor data contained in a QgsAttributesFormModel.
bool collapsed() const
For group box containers returns if this group box is collapsed.
void setColumnCount(int count)
Sets the number of columns for a container.
int columnCount() const
Returns the number of columns in a container.
int horizontalStretch() const
Returns the horizontal stretch factor for the element.
void setHorizontalStretch(int stretch)
Sets the horizontal stretch factor for the element.
int verticalStretch() const
Returns the vertical stretch factor for the element.
void setVerticalStretch(int stretch)
Sets the vertical stretch factor for the element.
void setCollapsed(bool collapsed)
For group box containers sets if this group box is collapsed.
Describes editor data contained in a QgsAttributesFormModel.
AttributesFormItemType
Custom item types.
@ Container
Container for the form, which may be tab, group or row.
@ Relation
Relation between two vector layers.
@ Field
Vector layer field.
@ SpacerWidget
Spacer widget type,.
@ WidgetType
In the available widgets tree, the type of widget.
@ TextWidget
Text widget type,.
Holds parent-child relations as well as item data contained in a QgsAttributesFormModel.
QIcon icon() const
Returns the icon of the item.
void setIcon(const QIcon &icon)
Sets an icon for the item.
QgsAttributesFormItem()=default
QString name() const
Returns the name of the item.
int childCount() const
Returns the number of children items for the given item.
QgsAttributesFormItem * child(int row)
Access the child item located at row position.
QgsAttributesFormItem * parent()
Returns the parent object of the item.
QgsAttributesFormItem * firstChildRecursive(const QgsAttributesFormData::AttributesFormItemType &itemType, const QString &itemId) const
Access the first child item that matches itemType and itemId, recursively.
int row() const
Returns the position of the item regarding its parent.
QgsAttributesFormData::AttributesFormItemType type() const
Returns the type of the item.
QString id() const
Returns the id of the item.
QVariant data(int role) const
Returns the data stored in the item, corresponding to the given role.
QString displayName() const
Returns the display name of the item.
void addedChildren(QgsAttributesFormItem *item, int indexFrom, int indexTo)
Notifies other objects when children have been added to the item, informing the indices where added c...
QgsAttributesFormItem * firstTopChild(const QgsAttributesFormData::AttributesFormItemType itemType, const QString &itemId) const
Access the first top-level child item that matches itemType and itemId.
void externalItemDropped(QModelIndex &index)
Informs that items were inserted (via drop) in the model from another model.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Qt::DropActions supportedDragActions() const override
void updateAliasForFieldItems(const QString &fieldName, const QString &fieldAlias)
Updates the aliases of all matching fields in the model.
QStringList mimeTypes() const override
QgsAttributesFormLayoutModel(QgsVectorLayer *layer, QgsProject *project, QObject *parent=nullptr)
Constructor for QgsAttributesFormLayoutModel, with the given parent.
QMimeData * mimeData(const QModelIndexList &indexes) const override
bool removeRow(int row, const QModelIndex &parent=QModelIndex())
Removes the index located at row within the given parent.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Qt::DropActions supportedDropActions() const override
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
void updateFieldConfigForFieldItems(const QString &fieldName, const QgsAttributesFormData::FieldConfig &config)
Updates the field config of all matching fields in the model.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
void addContainer(QModelIndex &parent, const QString &name, int columnCount, Qgis::AttributeEditorContainerType type)
Adds a new container to parent.
QList< QgsAddAttributeFormContainerDialog::ContainerPair > listOfContainers() const
Returns a list of containers stored in the model, structured as pairs (name, container model index).
QgsAttributeEditorElement * createAttributeEditorWidget(const QModelIndex &index, QgsAttributeEditorElement *parent) const
Creates a new attribute editor element based on the definition stored in a form layout model index.
Qt::ItemFlags flags(const QModelIndex &index) const override
void internalItemDropped(QModelIndex &index)
Informs that items were moved (via drop) in the model from the same model.
void insertChild(const QModelIndex &parent, int row, const QString &itemId, QgsAttributesFormData::AttributesFormItemType itemType, const QString &itemName)
Inserts a new child to parent model index at the given row position.
Abstract class for tree models allowing for configuration of attributes forms.
void emitDataChangedRecursively(const QModelIndex &parent=QModelIndex(), const QVector< int > &roles=QVector< int >())
Emits dataChanged signal for all parent items in a model.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
std::unique_ptr< QgsAttributesFormItem > mRootItem
bool showAliases() const
Returns whether field aliases are preferred over field names as item text.
virtual void populate()=0
Populates the model with initial data read from the layer.
@ ItemFieldConfigRole
Prior to QGIS 3.44, this was available as FieldConfigRole.
@ ItemDisplayRole
Display text for the item.
@ ItemNameRole
Prior to QGIS 3.44, this was available as FieldNameRole.
@ ItemDataRole
Prior to QGIS 3.44, this was available as DnDTreeRole.
@ ItemIdRole
Items may have ids to ease comparison. Used by Relations, fields, actions and containers.
void setShowAliases(bool show)
Sets whether field aliases should be preferred over field names as item text.
QModelIndex firstRecursiveMatchingModelIndex(const QgsAttributesFormData::AttributesFormItemType &itemType, const QString &itemId) const
Returns the first model index that matches the given itemType and itemId, recursively.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QgsAttributesFormModel(QgsVectorLayer *layer, QgsProject *project, QObject *parent=nullptr)
Constructor for QgsAttributesFormModel, with the given parent.
QModelIndex parent(const QModelIndex &index) const override
~QgsAttributesFormModel() override
QModelIndex firstTopMatchingModelIndex(const QgsAttributesFormData::AttributesFormItemType &itemType, const QString &itemId) const
Returns the first top-level model index that matches the given itemType and itemId.
bool indexLessThan(const QModelIndex &a, const QModelIndex &b) const
Auxiliary function to sort indexes, returning true if index a is less than index b.
void fieldConfigDataChanged(QgsAttributesFormItem *item)
Notifies other objects that the field config data has changed in the item.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
QgsAttributesFormItem * rootItem() const
Returns the root item in this model.
QVector< int > rootToLeafPath(QgsAttributesFormItem *item) const
Returns a QVector of iterative positions from root item to the given item.
QgsAttributesFormItem * itemForIndex(const QModelIndex &index) const
Returns the underlying item that corresponds to the given index.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
QgsAttributesFormModel * sourceAttributesFormModel() const
Returns the source model.
void setFilterText(const QString &filterText=QString())
Sets the filter text.
const QString filterText() const
Returns the text used to filter source model items.
QgsAttributesFormProxyModel(QObject *parent=nullptr)
Constructor for QgsAttributesFormProxyModel, with the given parent.
void setAttributesFormSourceModel(QgsAttributesFormModel *model)
Sets the source model for the proxy model.
Stores information about constraints which may be present on a field.
An expression with an additional enabled flag.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:113
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
Represents a vector layer which manages a vector based dataset.
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
The TabStyle struct defines color and font overrides for form fields, tabs and groups labels.
Holds the configuration for a field.
Qgis::FieldDuplicatePolicy mDuplicatePolicy
QMap< QString, QVariant > mEditorWidgetConfig
Qgis::FieldDomainSplitPolicy mSplitPolicy
Qgis::FieldDomainMergePolicy mMergePolicy
Qgis::AttributeFormReuseLastValuePolicy mReuseLastValuePolicy