Skip to content

Commit d11e667

Browse files
committedFeb 24, 2016
Quote fields in size/width assistant (fix #14257)
1 parent cd145bb commit d11e667

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed
 

‎python/gui/qgsfieldexpressionwidget.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ class QgsFieldExpressionWidget : QWidget
4646
*/
4747
QString currentText() const;
4848

49+
/** Returns the currently selected field or expression. If a field is currently selected, the returned
50+
* value will be converted to a valid expression referencing this field (ie enclosing the field name with
51+
* appropriate quotations).
52+
* @note added in QGIS 2.14
53+
*/
54+
QString asExpression() const;
55+
4956
//! Returns the currently used layer
5057
QgsVectorLayer* layer() const;
5158

‎src/gui/qgsfieldexpressionwidget.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ QString QgsFieldExpressionWidget::currentText() const
103103
return mCombo->currentText();
104104
}
105105

106+
QString QgsFieldExpressionWidget::asExpression() const
107+
{
108+
return isExpression() ? currentText() : QgsExpression::quotedColumnRef( currentText() );
109+
}
110+
106111
bool QgsFieldExpressionWidget::isValidExpression( QString *expressionError ) const
107112
{
108113
QString temp;

‎src/gui/qgsfieldexpressionwidget.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
8989
*/
9090
QString currentText() const;
9191

92+
/** Returns the currently selected field or expression. If a field is currently selected, the returned
93+
* value will be converted to a valid expression referencing this field (ie enclosing the field name with
94+
* appropriate quotations).
95+
* @note added in QGIS 2.14
96+
*/
97+
QString asExpression() const;
98+
9299
//! Returns the currently used layer
93100
QgsVectorLayer* layer() const;
94101

‎src/gui/symbology-ng/qgssizescalewidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ void QgsSizeScaleWidget::showEvent( QShowEvent* )
222222
QgsScaleExpression *QgsSizeScaleWidget::createExpression() const
223223
{
224224
return new QgsScaleExpression( QgsScaleExpression::Type( scaleMethodComboBox->itemData( scaleMethodComboBox->currentIndex() ).toInt() ),
225-
mExpressionWidget->currentField(),
225+
mExpressionWidget->asExpression(),
226226
minValueSpinBox->value(),
227227
maxValueSpinBox->value(),
228228
minSizeSpinBox->value(),

‎tests/src/gui/testqgsfieldexpressionwidget.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class TestQgsFieldExpressionWidget : public QObject
4848
void cleanup(); // will be called after every testfunction.
4949

5050
void testRemoveJoin();
51+
void asExpression();
5152

5253
private:
5354
QgsFieldExpressionWidget* mWidget;
@@ -133,6 +134,33 @@ void TestQgsFieldExpressionWidget::testRemoveJoin()
133134
// 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.
134135
}
135136

137+
void TestQgsFieldExpressionWidget::asExpression()
138+
{
139+
QgsVectorLayer* layer = new QgsVectorLayer( "point?field=fld:int&field=fld2:int&field=fld3:int", "x", "memory" );
140+
QgsMapLayerRegistry::instance()->addMapLayer( layer );
141+
142+
QScopedPointer< QgsFieldExpressionWidget > widget( new QgsFieldExpressionWidget() );
143+
widget->setLayer( layer );
144+
145+
// check with field set
146+
widget->setField( "fld" );
147+
QCOMPARE( widget->asExpression(), QString( "\"fld\"" ) );
148+
149+
// check with expressions set
150+
widget->setField( "fld + 1" );
151+
QCOMPARE( widget->asExpression(), QString( "fld + 1" ) );
152+
widget->setField( "1" );
153+
QCOMPARE( widget->asExpression(), QString( "1" ) );
154+
widget->setField( "\"fld2\"" );
155+
QCOMPARE( widget->asExpression(), QString( "\"fld2\"" ) );
156+
157+
// check switching back to a field
158+
widget->setField( "fld3" );
159+
QCOMPARE( widget->asExpression(), QString( "\"fld3\"" ) );
160+
161+
QgsMapLayerRegistry::instance()->removeMapLayer( layer );
162+
}
163+
136164

137165
QTEST_MAIN( TestQgsFieldExpressionWidget )
138166
#include "testqgsfieldexpressionwidget.moc"

0 commit comments

Comments
 (0)