17#include "qclipboard.h"
31 mPreviewLabel->clear();
32 mPreviewLabel->setContextMenuPolicy( Qt::ActionsContextMenu );
33 mCopyPreviewAction =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionEditCopy.svg" ) ), tr(
"Copy Expression Value" ),
this );
34 mPreviewLabel->addAction( mCopyPreviewAction );
35 mFeaturePickerWidget->setShowBrowserButtons(
true );
38 connect( mPreviewLabel, &QLabel::linkActivated,
this, &QgsExpressionPreviewWidget::linkActivated );
39 connect( mCopyPreviewAction, &QAction::triggered,
this, &QgsExpressionPreviewWidget::copyFullExpressionValue );
44 if ( layer != mLayer )
47 mFeaturePickerWidget->setLayer( layer );
53 if ( expression != mExpressionText )
55 mExpressionText = expression;
63 if ( feature != mExpressionContext.
feature() )
73 mUseGeomCalculator =
true;
78 mExpressionContext = context;
81void QgsExpressionPreviewWidget::refreshPreview()
85 if ( mExpressionText.isEmpty() )
87 mPreviewLabel->clear();
88 mPreviewLabel->setStyleSheet( QString() );
89 mCopyPreviewAction->setEnabled(
false );
90 setExpressionToolTip( QString() );
98 if ( mUseGeomCalculator )
104 const QVariant value = mExpression.
evaluate( &mExpressionContext );
108 mPreviewLabel->setText( preview );
109 mCopyPreviewAction->setEnabled(
true );
121 mPreviewLabel->setText( tr(
"No feature was found on this layer to evaluate the expression." ) );
122 mPreviewLabel->setStyleSheet( QStringLiteral(
"color: rgba(220, 125, 0, 255);" ) );
124 setParserError(
false );
125 setEvalError(
false );
130 const QString errorString = mExpression.
parserErrorString().replace( QLatin1String(
"\n" ), QLatin1String(
"<br>" ) );
133 tooltip = QStringLiteral(
"<b>%1:</b>"
134 "%2" ).arg( tr(
"Parser Errors" ), errorString );
137 tooltip += QStringLiteral(
"<b>%1:</b> %2" ).arg( tr(
"Eval Error" ), mExpression.
evalErrorString() );
139 mPreviewLabel->setText( tr(
"Expression is invalid <a href=""more"">(more info)</a>" ) );
140 mPreviewLabel->setStyleSheet( QStringLiteral(
"color: rgba(255, 6, 10, 255);" ) );
141 setExpressionToolTip( tooltip );
145 mCopyPreviewAction->setEnabled(
false );
149 mPreviewLabel->setStyleSheet( QString() );
151 if ( longerPreview != preview )
152 setExpressionToolTip( longerPreview );
154 setExpressionToolTip( QString() );
156 setParserError(
false );
157 setEvalError(
false );
158 mCopyPreviewAction->setEnabled(
true );
163void QgsExpressionPreviewWidget::linkActivated(
const QString & )
166 mv.setWindowTitle( tr(
"More Info on Expression Error" ) );
167 mv.setMessageAsHtml( mToolTip );
171void QgsExpressionPreviewWidget::setExpressionToolTip(
const QString &toolTip )
173 if ( toolTip == mToolTip )
177 if ( toolTip.isEmpty() )
179 mPreviewLabel->setToolTip( tr(
"Right-click to copy" ) );
183 mPreviewLabel->setToolTip( tr(
"%1 (right-click to copy)" ).arg( mToolTip ) );
188void QgsExpressionPreviewWidget::setParserError(
bool parserError )
201void QgsExpressionPreviewWidget::setEvalError(
bool evalError )
215void QgsExpressionPreviewWidget::copyFullExpressionValue()
217 QClipboard *clipboard = QApplication::clipboard();
218 const QVariant value = mExpression.
evaluate( &mExpressionContext );
220 QgsDebugMsgLevel( QStringLiteral(
"set clipboard: %1" ).arg( copiedValue ), 4 );
221 clipboard->setText( copiedValue );
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsFeature feature() const
Convenience function for retrieving the feature for the context, if set.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
QString evalErrorString() const
Returns evaluation error.
QString parserErrorString() const
Returns parser error.
static QString formatPreviewString(const QVariant &value, bool htmlOutput=true, int maximumPreviewLength=60)
Formats an expression result for friendly display to the user.
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
void setGeomCalculator(const QgsDistanceArea *calc)
Sets the geometry calculator used for distance and area calculations in expressions.
bool hasEvalError() const
Returns true if an error occurred when evaluating last input.
bool needsGeometry() const
Returns true if the expression uses feature geometry for some computation.
QVariant evaluate()
Evaluate the feature and return the result.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool isValid() const
Returns the validity of this feature.
A generic message view for displaying QGIS messages.
Represents a vector layer which manages a vector based data sets.
#define QgsDebugMsgLevel(str, level)