Skip to content
Permalink
Browse files

[feature][needs-docs] Add bg color option to form containers

Because gray is boring.
  • Loading branch information
elpaso committed Feb 24, 2019
1 parent c672893 commit 8d3f73a62a087d4842511563b15307ba4101e983
@@ -130,7 +130,7 @@ attribute form if it is set to the drag and drop designer.
%End
public:

QgsAttributeEditorContainer( const QString &name, QgsAttributeEditorElement *parent );
QgsAttributeEditorContainer( const QString &name, QgsAttributeEditorElement *parent, const QColor &backgroundColor = QColor() );
%Docstring
Creates a new attribute editor container

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

.. versionadded:: 3.0
%End

QColor backgroundColor() const;
%Docstring
backgroundColor

:return: background color of the container

.. versionadded:: 3.8
%End

void setBackgroundColor( const QColor &backgroundColor );
%Docstring
Sets the background color to ``backgroundColor``
%End

};
@@ -134,7 +134,7 @@ Holding Shift modifier key when attempting to toggle collapsed state will expand

.. 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:
To add Collapsible properties in promoted QtDesigner widgets, you can add the following "Dynamic properties" by clicking on the green + in the properties palette:
bool collapsed, bool saveCollapsedState, bool saveCheckedState, QString syncGroup
%End

@@ -21,6 +21,7 @@
#include "qgsfieldcombobox.h"
#include "qgsqmlwidgetwrapper.h"
#include "qgsapplication.h"
#include "qgscolorbutton.h"

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

itemData.setColumnCount( container->columnCount() );
itemData.setShowAsGroupBox( container->isGroupBox() );
itemData.setBackgroundColor( container->backgroundColor() );
itemData.setVisibilityExpression( container->visibilityExpression() );
newWidget = tree->addItem( parent, itemData );

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

case DnDTreeItemData::Container:
{
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( item->text( 0 ), parent );
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( item->text( 0 ), parent, itemData.backgroundColor() );
container->setColumnCount( itemData.columnCount() );
container->setIsGroupBox( forceGroup ? true : itemData.showAsGroupBox() );
container->setVisibilityExpression( itemData.visibilityExpression() );
container->setBackgroundColor( itemData.backgroundColor( ) );

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

QgsCollapsibleGroupBox *styleGroupBox = new QgsCollapsibleGroupBox( tr( "Style" ), layout->widget() );
styleGroupBox->setObjectName( QLatin1Literal( "attributeFormPropertiesContainerStyle" ) );
QFormLayout *customizeGroupBoxLayout = new QFormLayout( styleGroupBox ) ;
QgsColorButton *backgroundColorButton = new QgsColorButton( styleGroupBox, tr( "Select the background color for the container" ) );
backgroundColorButton->setShowNull( true );
backgroundColorButton->setColor( itemData.backgroundColor() );
customizeGroupBoxLayout->addRow( new QLabel( tr( "Background color" ), styleGroupBox ),
backgroundColorButton );
styleGroupBox->setLayout( customizeGroupBoxLayout );
layout->addRow( styleGroupBox );

QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok
| QDialogButtonBox::Cancel );

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

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

QColor QgsAttributesFormProperties::DnDTreeItemData::backgroundColor() const
{
return mBackgroundColor;
}

void QgsAttributesFormProperties::DnDTreeItemData::setBackgroundColor( const QColor &backgroundColor )
{
mBackgroundColor = backgroundColor;
}

@@ -88,10 +88,11 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
//do we need that
DnDTreeItemData() = default;

DnDTreeItemData( Type type, const QString &name, const QString &displayName )
DnDTreeItemData( Type type, const QString &name, const QString &displayName, const QColor &backgroundColor = QColor() )
: mType( type )
, mName( name )
, mDisplayName( displayName )
, mBackgroundColor( backgroundColor )
{}

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

QColor backgroundColor() const;
void setBackgroundColor( const QColor &backgroundColor );

private:
Type mType = Field;
QString mName;
@@ -134,6 +138,7 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
QgsOptionalExpression mVisibilityExpression;
RelationEditorConfiguration mRelationEditorConfiguration;
QmlElementEditorConfiguration mQmlElementEditorConfiguration;
QColor mBackgroundColor;
};


