Skip to content

Commit

Permalink
Show special strings for expression preview if result is a geometry
Browse files Browse the repository at this point in the history
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 1d872dc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
34 changes: 28 additions & 6 deletions src/gui/qgsexpressionbuilderwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "qgsmessageviewer.h"
#include "qgsapplication.h"
#include "qgspythonrunner.h"
#include "qgsgeometry.h"
#include "qgsfeature.h"

#include <QSettings>
#include <QMenu>
Expand Down Expand Up @@ -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
{
Expand All @@ -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 ) );
}
}

Expand All @@ -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;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsexpressionbuilderwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
10 changes: 1 addition & 9 deletions src/ui/qgsexpressionbuilder.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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">
Expand Down

0 comments on commit 1d872dc

Please sign in to comment.