Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Alias expressions: use the DD override widget
  • Loading branch information
elpaso committed Apr 13, 2020
1 parent 47d6cb9 commit ed55b41
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 59 deletions.
24 changes: 21 additions & 3 deletions python/core/auto_generated/qgsattributeeditorelement.sip.in
Expand Up @@ -116,17 +116,35 @@ Controls if this element should be labeled with a title (field, relation or grou

QString labelExpression() const;
%Docstring
Returns the (possibly empty) label expression
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 for the field to ``labelExpression``.
If set to an empty string the field label will be taken from
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

Expand Down
26 changes: 24 additions & 2 deletions python/core/auto_generated/qgseditformconfig.sip.in
Expand Up @@ -193,15 +193,37 @@ Labeling on top leaves more horizontal space for the widget itself.

QString labelExpression( const QString &fieldName ) const;
%Docstring
Returns the (possibly empty) expression for the label of ``fieldName``, to be evaluated in the form context.
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 );
void setLabelExpression( const QString &fieldName, const QString &labelExpression, bool isActive );
%Docstring
Set the label expression for ``fieldName`` to ``labelExpression``, to be evaluated in the form context.

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

.. versionadded:: 3.14
%End

void setLabelExpressionIsActive( const QString &fieldName, bool isActive );
%Docstring
Set the label expression active state for ``fieldName`` to ``isActive``

.. versionadded:: 3.14
%End

