Skip to content

Commit a615a3b

Browse files
authoredFeb 25, 2019
Merge pull request #9260 from elpaso/form-background-color
Add bg color option to form containers
2 parents 77f500b + 94b5fed commit a615a3b

10 files changed

+114
-11
lines changed
 

‎python/core/auto_generated/qgsattributeeditorelement.sip.in

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,13 @@ attribute form if it is set to the drag and drop designer.
130130
%End
131131
public:
132132

133-
QgsAttributeEditorContainer( const QString &name, QgsAttributeEditorElement *parent );
133+
QgsAttributeEditorContainer( const QString &name, QgsAttributeEditorElement *parent, const QColor &backgroundColor = QColor() );
134134
%Docstring
135135
Creates a new attribute editor container
136136

137137
:param name: The name to show as title
138138
:param parent: The parent. May be another container.
139+
:param backgroundColor: The optional background color of the container.
139140
%End
140141

141142

@@ -222,6 +223,20 @@ show or hide this container based on an expression incorporating
222223
the field value controlled by editor widgets.
223224

224225
.. versionadded:: 3.0
226+
%End
227+
228+
QColor backgroundColor() const;
229+
%Docstring
230+
backgroundColor
231+
232+
:return: background color of the container
233+
234+
.. versionadded:: 3.8
235+
%End
236+
237+
void setBackgroundColor( const QColor &backgroundColor );
238+
%Docstring
239+
Sets the background color to ``backgroundColor``
225240
%End
226241

227242
};

‎python/gui/auto_generated/qgscollapsiblegroupbox.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Holding Shift modifier key when attempting to toggle collapsed state will expand
134134

135135
.. note::
136136

137-
To add Collapsible properties in promoted QtDesigner widgets, you can add the following "Dynamic properties" by clicking on the green + in the propreties palette:
137+
To add Collapsible properties in promoted QtDesigner widgets, you can add the following "Dynamic properties" by clicking on the green + in the properties palette:
138138
bool collapsed, bool saveCollapsedState, bool saveCheckedState, QString syncGroup
139139
%End
140140

‎src/app/qgsattributesformproperties.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgsfieldcombobox.h"
2222
#include "qgsqmlwidgetwrapper.h"
2323
#include "qgsapplication.h"
24+
#include "qgscolorbutton.h"
2425

