QGIS API Documentation 4.3.0-Master (9ff14a2eeba)
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
90
92 {
93 operator QVariant();
94
97 QVariant nmRelationId;
99 QString label;
100 };
101
103 {
104 QString qmlCode;
105 };
106
108 {
109 QString htmlCode;
110 };
111
113 {
114 QString text;
115 };
116
118 {
119 bool drawLine = false;
120 };
121
129 class GUI_EXPORT AttributeFormItemData
130 {
131 public:
133
135 operator QVariant() { return QVariant::fromValue<AttributeFormItemData>( *this ); }
136
138 int columnCount() const { return mColumnCount; }
139
141 void setColumnCount( int count ) { mColumnCount = count; }
142
149 Qgis::AttributeEditorContainerType containerType() const;
150
157 void setContainerType( Qgis::AttributeEditorContainerType type );
158
167 bool collapsed() const { return mCollapsed; };
168
176 void setCollapsed( bool collapsed ) { mCollapsed = collapsed; };
177
183 const QgsAttributeEditorElement::LabelStyle labelStyle() const;
184
190 void setLabelStyle( const QgsAttributeEditorElement::LabelStyle &labelStyle );
191
193 bool showLabel() const;
194
196 void setShowLabel( bool showLabel );
197
206 int horizontalStretch() const { return mHorizontalStretch; }
207
216 void setHorizontalStretch( int stretch ) { mHorizontalStretch = stretch; }
217
226 int verticalStretch() const { return mVerticalStretch; }
227
236 void setVerticalStretch( int stretch ) { mVerticalStretch = stretch; }
237
243 QgsOptionalExpression visibilityExpression() const;
244
251 void setVisibilityExpression( const QgsOptionalExpression &visibilityExpression );
252
261 QgsOptionalExpression collapsedExpression() const;
262
271 void setCollapsedExpression( const QgsOptionalExpression &collapsedExpression );
272
278 RelationEditorConfiguration relationEditorConfiguration() const;
279
285 void setRelationEditorConfiguration( const RelationEditorConfiguration &relationEditorConfiguration );
286
292 QmlElementEditorConfiguration qmlElementEditorConfiguration() const;
293
299 void setQmlElementEditorConfiguration( const QmlElementEditorConfiguration &qmlElementEditorConfiguration );
300
306 HtmlElementEditorConfiguration htmlElementEditorConfiguration() const;
307
313 void setHtmlElementEditorConfiguration( const HtmlElementEditorConfiguration &htmlElementEditorConfiguration );
314
319 SpacerElementEditorConfiguration spacerElementEditorConfiguration() const;
320
325 void setSpacerElementEditorConfiguration( SpacerElementEditorConfiguration spacerElementEditorConfiguration );
326
332 QColor backgroundColor() const;
333
339 void setBackgroundColor( const QColor &backgroundColor );
340
345 TextElementEditorConfiguration textElementEditorConfiguration() const;
346
351 void setTextElementEditorConfiguration( const TextElementEditorConfiguration &textElementEditorConfiguration );
352
353 private:
354 int mColumnCount = 1;
356 bool mShowLabel = true;
357 int mHorizontalStretch = 0;
358 int mVerticalStretch = 0;
359 QgsOptionalExpression mVisibilityExpression;
360 RelationEditorConfiguration mRelationEditorConfiguration;
361 QmlElementEditorConfiguration mQmlElementEditorConfiguration;
362 HtmlElementEditorConfiguration mHtmlElementEditorConfiguration;
363 TextElementEditorConfiguration mTextElementEditorConfiguration;
364 SpacerElementEditorConfiguration mSpacerElementEditorConfiguration;
365 QColor mBackgroundColor;
366 bool mCollapsed = false;
367 QgsOptionalExpression mCollapsedExpression;
369 };
370};
371
372
380class GUI_EXPORT QgsAttributesFormItem : public QObject
381{
382 Q_OBJECT
383
384 public:
386
397 explicit QgsAttributesFormItem( QgsAttributesFormData::AttributesFormItemType itemType, const QString &name, const QString &displayName = QString(), QgsAttributesFormItem *parent = nullptr );
398
409 explicit QgsAttributesFormItem(
412 const QString &name,
413 const QString &displayName = QString(),
415 );
416
423
429 QgsAttributesFormItem *firstTopChild( const QgsAttributesFormData::AttributesFormItemType itemType, const QString &itemId ) const;
430
436 QgsAttributesFormItem *firstChildRecursive( const QgsAttributesFormData::AttributesFormItemType &itemType, const QString &itemId ) const;
437
441 int childCount() const;
442
448 QgsAttributesFormItem *parent() { return mParent; }
449
453 int row() const;
454
458 QVariant data( int role ) const;
459
463 bool setData( int role, const QVariant &value );
464
468 void addChild( std::unique_ptr< QgsAttributesFormItem > &&child );
469
473 void insertChild( int position, std::unique_ptr< QgsAttributesFormItem > &&item );
474
478 void deleteChildAtIndex( int index );
479
492 std::unique_ptr< QgsAttributesFormItem > takeChild( int index );
493
497 void deleteChildren();
498
503
507 QString id() const { return mId; }
508
512 QString name() const { return mName; }
513
517 QString displayName() const { return mDisplayName; }
518
524 QIcon icon() const { return mIcon; }
525
531 void setIcon( const QIcon &icon ) { mIcon = icon; }
532
538 static bool isGroup( QgsAttributesFormItem *item );
539
540 signals:
541
547 void addedChildren( QgsAttributesFormItem *item, int indexFrom, int indexTo );
548
549 private:
550 QString mName = QString();
551 QString mDisplayName = QString();
552 QIcon mIcon;
554 QString mId = QString();
556 QgsAttributesFormData::FieldConfig mFieldConfigData;
557
558 std::vector< std::unique_ptr< QgsAttributesFormItem > > mChildren;
559 QgsAttributesFormItem *mParent = nullptr;
560
561 Q_DISABLE_COPY( QgsAttributesFormItem )
562};
563
564
572class GUI_EXPORT QgsAttributesFormModel : public QAbstractItemModel
573{
574 Q_OBJECT
575
576 public:
592
598 explicit QgsAttributesFormModel( QgsVectorLayer *layer, QgsProject *project, QObject *parent = nullptr );
599
601
602 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
603 QModelIndex parent( const QModelIndex &index ) const override;
604
605 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
606 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
607
608 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
609
615 QModelIndex firstTopMatchingModelIndex( const QgsAttributesFormData::AttributesFormItemType &itemType, const QString &itemId ) const;
616
622 QModelIndex firstRecursiveMatchingModelIndex( const QgsAttributesFormData::AttributesFormItemType &itemType, const QString &itemId ) const;
623
629 bool showAliases() const;
630
636 void setShowAliases( bool show );
637
643 QgsAttributesFormItem *itemForIndex( const QModelIndex &index ) const;
644
651
652 public slots:
653
657 virtual void populate() = 0;
658
659 signals:
660
667
668 protected:
680 bool indexLessThan( const QModelIndex &a, const QModelIndex &b ) const;
681
687 QVector<int> rootToLeafPath( QgsAttributesFormItem *item ) const;
688
689
698 void emitDataChangedRecursively( const QModelIndex &parent = QModelIndex(), const QVector<int> &roles = QVector<int>() );
699
710
711 std::unique_ptr< QgsAttributesFormItem > mRootItem;
714
715 bool mShowAliases = false;
716};
717
718
727{
728 Q_OBJECT
729
730 public:
736 explicit QgsAttributesAvailableWidgetsModel( QgsVectorLayer *layer, QgsProject *project, QObject *parent = nullptr );
737
738 Qt::ItemFlags flags( const QModelIndex &index ) const override;
739 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
740
741 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
742
743 Qt::DropActions supportedDragActions() const override;
744 QStringList mimeTypes() const override;
745 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
746
752 QModelIndex fieldContainer() const;
753
759 QModelIndex relationContainer() const;
760
766 QModelIndex actionContainer() const;
767
771 QModelIndex fieldModelIndex( const QString &fieldName ) const;
772
773 public slots:
774 void populate() override;
775
781 void populateLayerActions( const QList< QgsAction > actions );
782
783 private:
789 void populateActionItems( const QList<QgsAction> actions );
790};
791
792
802{
803 Q_OBJECT
804
805 public:
812 explicit QgsAttributesFormLayoutModel( QgsVectorLayer *layer, QgsProject *project, QObject *parent = nullptr );
813
814 Qt::ItemFlags flags( const QModelIndex &index ) const override;
815 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
816
817 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
818
822 bool removeRow( int row, const QModelIndex &parent = QModelIndex() );
823
824 Qt::DropActions supportedDragActions() const override;
825 Qt::DropActions supportedDropActions() const override;
826 QStringList mimeTypes() const override;
827 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
828 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
829 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
830
835
839 QList< QgsAddAttributeFormContainerDialog::ContainerPair > listOfContainers() const;
840
846 void addContainer( QModelIndex &parent, const QString &name, int columnCount, Qgis::AttributeEditorContainerType type );
847
856 void updateFieldConfigForFieldItems( const QString &fieldName, const QgsAttributesFormData::FieldConfig &config );
857
866 void updateAliasForFieldItems( const QString &fieldName, const QString &fieldAlias );
867
873 void insertChild( const QModelIndex &parent, int row, const QString &itemId, QgsAttributesFormData::AttributesFormItemType itemType, const QString &itemName );
874
875 public slots:
876 void populate() override;
877
878 signals:
880 void externalItemsDropped( const QModelIndexList &indexes );
882 void internalItemsDropped( const QModelIndexList &indexes );
883
884 private:
886 void updateFieldConfigForFieldItemsRecursive( QgsAttributesFormItem *parent, const QString &fieldName, const QgsAttributesFormData::FieldConfig &config );
888 void updateAliasForFieldItemsRecursive( QgsAttributesFormItem *parent, const QString &fieldName, const QString &fieldAlias );
889
890 QList< QgsAddAttributeFormContainerDialog::ContainerPair > recursiveListOfContainers( QgsAttributesFormItem *parent ) const;
891 void loadAttributeEditorElementItem( QgsAttributeEditorElement *const editorElement, QgsAttributesFormItem *parent, const int position = -1 );
892
899 void setFieldItemDataFromLayer( QgsAttributesFormItem *item );
900
909 QModelIndexList curateIndexesForMimeData( const QModelIndexList &indexes ) const;
910
917 void performInternalMove( const QList< QPersistentModelIndex > &draggedIndexes, const QModelIndex &parent, int row );
918
919 // Capture source items being dragged in an ongoing internal move.
920 mutable QList< QPersistentModelIndex > mDraggedLayoutIndexes;
921};
922
923
931class GUI_EXPORT QgsAttributesFormProxyModel : public QSortFilterProxyModel
932{
933 Q_OBJECT
934
935 public:
939 QgsAttributesFormProxyModel( QObject *parent = nullptr );
940
945
949 const QString filterText() const;
950
957
958 public slots:
960 void setFilterText( const QString &filterText = QString() );
961
962 protected:
963 bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
964
965 private:
966 QgsAttributesFormModel *mModel = nullptr;
967 QString mFilterText;
968};
969
970
974
975#endif // QGSATTRIBUTESFORMMODEL_H
AttributeEditorContainerType
Attribute editor container types.
Definition qgis.h:6120
AttributeFormReuseLastValuePolicy
Attribute form policy for reusing last entered values.
Definition qgis.h:6179
@ NotAllowed
Reuse of last values not allowed.
Definition qgis.h:6180
FieldDomainMergePolicy
Merge policy for field domains.
Definition qgis.h:4121
@ DefaultValue
Use default field value.
Definition qgis.h:4122
FieldDomainSplitPolicy
Split policy for field domains.
Definition qgis.h:4104
@ Duplicate
Duplicate original value.
Definition qgis.h:4106
FieldDuplicatePolicy
Duplicate policy for fields.
Definition qgis.h:4141
@ Duplicate
Duplicate original value.
Definition qgis.h:4143
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.
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 internalItemsDropped(const QModelIndexList &indexes)
Informs that items were moved (via drop) in the model from the same model.
void externalItemsDropped(const QModelIndexList &indexes)
Informs that items were inserted (via drop) in the model from another 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.
static QIcon iconForItemType(QgsAttributesFormData::AttributesFormItemType itemType)
Returns the icon used for items of the given itemType, both in the available widgets tree and in the ...
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:114
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