Skip to content
Permalink
Browse files

Improve preview of current variable value in expression builder

  • Loading branch information
nyalldawson committed Jan 27, 2016
1 parent 0356752 commit 39f0035246bc80d1aec4a98e62d877b3a5949d4e
@@ -692,6 +692,14 @@ class QgsExpression
*/
static QString group( const QString& group );

/** Formats an expression result for friendly display to the user. Truncates the result to a sensible
* length, and presents text representations of non numeric/text types (eg geometries and features).
* @param value expression result to format
* @returns formatted string, may contain HTML formatting characters
* @note added in QGIS 2.14
*/
static QString formatPreviewString( const QVariant& value );

protected:
void initGeomCalculator();
};
@@ -4561,13 +4561,9 @@ QString QgsExpression::variableHelpText( const QString &variableName, bool showV
{
valueString = QCoreApplication::translate( "variable_help", "not set" );
}
else if ( value.type() == QVariant::String )
{
valueString = QString( "'<b>%1</b>'" ).arg( value.toString() );
}
else
{
valueString = QString( "<b>%1</b>" ).arg( value.toString() );
valueString = QString( "<b>%1</b>" ).arg( formatPreviewString( value ) );
}
text.append( QCoreApplication::translate( "variable_help", "<p>Current value: %1</p>" ).arg( valueString ) );
}
@@ -4602,6 +4598,47 @@ QString QgsExpression::group( const QString& name )
return gGroups.value( name, name );
}

QString QgsExpression::formatPreviewString( const QVariant& value )
{
if ( value.canConvert<QgsGeometry>() )
{
//result is a geometry
QgsGeometry geom = value.value<QgsGeometry>();
if ( geom.isEmpty() )
return tr( "<i>&lt;empty geometry&gt;</i>" );
else
return tr( "<i>&lt;geometry: %1&gt;</i>" ).arg( QgsWKBTypes::displayString( geom.geometry()->wkbType() ) );
}
else if ( value.canConvert< QgsFeature >() )
{
//result is a feature
QgsFeature feat = value.value<QgsFeature>();
return tr( "<i>&lt;feature: %1&gt;</i>" ).arg( feat.id() );
}
else if ( value.canConvert< QgsExpression::Interval >() )
{
//result is a feature
QgsExpression::Interval interval = value.value<QgsExpression::Interval>();
return tr( "<i>&lt;interval: %1 days&gt;</i>" ).arg( interval.days() );
}
else if ( value.type() == QVariant::String )
{
QString previewString = value.toString();
if ( previewString.length() > 63 )
{
return QString( tr( "'%1...'" ) ).arg( previewString.left( 60 ) );
}
else
{
return previewString.prepend( '\'' ).append( '\'' );
}
}
else
{
return value.toString();
}
}

QVariant QgsExpression::Function::func( const QVariantList& values, const QgsFeature* feature, QgsExpression* parent )
{
//default implementation creates a QgsFeatureBasedExpressionContext
@@ -1052,6 +1052,14 @@ class CORE_EXPORT QgsExpression
*/
static QString group( const QString& group );

/** Formats an expression result for friendly display to the user. Truncates the result to a sensible
* length, and presents text representations of non numeric/text types (eg geometries and features).
* @param value expression result to format
* @returns formatted string, may contain HTML formatting characters
* @note added in QGIS 2.14
*/
static QString formatPreviewString( const QVariant& value );

protected:
/**
* Used by QgsOgcUtils to create an empty
@@ -557,7 +557,7 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
mExpressionContext.setFeature( mFeature );
QVariant value = exp.evaluate( &mExpressionContext );
if ( !exp.hasEvalError() )
lblPreview->setText( formatPreviewString( value ) );
lblPreview->setText( QgsExpression::formatPreviewString( value ) );
}
else
{
@@ -572,7 +572,7 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
QVariant value = exp.evaluate( &mExpressionContext );
if ( !exp.hasEvalError() )
{
lblPreview->setText( formatPreviewString( value ) );
lblPreview->setText( QgsExpression::formatPreviewString( value ) );
}
}

@@ -598,44 +598,6 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
}
}

QString QgsExpressionBuilderWidget::formatPreviewString( const QVariant& value ) const
{
if ( value.canConvert<QgsGeometry>() )
{
//result is a geometry
QgsGeometry geom = value.value<QgsGeometry>();
if ( geom.isEmpty() )
return tr( "<i>&lt;empty geometry&gt;</i>" );
else
return tr( "<i>&lt;geometry: %1&gt;</i>" ).arg( QgsWKBTypes::displayString( geom.geometry()->wkbType() ) );
}
else if ( value.canConvert< QgsFeature >() )
{
//result is a feature
QgsFeature feat = value.value<QgsFeature>();
return tr( "<i>&lt;feature: %1&gt;</i>" ).arg( feat.id() );
}
else if ( value.canConvert< QgsExpression::Interval >() )
{
//result is a feature
QgsExpression::Interval interval = value.value<QgsExpression::Interval>();
return tr( "<i>&lt;interval: %1 days&gt;</i>" ).arg( interval.days() );
}
else
{
QString previewString = value.toString();

if ( previewString.length() > 63 )
{
return QString( tr( "%1..." ) ).arg( previewString.left( 60 ) );
}
else
{
return previewString;
}
}
}

void QgsExpressionBuilderWidget::loadExpressionContext()
{
QStringList variableNames = mExpressionContext.filteredVariableNames();
@@ -261,12 +261,6 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
QString loadFunctionHelp( QgsExpressionItem* functionName );
QString helpStylesheet() const;

/** Formats an expression preview result for display in the widget
* by truncating the string
* @param value expression preview result to format
*/
QString formatPreviewString( const QVariant& value ) const;

void loadExpressionContext();

bool mAutoSave;

0 comments on commit 39f0035

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