2526
QgsAttributesFormProperties::QgsAttributesFormProperties( QgsVectorLayer *layer, QWidget *parent )
2627
: QWidget( parent )
@@ -451,6 +452,7 @@ QTreeWidgetItem *QgsAttributesFormProperties::loadAttributeEditorTreeItem( QgsAt
451452

452453
itemData.setColumnCount( container->columnCount() );
453454
itemData.setShowAsGroupBox( container->isGroupBox() );
455+
itemData.setBackgroundColor( container->backgroundColor() );
454456
itemData.setVisibilityExpression( container->visibilityExpression() );
455457
newWidget = tree->addItem( parent, itemData );
456458

@@ -582,10 +584,11 @@ QgsAttributeEditorElement *QgsAttributesFormProperties::createAttributeEditorWid
582584

583585
case DnDTreeItemData::Container:
584586
{
585-
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( item->text( 0 ), parent );
587+
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( item->text( 0 ), parent, itemData.backgroundColor() );
586588
container->setColumnCount( itemData.columnCount() );
587589
container->setIsGroupBox( forceGroup ? true : itemData.showAsGroupBox() );
588590
container->setVisibilityExpression( itemData.visibilityExpression() );
591+
container->setBackgroundColor( itemData.backgroundColor( ) );
589592

590593
for ( int t = 0; t < item->childCount(); t++ )
591594
{
@@ -1014,6 +1017,7 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
10141017
case QgsAttributesFormProperties::DnDTreeItemData::Container:
10151018
{
10161019
QDialog dlg;
1020+
dlg.setObjectName( QLatin1Literal( "attributeFormPropertiesContainerDialog" ) );
10171021
dlg.setWindowTitle( tr( "Configure Container" ) );
10181022
QFormLayout *layout = new QFormLayout() ;
10191023
dlg.setLayout( layout );
@@ -1046,6 +1050,17 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
10461050
layout->addRow( showAsGroupBox );
10471051
}
10481052

1053+
QgsCollapsibleGroupBox *styleGroupBox = new QgsCollapsibleGroupBox( tr( "Style" ), layout->widget() );
1054+
styleGroupBox->setObjectName( QLatin1Literal( "attributeFormPropertiesContainerStyle" ) );
1055+
QFormLayout *customizeGroupBoxLayout = new QFormLayout( styleGroupBox ) ;
1056+
QgsColorButton *backgroundColorButton = new QgsColorButton( styleGroupBox, tr( "Container Background Color" ) );
1057+
backgroundColorButton->setShowNull( true );
1058+
backgroundColorButton->setColor( itemData.backgroundColor() );
1059+
customizeGroupBoxLayout->addRow( new QLabel( tr( "Background color" ), styleGroupBox ),
1060+
backgroundColorButton );
1061+
styleGroupBox->setLayout( customizeGroupBoxLayout );
1062+
layout->addRow( styleGroupBox );
1063+
10491064
QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok
10501065
| QDialogButtonBox::Cancel );
10511066

@@ -1060,6 +1075,7 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
10601075
itemData.setShowAsGroupBox( showAsGroupBox ? showAsGroupBox->isChecked() : true );
10611076
itemData.setName( title->text() );
10621077
itemData.setShowLabel( showLabelCheckbox->isChecked() );
1078+
itemData.setBackgroundColor( backgroundColorButton->color() );
10631079

10641080
QgsOptionalExpression visibilityExpression;
10651081
visibilityExpression.setData( QgsExpression( visibilityExpressionWidget->expression() ) );
@@ -1379,3 +1395,13 @@ void QgsAttributesFormProperties::DnDTreeItemData::setQmlElementEditorConfigurat
13791395
mQmlElementEditorConfiguration = qmlElementEditorConfiguration;
13801396
}
13811397

1398+
QColor QgsAttributesFormProperties::DnDTreeItemData::backgroundColor() const
1399+
{
1400+
return mBackgroundColor;
1401+
}
1402+
1403+
void QgsAttributesFormProperties::DnDTreeItemData::setBackgroundColor( const QColor &backgroundColor )
1404+
{
1405+
mBackgroundColor = backgroundColor;
1406+
}
1407+

‎src/app/qgsattributesformproperties.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
8888
//do we need that
8989
DnDTreeItemData() = default;
9090

91-
DnDTreeItemData( Type type, const QString &name, const QString &displayName )
91+
DnDTreeItemData( Type type, const QString &name, const QString &displayName, const QColor &backgroundColor = QColor() )
9292
: mType( type )
9393
, mName( name )
9494
, mDisplayName( displayName )
95+
, mBackgroundColor( backgroundColor )
9596
{}
9697

9798
QString name() const { return mName; }
@@ -123,6 +124,9 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
123124
QmlElementEditorConfiguration qmlElementEditorConfiguration() const;
124125
void setQmlElementEditorConfiguration( QmlElementEditorConfiguration qmlElementEditorConfiguration );
125126

127+
QColor backgroundColor() const;
128+
void setBackgroundColor( const QColor &backgroundColor );
129+
126130
private:
127131
Type mType = Field;
128132
QString mName;
@@ -134,6 +138,7 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
134138
QgsOptionalExpression mVisibilityExpression;
135139
RelationEditorConfiguration mRelationEditorConfiguration;
136140
QmlElementEditorConfiguration mQmlElementEditorConfiguration;
141+
QColor mBackgroundColor;
137142
};
138143

139144

‎src/core/qgsattributeeditorelement.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ void QgsAttributeEditorContainer::setVisibilityExpression( const QgsOptionalExpr
3939
mVisibilityExpression = visibilityExpression;
4040
}
4141

