Skip to content

Commit

Permalink
Label expressions: address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Apr 13, 2020
1 parent 7334ad0 commit 1219d61
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
4 changes: 3 additions & 1 deletion python/core/auto_generated/qgsattributeeditorelement.sip.in
Expand Up @@ -123,7 +123,9 @@ Returns the (possibly empty) label expression

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

.. versionadded:: 3.14
%End
Expand Down
8 changes: 4 additions & 4 deletions python/core/auto_generated/qgseditformconfig.sip.in
Expand Up @@ -191,16 +191,16 @@ on the left hand side.
Labeling on top leaves more horizontal space for the widget itself.
%End

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

.. versionadded:: 3.14
%End

void setLabelExpression( int idx, const QString &labelExpression );
void setLabelExpression( const QString &fieldName, const QString &labelExpression );
%Docstring
Set the label expression to ``labelExpression``, to be evaluated in the form context.
Set the label expression for ``fieldName`` to ``labelExpression``, to be evaluated in the form context.

.. versionadded:: 3.14
%End
Expand Down
5 changes: 3 additions & 2 deletions src/core/qgsattributeeditorelement.h
Expand Up @@ -129,7 +129,6 @@ class CORE_EXPORT QgsAttributeEditorElement SIP_ABSTRACT

/**
* Controls if this element should be labeled with a title (field, relation or groupname).
*
* \since QGIS 2.18
*/
void setShowLabel( bool showLabel );
Expand All @@ -141,7 +140,9 @@ class CORE_EXPORT QgsAttributeEditorElement SIP_ABSTRACT
QString labelExpression() const;

/**
* Sets the label expression to \a labelExpression
* Sets the label expression for the field to \a labelExpression.
* If set to an empty string the field label will be taken from
* the label alias if set or from the field name otherwise.
* \since QGIS 3.14
*/
void setLabelExpression( const QString &labelExpression );
Expand Down
12 changes: 6 additions & 6 deletions src/core/qgseditformconfig.cpp
Expand Up @@ -211,20 +211,20 @@ void QgsEditFormConfig::setLabelOnTop( int idx, bool onTop )
}
}

QString QgsEditFormConfig::labelExpression( int idx ) const
QString QgsEditFormConfig::labelExpression( const QString &fieldName ) const
{
if ( idx >= 0 && idx < d->mFields.count() )
return d->mLabelExpressions.value( d->mFields.at( idx ).name(), QString() );
if ( d->mFields.indexOf( fieldName ) != -1 )
return d->mLabelExpressions.value( fieldName, QString() );
else
return QString();
}

void QgsEditFormConfig::setLabelExpression( int idx, const QString &labelExpression )
void QgsEditFormConfig::setLabelExpression( const QString &fieldName, const QString &labelExpression )
{
if ( idx >= 0 && idx < d->mFields.count() )
if ( d->mFields.indexOf( fieldName ) != -1 )
{
d.detach();
d->mLabelExpressions[ d->mFields.at( idx ).name() ] = labelExpression;
d->mLabelExpressions[ fieldName ] = labelExpression;
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/core/qgseditformconfig.h
Expand Up @@ -208,28 +208,28 @@ class CORE_EXPORT QgsEditFormConfig
* If this returns TRUE, the widget at the given index will receive its label on the previous line
* while if it returns FALSE, the widget will receive its label on the left hand side.
* Labeling on top leaves more horizontal space for the widget itself.
**/
*/
bool labelOnTop( int idx ) const;

/**
* If this is set to TRUE, the widget at the given index will receive its label on
* the previous line while if it is set to FALSE, the widget will receive its label
* on the left hand side.
* Labeling on top leaves more horizontal space for the widget itself.
**/
*/
void setLabelOnTop( int idx, bool onTop );

/**
* Returns the (possibly empty) expression for the label, to be evaluated in the form context.
* Returns the (possibly empty) expression for the label of \a fieldName, to be evaluated in the form context.
* \since QGIS 3.14
**/
QString labelExpression( int idx ) const;
*/
QString labelExpression( const QString &fieldName ) const;

/**
* Set the label expression to \a labelExpression, to be evaluated in the form context.
* Set the label expression for \a fieldName to \a labelExpression, to be evaluated in the form context.
* \since QGIS 3.14
**/
void setLabelExpression( int idx, const QString &labelExpression );
*/
void setLabelExpression( const QString &fieldName, const QString &labelExpression );

// Python form init function stuff

Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsattributeform.cpp
Expand Up @@ -1529,7 +1529,7 @@ void QgsAttributeForm::init()
QString labelText = fieldName;
labelText.replace( '&', QStringLiteral( "&&" ) ); // need to escape '&' or they'll be replace by _ in the label text

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

if ( widgetSetup.type() == QLatin1String( "Hidden" ) )
Expand Down
7 changes: 4 additions & 3 deletions src/gui/vector/qgsattributesformproperties.cpp
Expand Up @@ -846,15 +846,16 @@ void QgsAttributesFormProperties::apply()
QTreeWidgetItem *fieldItem = fieldContainer->child( i );
FieldConfig cfg = fieldItem->data( 0, FieldConfigRole ).value<FieldConfig>();

int idx = mLayer->fields().indexOf( fieldItem->data( 0, FieldNameRole ).toString() );
const QString fieldName { fieldItem->data( 0, FieldNameRole ).toString() };
int idx = mLayer->fields().indexOf( fieldName );

//continue in case field does not exist anymore
if ( idx < 0 )
continue;

editFormConfig.setReadOnly( idx, !cfg.mEditable );
editFormConfig.setLabelOnTop( idx, cfg.mLabelOnTop );
editFormConfig.setLabelExpression( idx, cfg.mAliasExpression );
editFormConfig.setLabelExpression( fieldName, cfg.mAliasExpression );
mLayer->setEditorWidgetSetup( idx, QgsEditorWidgetSetup( cfg.mEditorWidgetType, cfg.mEditorWidgetConfig ) );

QgsFieldConstraints constraints = cfg.mFieldConstraints;
Expand Down Expand Up @@ -935,7 +936,7 @@ void QgsAttributesFormProperties::apply()
QgsAttributesFormProperties::FieldConfig::FieldConfig( QgsVectorLayer *layer, int idx )
{
mAlias = layer->fields().at( idx ).alias();
mAliasExpression = layer->editFormConfig().labelExpression( idx );
mAliasExpression = layer->editFormConfig().labelExpression( layer->fields().field( idx ).name() );
mComment = layer->fields().at( idx ).comment();
mEditable = !layer->editFormConfig().readOnly( idx );
mEditableEnabled = layer->fields().fieldOrigin( idx ) != QgsFields::OriginJoin
Expand Down

0 comments on commit 1219d61

Please sign in to comment.