Skip to content

Commit

Permalink
Fix setting a layer's Display Name field to a field name containing
Browse files Browse the repository at this point in the history
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 19, 2020
1 parent 1e72b44 commit 3dd267c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/gui/vector/qgsvectorlayerproperties.cpp
Expand Up @@ -637,7 +637,7 @@ void QgsVectorLayerProperties::apply()
}
}

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

mLayer->actions()->clearActions();
Expand Down
27 changes: 27 additions & 0 deletions tests/src/gui/testqgsfieldexpressionwidget.cpp
Expand Up @@ -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() );
Expand Down Expand Up @@ -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 );
}

Expand Down

0 comments on commit 3dd267c

Please sign in to comment.