Skip to content

Commit

Permalink
Form alias data defined: cleanup and simplify
Browse files Browse the repository at this point in the history
Moved storage to QgsEditFormConfig
  • Loading branch information
elpaso committed Apr 14, 2020
1 parent 3d43a91 commit f2f7244
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 237 deletions.
36 changes: 1 addition & 35 deletions python/core/auto_generated/qgsattributeeditorelement.sip.in
Expand Up @@ -15,7 +15,7 @@ class QgsAttributeEditorElement /Abstract/
This is an abstract base class for any elements of a drag and drop form.

This can either be a container which will be represented on the screen
as a tab widget or ca collapsible group box. Or it can be a field which will
as a tab widget or a collapsible group box. Or it can be a field which will
then be represented based on the QgsEditorWidget type and configuration.
Or it can be a relation and embed the form of several children of another
layer.
Expand Down Expand Up @@ -112,40 +112,6 @@ Controls if this element should be labeled with a title (field, relation or grou
Controls if this element should be labeled with a title (field, relation or groupname).

.. versionadded:: 2.18
%End

QString labelExpression() const;
%Docstring
Returns the (possibly empty or inactive) label expression.

.. seealso:: :py:func:`labelExpressionIsActive`

.. versionadded:: 3.14
%End

void setLabelExpression( const QString &labelExpression );
%Docstring
Sets the label expression override for the field to ``labelExpression``.
If the override is not active or it is set to an empty string the field label will be taken from
the label alias if set or from the field name otherwise.

.. seealso:: :py:func:`setLabelExpressionIsActive`

.. versionadded:: 3.14
%End

bool labelExpressionIsActive() const;
%Docstring
Returns the status of the label expression override.

.. versionadded:: 3.14
%End

void setLabelExpressionIsActive( bool labelExpressionIsActive );
%Docstring
Sets the status of a label expression override to ``labelExpressionIsActive``.

.. versionadded:: 3.14
%End

protected:
Expand Down
56 changes: 29 additions & 27 deletions python/core/auto_generated/qgseditformconfig.sip.in
Expand Up @@ -67,6 +67,13 @@ Constructor for TabData
CodeSourceEnvironment
};

enum DataDefinedProperty
{
NoProperty,
AllProperties,
Alias,
};

QgsEditFormConfig( const QgsEditFormConfig &o );
%Docstring
Copy constructor
Expand Down Expand Up @@ -191,33 +198,6 @@ on the left hand side.
Labeling on top leaves more horizontal space for the widget itself.
%End

QString labelExpression( const QString &fieldName ) const;
%Docstring
Returns the (possibly empty or inactive) expression for the label of ``fieldName``, to be evaluated in the form context.

.. note::

The returned expression might not be active.

.. seealso:: :py:func:`labelExpressionIsActive`

.. versionadded:: 3.14
%End

bool labelExpressionIsActive( const QString &fieldName ) const;
%Docstring
Returns true if the label expression for the label of ``fieldName`` is active and it is not empty.

.. versionadded:: 3.14
%End

void setLabelExpression( const QString &fieldName, const QString &labelExpression, bool isActive );
%Docstring
Set the label expression for ``fieldName`` to ``labelExpression`` and the active state to ``isActive``,
to be evaluated in the form context.

.. versionadded:: 3.14
%End


QString initFunction() const;
Expand Down Expand Up @@ -302,6 +282,28 @@ Deserialize drag and drop designer elements.
explicit QgsEditFormConfig();
%Docstring
Create a new edit form config. Normally invoked by :py:class:`QgsVectorLayer`
%End

void setDataDefinedFieldProperties( const QString &fieldName, const QgsPropertyCollection &properties );
%Docstring
Set data defined properties for ``fieldName`` to ``properties``

.. versionadded:: 4.14
%End

QgsPropertyCollection dataDefinedFieldProperties( const QString &fieldName ) const;
%Docstring
Returns data defined properties for ``fieldName``

.. versionadded:: 4.14
%End


static const QgsPropertiesDefinition &propertyDefinitions();
%Docstring
Returns data defined property definitions.

.. versionadded:: 4.14
%End

};
Expand Down
22 changes: 1 addition & 21 deletions src/core/qgsattributeeditorelement.cpp
Expand Up @@ -16,6 +16,7 @@
#include "qgsattributeeditorelement.h"
#include "qgsrelationmanager.h"


void QgsAttributeEditorContainer::addChildElement( QgsAttributeEditorElement *widget )
{
mChildren.append( widget );
Expand Down Expand Up @@ -115,7 +116,6 @@ QDomElement QgsAttributeEditorElement::toDomElement( QDomDocument &doc ) const
QDomElement elem = doc.createElement( typeIdentifier() );
elem.setAttribute( QStringLiteral( "name" ), mName );
elem.setAttribute( QStringLiteral( "showLabel" ), mShowLabel );

saveConfiguration( elem );
return elem;
}
Expand All @@ -130,26 +130,6 @@ void QgsAttributeEditorElement::setShowLabel( bool showLabel )
mShowLabel = showLabel;
}

