Navigation Menu

Skip to content

Commit

Permalink
Improve preview of current variable value in expression builder
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 27, 2016
1 parent 0356752 commit 39f0035
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 51 deletions.
8 changes: 8 additions & 0 deletions python/core/qgsexpression.sip
Expand Up @@ -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();
};
47 changes: 42 additions & 5 deletions src/core/qgsexpression.cpp
Expand Up @@ -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 ) );
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsexpression.h
Expand Up @@ -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
Expand Down
42 changes: 2 additions & 40 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -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
{
Expand All @@ -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 ) );
}
}

Expand All @@ -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();
Expand Down
6 changes: 0 additions & 6 deletions src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -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;
Expand Down

0 comments on commit 39f0035

Please sign in to comment.