Skip to content
Permalink
Browse files

Quote fields in size/width assistant (fix #14257)

  • Loading branch information
nyalldawson committed Feb 24, 2016
1 parent cd145bb commit d11e66749b2c1b2f830de7ae8274d65b97878dac
@@ -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;

@@ -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;
@@ -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;

@@ -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(),
@@ -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"

0 comments on commit d11e667

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