QString QgsAttributeEditorElement::labelExpression() const
{
return mLabelExpression;
}

void QgsAttributeEditorElement::setLabelExpression( const QString &labelExpression )
{
mLabelExpression = labelExpression;
}

bool QgsAttributeEditorElement::labelExpressionIsActive() const
{
return mLabelExpressionIsActive;
}

void QgsAttributeEditorElement::setLabelExpressionIsActive( bool labelExpressionIsActive )
{
mLabelExpressionIsActive = labelExpressionIsActive;
}

void QgsAttributeEditorRelation::saveConfiguration( QDomElement &elem ) const
{
elem.setAttribute( QStringLiteral( "relation" ), mRelation.id() );
Expand Down
35 changes: 4 additions & 31 deletions src/core/qgsattributeeditorelement.h
Expand Up @@ -19,6 +19,7 @@
#include "qgis_core.h"
#include "qgsrelation.h"
#include "qgsoptionalexpression.h"
#include "qgspropertycollection.h"
#include <QColor>

class QgsRelationManager;
Expand All @@ -28,7 +29,7 @@ class QgsRelationManager;
* This is an abstract base class for any elements of a drag and drop form.
*
* This can either be a container which will be represented on the screen
* as a tab widget or ca collapsible group box. Or it can be a field which will
* as a tab widget or a collapsible group box. Or it can be a field which will
* then be represented based on the QgsEditorWidget type and configuration.
* Or it can be a relation and embed the form of several children of another
* layer.
Expand Down Expand Up @@ -133,40 +134,11 @@ class CORE_EXPORT QgsAttributeEditorElement SIP_ABSTRACT
*/
void setShowLabel( bool showLabel );

/**
* Returns the (possibly empty or inactive) label expression.
* \see labelExpressionIsActive()
* \since QGIS 3.14
*/
QString labelExpression() const;

/**
* Sets the label expression override for the field to \a labelExpression.
* If the override is not active or it is set to an empty string the field label will be taken from
* the label alias if set or from the field name otherwise.
* \see setLabelExpressionIsActive
* \since QGIS 3.14
*/
void setLabelExpression( const QString &labelExpression );

/**
* Returns the status of the label expression override.
* \since QGIS 3.14
*/
bool labelExpressionIsActive() const;

/**
* Sets the status of a label expression override to \a labelExpressionIsActive.
* \since QGIS 3.14
*/
void setLabelExpressionIsActive( bool labelExpressionIsActive );

protected:
#ifndef SIP_RUN
AttributeEditorType mType;
QString mName;
QString mLabelExpression;
bool mLabelExpressionIsActive;
//QgsPropertyCollection mDataDefinedProperties;
QgsAttributeEditorElement *mParent = nullptr;
bool mShowLabel;
#endif
Expand All @@ -187,6 +159,7 @@ class CORE_EXPORT QgsAttributeEditorElement SIP_ABSTRACT
* \since QGIS 2.18
*/
virtual QString typeIdentifier() const = 0;

};


Expand Down
80 changes: 31 additions & 49 deletions src/core/qgseditformconfig.cpp
Expand Up @@ -23,8 +23,6 @@
#include "qgsxmlutils.h"
#include "qgsapplication.h"

//#include "qgseditorwidgetregistry.h"

QgsAttributeEditorContainer::~QgsAttributeEditorContainer()
{
qDeleteAll( mChildren );
Expand All @@ -35,6 +33,22 @@ QgsEditFormConfig::QgsEditFormConfig()
{
}

void QgsEditFormConfig::setDataDefinedFieldProperties( const QString &fieldName, const QgsPropertyCollection &properties )
{
d.detach();
d->mDataDefinedFieldProperties[ fieldName ] = properties;
}

QgsPropertyCollection QgsEditFormConfig::dataDefinedFieldProperties( const QString &fieldName ) const
{
return d->mDataDefinedFieldProperties.value( fieldName );
}

const QgsPropertiesDefinition &QgsEditFormConfig::propertyDefinitions()
{
return QgsEditFormConfigPrivate::propertyDefinitions();
}

QVariantMap QgsEditFormConfig::widgetConfig( const QString &widgetName ) const
{
int fieldIndex = d->mFields.indexOf( widgetName );
Expand Down Expand Up @@ -211,34 +225,6 @@ void QgsEditFormConfig::setLabelOnTop( int idx, bool onTop )
}
}

QString QgsEditFormConfig::labelExpression( const QString &fieldName ) const
{
if ( d->mFields.indexOf( fieldName ) != -1 )
return d->mLabelExpressions.value( fieldName, { QString(), false } ).first;
else
return QString();
}

