Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use enums for attribute form container types instead of bools
Gives flexibility for adding additional container types in future
  • Loading branch information
nyalldawson committed Apr 27, 2023
1 parent 9d5af2a commit a434d0e
Show file tree
Hide file tree
Showing 15 changed files with 350 additions and 195 deletions.
6 changes: 6 additions & 0 deletions python/core/auto_additions/qgis.py
Expand Up @@ -3516,6 +3516,12 @@
Qgis.AttributeEditorType.__doc__ = 'Attribute editor types.\n\n.. note::\n\n Prior to QGIS 3.32 this was available as :py:class:`QgsAttributeEditorElement`.AttributeEditorType.\n\n.. versionadded:: 3.32\n\n' + '* ``AeTypeContainer``: ' + Qgis.AttributeEditorType.Container.__doc__ + '\n' + '* ``AeTypeField``: ' + Qgis.AttributeEditorType.Field.__doc__ + '\n' + '* ``AeTypeRelation``: ' + Qgis.AttributeEditorType.Relation.__doc__ + '\n' + '* ``AeTypeQmlElement``: ' + Qgis.AttributeEditorType.QmlElement.__doc__ + '\n' + '* ``AeTypeHtmlElement``: ' + Qgis.AttributeEditorType.HtmlElement.__doc__ + '\n' + '* ``AeTypeAction``: ' + Qgis.AttributeEditorType.Action.__doc__ + '\n' + '* ``AeTypeTextElement``: ' + Qgis.AttributeEditorType.TextElement.__doc__ + '\n' + '* ``AeTypeSpacerElement``: ' + Qgis.AttributeEditorType.SpacerElement.__doc__ + '\n' + '* ``AeTypeInvalid``: ' + Qgis.AttributeEditorType.Invalid.__doc__
# --
Qgis.AttributeEditorType.baseClass = Qgis
# monkey patching scoped based enum
Qgis.AttributeEditorContainerType.GroupBox.__doc__ = "A group box"
Qgis.AttributeEditorContainerType.Tab.__doc__ = "A tab widget"
Qgis.AttributeEditorContainerType.__doc__ = 'Attribute editor container types.\n\n.. versionadded:: 3.32\n\n' + '* ``GroupBox``: ' + Qgis.AttributeEditorContainerType.GroupBox.__doc__ + '\n' + '* ``Tab``: ' + Qgis.AttributeEditorContainerType.Tab.__doc__
# --
Qgis.AttributeEditorContainerType.baseClass = Qgis
QgsEditFormConfig.EditorLayout = Qgis.AttributeFormLayout
# monkey patching scoped based enum
QgsEditFormConfig.GeneratedLayout = Qgis.AttributeFormLayout.AutoGenerated
Expand Down
Expand Up @@ -28,7 +28,6 @@ Creates a new attribute editor container
:param backgroundColor: The optional background color of the container.
%End


~QgsAttributeEditorContainer();

virtual void addChildElement( QgsAttributeEditorElement *element /Transfer/ );
Expand All @@ -38,23 +37,47 @@ Add a child element to this container. This may be another container, a field or
:param element: The element to add as child
%End

virtual void setIsGroupBox( bool isGroupBox );
void setType( Qgis::AttributeEditorContainerType type );
%Docstring
Sets the container type.

.. seealso:: :py:func:`type`

.. versionadded:: 3.32
%End

Qgis::AttributeEditorContainerType type() const;
%Docstring
Returns the container type.

.. seealso:: :py:func:`setType`

.. versionadded:: 3.32
%End

virtual void setIsGroupBox( bool isGroupBox ) /Deprecated/;
%Docstring
Determines if this container is rendered as collapsible group box or tab in a tabwidget

:param isGroupBox: If ``True``, this will be a group box

.. deprecated::
use :py:func:`~QgsAttributeEditorContainer.setType` instead.
%End

virtual bool isGroupBox() const;
virtual bool isGroupBox() const /Deprecated/;
%Docstring
Returns if this container is going to be a group box

:return: ``True`` if it will be a group box, ``False`` if it will be a tab

.. deprecated::
Use :py:func:`~QgsAttributeEditorContainer.type` instead.
%End

bool collapsed() const;
%Docstring
For group box containers returns if this group box is collapsed.
For group box containers returns ``True`` if this group box is collapsed.

:return: ``True`` if the group box is collapsed, ``False`` otherwise.