42+
QColor QgsAttributeEditorContainer::backgroundColor() const
43+
{
44+
return mBackgroundColor;
45+
}
46+
47+
void QgsAttributeEditorContainer::setBackgroundColor( const QColor &backgroundColor )
48+
{
49+
mBackgroundColor = backgroundColor;
50+
}
51+
4252
QList<QgsAttributeEditorElement *> QgsAttributeEditorContainer::findElements( QgsAttributeEditorElement::AttributeEditorType type ) const
4353
{
4454
QList<QgsAttributeEditorElement *> results;

‎src/core/qgsattributeeditorelement.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgis_core.h"
2020
#include "qgsrelation.h"
2121
#include "qgsoptionalexpression.h"
22+
#include <QColor>
2223

2324
class QgsRelationManager;
2425

@@ -173,11 +174,13 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
173174
*
174175
* \param name The name to show as title
175176
* \param parent The parent. May be another container.
177+
* \param backgroundColor The optional background color of the container.
176178
*/
177-
QgsAttributeEditorContainer( const QString &name, QgsAttributeEditorElement *parent )
179+
QgsAttributeEditorContainer( const QString &name, QgsAttributeEditorElement *parent, const QColor &backgroundColor = QColor() )
178180
: QgsAttributeEditorElement( AeTypeContainer, name, parent )
179181
, mIsGroupBox( true )
180182
, mColumnCount( 1 )
183+
, mBackgroundColor( backgroundColor )
181184
{}
182185

183186

@@ -265,6 +268,18 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
265268
*/
266269
void setVisibilityExpression( const QgsOptionalExpression &visibilityExpression );
267270

271+
/**
272+
* \brief backgroundColor
273+
* \return background color of the container
274+
* \since QGIS 3.8
275+
*/
276+
QColor backgroundColor() const;
277+
278+
/**
279+
* Sets the background color to \a backgroundColor
280+
*/
281+
void setBackgroundColor( const QColor &backgroundColor );
282+
268283
private:
269284
void saveConfiguration( QDomElement &elem ) const override;
270285
QString typeIdentifier() const override;
@@ -273,6 +288,7 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
273288
QList<QgsAttributeEditorElement *> mChildren;
274289
int mColumnCount;
275290
QgsOptionalExpression mVisibilityExpression;
291+
QColor mBackgroundColor;
276292
};
277293

278294
/**

‎src/core/qgseditformconfig.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,9 @@ QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomEleme
525525

526526
if ( elem.tagName() == QLatin1String( "attributeEditorContainer" ) )
527527
{
528-
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:formcontainers" ).arg( layerId ), elem.attribute( QStringLiteral( "name" ) ) ), parent );
528+
QColor backgroundColor( elem.attribute( QStringLiteral( "backgroundColor" ), QString() ) );
529+
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:formcontainers" ).arg( layerId ),
530+
elem.attribute( QStringLiteral( "name" ) ) ), parent, backgroundColor );
529531
bool ok;
530532
int cc = elem.attribute( QStringLiteral( "columnCount" ) ).toInt( &ok );
531533
if ( !ok )
@@ -625,7 +627,8 @@ void QgsAttributeEditorContainer::saveConfiguration( QDomElement &elem ) const
625627
elem.setAttribute( QStringLiteral( "groupBox" ), mIsGroupBox ? 1 : 0 );
626628
elem.setAttribute( QStringLiteral( "visibilityExpressionEnabled" ), mVisibilityExpression.enabled() ? 1 : 0 );
627629
elem.setAttribute( QStringLiteral( "visibilityExpression" ), mVisibilityExpression->expression() );
628-
630+
if ( mBackgroundColor.isValid() )
631+
elem.setAttribute( QStringLiteral( "backgroundColor" ), mBackgroundColor.name( ) );
629632
Q_FOREACH ( QgsAttributeEditorElement *child, mChildren )
630633
{
631634
QDomDocument doc = elem.ownerDocument();

‎src/gui/qgsattributeform.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,10 +1707,12 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt
17071707
if ( columnCount <= 0 )
17081708
columnCount = 1;
17091709

1710+
QString widgetName;
17101711
QWidget *myContainer = nullptr;
17111712
if ( container->isGroupBox() )
17121713
{
17131714
QGroupBox *groupBox = new QGroupBox( parent );
1715+
widgetName = QStringLiteral( "QGroupBox" );
17141716
if ( container->showLabel() )
17151717
groupBox->setTitle( container->name() );
17161718
myContainer = groupBox;
@@ -1727,15 +1729,23 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt
17271729
scrollArea->setWidget( myContainer );
17281730
scrollArea->setWidgetResizable( true );
17291731
scrollArea->setFrameShape( QFrame::NoFrame );
1732+
widgetName = QStringLiteral( "QScrollArea QWidget" );
17301733

17311734
newWidgetInfo.widget = scrollArea;
17321735
}
17331736
else
17341737
{
17351738
newWidgetInfo.widget = myContainer;
1739+
widgetName = QStringLiteral( "QWidget" );
17361740
}
17371741
}
17381742

1743+
if ( container->backgroundColor().isValid() )
1744+
{
1745+
QString style {QStringLiteral( "background-color: %1;" ).arg( container->backgroundColor().name() )};
1746+
newWidgetInfo.widget->setStyleSheet( style );
1747+
}
1748+
17391749
QGridLayout *gbLayout = new QGridLayout();
17401750
myContainer->setLayout( gbLayout );
17411751

‎src/gui/qgscollapsiblegroupbox.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class GUI_EXPORT QgsCollapsibleGroupBoxBasic : public QGroupBox
171171
* Holding Alt modifier key when toggling collapsed state will synchronize the toggling across other collapsible group boxes with the same syncGroup QString value
172172
* Holding Shift modifier key when attempting to toggle collapsed state will expand current group box, then collapse any others with the same syncGroup QString value
173173
* \see basic class QgsCollapsibleGroupBoxBasic which does not auto-save states
174-
* \note To add Collapsible properties in promoted QtDesigner widgets, you can add the following "Dynamic properties" by clicking on the green + in the propreties palette:
174+
* \note To add Collapsible properties in promoted QtDesigner widgets, you can add the following "Dynamic properties" by clicking on the green + in the properties palette:
175175
* bool collapsed, bool saveCollapsedState, bool saveCheckedState, QString syncGroup
176176
*/
177177