bool QgsEditFormConfig::labelExpressionIsActive( const QString &fieldName ) const
{
if ( d->mFields.indexOf( fieldName ) != -1 )
{
const auto exp { d->mLabelExpressions.value( fieldName, { QString(), false } ) };
return exp.second && ! exp.first.isEmpty();
}
else
return false;
}

void QgsEditFormConfig::setLabelExpression( const QString &fieldName, const QString &labelExpression, bool isActive )
{
if ( d->mFields.indexOf( fieldName ) != -1 )
{
d.detach();
d->mLabelExpressions[ fieldName ] = { labelExpression, isActive };
}
}

QString QgsEditFormConfig::initFunction() const
{
return d->mInitFunction;
Expand Down Expand Up @@ -410,16 +396,14 @@ void QgsEditFormConfig::readXml( const QDomNode &node, QgsReadWriteContext &cont
d->mLabelOnTop.insert( labelOnTopElement.attribute( QStringLiteral( "name" ) ), static_cast< bool >( labelOnTopElement.attribute( QStringLiteral( "labelOnTop" ) ).toInt() ) );
}

d->mLabelExpressions.clear();
QDomNodeList labelExpressionsNodeList = node.namedItem( QStringLiteral( "labelExpression" ) ).toElement().childNodes();
for ( int i = 0; i < labelExpressionsNodeList.size(); ++i )
// Read data defined field properties
QDomNodeList fieldDDPropertiesNodeList = node.namedItem( QStringLiteral( "dataDefinedFieldProperties" ) ).toElement().childNodes();
for ( int i = 0; i < fieldDDPropertiesNodeList.size(); ++i )
{
QDomElement labelExpressionElement = labelExpressionsNodeList.at( i ).toElement();
d->mLabelExpressions.insert( labelExpressionElement.attribute( QStringLiteral( "name" ) ),
{
labelExpressionElement.attribute( QStringLiteral( "expression" ) ),
labelExpressionElement.attribute( QStringLiteral( "active" ) ).toInt( ) == 1
} );
QDomElement DDElement = fieldDDPropertiesNodeList.at( i ).toElement();
QgsPropertyCollection collection;
collection.readXml( DDElement, propertyDefinitions() );
d->mDataDefinedFieldProperties.insert( DDElement.attribute( QStringLiteral( "name" ) ), collection );
}

QDomNodeList widgetsNodeList = node.namedItem( QStringLiteral( "widgets" ) ).toElement().childNodes();
Expand Down Expand Up @@ -545,16 +529,16 @@ void QgsEditFormConfig::writeXml( QDomNode &node, const QgsReadWriteContext &con
}
node.appendChild( labelOnTopElem );

QDomElement labelExpressionElem = doc.createElement( QStringLiteral( "labelExpression" ) );
for ( auto labelExpressionsIt = d->mLabelExpressions.constBegin(); labelExpressionsIt != d->mLabelExpressions.constEnd(); ++labelExpressionsIt )
// Store data defined field properties
QDomElement ddFieldPropsElement = doc.createElement( QStringLiteral( "dataDefinedFieldProperties" ) );
for ( auto it = d->mDataDefinedFieldProperties.constBegin(); it != d->mDataDefinedFieldProperties.constEnd(); ++it )
{
QDomElement fieldElem = doc.createElement( QStringLiteral( "field" ) );
fieldElem.setAttribute( QStringLiteral( "name" ), labelExpressionsIt.key() );
fieldElem.setAttribute( QStringLiteral( "expression" ), labelExpressionsIt.value().first );
fieldElem.setAttribute( QStringLiteral( "active" ), labelExpressionsIt.value().second ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
labelExpressionElem.appendChild( fieldElem );
QDomElement ddPropsElement = doc.createElement( QStringLiteral( "field" ) );
ddPropsElement.setAttribute( QStringLiteral( "name" ), it.key() );
it.value().writeXml( ddPropsElement, propertyDefinitions() );
ddFieldPropsElement.appendChild( ddPropsElement );
}
node.appendChild( labelExpressionElem );
node.appendChild( ddFieldPropsElement );

QDomElement widgetsElem = doc.createElement( QStringLiteral( "widgets" ) );

Expand Down Expand Up @@ -655,8 +639,6 @@ QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomEleme
newElement->setShowLabel( elem.attribute( QStringLiteral( "showLabel" ) ).toInt() );
else
newElement->setShowLabel( true );
newElement->setLabelExpression( labelExpression( newElement->name() ) );
newElement->setLabelExpressionIsActive( labelExpressionIsActive( newElement->name() ) );
}

return newElement;
Expand Down

0 comments on commit f2f7244

Please sign in to comment.