QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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#define SIP_NO_FILE
21
22#include "qgsaction.h"
23#include "qgsaddtaborgroup.h"
26#include "qgsvectorlayer.h"
27
28#include <QAbstractItemModel>
29#include <QPushButton>
30#include <QSortFilterProxyModel>
31
40class GUI_EXPORT QgsAttributesFormData
41{
42 public:
61
88
90 {
91 operator QVariant();
92
95 QVariant nmRelationId;
97 QString label;
98 };
99
101 {
102 QString qmlCode;
103 };
104
106 {
107 QString htmlCode;
108 };
109
111 {
112 QString text;
113 };
114
116 {
117 bool drawLine = false;
118 };
119
127 class GUI_EXPORT AttributeFormItemData
128 {
129 public:
131
133 operator QVariant() { return QVariant::fromValue<AttributeFormItemData>( *this ); }
134
136 int columnCount() const { return mColumnCount; }
137
139 void setColumnCount( int count ) { mColumnCount = count; }
140
147 Qgis::AttributeEditorContainerType containerType() const;
148
155 void setContainerType( Qgis::AttributeEditorContainerType type );
156
165 bool collapsed() const { return mCollapsed; };
166
174 void setCollapsed( bool collapsed ) { mCollapsed = collapsed; };
175
181 const QgsAttributeEditorElement::LabelStyle labelStyle() const;
182
188 void setLabelStyle( const QgsAttributeEditorElement::LabelStyle &labelStyle );
189
191 bool showLabel() const;
192
194 void setShowLabel( bool showLabel );
195
204 int horizontalStretch() const { return mHorizontalStretch; }
205
214 void setHorizontalStretch( int stretch ) { mHorizontalStretch = stretch; }
215
224 int verticalStretch() const { return mVerticalStretch; }
225
234 void setVerticalStretch( int stretch ) { mVerticalStretch = stretch; }
235
241 QgsOptionalExpression visibilityExpression() const;
242
249 void setVisibilityExpression( const QgsOptionalExpression &visibilityExpression );
250
259 QgsOptionalExpression collapsedExpression() const;
260
269 void setCollapsedExpression( const QgsOptionalExpression &collapsedExpression );
270
276 RelationEditorConfiguration relationEditorConfiguration() const;
277
283 void setRelationEditorConfiguration( const RelationEditorConfiguration &relationEditorConfiguration );
284
290 QmlElementEditorConfiguration qmlElementEditorConfiguration() const;
291
297 void setQmlElementEditorConfiguration( const QmlElementEditorConfiguration &qmlElementEditorConfiguration );
298
304 HtmlElementEditorConfiguration htmlElementEditorConfiguration() const;
305
311 void setHtmlElementEditorConfiguration( const HtmlElementEditorConfiguration &htmlElementEditorConfiguration );
312
317 SpacerElementEditorConfiguration spacerElementEditorConfiguration() const;
318
323 void setSpacerElementEditorConfiguration( SpacerElementEditorConfiguration spacerElementEditorConfiguration );
324
330 QColor backgroundColor() const;
331
337 void setBackgroundColor( const QColor &backgroundColor );
338
343 TextElementEditorConfiguration textElementEditorConfiguration() const;
344
349 void setTextElementEditorConfiguration( const TextElementEditorConfiguration &textElementEditorConfiguration );
350
351 private:
352 int mColumnCount = 1;
354 bool mShowLabel = true;
355 int mHorizontalStretch = 0;
356 int mVerticalStretch = 0;
357 QgsOptionalExpression mVisibilityExpression;
358 RelationEditorConfiguration mRelationEditorConfiguration;
359 QmlElementEditorConfiguration mQmlElementEditorConfiguration;
360 HtmlElementEditorConfiguration mHtmlElementEditorConfiguration;
361 TextElementEditorConfiguration mTextElementEditorConfiguration;
362 SpacerElementEditorConfiguration mSpacerElementEditorConfiguration;
363 QColor mBackgroundColor;
364 bool mCollapsed = false;
365 QgsOptionalExpression mCollapsedExpression;
367 };
368};
369
370
378class GUI_EXPORT QgsAttributesFormItem : public QObject
379{
380 Q_OBJECT
381
382 public:
384
395 explicit QgsAttributesFormItem( QgsAttributesFormData::AttributesFormItemType itemType, const QString &name, const QString &displayName = QString(), QgsAttributesFormItem *parent = nullptr );
396
408
415
421 QgsAttributesFormItem *firstTopChild( const QgsAttributesFormData::AttributesFormItemType itemType, const QString &itemId ) const;
422
428 QgsAttributesFormItem *firstChildRecursive( const QgsAttributesFormData::AttributesFormItemType &itemType, const QString &itemId ) const;
429
433 int childCount() const;
434
440 QgsAttributesFormItem *parent() { return mParent; }
441
445 int row() const;
446
450 QVariant data( int role ) const;
451
455 bool setData( int role, const QVariant &value );
456
460 void addChild( std::unique_ptr< QgsAttributesFormItem > &&child );
461
465 void insertChild( int position, std::unique_ptr< QgsAttributesFormItem > &&item );
466
470 void deleteChildAtIndex( int index );
471
475 void deleteChildren();
476
481
485 QString id() const { return mId; }
486
490 QString name() const { return mName; }
491
495 QString displayName() const { return mDisplayName; }
496
502 QIcon icon() const { return mIcon; }
503
509 void setIcon( const QIcon &icon ) { mIcon = icon; }
510
516 static bool isGroup( QgsAttributesFormItem *item );
517
518 signals:
519
525 void addedChildren( QgsAttributesFormItem *item, int indexFrom, int indexTo );
526
527 private:
528 QString mName = QString();
529 QString mDisplayName = QString();
530 QIcon mIcon;
532 QString mId = QString();
534 QgsAttributesFormData::FieldConfig mFieldConfigData;
535
536 std::vector< std::unique_ptr< QgsAttributesFormItem > > mChildren;
537 QgsAttributesFormItem *mParent = nullptr;
538
539 Q_DISABLE_COPY( QgsAttributesFormItem )
540};
541
542
550class GUI_EXPORT QgsAttributesFormModel : public QAbstractItemModel
551{
552 Q_OBJECT
553
554 public:
570
576 explicit QgsAttributesFormModel( QgsVectorLayer *layer, QgsProject *project, QObject *parent = nullptr );
577
579
580 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
581 QModelIndex parent( const QModelIndex &index ) const override;
582
583 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
584 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
585
586 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
587
593 QModelIndex firstTopMatchingModelIndex( const QgsAttributesFormData::AttributesFormItemType &itemType, const QString &itemId ) const;
594
600 QModelIndex firstRecursiveMatchingModelIndex( const QgsAttributesFormData::AttributesFormItemType &itemType, const QString &itemId ) const;
601
607 bool showAliases() const;
608
614 void setShowAliases( bool show );
615
621 QgsAttributesFormItem *itemForIndex( const QModelIndex &index ) const;
622
629
630 public slots:
631
635 virtual void populate() = 0;
636
637 signals:
638
645
646 protected:
658 bool indexLessThan( const QModelIndex &a, const QModelIndex &b ) const;
659
665 QVector<int> rootToLeafPath( QgsAttributesFormItem *item ) const;
666
667
676 void emitDataChangedRecursively( const QModelIndex &parent = QModelIndex(), const QVector<int> &roles = QVector<int>() );
677
678 std::unique_ptr< QgsAttributesFormItem > mRootItem;
681
682 bool mShowAliases = false;
683};
684
685
694{
695 Q_OBJECT
696
697 public:
703 explicit QgsAttributesAvailableWidgetsModel( QgsVectorLayer *layer, QgsProject *project, QObject *parent = nullptr );
704
705 Qt::ItemFlags flags( const QModelIndex &index ) const override;
706 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
707
708 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
709
710 Qt::DropActions supportedDragActions() const override;
711 QStringList mimeTypes() const override;
712 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
713
719 QModelIndex fieldContainer() const;
720
726 QModelIndex relationContainer() const;
727
733 QModelIndex actionContainer() const;
734
738 QModelIndex fieldModelIndex( const QString &fieldName ) const;
739
740 public slots:
741 void populate() override;
742
748 void populateLayerActions( const QList< QgsAction > actions );
749
750 private:
756 void populateActionItems( const QList<QgsAction> actions );
757};
758
759
768{
769 Q_OBJECT
770
771 public:
778 explicit QgsAttributesFormLayoutModel( QgsVectorLayer *layer, QgsProject *project, QObject *parent = nullptr );
779
780 Qt::ItemFlags flags( const QModelIndex &index ) const override;
781 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
782
783 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
784
788 bool removeRow( int row, const QModelIndex &parent = QModelIndex() );
789
790 Qt::DropActions supportedDragActions() const override;
791 Qt::DropActions supportedDropActions() const override;
792 QStringList mimeTypes() const override;
793 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
794 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
795 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
796
801
805 QList< QgsAddAttributeFormContainerDialog::ContainerPair > listOfContainers() const;
806
812 void addContainer( QModelIndex &parent, const QString &name, int columnCount, Qgis::AttributeEditorContainerType type );
813
822 void updateFieldConfigForFieldItems( const QString &fieldName, const QgsAttributesFormData::FieldConfig &config );
823
832 void updateAliasForFieldItems( const QString &fieldName, const QString &fieldAlias );
833
839 void insertChild( const QModelIndex &parent, int row, const QString &itemId, QgsAttributesFormData::AttributesFormItemType itemType, const QString &itemName );
840
841 public slots:
842 void populate() override;
843
844 signals:
846 void externalItemDropped( QModelIndex &index );
848 void internalItemDropped( QModelIndex &index );
849
850 private:
852 void updateFieldConfigForFieldItemsRecursive( QgsAttributesFormItem *parent, const QString &fieldName, const QgsAttributesFormData::FieldConfig &config );
854 void updateAliasForFieldItemsRecursive( QgsAttributesFormItem *parent, const QString &fieldName, const QString &fieldAlias );
855
856 QList< QgsAddAttributeFormContainerDialog::ContainerPair > recursiveListOfContainers( QgsAttributesFormItem *parent ) const;
857 void loadAttributeEditorElementItem( QgsAttributeEditorElement *const editorElement, QgsAttributesFormItem *parent, const int position = -1 );
858
867 QModelIndexList curateIndexesForMimeData( const QModelIndexList &indexes ) const;
868};
869
870
878class GUI_EXPORT QgsAttributesFormProxyModel : public QSortFilterProxyModel
879{
880 Q_OBJECT
881
882 public:
886 QgsAttributesFormProxyModel( QObject *parent = nullptr );
887
892
896 const QString filterText() const;
897
904
905 public slots:
907 void setFilterText( const QString &filterText = QString() );
908
909 protected:
910 bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
911
912 private:
913 QgsAttributesFormModel *mModel = nullptr;
914 QString mFilterText;
915};
916
917
921
922#endif // QGSATTRIBUTESFORMMODEL_H
AttributeEditorContainerType
Attribute editor container types.
Definition qgis.h:5470
AttributeFormReuseLastValuePolicy
Attribute form policy for reusing last entered values.
Definition qgis.h:5529
@ NotAllowed
Reuse of last values not allowed.
Definition qgis.h:5530
FieldDomainMergePolicy
Merge policy for field domains.
Definition qgis.h:3923
@ DefaultValue
Use default field value.
Definition qgis.h:3924
FieldDomainSplitPolicy
Split policy for field domains.
Definition qgis.h:3906
@ Duplicate
Duplicate original value.
Definition qgis.h:3908
FieldDuplicatePolicy
Duplicate policy for fields.
Definition qgis.h:3943
@ Duplicate
Duplicate original value.
Definition qgis.h:3945
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:109
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