Expand All @@ -66,7 +89,7 @@ For group box containers returns if this group box is collapsed.
%End
void setCollapsed( bool collapsed );
%Docstring
For group box containers sets if this group box is ``collapsed``.
For group box containers sets if this group box is ``collapsed``.

.. seealso:: :py:func:`collapsed`

Expand Down Expand Up @@ -97,17 +120,21 @@ Clear all children from this container.

void setName( const QString &name );
%Docstring
Change the name of this container
Change the name of this container.
%End

int columnCount() const;
%Docstring
Gets the number of columns in this group
Gets the number of columns in this group.

.. seealso:: :py:func:`setColumnCount`
%End

void setColumnCount( int columnCount );
%Docstring
Set the number of columns in this group
Set the number of columns in this group.

.. seealso:: :py:func:`columnCount`
%End

virtual QgsAttributeEditorElement *clone( QgsAttributeEditorElement *parent ) const /Factory/;
Expand Down Expand Up @@ -140,16 +167,18 @@ the field value controlled by editor widgets.

QColor backgroundColor() const;
%Docstring
backgroundColor
Returns the background color of the container.

:return: background color of the container
.. seealso:: :py:func:`setBackgroundColor`

.. versionadded:: 3.8
%End

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

.. seealso:: :py:func:`backgroundColor`
%End

};
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -1992,6 +1992,12 @@ The development version
Invalid,
};

enum class AttributeEditorContainerType
{
GroupBox,
Tab,
};

