QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
21{
22 qDeleteAll( mChildren );
23}
24
26{
27 mChildren.append( widget );
28}
29
31{
32 if ( isGroupBox )
34 else
36}
37
39{
41}
42
43void QgsAttributeEditorContainer::setName( const QString &name )
44{
45 mName = name;
46}
47
49{
50 return mVisibilityExpression;
51}
52
54{
55 if ( visibilityExpression == mVisibilityExpression )
56 return;
57
58 mVisibilityExpression = visibilityExpression;
59}
60
62{
63 return mCollapsedExpression;
64}
65
67{
68 if ( collapsedExpression == mCollapsedExpression )
69 return;
70
71 mCollapsedExpression = collapsedExpression;
72}
73
75{
76 return mBackgroundColor;
77}
78
79void QgsAttributeEditorContainer::setBackgroundColor( const QColor &backgroundColor )
80{
81 mBackgroundColor = backgroundColor;
82}
83
84QList<QgsAttributeEditorElement *> QgsAttributeEditorContainer::findElements( Qgis::AttributeEditorType type ) const
85{
86 QList<QgsAttributeEditorElement *> results;
87
88 const auto constMChildren = mChildren;
89 for ( QgsAttributeEditorElement *elem : constMChildren )
90 {
91 if ( elem->type() == type )
92 {
93 results.append( elem );
94 }
95
96 if ( elem->type() == Qgis::AttributeEditorType::Container )
97 {
98 QgsAttributeEditorContainer *cont = dynamic_cast<QgsAttributeEditorContainer *>( elem );
99 if ( cont )
100 results += cont->findElements( type );
101 }
102 }
103
104 return results;
105}
106
108{
109 qDeleteAll( mChildren );
110 mChildren.clear();
111}
112
114{
115 return mColumnCount;
116}
117
119{
120 mColumnCount = columnCount;
121}
122
124{
126
127 const auto childElements = children();
128
129 for ( QgsAttributeEditorElement *child : childElements )
130 {
131 element->addChildElement( child->clone( element ) );
132 }
133 element->mType = mType;
134 element->mColumnCount = mColumnCount;
135 element->mVisibilityExpression = mVisibilityExpression;
136 element->mCollapsed = mCollapsed;
137 element->mCollapsedExpression = mCollapsedExpression;
138 element->mLabelStyle = mLabelStyle;
139
140 return element;
141}
142
143void QgsAttributeEditorContainer::saveConfiguration( QDomElement &elem, QDomDocument &doc ) const
144{
145 Q_UNUSED( doc )
146 elem.setAttribute( QStringLiteral( "columnCount" ), mColumnCount );
147 elem.setAttribute( QStringLiteral( "groupBox" ), mType == Qgis::AttributeEditorContainerType::GroupBox ? 1 : 0 );
148 elem.setAttribute( QStringLiteral( "type" ), qgsEnumValueToKey( mType ) );
149 elem.setAttribute( QStringLiteral( "collapsed" ), mCollapsed );
150 elem.setAttribute( QStringLiteral( "collapsedExpressionEnabled" ), mCollapsedExpression.enabled() ? 1 : 0 );
151 elem.setAttribute( QStringLiteral( "collapsedExpression" ), mCollapsedExpression->expression() );
152 elem.setAttribute( QStringLiteral( "visibilityExpressionEnabled" ), mVisibilityExpression.enabled() ? 1 : 0 );
153 elem.setAttribute( QStringLiteral( "visibilityExpression" ), mVisibilityExpression->expression() );
154 if ( mBackgroundColor.isValid() )
155 elem.setAttribute( QStringLiteral( "backgroundColor" ), mBackgroundColor.name( ) );
156 const auto constMChildren = mChildren;
157 for ( QgsAttributeEditorElement *child : constMChildren )
158 {
159 QDomDocument doc = elem.ownerDocument();
160 elem.appendChild( child->toDomElement( doc ) );
161 }
162}
163
164void QgsAttributeEditorContainer::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
165{
166 mBackgroundColor = element.attribute( QStringLiteral( "backgroundColor" ), QString() );
167 bool ok;
168 int cc = element.attribute( QStringLiteral( "columnCount" ) ).toInt( &ok );
169 if ( !ok )
170 cc = 0;
171 setColumnCount( cc );
172
173 if ( element.hasAttribute( QStringLiteral( "type" ) ) )
174 {
175 mType = qgsEnumKeyToValue( element.attribute( QStringLiteral( "type" ) ), Qgis::AttributeEditorContainerType::GroupBox );
176 }
177 else
178 {
179 const bool isGroupBox = element.attribute( QStringLiteral( "groupBox" ) ).toInt( &ok );
180 if ( ok )
182 else
184 }
185
186 const bool isCollapsed = element.attribute( QStringLiteral( "collapsed" ) ).toInt( &ok );
187 if ( ok )
188 setCollapsed( isCollapsed );
189 else
190 setCollapsed( false );
191
192 const bool collapsedExpressionEnabled = element.attribute( QStringLiteral( "collapsedExpressionEnabled" ) ).toInt( &ok );
194 if ( ok )
195 {
196 collapsedExpression.setEnabled( collapsedExpressionEnabled );
197 collapsedExpression.setData( QgsExpression( element.attribute( QStringLiteral( "collapsedExpression" ) ) ) );
198 }
200
201
202 const bool visibilityExpressionEnabled = element.attribute( QStringLiteral( "visibilityExpressionEnabled" ) ).toInt( &ok );
204 if ( ok )
205 {
206 visibilityExpression.setEnabled( visibilityExpressionEnabled );
207 visibilityExpression.setData( QgsExpression( element.attribute( QStringLiteral( "visibilityExpression" ) ) ) );
208 }
210
211 const QDomNodeList childNodeList = element.childNodes();
212
213 for ( int i = 0; i < childNodeList.size(); i++ )
214 {
215 const QDomElement childElem = childNodeList.at( i ).toElement();
216
217 QgsAttributeEditorElement *myElem = create( childElem, layerId, fields, context, this );
218 if ( myElem )
219 addChildElement( myElem );
220 }
221}
222
223QString QgsAttributeEditorContainer::typeIdentifier() const
224{
225 return QStringLiteral( "attributeEditorContainer" );
226}
227
AttributeEditorType
Attribute editor types.
Definition: qgis.h:4384
This is a container for attribute editors, used to group them visually in the attribute form if it is...
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.
This is an abstract base class for any elements of a drag and drop form.
QgsAttributeEditorElement * parent() const
Gets the parent of this element.
QgsAttributeEditorElement * mParent
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.
Class for parsing and evaluation of expressions (formerly called "search strings").
Container of fields for a vector layer.
Definition: qgsfields.h:45
An expression with an additional enabled flag.
bool enabled() const
Check if this optional is enabled.
Definition: qgsoptional.h:86
void setData(const T &data)
Set the payload data.
Definition: qgsoptional.h:122
void setEnabled(bool enabled)
Set if this optional is enabled.
Definition: qgsoptional.h:95
The class is used as a container of context for various read/write operations on other 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:5417
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition: qgis.h:5398