QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 mBackgroundColor;
51 }
52 
53 void QgsAttributeEditorContainer::setBackgroundColor( const QColor &backgroundColor )
54 {
55  mBackgroundColor = backgroundColor;
56 }
57 
59 {
60  QList<QgsAttributeEditorElement *> results;
61 
62  const auto constMChildren = mChildren;
63  for ( QgsAttributeEditorElement *elem : constMChildren )
64  {
65  if ( elem->type() == type )
66  {
67  results.append( elem );
68  }
69 
70  if ( elem->type() == AeTypeContainer )
71  {
72  QgsAttributeEditorContainer *cont = dynamic_cast<QgsAttributeEditorContainer *>( elem );
73  if ( cont )
74  results += cont->findElements( type );
75  }
76  }
77 
78  return results;
79 }
80 
82 {
83  qDeleteAll( mChildren );
84  mChildren.clear();
85 }
86 
88 {
89  return mColumnCount;
90 }
91 
93 {
94  mColumnCount = columnCount;
95 }
96 
98 {
100 
101  const auto childElements = children();
102 
103  for ( QgsAttributeEditorElement *child : childElements )
104  {
105  element->addChildElement( child->clone( element ) );
106  }
107  element->mIsGroupBox = mIsGroupBox;
108  element->mColumnCount = mColumnCount;
109  element->mVisibilityExpression = mVisibilityExpression;
110 
111  return element;
112 }
113 
114 void QgsAttributeEditorContainer::saveConfiguration( QDomElement &elem, QDomDocument &doc ) const
115 {
116  Q_UNUSED( doc )
117  elem.setAttribute( QStringLiteral( "columnCount" ), mColumnCount );
118  elem.setAttribute( QStringLiteral( "groupBox" ), mIsGroupBox ? 1 : 0 );
119  elem.setAttribute( QStringLiteral( "visibilityExpressionEnabled" ), mVisibilityExpression.enabled() ? 1 : 0 );
120  elem.setAttribute( QStringLiteral( "visibilityExpression" ), mVisibilityExpression->expression() );
121  if ( mBackgroundColor.isValid() )
122  elem.setAttribute( QStringLiteral( "backgroundColor" ), mBackgroundColor.name( ) );
123  const auto constMChildren = mChildren;
124  for ( QgsAttributeEditorElement *child : constMChildren )
125  {
126  QDomDocument doc = elem.ownerDocument();
127  elem.appendChild( child->toDomElement( doc ) );
128  }
129 }
130 
131 void QgsAttributeEditorContainer::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
132 {
133  mBackgroundColor = element.attribute( QStringLiteral( "backgroundColor" ), QString() );
134  bool ok;
135  int cc = element.attribute( QStringLiteral( "columnCount" ) ).toInt( &ok );
136  if ( !ok )
137  cc = 0;
138  setColumnCount( cc );
139 
140  const bool isGroupBox = element.attribute( QStringLiteral( "groupBox" ) ).toInt( &ok );
141  if ( ok )
143  else
145 
146  const bool visibilityExpressionEnabled = element.attribute( QStringLiteral( "visibilityExpressionEnabled" ) ).toInt( &ok );
148  if ( ok )
149  {
150  visibilityExpression.setEnabled( visibilityExpressionEnabled );
151  visibilityExpression.setData( QgsExpression( element.attribute( QStringLiteral( "visibilityExpression" ) ) ) );
152  }
154 
155  const QDomNodeList childNodeList = element.childNodes();
156 
157  for ( int i = 0; i < childNodeList.size(); i++ )
158  {
159  const QDomElement childElem = childNodeList.at( i ).toElement();
160 
161  QgsAttributeEditorElement *myElem = create( childElem, layerId, fields, context, this );
162  if ( myElem )
163  addChildElement( myElem );
164  }
165 }
166 
167 QString QgsAttributeEditorContainer::typeIdentifier() const
168 {
169  return QStringLiteral( "attributeEditorContainer" );
170 }
171 
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.
virtual bool isGroupBox() const
Returns if this container is going to be rendered as a group box.
void setVisibilityExpression(const QgsOptionalExpression &visibilityExpression)
The visibility expression is used in the attribute form to show or hide this container based on an ex...
virtual 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
backgroundColor
virtual QList< QgsAttributeEditorElement * > findElements(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.
QList< QgsAttributeEditorElement * > children() const
Gets a list of the children elements of this container.
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 * mParent
AttributeEditorType type() const
The type of this element.
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.
QgsAttributeEditorElement * parent() const
Gets the parent of this 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:89
void setData(const T &data)
Set the payload data.
Definition: qgsoptional.h:129
void setEnabled(bool enabled)
Set if this optional is enabled.
Definition: qgsoptional.h:99
The class is used as a container of context for various read/write operations on other objects.