enum class AttributeFormLayout
{
AutoGenerated,
Expand Down
33 changes: 27 additions & 6 deletions src/core/editform/qgsattributeeditorcontainer.cpp
Expand Up @@ -27,6 +27,19 @@ void QgsAttributeEditorContainer::addChildElement( QgsAttributeEditorElement *wi
mChildren.append( widget );
}

void QgsAttributeEditorContainer::setIsGroupBox( bool isGroupBox )
{
if ( isGroupBox )
setType( Qgis::AttributeEditorContainerType::GroupBox );
else
setType( Qgis::AttributeEditorContainerType::Tab );
}

bool QgsAttributeEditorContainer::isGroupBox() const
{
return mType == Qgis::AttributeEditorContainerType::GroupBox;
}

void QgsAttributeEditorContainer::setName( const QString &name )
{
mName = name;
Expand Down Expand Up @@ -117,7 +130,7 @@ QgsAttributeEditorElement *QgsAttributeEditorContainer::clone( QgsAttributeEdito
{
element->addChildElement( child->clone( element ) );
}
element->mIsGroupBox = mIsGroupBox;
element->mType = mType;
element->mColumnCount = mColumnCount;
element->mVisibilityExpression = mVisibilityExpression;
element->mCollapsed = mCollapsed;
Expand All @@ -131,7 +144,8 @@ void QgsAttributeEditorContainer::saveConfiguration( QDomElement &elem, QDomDocu
{
Q_UNUSED( doc )
elem.setAttribute( QStringLiteral( "columnCount" ), mColumnCount );
elem.setAttribute( QStringLiteral( "groupBox" ), mIsGroupBox ? 1 : 0 );
elem.setAttribute( QStringLiteral( "groupBox" ), mType == Qgis::AttributeEditorContainerType::GroupBox ? 1 : 0 );
elem.setAttribute( QStringLiteral( "type" ), qgsEnumValueToKey( mType ) );
elem.setAttribute( QStringLiteral( "collapsed" ), mCollapsed );
elem.setAttribute( QStringLiteral( "collapsedExpressionEnabled" ), mCollapsedExpression.enabled() ? 1 : 0 );
elem.setAttribute( QStringLiteral( "collapsedExpression" ), mCollapsedExpression->expression() );
Expand All @@ -156,11 +170,18 @@ void QgsAttributeEditorContainer::loadConfiguration( const QDomElement &element,
cc = 0;
setColumnCount( cc );

const bool isGroupBox = element.attribute( QStringLiteral( "groupBox" ) ).toInt( &ok );
if ( ok )
setIsGroupBox( isGroupBox );
if ( element.hasAttribute( QStringLiteral( "type" ) ) )
{
mType = qgsEnumKeyToValue( element.attribute( QStringLiteral( "type" ) ), Qgis::AttributeEditorContainerType::GroupBox );
}
else
setIsGroupBox( mParent );
{
const bool isGroupBox = element.attribute( QStringLiteral( "groupBox" ) ).toInt( &ok );
if ( ok )
setType( isGroupBox ? Qgis::AttributeEditorContainerType::GroupBox : Qgis::AttributeEditorContainerType::Tab );
else
setType( mParent ? Qgis::AttributeEditorContainerType::GroupBox : Qgis::AttributeEditorContainerType::Tab );
}

const bool isCollapsed = element.attribute( QStringLiteral( "collapsed" ) ).toInt( &ok );
if ( ok )
Expand Down
53 changes: 38 additions & 15 deletions src/core/editform/qgsattributeeditorcontainer.h
Expand Up @@ -38,12 +38,9 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
*/
QgsAttributeEditorContainer( const QString &name, QgsAttributeEditorElement *parent, const QColor &backgroundColor = QColor() )
: QgsAttributeEditorElement( Qgis::AttributeEditorType::Container, name, parent )
, mIsGroupBox( true )
, mColumnCount( 1 )
, mBackgroundColor( backgroundColor )
{}


~QgsAttributeEditorContainer() override;

/**
Expand All @@ -53,22 +50,41 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
*/
virtual void addChildElement( QgsAttributeEditorElement *element SIP_TRANSFER );

/**
* Sets the container type.
*
* \see type()
* \since QGIS 3.32
*/
void setType( Qgis::AttributeEditorContainerType type ) { mType = type; }

/**
* Returns the container type.
*
* \see setType()
* \since QGIS 3.32
*/
Qgis::AttributeEditorContainerType type() const { return mType; }

/**
* Determines if this container is rendered as collapsible group box or tab in a tabwidget
*
* \param isGroupBox If TRUE, this will be a group box
* \deprecated use setType() instead.
*/
virtual void setIsGroupBox( bool isGroupBox ) { mIsGroupBox = isGroupBox; }
Q_DECL_DEPRECATED virtual void setIsGroupBox( bool isGroupBox ) SIP_DEPRECATED;

/**
* Returns if this container is going to be a group box
*
* \returns TRUE if it will be a group box, FALSE if it will be a tab
*
* \deprecated Use type() instead.
*/
virtual bool isGroupBox() const { return mIsGroupBox; }
Q_DECL_DEPRECATED virtual bool isGroupBox() const SIP_DEPRECATED;

/**
* For group box containers returns if this group box is collapsed.
* For group box containers returns TRUE if this group box is collapsed.
*
* \returns TRUE if the group box is collapsed, FALSE otherwise.
* \see collapsed()
Expand All @@ -78,7 +94,7 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
bool collapsed() const { return mCollapsed; };

/**
* For group box containers sets if this group box is \a collapsed.
* For group box containers sets if this group box is \a collapsed.
*
* \see collapsed()
* \see setCollapsed()
Expand Down Expand Up @@ -108,17 +124,21 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
void clear();

/**
* Change the name of this container
* Change the name of this container.
*/
void setName( const QString &name );

/**
* Gets the number of columns in this group
* Gets the number of columns in this group.
*
* \see setColumnCount()
*/
int columnCount() const;

/**
* Set the number of columns in this group
* Set the number of columns in this group.
*
* \see columnCount()
*/
void setColumnCount( int columnCount );

Expand Down Expand Up @@ -172,14 +192,17 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
void setCollapsedExpression( const QgsOptionalExpression &collapsedExpression ) SIP_SKIP;

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

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

Expand All @@ -188,9 +211,9 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
void loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields ) override;
QString typeIdentifier() const override;

bool mIsGroupBox;
Qgis::AttributeEditorContainerType mType = Qgis::AttributeEditorContainerType::GroupBox;
QList<QgsAttributeEditorElement *> mChildren;
int mColumnCount;
int mColumnCount = 1;
QgsOptionalExpression mVisibilityExpression;
QColor mBackgroundColor;
bool mCollapsed = false;
Expand Down
12 changes: 12 additions & 0 deletions src/core/qgis.h
Expand Up @@ -3449,6 +3449,18 @@ class CORE_EXPORT Qgis
};
Q_ENUM( AttributeEditorType )

/**
* Attribute editor container types.
*
* \since QGIS 3.32
*/
enum class AttributeEditorContainerType : int
{
GroupBox, //!< A group box
Tab, //!< A tab widget
};
Q_ENUM( AttributeEditorContainerType )

/**
* Available form types for layout of the attribute form editor.
*
Expand Down

0 comments on commit a434d0e

Please sign in to comment.