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 );
45 mFeaturePickerWidget->setLayer( layer );
50 mExpressionText = expression;
65 mUseGeomCalculator =
true;
70 mExpressionContext = context;
73void QgsExpressionPreviewWidget::refreshPreview()
77 if ( mExpressionText.isEmpty() )
79 mPreviewLabel->clear();
80 mPreviewLabel->setStyleSheet( QString() );
81 mCopyPreviewAction->setEnabled(
false );
82 setExpressionToolTip( QString() );
90 if ( mUseGeomCalculator )
96 const QVariant value = mExpression.
evaluate( &mExpressionContext );
100 mPreviewLabel->setText( preview );
101 mCopyPreviewAction->setEnabled(
true );
113 mPreviewLabel->setText( tr(
"No feature was found on this layer to evaluate the expression." ) );
114 mPreviewLabel->setStyleSheet( QStringLiteral(
"color: rgba(220, 125, 0, 255);" ) );
116 setParserError(
false );
117 setEvalError(
false );
122 const QString errorString = mExpression.
parserErrorString().replace( QLatin1String(
"\n" ), QLatin1String(
"<br>" ) );
125 tooltip = QStringLiteral(
"<b>%1:</b>"
126 "%2" ).arg( tr(
"Parser Errors" ), errorString );
129 tooltip += QStringLiteral(
"<b>%1:</b> %2" ).arg( tr(
"Eval Error" ), mExpression.
evalErrorString() );
131 mPreviewLabel->setText( tr(
"Expression is invalid <a href=""more"">(more info)</a>" ) );
132 mPreviewLabel->setStyleSheet( QStringLiteral(
"color: rgba(255, 6, 10, 255);" ) );
133 setExpressionToolTip( tooltip );
137 mCopyPreviewAction->setEnabled(
false );
141 mPreviewLabel->setStyleSheet( QString() );
143 if ( longerPreview != preview )
144 setExpressionToolTip( longerPreview );
146 setExpressionToolTip( QString() );
148 setParserError(
false );
149 setEvalError(
false );
150 mCopyPreviewAction->setEnabled(
true );
155void QgsExpressionPreviewWidget::linkActivated(
const QString & )
158 mv.setWindowTitle( tr(
"More Info on Expression Error" ) );
159 mv.setMessageAsHtml( mToolTip );
163void QgsExpressionPreviewWidget::setExpressionToolTip(
const QString &toolTip )
165 if ( toolTip == mToolTip )
169 if ( toolTip.isEmpty() )
171 mPreviewLabel->setToolTip( tr(
"Right-click to copy" ) );
175 mPreviewLabel->setToolTip( tr(
"%1 (right-click to copy)" ).arg( mToolTip ) );
180void QgsExpressionPreviewWidget::setParserError(
bool parserError )
193void QgsExpressionPreviewWidget::setEvalError(
bool evalError )
207void QgsExpressionPreviewWidget::copyFullExpressionValue()
209 QClipboard *clipboard = QApplication::clipboard();
210 const QVariant value = mExpression.
evaluate( &mExpressionContext );
212 QgsDebugMsgLevel( QStringLiteral(
"set clipboard: %1" ).arg( copiedValue ), 4 );
213 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)