Skip to content
Permalink
Browse files

Fix setting a layer's Display Name field to a field name containing

spaces or other special characters

Because we actually store the field name as an expression, we need
to make sure we correctly represent these field names as valid
expressions and not just the raw name string

(cherry picked from commit 5d28222)
  • Loading branch information
nyalldawson committed Jul 21, 2020
1 parent 08576cc commit f407d99c38275ba98cdc1bbff0fb713ffb5297a3
Showing with 28 additions and 1 deletion.
  1. +1 −1 src/app/qgsvectorlayerproperties.cpp
  2. +27 −0 tests/src/gui/testqgsfieldexpressionwidget.cpp
@@ -715,7 +715,7 @@ void QgsVectorLayerProperties::apply()
}
}

mLayer->setDisplayExpression( mDisplayExpressionWidget->currentField() );
mLayer->setDisplayExpression( mDisplayExpressionWidget->asExpression() );
mLayer->setMapTipTemplate( mMapTipWidget->text() );

mLayer->actions()->clearActions();
@@ -136,6 +136,8 @@ void TestQgsFieldExpressionWidget::testRemoveJoin()
void TestQgsFieldExpressionWidget::asExpression()
{
QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "point?field=fld:int&field=fld2:int&field=fld3:int" ), QStringLiteral( "x" ), QStringLiteral( "memory" ) );
layer->dataProvider()->addAttributes( QList< QgsField >() << QgsField( QStringLiteral( "a space" ), QVariant::String ) );
layer->updateFields();
QgsProject::instance()->addMapLayer( layer );

std::unique_ptr< QgsFieldExpressionWidget > widget( new QgsFieldExpressionWidget() );
@@ -196,6 +198,31 @@ void TestQgsFieldExpressionWidget::asExpression()
QVERIFY( spy2.constLast().at( 0 ).toString().isEmpty() );
QVERIFY( spy2.constLast().at( 1 ).toBool() );

// field name with space
widget->setField( QStringLiteral( "a space" ) );
QCOMPARE( widget->asExpression(), QStringLiteral( "\"a space\"" ) );
bool isExpression = true;
QCOMPARE( widget->currentField( &isExpression ), QStringLiteral( "a space" ) );
QVERIFY( !isExpression );
QCOMPARE( spy.count(), 7 );
QCOMPARE( spy.constLast().at( 0 ).toString(), QStringLiteral( "a space" ) );
QCOMPARE( spy2.count(), 7 );
QCOMPARE( spy2.constLast().at( 0 ).toString(), QStringLiteral( "a space" ) );
QVERIFY( spy2.constLast().at( 1 ).toBool() );

widget->setField( QString() );
QVERIFY( widget->asExpression().isEmpty() );
widget->setExpression( QStringLiteral( "\"a space\"" ) );
QCOMPARE( widget->asExpression(), QStringLiteral( "\"a space\"" ) );
isExpression = true;
QCOMPARE( widget->currentField( &isExpression ), QStringLiteral( "a space" ) );
QVERIFY( !isExpression );
QCOMPARE( spy.count(), 9 );
QCOMPARE( spy.constLast().at( 0 ).toString(), QStringLiteral( "a space" ) );
QCOMPARE( spy2.count(), 9 );
QCOMPARE( spy2.constLast().at( 0 ).toString(), QStringLiteral( "a space" ) );
QVERIFY( spy2.constLast().at( 1 ).toBool() );

QgsProject::instance()->removeMapLayer( layer );
}

0 comments on commit f407d99

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