QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsattributeeditorcontainer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsattributeeditorcontainer.cpp - QgsAttributeEditorContainer
3
4 ---------------------
5 begin : 12.01.2021
6 copyright : (C) 2021 by Denis Rouzaud
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18
19#include <QString>
20
21using namespace Qt::StringLiterals;
22
27
29{
30 mChildren.append( widget );
31}
32
40
45
47{
48 mName = name;
49}
50
52{
53 return mVisibilityExpression;
54}
55
57{
58 if ( visibilityExpression == mVisibilityExpression )
59 return;
60
61 mVisibilityExpression = visibilityExpression;
62}
63
65{
66 return mCollapsedExpression;
67}
68
70{
71 if ( collapsedExpression == mCollapsedExpression )
72 return;
73
74 mCollapsedExpression = collapsedExpression;
75}
76
78{
79 return mBackgroundColor;
80}
81
83{
84 mBackgroundColor = backgroundColor;
85}
86
88{
89 QList<QgsAttributeEditorElement *> results;
90
91 const auto constMChildren = mChildren;
92 for ( QgsAttributeEditorElement *elem : constMChildren )
93 {
94 if ( elem->type() == type )
95 {
96 results.append( elem );
97 }
98
99 if ( elem->type() == Qgis::AttributeEditorType::Container )
100 {
101 QgsAttributeEditorContainer *cont = dynamic_cast<QgsAttributeEditorContainer *>( elem );
102 if ( cont )
103 results += cont->findElements( type );
104 }
105 }
106
107 return results;
108}
109
111{
112 qDeleteAll( mChildren );
113 mChildren.clear();
114}
115
117{
118 return mColumnCount;
119}
120
125
127{
129
130 const auto childElements = children();
131
132 for ( QgsAttributeEditorElement *child : childElements )
133 {
134 element->addChildElement( child->clone( element ) );
135 }
136 element->mType = mType;
137 element->mColumnCount = mColumnCount;
138 element->mVisibilityExpression = mVisibilityExpression;
139 element->mCollapsed = mCollapsed;
140 element->mCollapsedExpression = mCollapsedExpression;
141 element->mLabelStyle = mLabelStyle;
142
143 return element;
144}
145
146void QgsAttributeEditorContainer::saveConfiguration( QDomElement &elem, QDomDocument &doc ) const
147{
148 Q_UNUSED( doc )
149 elem.setAttribute( u"columnCount"_s, mColumnCount );
150 elem.setAttribute( u"groupBox"_s, mType == Qgis::AttributeEditorContainerType::GroupBox ? 1 : 0 );
151 elem.setAttribute( u"type"_s, qgsEnumValueToKey( mType ) );
152 elem.setAttribute( u"collapsed"_s, mCollapsed );
153 elem.setAttribute( u"collapsedExpressionEnabled"_s, mCollapsedExpression.enabled() ? 1 : 0 );
154 elem.setAttribute( u"collapsedExpression"_s, mCollapsedExpression->expression() );
155 elem.setAttribute( u"visibilityExpressionEnabled"_s, mVisibilityExpression.enabled() ? 1 : 0 );
156 elem.setAttribute( u"visibilityExpression"_s, mVisibilityExpression->expression() );
157 if ( mBackgroundColor.isValid() )
158 elem.setAttribute( u"backgroundColor"_s, mBackgroundColor.name( ) );
159 const auto constMChildren = mChildren;
160 for ( QgsAttributeEditorElement *child : constMChildren )
161 {
162 QDomDocument doc = elem.ownerDocument();
163 elem.appendChild( child->toDomElement( doc ) );
164 }
165}
166
167void QgsAttributeEditorContainer::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
168{
169 mBackgroundColor = element.attribute( u"backgroundColor"_s, QString() );
170 bool ok;
171 int cc = element.attribute( u"columnCount"_s ).toInt( &ok );
172 if ( !ok )
173 cc = 0;
174 setColumnCount( cc );
175
176 if ( element.hasAttribute( u"type"_s ) )
177 {
178 mType = qgsEnumKeyToValue( element.attribute( u"type"_s ), Qgis::AttributeEditorContainerType::GroupBox );
179 }
180 else
181 {
182 const bool isGroupBox = element.attribute( u"groupBox"_s ).toInt( &ok );
183 if ( ok )
185 else
187 }
188
189 const bool isCollapsed = element.attribute( u"collapsed"_s ).toInt( &ok );
190 if ( ok )
191 setCollapsed( isCollapsed );
192 else
193 setCollapsed( false );
194
195 const bool collapsedExpressionEnabled = element.attribute( u"collapsedExpressionEnabled"_s ).toInt( &ok );
196 QgsOptionalExpression collapsedExpression;
197 if ( ok )
198 {
199 collapsedExpression.setEnabled( collapsedExpressionEnabled );
200 collapsedExpression.setData( QgsExpression( element.attribute( u"collapsedExpression"_s ) ) );
201 }
203
204
205 const bool visibilityExpressionEnabled = element.attribute( u"visibilityExpressionEnabled"_s ).toInt( &ok );
206 QgsOptionalExpression visibilityExpression;
207 if ( ok )
208 {
209 visibilityExpression.setEnabled( visibilityExpressionEnabled );
210 visibilityExpression.setData( QgsExpression( element.attribute( u"visibilityExpression"_s ) ) );
211 }
213
214 const QDomNodeList childNodeList = element.childNodes();
215
216 for ( int i = 0; i < childNodeList.size(); i++ )
217 {
218 const QDomElement childElem = childNodeList.at( i ).toElement();
219
220 QgsAttributeEditorElement *myElem = create( childElem, layerId, fields, context, this );
221 if ( myElem )
222 addChildElement( myElem );
223 }
224}
225
226QString QgsAttributeEditorContainer::typeIdentifier() const
227{
228 return u"attributeEditorContainer"_s;
229}
230
AttributeEditorType
Attribute editor types.
Definition qgis.h:5717
@ Container
A container.
Definition qgis.h:5718
QgsAttributeEditorContainer(const QString &name, QgsAttributeEditorElement *parent, const QColor &backgroundColor=QColor())
Creates a new attribute editor container.
virtual void addChildElement(QgsAttributeEditorElement *element)
Add a child element to this container.
QgsOptionalExpression visibilityExpression() const
The visibility expression is used in the attribute form to show or hide this container based on an ex...
void setColumnCount(int columnCount)
Set the number of columns in this group.
void setVisibilityExpression(const QgsOptionalExpression &visibilityExpression)
The visibility expression is used in the attribute form to show or hide this container based on an ex...
QgsOptionalExpression collapsedExpression() const
The collapsed expression is used in the attribute form to set the collapsed status of the group box c...
Qgis::AttributeEditorContainerType type() const
Returns the container type.
void setType(Qgis::AttributeEditorContainerType type)
Sets the container type.
void setCollapsedExpression(const QgsOptionalExpression &collapsedExpression)
The collapsed expression is used in the attribute form to set the collapsed status of the group box o...
QList< QgsAttributeEditorElement * > children() const
Gets a list of the children elements of this container.
virtual Q_DECL_DEPRECATED void setIsGroupBox(bool isGroupBox)
Determines if this container is rendered as collapsible group box or tab in a tabwidget.
void clear()
Clear all children from this container.
QgsAttributeEditorElement * clone(QgsAttributeEditorElement *parent) const override
Creates a deep copy of this element.
void setName(const QString &name)
Change the name of this container.
QColor backgroundColor() const
Returns the background color of the container.
void setCollapsed(bool collapsed)
For group box containers sets if this group box is collapsed.
virtual Q_DECL_DEPRECATED bool isGroupBox() const
Returns if this container is going to be a group box.
virtual QList< QgsAttributeEditorElement * > findElements(Qgis::AttributeEditorType type) const
Traverses the element tree to find any element of the specified type.
int columnCount() const
Gets the number of columns in this group.
void setBackgroundColor(const QColor &backgroundColor)
Sets the background color to backgroundColor.
An abstract base class for any elements of a drag and drop form.
QgsAttributeEditorElement * parent() const
Gets the parent of this element.
QgsAttributeEditorElement(Qgis::AttributeEditorType type, const QString &name, QgsAttributeEditorElement *parent=nullptr)
Constructor.
QgsAttributeEditorElement * mParent
Qgis::AttributeEditorType mType
QString name() const
Returns the name of this element.
static QgsAttributeEditorElement * create(const QDomElement &element, const QString &layerId, const QgsFields &fields, const QgsReadWriteContext &context, QgsAttributeEditorElement *parent=nullptr)
Constructs the editor element from the given element.
Container of fields for a vector layer.
Definition qgsfields.h:46
An expression with an additional enabled flag.
bool enabled() const
Check if this optional is enabled.
Definition qgsoptional.h:86
A container for the context for various read/write operations on objects.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:7110
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7091