QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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
7  email : [email protected]
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 
30 void QgsAttributeEditorContainer::setName( const QString &name )
31 {
32  mName = name;
33 }
34 
36 {
37  return mVisibilityExpression;
38 }
39 
41 {
42  if ( visibilityExpression == mVisibilityExpression )
43  return;
44 
45  mVisibilityExpression = visibilityExpression;
46 }
47 
49 {
50  return mCollapsedExpression;
51 }
52 
54 {
55  if ( collapsedExpression == mCollapsedExpression )
56  return;
57 
58  mCollapsedExpression = collapsedExpression;
59 }
60 
62 {
63  return mBackgroundColor;
64 }
65 
66 void QgsAttributeEditorContainer::setBackgroundColor( const QColor &backgroundColor )
67 {
68  mBackgroundColor = backgroundColor;
69 }
70 
72 {
73  QList<QgsAttributeEditorElement *> results;
74 
75  const auto constMChildren = mChildren;
76  for ( QgsAttributeEditorElement *elem : constMChildren )
77  {
78  if ( elem->type() == type )
79  {
80  results.append( elem );
81  }
82 
83  if ( elem->type() == AeTypeContainer )
84  {
85  QgsAttributeEditorContainer *cont = dynamic_cast<QgsAttributeEditorContainer *>( elem );
86  if ( cont )
87  results += cont->findElements( type );
88  }
89  }
90 
91  return results;
92 }
93 
95 {
96  qDeleteAll( mChildren );
97  mChildren.clear();
98 }
99 
101 {
102  return mColumnCount;
103 }
104 
106 {
107  mColumnCount = columnCount;
108 }
109 
111 {
113 
114  const auto childElements = children();
115 
116  for ( QgsAttributeEditorElement *child : childElements )
117  {
118  element->addChildElement( child->clone( element ) );
119  }
120  element->mIsGroupBox = mIsGroupBox;
121  element->mColumnCount = mColumnCount;
122  element->mVisibilityExpression = mVisibilityExpression;
123  element->mCollapsed = mCollapsed;
124  element->mCollapsedExpression = mCollapsedExpression;
125  element->mLabelStyle = mLabelStyle;
126 
127  return element;
128 }
129 
130 void QgsAttributeEditorContainer::saveConfiguration( QDomElement &elem, QDomDocument &doc ) const
131 {
132  Q_UNUSED( doc )
133  elem.setAttribute( QStringLiteral( "columnCount" ), mColumnCount );
134  elem.setAttribute( QStringLiteral( "groupBox" ), mIsGroupBox ? 1 : 0 );
135  elem.setAttribute( QStringLiteral( "collapsed" ), mCollapsed );
136  elem.setAttribute( QStringLiteral( "collapsedExpressionEnabled" ), mCollapsedExpression.enabled() ? 1 : 0 );
137  elem.setAttribute( QStringLiteral( "collapsedExpression" ), mCollapsedExpression->expression() );
138  elem.setAttribute( QStringLiteral( "visibilityExpressionEnabled" ), mVisibilityExpression.enabled() ? 1 : 0 );
139  elem.setAttribute( QStringLiteral( "visibilityExpression" ), mVisibilityExpression->expression() );
140  if ( mBackgroundColor.isValid() )
141  elem.setAttribute( QStringLiteral( "backgroundColor" ), mBackgroundColor.name( ) );
142  const auto constMChildren = mChildren;
143  for ( QgsAttributeEditorElement *child : constMChildren )
144  {
145  QDomDocument doc = elem.ownerDocument();
146  elem.appendChild( child->toDomElement( doc ) );
147  }
148 }
149 
150 void QgsAttributeEditorContainer::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
151 {
152  mBackgroundColor = element.attribute( QStringLiteral( "backgroundColor" ), QString() );
153  bool ok;
154  int cc = element.attribute( QStringLiteral( "columnCount" ) ).toInt( &ok );
155  if ( !ok )
156  cc = 0;
157  setColumnCount( cc );
158 
159  const bool isGroupBox = element.attribute( QStringLiteral( "groupBox" ) ).toInt( &ok );
160  if ( ok )
162  else
164 
165  const bool isCollapsed = element.attribute( QStringLiteral( "collapsed" ) ).toInt( &ok );
166  if ( ok )
167  setCollapsed( isCollapsed );
168  else
169  setCollapsed( false );
170 
171  const bool collapsedExpressionEnabled = element.attribute( QStringLiteral( "collapsedExpressionEnabled" ) ).toInt( &ok );
173  if ( ok )
174  {
175  collapsedExpression.setEnabled( collapsedExpressionEnabled );
176  collapsedExpression.setData( QgsExpression( element.attribute( QStringLiteral( "collapsedExpression" ) ) ) );
177  }
179 
180 
181  const bool visibilityExpressionEnabled = element.attribute( QStringLiteral( "visibilityExpressionEnabled" ) ).toInt( &ok );
183  if ( ok )
184  {
185  visibilityExpression.setEnabled( visibilityExpressionEnabled );
186  visibilityExpression.setData( QgsExpression( element.attribute( QStringLiteral( "visibilityExpression" ) ) ) );
187  }
189 
190  const QDomNodeList childNodeList = element.childNodes();
191 
192  for ( int i = 0; i < childNodeList.size(); i++ )
193  {
194  const QDomElement childElem = childNodeList.at( i ).toElement();
195 
196  QgsAttributeEditorElement *myElem = create( childElem, layerId, fields, context, this );
197  if ( myElem )
198  addChildElement( myElem );
199  }
200 }
201 
202 QString QgsAttributeEditorContainer::typeIdentifier() const
203 {
204  return QStringLiteral( "attributeEditorContainer" );
205 }
206 
QgsAttributeEditorContainer::setCollapsedExpression
void setCollapsedExpression(const QgsOptionalExpression &collapsedExpression)
The collapsed expression is used in the attribute form to set the collapsed status of the group box o...
Definition: qgsattributeeditorcontainer.cpp:53
QgsAttributeEditorContainer::setVisibilityExpression
void setVisibilityExpression(const QgsOptionalExpression &visibilityExpression)
The visibility expression is used in the attribute form to show or hide this container based on an ex...
Definition: qgsattributeeditorcontainer.cpp:40
QgsAttributeEditorElement
This is an abstract base class for any elements of a drag and drop form.
Definition: qgsattributeeditorelement.h:37
QgsAttributeEditorContainer::~QgsAttributeEditorContainer
~QgsAttributeEditorContainer() override
Definition: qgsattributeeditorcontainer.cpp:20
QgsOptional::enabled
bool enabled() const
Check if this optional is enabled.
Definition: qgsoptional.h:103
QgsAttributeEditorContainer::QgsAttributeEditorContainer
QgsAttributeEditorContainer(const QString &name, QgsAttributeEditorElement *parent, const QColor &backgroundColor=QColor())
Creates a new attribute editor container.
Definition: qgsattributeeditorcontainer.h:52
QgsAttributeEditorContainer::columnCount
int columnCount() const
Gets the number of columns in this group.
Definition: qgsattributeeditorcontainer.cpp:100
QgsAttributeEditorContainer::findElements
virtual QList< QgsAttributeEditorElement * > findElements(AttributeEditorType type) const
Traverses the element tree to find any element of the specified type.
Definition: qgsattributeeditorcontainer.cpp:71
QgsAttributeEditorElement::create
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.
Definition: qgsattributeeditorelement.cpp:58
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
qgsattributeeditorcontainer.h
QgsAttributeEditorContainer::clone
QgsAttributeEditorElement * clone(QgsAttributeEditorElement *parent) const override
Creates a deep copy of this element.
Definition: qgsattributeeditorcontainer.cpp:110
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsAttributeEditorContainer::isGroupBox
virtual bool isGroupBox() const
Returns if this container is going to be a group box.
Definition: qgsattributeeditorcontainer.h:81
QgsAttributeEditorContainer::setName
void setName(const QString &name)
Change the name of this container.
Definition: qgsattributeeditorcontainer.cpp:30
QgsAttributeEditorContainer::children
QList< QgsAttributeEditorElement * > children() const
Gets a list of the children elements of this container.
Definition: qgsattributeeditorcontainer.h:107
QgsAttributeEditorElement::AeTypeContainer
@ AeTypeContainer
A container.
Definition: qgsattributeeditorelement.h:119
QgsAttributeEditorElement::mParent
QgsAttributeEditorElement * mParent
Definition: qgsattributeeditorelement.h:220
QgsOptional::setData
void setData(const T &data)
Set the payload data.
Definition: qgsoptional.h:143
QgsAttributeEditorElement::mLabelStyle
LabelStyle mLabelStyle
Definition: qgsattributeeditorelement.h:222
QgsAttributeEditorContainer::setColumnCount
void setColumnCount(int columnCount)
Set the number of columns in this group.
Definition: qgsattributeeditorcontainer.cpp:105
QgsOptionalExpression
An expression with an additional enabled flag.
Definition: qgsoptionalexpression.h:39
QgsAttributeEditorContainer::setIsGroupBox
virtual void setIsGroupBox(bool isGroupBox)
Determines if this container is rendered as collapsible group box or tab in a tabwidget.
Definition: qgsattributeeditorcontainer.h:74
QgsAttributeEditorElement::mName
QString mName
Definition: qgsattributeeditorelement.h:219
QgsAttributeEditorContainer::setBackgroundColor
void setBackgroundColor(const QColor &backgroundColor)
Sets the background color to backgroundColor.
Definition: qgsattributeeditorcontainer.cpp:66
QgsAttributeEditorContainer::clear
void clear()
Clear all children from this container.
Definition: qgsattributeeditorcontainer.cpp:94
QgsOptional::setEnabled
void setEnabled(bool enabled)
Set if this optional is enabled.
Definition: qgsoptional.h:113
QgsAttributeEditorContainer
This is a container for attribute editors, used to group them visually in the attribute form if it is...
Definition: qgsattributeeditorcontainer.h:27
QgsAttributeEditorElement::type
AttributeEditorType type() const
The type of this element.
Definition: qgsattributeeditorelement.h:163
QgsAttributeEditorContainer::collapsedExpression
QgsOptionalExpression collapsedExpression() const
The collapsed expression is used in the attribute form to set the collapsed status of the group box c...
Definition: qgsattributeeditorcontainer.cpp:48
QgsAttributeEditorContainer::visibilityExpression
QgsOptionalExpression visibilityExpression() const
The visibility expression is used in the attribute form to show or hide this container based on an ex...
Definition: qgsattributeeditorcontainer.cpp:35
QgsAttributeEditorElement::name
QString name() const
Returns the name of this element.
Definition: qgsattributeeditorelement.h:156
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsAttributeEditorElement::AttributeEditorType
AttributeEditorType
Definition: qgsattributeeditorelement.h:117
QgsAttributeEditorContainer::addChildElement
virtual void addChildElement(QgsAttributeEditorElement *element)
Add a child element to this container.
Definition: qgsattributeeditorcontainer.cpp:25
QgsAttributeEditorContainer::setCollapsed
void setCollapsed(bool collapsed)
For group box containers sets if this group box is collapsed.
Definition: qgsattributeeditorcontainer.h:100
QgsAttributeEditorElement::parent
QgsAttributeEditorElement * parent() const
Gets the parent of this element.
Definition: qgsattributeeditorelement.h:170
QgsAttributeEditorContainer::backgroundColor
QColor backgroundColor() const
backgroundColor
Definition: qgsattributeeditorcontainer.cpp:61