QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
23
25{
26 mChildren.append( widget );
27}
28
36
41
43{
44 mName = name;
45}
46
48{
49 return mVisibilityExpression;
50}
51
53{
54 if ( visibilityExpression == mVisibilityExpression )
55 return;
56
57 mVisibilityExpression = visibilityExpression;
58}
59
61{
62 return mCollapsedExpression;
63}
64
66{
67 if ( collapsedExpression == mCollapsedExpression )
68 return;
69
70 mCollapsedExpression = collapsedExpression;
71}
72
74{
75 return mBackgroundColor;
76}
77
79{
80 mBackgroundColor = backgroundColor;
81}
82
84{
85 QList<QgsAttributeEditorElement *> results;
86
87 const auto constMChildren = mChildren;
88 for ( QgsAttributeEditorElement *elem : constMChildren )
89 {
90 if ( elem->type() == type )
91 {
92 results.append( elem );
93 }
94
95 if ( elem->type() == Qgis::AttributeEditorType::Container )
96 {
97 QgsAttributeEditorContainer *cont = dynamic_cast<QgsAttributeEditorContainer *>( elem );
98 if ( cont )
99 results += cont->findElements( type );
100 }
101 }
102
103 return results;
104}
105
107{
108 qDeleteAll( mChildren );
109 mChildren.clear();
110}
111
113{
114 return mColumnCount;
115}
116
121
123{
125
126 const auto childElements = children();
127
128 for ( QgsAttributeEditorElement *child : childElements )
129 {
130 element->addChildElement( child->clone( element ) );
131 }
132 element->mType = mType;
133 element->mColumnCount = mColumnCount;
134 element->mVisibilityExpression = mVisibilityExpression;
135 element->mCollapsed = mCollapsed;
136 element->mCollapsedExpression = mCollapsedExpression;
137 element->mLabelStyle = mLabelStyle;
138
139 return element;
140}
141
142void QgsAttributeEditorContainer::saveConfiguration( QDomElement &elem, QDomDocument &doc ) const
143{
144 Q_UNUSED( doc )
145 elem.setAttribute( QStringLiteral( "columnCount" ), mColumnCount );
146 elem.setAttribute( QStringLiteral( "groupBox" ), mType == Qgis::AttributeEditorContainerType::GroupBox ? 1 : 0 );
147 elem.setAttribute( QStringLiteral( "type" ), qgsEnumValueToKey( mType ) );
148 elem.setAttribute( QStringLiteral( "collapsed" ), mCollapsed );
149 elem.setAttribute( QStringLiteral( "collapsedExpressionEnabled" ), mCollapsedExpression.enabled() ? 1 : 0 );
150 elem.setAttribute( QStringLiteral( "collapsedExpression" ), mCollapsedExpression->expression() );
151 elem.setAttribute( QStringLiteral( "visibilityExpressionEnabled" ), mVisibilityExpression.enabled() ? 1 : 0 );
152 elem.setAttribute( QStringLiteral( "visibilityExpression" ), mVisibilityExpression->expression() );
153 if ( mBackgroundColor.isValid() )
154 elem.setAttribute( QStringLiteral( "backgroundColor" ), mBackgroundColor.name( ) );
155 const auto constMChildren = mChildren;
156 for ( QgsAttributeEditorElement *child : constMChildren )
157 {
158 QDomDocument doc = elem.ownerDocument();
159 elem.appendChild( child->toDomElement( doc ) );
160 }
161}
162
163void QgsAttributeEditorContainer::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
164{
165 mBackgroundColor = element.attribute( QStringLiteral( "backgroundColor" ), QString() );
166 bool ok;
167 int cc = element.attribute( QStringLiteral( "columnCount" ) ).toInt( &ok );
168 if ( !ok )
169 cc = 0;
170 setColumnCount( cc );
171
172 if ( element.hasAttribute( QStringLiteral( "type" ) ) )
173 {
174 mType = qgsEnumKeyToValue( element.attribute( QStringLiteral( "type" ) ), Qgis::AttributeEditorContainerType::GroupBox );
175 }
176 else
177 {
178 const bool isGroupBox = element.attribute( QStringLiteral( "groupBox" ) ).toInt( &ok );
179 if ( ok )
181 else
183 }
184
185 const bool isCollapsed = element.attribute( QStringLiteral( "collapsed" ) ).toInt( &ok );
186 if ( ok )
187 setCollapsed( isCollapsed );
188 else
189 setCollapsed( false );
190
191 const bool collapsedExpressionEnabled = element.attribute( QStringLiteral( "collapsedExpressionEnabled" ) ).toInt( &ok );
192 QgsOptionalExpression collapsedExpression;
193 if ( ok )
194 {
195 collapsedExpression.setEnabled( collapsedExpressionEnabled );
196 collapsedExpression.setData( QgsExpression( element.attribute( QStringLiteral( "collapsedExpression" ) ) ) );
197 }
199
200
201 const bool visibilityExpressionEnabled = element.attribute( QStringLiteral( "visibilityExpressionEnabled" ) ).toInt( &ok );
202 QgsOptionalExpression visibilityExpression;
203 if ( ok )
204 {
205 visibilityExpression.setEnabled( visibilityExpressionEnabled );
206 visibilityExpression.setData( QgsExpression( element.attribute( QStringLiteral( "visibilityExpression" ) ) ) );
207 }
209
210 const QDomNodeList childNodeList = element.childNodes();
211
212 for ( int i = 0; i < childNodeList.size(); i++ )
213 {
214 const QDomElement childElem = childNodeList.at( i ).toElement();
215
216 QgsAttributeEditorElement *myElem = create( childElem, layerId, fields, context, this );
217 if ( myElem )
218 addChildElement( myElem );
219 }
220}
221
222QString QgsAttributeEditorContainer::typeIdentifier() const
223{
224 return QStringLiteral( "attributeEditorContainer" );
225}
226
AttributeEditorType
Attribute editor types.
Definition qgis.h:5451
@ Container
A container.
Definition qgis.h:5452
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:6817
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6798