‎tests/src/python/test_qgseditformconfig.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
QgsVectorLayer,
2121
QgsReadWriteContext,
2222
QgsEditFormConfig,
23-
QgsFetchedContent)
23+
QgsFetchedContent,
24+
QgsAttributeEditorContainer,
25+
)
2426
from qgis.gui import QgsGui
2527

2628
from qgis.testing import start_app, unittest
2729
from qgis.PyQt.QtXml import QDomDocument, QDomElement
30+
from qgis.PyQt.QtGui import QColor
2831
from utilities import unitTestDataPath
2932
import socketserver
3033
import threading
@@ -84,14 +87,16 @@ def testFormUi(self):
8487
config.setLayout(QgsEditFormConfig.GeneratedLayout)
8588
self.assertEqual(config.layout(), QgsEditFormConfig.GeneratedLayout)
8689

87-
uiLocal = os.path.join(unitTestDataPath(), '/qgis_local_server/layer_attribute_form.ui')
90+
uiLocal = os.path.join(
91+
unitTestDataPath(), '/qgis_local_server/layer_attribute_form.ui')
8892
config.setUiForm(uiLocal)
8993
self.assertEqual(config.layout(), QgsEditFormConfig.UiFileLayout)
9094

9195
config.setLayout(QgsEditFormConfig.GeneratedLayout)
9296
self.assertEqual(config.layout(), QgsEditFormConfig.GeneratedLayout)
9397

94-
uiUrl = 'http://localhost:' + str(self.port) + '/qgis_local_server/layer_attribute_form.ui'
98+
uiUrl = 'http://localhost:' + \
99+
str(self.port) + '/qgis_local_server/layer_attribute_form.ui'
95100
config.setUiForm(uiUrl)
96101
self.assertEqual(config.layout(), QgsEditFormConfig.UiFileLayout)
97102
content = QgsApplication.networkContentFetcherRegistry().fetch(uiUrl)
@@ -140,6 +145,19 @@ def testLabelOnTop(self):
140145
self.assertFalse(config.labelOnTop(0))
141146
self.assertFalse(config.labelOnTop(1))
142147

148+
def test_backgroundColorSerialize(self):
149+
"""Test backgroundColor serialization"""
150+
151+
layer = self.createLayer()
152+
config = layer.editFormConfig()
153+
color_name = '#ff00ff'
154+
container = QgsAttributeEditorContainer('container name', None, QColor('#ff00ff'))
155+
doc = QDomDocument()
156+
element = container.toDomElement(doc)
157+
config = QgsEditFormConfig()
158+
container2 = config.attributeEditorElementFromDomElement(element, None, self.layer.id())
159+
self.assertEqual(container2.backgroundColor().name(), color_name)
160+
143161

144162
if __name__ == '__main__':
145163
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.