From d11e66749b2c1b2f830de7ae8274d65b97878dac Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 24 Feb 2016 11:32:54 +1100 Subject: [PATCH] Quote fields in size/width assistant (fix #14257) --- python/gui/qgsfieldexpressionwidget.sip | 7 +++++ src/gui/qgsfieldexpressionwidget.cpp | 5 ++++ src/gui/qgsfieldexpressionwidget.h | 7 +++++ src/gui/symbology-ng/qgssizescalewidget.cpp | 2 +- .../src/gui/testqgsfieldexpressionwidget.cpp | 28 +++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/python/gui/qgsfieldexpressionwidget.sip b/python/gui/qgsfieldexpressionwidget.sip index 81de081165d..5644260c1f9 100644 --- a/python/gui/qgsfieldexpressionwidget.sip +++ b/python/gui/qgsfieldexpressionwidget.sip @@ -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; diff --git a/src/gui/qgsfieldexpressionwidget.cpp b/src/gui/qgsfieldexpressionwidget.cpp index 150de1c283f..4b35ec7815d 100644 --- a/src/gui/qgsfieldexpressionwidget.cpp +++ b/src/gui/qgsfieldexpressionwidget.cpp @@ -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; diff --git a/src/gui/qgsfieldexpressionwidget.h b/src/gui/qgsfieldexpressionwidget.h index 16e8c77d125..873110a627c 100644 --- a/src/gui/qgsfieldexpressionwidget.h +++ b/src/gui/qgsfieldexpressionwidget.h @@ -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; diff --git a/src/gui/symbology-ng/qgssizescalewidget.cpp b/src/gui/symbology-ng/qgssizescalewidget.cpp index cd6a65e34c4..12b92b3200f 100644 --- a/src/gui/symbology-ng/qgssizescalewidget.cpp +++ b/src/gui/symbology-ng/qgssizescalewidget.cpp @@ -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(), diff --git a/tests/src/gui/testqgsfieldexpressionwidget.cpp b/tests/src/gui/testqgsfieldexpressionwidget.cpp index 8305074485c..bdf78f53de2 100644 --- a/tests/src/gui/testqgsfieldexpressionwidget.cpp +++ b/tests/src/gui/testqgsfieldexpressionwidget.cpp @@ -48,6 +48,7 @@ class TestQgsFieldExpressionWidget : public QObject void cleanup(); // will be called after every testfunction. void testRemoveJoin(); + void asExpression(); private: QgsFieldExpressionWidget* mWidget; @@ -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"