Skip to content
Permalink
Browse files

move the expression preview to a dedicated class

  • Loading branch information
3nids committed Mar 27, 2020
1 parent f4a5986 commit 97dafe1fe90f2666cff20a24b9e6e414d1a783c4
@@ -0,0 +1,126 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsexpressionpreviewwidget.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/







class QgsExpressionPreviewWidget : QWidget
{
%Docstring
QgsExpressionPreviewWidget is a widget to preview an expression result.
If the layer is set, one can browse across features to see the different outputs.

.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgsexpressionpreviewwidget.h"
%End
public:
explicit QgsExpressionPreviewWidget( QWidget *parent = 0 );
%Docstring
Constructor
%End

void setLayer( QgsVectorLayer *layer );
%Docstring
Sets the layer used in the preview
%End

void setExpressionText( const QString &expression );
%Docstring
Sets the expression
%End

QgsExpressionContext expressionContext() const;
%Docstring
Returns the expression context for the widget. The context is used for the expression
preview result and for populating the list of available functions and variables.

.. seealso:: :py:func:`setExpressionContext`
%End

void setExpressionContext( const QgsExpressionContext &context );
%Docstring
Sets the expression context for the widget. The context is used for the expression
preview result and to populate the list of available functions and variables.

:param context: expression context

.. versionadded:: 2.12
%End

void setGeomCalculator( const QgsDistanceArea &da );
%Docstring
Sets geometry calculator used in distance/area calculations.
%End

bool evalError() const;
%Docstring
Will be set to ``True`` if the current expression text reported an eval error
with the context.
%End

bool parserError() const;
%Docstring
Will be set to ``True`` if the current expression text reports a parser error
with the context.
%End

const QgsExpressionNode *rootNode() const;
%Docstring
Returns the root node of the expression
%End

QList<QgsExpression::ParserError> parserErrors() const;
%Docstring
Returns the expression parser erros
%End

signals:

void expressionParsed( bool isValid );
%Docstring
Emitted when the user changes the expression in the widget.
Users of this widget should connect to this signal to decide if to let the user
continue.

:param isValid: Is ``True`` if the expression the user has typed is valid.
%End

void evalErrorChanged();
%Docstring
Will be set to ``True`` if the current expression text reported an eval error
with the context.
%End

void parserErrorChanged();
%Docstring
Will be set to ``True`` if the current expression text reported a parser error
with the context.
%End

void toolTipChanged( const QString &toolTip );
%Docstring
Emitted whenever the tool tip changed
%End

public slots:

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsexpressionpreviewwidget.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -74,6 +74,7 @@
%Include auto_generated/qgsexpressionbuilderwidget.sip
%Include auto_generated/qgsexpressionhighlighter.sip
%Include auto_generated/qgsexpressionlineedit.sip
%Include auto_generated/qgsexpressionpreviewwidget.sip
%Include auto_generated/qgsexpressionselectiondialog.sip
%Include auto_generated/qgsexpressiontreeview.sip
%Include auto_generated/qgsextentgroupbox.sip
@@ -393,6 +393,7 @@ SET(QGIS_GUI_SRCS
qgsexpressionbuilderwidget.cpp
qgsexpressionhighlighter.cpp
qgsexpressionlineedit.cpp
qgsexpressionpreviewwidget.cpp
qgsexpressionselectiondialog.cpp
qgsexpressionstoredialog.cpp
qgsexpressiontreeview.cpp
@@ -620,6 +621,7 @@ SET(QGIS_GUI_HDRS
qgsexpressionbuilderwidget.h
qgsexpressionhighlighter.h
qgsexpressionlineedit.h
qgsexpressionpreviewwidget.h
qgsexpressionselectiondialog.h
qgsexpressiontreeview.h
qgsextentgroupbox.h
@@ -0,0 +1,156 @@
/***************************************************************************
qgsexpressionpreviewwidget.cpp
--------------------------------------
Date : march 2020 - quarantine day 12
Copyright : (C) 2020 by Denis Rouzaud
Email : denis@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsexpressionpreviewwidget.h"
#include "qgsmessageviewer.h"
#include "qgsvectorlayer.h"



QgsExpressionPreviewWidget::QgsExpressionPreviewWidget( QWidget *parent )
: QWidget( parent )
{
setupUi( this );
mPreviewLabel->clear();

connect( mPreviewLabel, &QLabel::linkActivated, this, &QgsExpressionPreviewWidget::linkActivated );
}

void QgsExpressionPreviewWidget::setLayer( QgsVectorLayer *layer )
{
mLayer = layer;
mFeatureListComboBox->setSourceLayer( layer );
}

void QgsExpressionPreviewWidget::setExpressionText( const QString &expression )
{
// If the string is empty the expression will still "fail" although
// we don't show the user an error as it will be confusing.
if ( expression.isEmpty() )
{
mPreviewLabel->clear();
mPreviewLabel->setStyleSheet( QString() );
setExpressionToolTip( QString() );
emit expressionParsed( false );
mExpression = QgsExpression();
}
else
{
mExpression = QgsExpression( expression );

if ( mLayer )
{
// Only set calculator if we have layer, else use default.
mExpression.setGeomCalculator( &mDa );

if ( !mExpressionContext.feature().isValid() )
{
// no feature passed yet, try to get from layer
QgsFeature f;
mLayer->getFeatures( QgsFeatureRequest().setLimit( 1 ) ).nextFeature( f );
mExpressionContext.setFeature( f );
}
}

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

if ( mExpression.hasParserError() || mExpression.hasEvalError() )
{
QString errorString = mExpression.parserErrorString().replace( "\n", "<br>" );
QString tooltip;
if ( mExpression.hasParserError() )
tooltip = QStringLiteral( "<b>%1:</b>"
"%2" ).arg( tr( "Parser Errors" ), errorString );
// Only show the eval error if there is no parser error.
if ( !mExpression.hasParserError() && mExpression.hasEvalError() )
tooltip += QStringLiteral( "<b>%1:</b> %2" ).arg( tr( "Eval Error" ), mExpression.evalErrorString() );

mPreviewLabel->setText( tr( "Expression is invalid <a href=""more"">(more info)</a>" ) );
mPreviewLabel->setStyleSheet( QStringLiteral( "color: rgba(255, 6, 10, 255);" ) );
setExpressionToolTip( tooltip );
emit expressionParsed( false );
setParserError( mExpression.hasParserError() );
setEvalError( mExpression.hasEvalError() );
}
else
{
mPreviewLabel->setStyleSheet( QString() );
setExpressionToolTip( QString() );
emit expressionParsed( true );
setParserError( false );
setEvalError( false );
}
}
}

void QgsExpressionPreviewWidget::setGeomCalculator( const QgsDistanceArea &da )
{
mDa = da;
}

void QgsExpressionPreviewWidget::setExpressionContext( const QgsExpressionContext &context )
{
mExpressionContext = context;
}

void QgsExpressionPreviewWidget::linkActivated( const QString & )
{
QgsMessageViewer *mv = new QgsMessageViewer( this );
mv->setWindowTitle( tr( "More Info on Expression Error" ) );
mv->setMessageAsHtml( mToolTip );
mv->exec();
}

void QgsExpressionPreviewWidget::setExpressionToolTip( const QString &toolTip )
{
if ( toolTip == mToolTip )
return;

mToolTip = toolTip;
mPreviewLabel->setToolTip( mToolTip );
emit toolTipChanged( mToolTip );
}

void QgsExpressionPreviewWidget::setParserError( bool parserError )
{
if ( parserError != mParserError )
{
mParserError = parserError;
emit parserErrorChanged();
}
}
bool QgsExpressionPreviewWidget::parserError() const
{
return mParserError;
}

void QgsExpressionPreviewWidget::setEvalError( bool evalError )
{
if ( evalError == mEvalError )
return;

mEvalError = evalError;
emit evalErrorChanged();
}

bool QgsExpressionPreviewWidget::evalError() const
{
return mEvalError;
}

0 comments on commit 97dafe1

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