Skip to content

Commit 8d3f73a

Browse files
committed
[feature][needs-docs] Add bg color option to form containers
Because gray is boring.
1 parent c672893 commit 8d3f73a

File tree

8 files changed

+90
-7
lines changed

8 files changed

+90
-7
lines changed

python/core/auto_generated/qgsattributeeditorelement.sip.in

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ 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

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

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

227241
};

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( "Select the background color for the container" ) );
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: 16 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

@@ -174,10 +175,11 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
174175
* \param name The name to show as title
175176
* \param parent The parent. May be another container.
176177
*/
177-
QgsAttributeEditorContainer( const QString &name, QgsAttributeEditorElement *parent )
178+
QgsAttributeEditorContainer( const QString &name, QgsAttributeEditorElement *parent, const QColor &backgroundColor = QColor() )
178179
: QgsAttributeEditorElement( AeTypeContainer, name, parent )
179180
, mIsGroupBox( true )
180181
, mColumnCount( 1 )
182+
, mBackgroundColor( backgroundColor )
181183
{}
182184

183185

@@ -265,6 +267,18 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
265267
*/
266268
void setVisibilityExpression( const QgsOptionalExpression &visibilityExpression );
267269

270+
/**
271+
* \brief backgroundColor
272+
* \return background color of the container
273+
* \since QGIS 3.8
274+
*/
275+
QColor backgroundColor() const;
276+
277+
/**
278+
* Sets the background color to \a backgroundColor
279+
*/
280+
void setBackgroundColor( const QColor &backgroundColor );
281+
268282
private:
269283
void saveConfiguration( QDomElement &elem ) const override;
270284
QString typeIdentifier() const override;
@@ -273,6 +287,7 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
273287
QList<QgsAttributeEditorElement *> mChildren;
274288
int mColumnCount;
275289
QgsOptionalExpression mVisibilityExpression;
290+
QColor mBackgroundColor;
276291
};
277292

278293
/**

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

0 commit comments

Comments
 (0)