Expand Down
10 changes: 10 additions & 0 deletions src/core/qgsattributeeditorelement.cpp
Expand Up @@ -140,6 +140,16 @@ void QgsAttributeEditorElement::setLabelExpression( const QString &labelExpressi
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
21 changes: 18 additions & 3 deletions src/core/qgsattributeeditorelement.h
Expand Up @@ -134,24 +134,39 @@ class CORE_EXPORT QgsAttributeEditorElement SIP_ABSTRACT
void setShowLabel( bool showLabel );

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

/**
* Sets the label expression for the field to \a labelExpression.
* If set to an empty string the field label will be taken from
* 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;
QgsAttributeEditorElement *mParent = nullptr;
bool mShowLabel;
#endif
Expand Down
27 changes: 22 additions & 5 deletions src/core/qgseditformconfig.cpp
Expand Up @@ -214,17 +214,28 @@ 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() );
return d->mLabelExpressions.value( fieldName, { QString(), false } ).first;
else
return QString();
}

void QgsEditFormConfig::setLabelExpression( const QString &fieldName, const QString &labelExpression )
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;
d->mLabelExpressions[ fieldName ] = { labelExpression, isActive };
}
}

Expand Down Expand Up @@ -404,7 +415,11 @@ void QgsEditFormConfig::readXml( const QDomNode &node, QgsReadWriteContext &cont
for ( int i = 0; i < labelExpressionsNodeList.size(); ++i )
{
QDomElement labelExpressionElement = labelExpressionsNodeList.at( i ).toElement();
d->mLabelExpressions.insert( labelExpressionElement.attribute( QStringLiteral( "name" ) ), labelExpressionElement.attribute( QStringLiteral( "labelExpression" ) ) );
d->mLabelExpressions.insert( labelExpressionElement.attribute( QStringLiteral( "name" ) ),
{
labelExpressionElement.attribute( QStringLiteral( "expression" ) ),
labelExpressionElement.attribute( QStringLiteral( "active" ) ).toInt( ) == 1
} );
}

QDomNodeList widgetsNodeList = node.namedItem( QStringLiteral( "widgets" ) ).toElement().childNodes();
Expand Down Expand Up @@ -535,7 +550,8 @@ void QgsEditFormConfig::writeXml( QDomNode &node, const QgsReadWriteContext &con
{
QDomElement fieldElem = doc.createElement( QStringLiteral( "field" ) );
fieldElem.setAttribute( QStringLiteral( "name" ), labelExpressionsIt.key() );
fieldElem.setAttribute( QStringLiteral( "labelExpression" ), labelExpressionsIt.value() );
fieldElem.setAttribute( QStringLiteral( "expression" ), labelExpressionsIt.value().first );
fieldElem.setAttribute( QStringLiteral( "active" ), labelExpressionsIt.value().second ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
labelExpressionElem.appendChild( fieldElem );
}
node.appendChild( labelExpressionElem );
Expand Down Expand Up @@ -640,6 +656,7 @@ QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomEleme
else
newElement->setShowLabel( true );
newElement->setLabelExpression( labelExpression( newElement->name() ) );
newElement->setLabelExpressionIsActive( labelExpressionIsActive( newElement->name() ) );
}

return newElement;
Expand Down
19 changes: 17 additions & 2 deletions src/core/qgseditformconfig.h
Expand Up @@ -220,16 +220,31 @@ class CORE_EXPORT QgsEditFormConfig
void setLabelOnTop( int idx, bool onTop );

/**
* Returns the (possibly empty) expression for the label of \a fieldName, to be evaluated in the form context.
* Returns the (possibly empty or inactive) expression for the label of \a fieldName, to be evaluated in the form context.
* \note The returned expression might not be active.
* \see labelExpressionIsActive()
* \since QGIS 3.14
*/
QString labelExpression( const QString &fieldName ) const;

/**
* Returns true if the label expression for the label of \a fieldName is active and it is not empty.
* \since QGIS 3.14
*/
bool labelExpressionIsActive( const QString &fieldName ) const;

/**
* Set the label expression for \a fieldName to \a labelExpression, to be evaluated in the form context.
* \see setLabelExpressionIsActive() to control its status
* \since QGIS 3.14
*/
void setLabelExpression( const QString &fieldName, const QString &labelExpression, bool isActive );

/**
* Set the label expression active state for \a fieldName to \a isActive
* \since QGIS 3.14
*/
void setLabelExpression( const QString &fieldName, const QString &labelExpression );
void setLabelExpressionIsActive( const QString &fieldName, bool isActive );

// Python form init function stuff

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgseditformconfig_p.h
Expand Up @@ -60,7 +60,7 @@ class QgsEditFormConfigPrivate : public QSharedData

QMap< QString, bool> mFieldEditables;
QMap< QString, bool> mLabelOnTop;
QMap< QString, QString> mLabelExpressions;
QMap< QString, QPair<QString, bool>> mLabelExpressions;

QMap<QString, QVariantMap > mWidgetConfigs;

Expand Down
21 changes: 16 additions & 5 deletions src/gui/attributeformconfig/qgsattributetypedialog.cpp
Expand Up @@ -73,8 +73,12 @@ QgsAttributeTypeDialog::QgsAttributeTypeDialog( QgsVectorLayer *vl, int fieldIdx
mExpressionWidget->registerExpressionContextGenerator( this );
mExpressionWidget->setLayer( mLayer );

mAliasExpression->registerExpressionContextGenerator( this );
mAliasExpression->setLayer( mLayer );
mAliasExpressionProperty = QgsProperty::fromExpression( QString() );
mAliasExpressionButton->registerExpressionContextGenerator( this );
connect( mAliasExpressionButton, &QgsPropertyOverrideButton::changed, this, [ = ]
{
mAliasExpressionProperty = mAliasExpressionButton->toProperty();
} );

connect( mExpressionWidget, &QgsExpressionLineEdit::expressionChanged, this, &QgsAttributeTypeDialog::defaultExpressionChanged );
connect( mUniqueCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
Expand Down Expand Up @@ -358,14 +362,21 @@ QString QgsAttributeTypeDialog::alias() const
return mAlias->text();
}

void QgsAttributeTypeDialog::setAliasExpression( const QString &aliasExpression )
void QgsAttributeTypeDialog::setAliasExpression( const QString &aliasExpression, bool isActive )
{
mAliasExpression->setExpression( aliasExpression );
mAliasExpressionProperty.setExpressionString( aliasExpression );
mAliasExpressionProperty.setActive( isActive );
mAliasExpressionButton->setToProperty( mAliasExpressionProperty );
}

QString QgsAttributeTypeDialog::aliasExpression() const
{
return mAliasExpression->expression();
return mAliasExpressionProperty.asExpression();
}

bool QgsAttributeTypeDialog::aliasExpressionIsActive() const
{
return mAliasExpressionProperty.isActive();
}

void QgsAttributeTypeDialog::setComment( const QString &comment )
Expand Down
12 changes: 10 additions & 2 deletions src/gui/attributeformconfig/qgsattributetypedialog.h
Expand Up @@ -77,17 +77,23 @@ class GUI_EXPORT QgsAttributeTypeDialog: public QWidget, private Ui::QgsAttribut
QString alias() const;

/**
* Sets expression for for label alias
* Sets expression for the label alias override to \a aliasExpression and its active state to \a isActive.
* \since QGIS 3.14
*/
void setAliasExpression( const QString &aliasExpression );
void setAliasExpression( const QString &aliasExpression, bool isActive );

/**
* Returns the expression for the label alias
* \since QGIS 3.14
*/
QString aliasExpression() const;

/**
* Returns TRUE if the expression override for the label alias is active.
* \since QGIS 3.14
*/
bool aliasExpressionIsActive() const;

/**
* Setter for label comment
*/
Expand Down Expand Up @@ -234,6 +240,8 @@ class GUI_EXPORT QgsAttributeTypeDialog: public QWidget, private Ui::QgsAttribut
//! Cached configuration dialog (lazy loaded)
QMap< QString, QgsEditorConfigWidget * > mEditorConfigWidgets;

QgsProperty mAliasExpressionProperty;

QStandardItem *currentItem() const;

QgsFeature mPreviewFeature;
Expand Down
6 changes: 4 additions & 2 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -1474,7 +1474,7 @@ void QgsAttributeForm::init()
layout->addWidget( widgetInfo.widget, row, column++ );
}

if ( ! widgetInfo.labelExpression.isEmpty() )
if ( ! widgetInfo.labelExpression.isEmpty() && widgetInfo.labelExpressionIsActive )
{
mExpressionLabels[ label ] = QgsExpression( widgetInfo.labelExpression );
}
Expand Down Expand Up @@ -1532,6 +1532,7 @@ void QgsAttributeForm::init()
labelText.replace( '&', QStringLiteral( "&&" ) ); // need to escape '&' or they'll be replace by _ in the label text

const QString labelExpression { mLayer->editFormConfig().labelExpression( field.name() ) };
const bool labelExpressionIsActive { mLayer->editFormConfig().labelExpressionIsActive( field.name() ) };
const QgsEditorWidgetSetup widgetSetup = QgsGui::editorWidgetRegistry()->findBest( mLayer, field.name() );

if ( widgetSetup.type() == QLatin1String( "Hidden" ) )
Expand All @@ -1545,7 +1546,7 @@ void QgsAttributeForm::init()
QSvgWidget *i = new QSvgWidget();
i->setFixedSize( 18, 18 );

if ( ! labelExpression.isEmpty() )
if ( ! labelExpression.isEmpty() && labelExpressionIsActive )
{
mExpressionLabels[ label ] = QgsExpression( labelExpression );
}
Expand Down Expand Up @@ -2089,6 +2090,7 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt

newWidgetInfo.showLabel = widgetDef->showLabel();
newWidgetInfo.labelExpression = widgetDef->labelExpression();
newWidgetInfo.labelExpressionIsActive = widgetDef->labelExpressionIsActive();

return newWidgetInfo;
}
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsattributeform.h
Expand Up @@ -352,6 +352,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
QWidget *widget = nullptr;
QString labelText;
QString labelExpression;
bool labelExpressionIsActive;
QString toolTip;
QString hint;
bool labelOnTop = false;
Expand Down

0 comments on commit ed55b41

Please sign in to comment.