Skip to content
Permalink
Browse files

Show special strings for expression preview if result is a geometry

or feature type. Makes it easier to determine what the return
type of various functions are for users.
  • Loading branch information
nyalldawson committed Nov 4, 2015
1 parent da94223 commit 1d872dc571202bf6589cbe013bf02047747b5f97
Showing with 31 additions and 17 deletions.
  1. +28 −6 src/gui/qgsexpressionbuilderwidget.cpp
  2. +2 −2 src/gui/qgsexpressionbuilderwidget.h
  3. +1 −9 src/ui/qgsexpressionbuilder.ui
@@ -19,6 +19,8 @@
#include "qgsmessageviewer.h"
#include "qgsapplication.h"
#include "qgspythonrunner.h"
#include "qgsgeometry.h"
#include "qgsfeature.h"

#include <QSettings>
#include <QMenu>
@@ -544,7 +546,7 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
mExpressionContext.setFeature( mFeature );
QVariant value = exp.evaluate( &mExpressionContext );
if ( !exp.hasEvalError() )
lblPreview->setText( formatPreviewString( value.toString() ) );
lblPreview->setText( formatPreviewString( value ) );
}
else
{
@@ -559,7 +561,7 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
QVariant value = exp.evaluate( &mExpressionContext );
if ( !exp.hasEvalError() )
{
lblPreview->setText( formatPreviewString( value.toString() ) );
lblPreview->setText( formatPreviewString( value ) );
}
}

@@ -585,15 +587,35 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
}
}

QString QgsExpressionBuilderWidget::formatPreviewString( const QString& previewString ) const
QString QgsExpressionBuilderWidget::formatPreviewString( const QVariant& value ) const
{
if ( previewString.length() > 63 )
if ( value.canConvert<QgsGeometry>() )
{
return QString( tr( "%1..." ) ).arg( previewString.left( 60 ) );
//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
{
return previewString;
QString previewString = value.toString();

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

@@ -271,9 +271,9 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp

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

void loadExpressionContext();

@@ -111,14 +111,6 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<weight>50</weight>
<italic>true</italic>
<bold>false</bold>
<underline>false</underline>
</font>
</property>
<property name="toolTip">
<string>Output preview is generated &lt;br&gt; using the first feature from the layer.</string>
</property>
@@ -363,7 +355,7 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="">
<widget class="QWidget" name="layoutWidget_2">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QTextEdit" name="txtHelpText">

0 comments on commit 1d872dc

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