Skip to content

Commit

Permalink
Merge pull request #41241 from 3nids/longer-preview
Browse files Browse the repository at this point in the history
QgsExpressionPreviewWidget: display longer preview in tooltip if needed
  • Loading branch information
3nids committed Jan 28, 2021
2 parents f073292 + 2f54585 commit c4a69f6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
3 changes: 2 additions & 1 deletion python/core/auto_generated/expression/qgsexpression.sip.in
Expand Up @@ -590,13 +590,14 @@ Returns the translated name for a function group.
:param group: untranslated group name
%End

static QString formatPreviewString( const QVariant &value, bool htmlOutput = true );
static QString formatPreviewString( const QVariant &value, bool htmlOutput = true, int maximumPreviewLength = 60 );
%Docstring
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 (e.g., geometries and features).

:param value: expression result to format
:param htmlOutput: set to ``True`` to allow HTML formatting, or ``False`` for plain text output
:param maximumPreviewLength: define the maximum character length of the preview

:return: formatted string, may contain HTML formatting characters if ``htmlOutput`` is ``True``

Expand Down
16 changes: 7 additions & 9 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -942,10 +942,8 @@ QString QgsExpression::group( const QString &name )
return sGroups()->value( name, name );
}

QString QgsExpression::formatPreviewString( const QVariant &value, const bool htmlOutput )
QString QgsExpression::formatPreviewString( const QVariant &value, const bool htmlOutput, int maximumPreviewLength )
{
static const int MAX_PREVIEW = 60;

const QString startToken = htmlOutput ? QStringLiteral( "<i>&lt;" ) : QStringLiteral( "<" );
const QString endToken = htmlOutput ? QStringLiteral( "&gt;</i>" ) : QStringLiteral( ">" );

Expand Down Expand Up @@ -1015,9 +1013,9 @@ QString QgsExpression::formatPreviewString( const QVariant &value, const bool ht
else if ( value.type() == QVariant::String )
{
const QString previewString = value.toString();
if ( previewString.length() > MAX_PREVIEW + 3 )
if ( previewString.length() > maximumPreviewLength + 3 )
{
return tr( "'%1…'" ).arg( previewString.left( MAX_PREVIEW ) );
return tr( "'%1…'" ).arg( previewString.left( maximumPreviewLength ) );
}
else
{
Expand All @@ -1036,9 +1034,9 @@ QString QgsExpression::formatPreviewString( const QVariant &value, const bool ht
separator = QStringLiteral( "," );

mapStr.append( QStringLiteral( " '%1': %2" ).arg( it.key(), formatPreviewString( it.value(), htmlOutput ) ) );
if ( mapStr.length() > MAX_PREVIEW - 3 )
if ( mapStr.length() > maximumPreviewLength - 3 )
{
mapStr = tr( "%1…" ).arg( mapStr.left( MAX_PREVIEW - 2 ) );
mapStr = tr( "%1…" ).arg( mapStr.left( maximumPreviewLength - 2 ) );
break;
}
}
Expand All @@ -1060,9 +1058,9 @@ QString QgsExpression::formatPreviewString( const QVariant &value, const bool ht

listStr.append( " " );
listStr.append( formatPreviewString( arrayValue, htmlOutput ) );
if ( listStr.length() > MAX_PREVIEW - 3 )
if ( listStr.length() > maximumPreviewLength - 3 )
{
listStr = QString( tr( "%1…" ) ).arg( listStr.left( MAX_PREVIEW - 2 ) );
listStr = QString( tr( "%1…" ) ).arg( listStr.left( maximumPreviewLength - 2 ) );
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/expression/qgsexpression.h
Expand Up @@ -595,10 +595,11 @@ class CORE_EXPORT QgsExpression
* length, and presents text representations of non numeric/text types (e.g., geometries and features).
* \param value expression result to format
* \param htmlOutput set to TRUE to allow HTML formatting, or FALSE for plain text output
* \param maximumPreviewLength define the maximum character length of the preview
* \returns formatted string, may contain HTML formatting characters if \a htmlOutput is TRUE
* \since QGIS 2.14
*/
static QString formatPreviewString( const QVariant &value, bool htmlOutput = true );
static QString formatPreviewString( const QVariant &value, bool htmlOutput = true, int maximumPreviewLength = 60 );

/**
* Create an expression allowing to evaluate if a field is equal to a
Expand Down
9 changes: 7 additions & 2 deletions src/gui/qgsexpressionpreviewwidget.cpp
Expand Up @@ -98,9 +98,10 @@ void QgsExpressionPreviewWidget::refreshPreview()
}

QVariant value = mExpression.evaluate( &mExpressionContext );
QString preview = QgsExpression::formatPreviewString( value );
if ( !mExpression.hasEvalError() )
{
mPreviewLabel->setText( QgsExpression::formatPreviewString( value ) );
mPreviewLabel->setText( preview );
}

if ( mExpression.hasParserError() || mExpression.hasEvalError() )
Expand All @@ -124,7 +125,11 @@ void QgsExpressionPreviewWidget::refreshPreview()
else
{
mPreviewLabel->setStyleSheet( QString() );
setExpressionToolTip( QString() );
QString longerPreview = QgsExpression::formatPreviewString( value, true, 255 );
if ( longerPreview != preview )
setExpressionToolTip( longerPreview );
else
setExpressionToolTip( QString() );
emit expressionParsed( true );
setParserError( false );
setEvalError( false );
Expand Down

0 comments on commit c4a69f6

Please sign in to comment.