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
  • Loading branch information
nyalldawson committed Jun 17, 2020
1 parent 1e72b44 commit 3dd267c91af6b43a644f9c38f9307618f58dcced
Showing with 28 additions and 1 deletion.
  1. +1 −1 src/gui/vector/qgsvectorlayerproperties.cpp
  2. +27 −0 tests/src/gui/testqgsfieldexpressionwidget.cpp
@@ -637,7 +637,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 3dd267c

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