@@ -39,6 +39,16 @@ void QgsAttributeEditorContainer::setVisibilityExpression( const QgsOptionalExpr
mVisibilityExpression = visibilityExpression;
}

QColor QgsAttributeEditorContainer::backgroundColor() const
{
return mBackgroundColor;
}

void QgsAttributeEditorContainer::setBackgroundColor( const QColor &backgroundColor )
{
mBackgroundColor = backgroundColor;
}

QList<QgsAttributeEditorElement *> QgsAttributeEditorContainer::findElements( QgsAttributeEditorElement::AttributeEditorType type ) const
{
QList<QgsAttributeEditorElement *> results;
@@ -19,6 +19,7 @@
#include "qgis_core.h"
#include "qgsrelation.h"
#include "qgsoptionalexpression.h"
#include <QColor>

class QgsRelationManager;

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


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

/**
* \brief backgroundColor
* \return background color of the container
* \since QGIS 3.8
*/
QColor backgroundColor() const;

/**
* Sets the background color to \a backgroundColor
*/
void setBackgroundColor( const QColor &backgroundColor );

private:
void saveConfiguration( QDomElement &elem ) const override;
QString typeIdentifier() const override;
@@ -273,6 +287,7 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
QList<QgsAttributeEditorElement *> mChildren;
int mColumnCount;
QgsOptionalExpression mVisibilityExpression;
QColor mBackgroundColor;
};

/**
@@ -525,7 +525,9 @@ QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomEleme

if ( elem.tagName() == QLatin1String( "attributeEditorContainer" ) )
{
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:formcontainers" ).arg( layerId ), elem.attribute( QStringLiteral( "name" ) ) ), parent );
QColor backgroundColor( elem.attribute( QStringLiteral( "backgroundColor" ), QString() ) );
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:formcontainers" ).arg( layerId ),
elem.attribute( QStringLiteral( "name" ) ) ), parent, backgroundColor );
bool ok;
int cc = elem.attribute( QStringLiteral( "columnCount" ) ).toInt( &ok );
if ( !ok )
@@ -625,7 +627,8 @@ void QgsAttributeEditorContainer::saveConfiguration( QDomElement &elem ) const
elem.setAttribute( QStringLiteral( "groupBox" ), mIsGroupBox ? 1 : 0 );
elem.setAttribute( QStringLiteral( "visibilityExpressionEnabled" ), mVisibilityExpression.enabled() ? 1 : 0 );
elem.setAttribute( QStringLiteral( "visibilityExpression" ), mVisibilityExpression->expression() );

if ( mBackgroundColor.isValid() )
elem.setAttribute( QStringLiteral( "backgroundColor" ), mBackgroundColor.name( ) );
Q_FOREACH ( QgsAttributeEditorElement *child, mChildren )
{
QDomDocument doc = elem.ownerDocument();
@@ -1707,10 +1707,12 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt
if ( columnCount <= 0 )
columnCount = 1;

QString widgetName;
QWidget *myContainer = nullptr;
if ( container->isGroupBox() )
{
QGroupBox *groupBox = new QGroupBox( parent );
widgetName = QStringLiteral( "QGroupBox" );
if ( container->showLabel() )
groupBox->setTitle( container->name() );
myContainer = groupBox;
@@ -1727,15 +1729,23 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt
scrollArea->setWidget( myContainer );
scrollArea->setWidgetResizable( true );
scrollArea->setFrameShape( QFrame::NoFrame );
widgetName = QStringLiteral( "QScrollArea QWidget" );

newWidgetInfo.widget = scrollArea;
}
else
{
newWidgetInfo.widget = myContainer;
widgetName = QStringLiteral( "QWidget" );
}
}

if ( container->backgroundColor().isValid() )
{
QString style {QStringLiteral( "background-color: %1;" ).arg( container->backgroundColor().name() )};
newWidgetInfo.widget->setStyleSheet( style );
}

QGridLayout *gbLayout = new QGridLayout();
myContainer->setLayout( gbLayout );

0 comments on commit 8d3f73a

Please sign in to comment.
You can’t perform that action at this time.