Skip to content

Commit

Permalink
Quote fields in size/width assistant (fix #14257)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 24, 2016
1 parent cd145bb commit d11e667
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
7 changes: 7 additions & 0 deletions python/gui/qgsfieldexpressionwidget.sip
Expand Up @@ -46,6 +46,13 @@ class QgsFieldExpressionWidget : QWidget
*/
QString currentText() const;

/** Returns the currently selected field or expression. If a field is currently selected, the returned
* value will be converted to a valid expression referencing this field (ie enclosing the field name with
* appropriate quotations).
* @note added in QGIS 2.14
*/
QString asExpression() const;

//! Returns the currently used layer
QgsVectorLayer* layer() const;

Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsfieldexpressionwidget.cpp
Expand Up @@ -103,6 +103,11 @@ QString QgsFieldExpressionWidget::currentText() const
return mCombo->currentText();
}

QString QgsFieldExpressionWidget::asExpression() const
{
return isExpression() ? currentText() : QgsExpression::quotedColumnRef( currentText() );
}

bool QgsFieldExpressionWidget::isValidExpression( QString *expressionError ) const
{
QString temp;
Expand Down
7 changes: 7 additions & 0 deletions src/gui/qgsfieldexpressionwidget.h
Expand Up @@ -89,6 +89,13 @@ class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
*/
QString currentText() const;

/** Returns the currently selected field or expression. If a field is currently selected, the returned
* value will be converted to a valid expression referencing this field (ie enclosing the field name with
* appropriate quotations).
* @note added in QGIS 2.14
*/
QString asExpression() const;

//! Returns the currently used layer
QgsVectorLayer* layer() const;

Expand Down
2 changes: 1 addition & 1 deletion src/gui/symbology-ng/qgssizescalewidget.cpp
Expand Up @@ -222,7 +222,7 @@ void QgsSizeScaleWidget::showEvent( QShowEvent* )
QgsScaleExpression *QgsSizeScaleWidget::createExpression() const
{
return new QgsScaleExpression( QgsScaleExpression::Type( scaleMethodComboBox->itemData( scaleMethodComboBox->currentIndex() ).toInt() ),
mExpressionWidget->currentField(),
mExpressionWidget->asExpression(),
minValueSpinBox->value(),
maxValueSpinBox->value(),
minSizeSpinBox->value(),
Expand Down
28 changes: 28 additions & 0 deletions tests/src/gui/testqgsfieldexpressionwidget.cpp
Expand Up @@ -48,6 +48,7 @@ class TestQgsFieldExpressionWidget : public QObject
void cleanup(); // will be called after every testfunction.

void testRemoveJoin();
void asExpression();

private:
QgsFieldExpressionWidget* mWidget;
Expand Down Expand Up @@ -133,6 +134,33 @@ void TestQgsFieldExpressionWidget::testRemoveJoin()
// QVERIFY( !isValid ); TODO: the expression should not be valid anymore since the field doesn't exist anymore. Maybe we need a new expression method to get more details.
}

void TestQgsFieldExpressionWidget::asExpression()
{
QgsVectorLayer* layer = new QgsVectorLayer( "point?field=fld:int&field=fld2:int&field=fld3:int", "x", "memory" );
QgsMapLayerRegistry::instance()->addMapLayer( layer );

QScopedPointer< QgsFieldExpressionWidget > widget( new QgsFieldExpressionWidget() );
widget->setLayer( layer );

// check with field set
widget->setField( "fld" );
QCOMPARE( widget->asExpression(), QString( "\"fld\"" ) );

// check with expressions set
widget->setField( "fld + 1" );
QCOMPARE( widget->asExpression(), QString( "fld + 1" ) );
widget->setField( "1" );
QCOMPARE( widget->asExpression(), QString( "1" ) );
widget->setField( "\"fld2\"" );
QCOMPARE( widget->asExpression(), QString( "\"fld2\"" ) );

// check switching back to a field
widget->setField( "fld3" );
QCOMPARE( widget->asExpression(), QString( "\"fld3\"" ) );

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


QTEST_MAIN( TestQgsFieldExpressionWidget )
#include "testqgsfieldexpressionwidget.moc"
Expand Down

0 comments on commit d11e667

Please sign in to comment.