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 );
37 mStackedWidget->setCurrentWidget( mPageFeaturePicker );
39 mCustomButtonNext->setEnabled(
false );
40 mCustomButtonPrev->setEnabled(
false );
42 connect( mCustomComboBox, qOverload< int >( &QComboBox::currentIndexChanged ),
this, &QgsExpressionPreviewWidget::setCustomChoice );
43 connect( mPreviewLabel, &QLabel::linkActivated,
this, &QgsExpressionPreviewWidget::linkActivated );
44 connect( mCopyPreviewAction, &QAction::triggered,
this, &QgsExpressionPreviewWidget::copyFullExpressionValue );
45 connect( mCustomButtonPrev, &QToolButton::clicked,
this, [
this]
47 mCustomComboBox->setCurrentIndex( std::max( 0, mCustomComboBox->currentIndex() - 1 ) );
49 connect( mCustomButtonNext, &QToolButton::clicked,
this, [
this]
51 mCustomComboBox->setCurrentIndex( std::min( mCustomComboBox->count() - 1, mCustomComboBox->currentIndex() + 1 ) );
57 if ( layer != mLayer )
60 mFeaturePickerWidget->setLayer( layer );
66 mCustomPreviewGeneratorFunction = previewContextGenerator;
67 mStackedWidget->setCurrentWidget( mPageCustomPicker );
68 mCustomLabel->setText( label );
69 mCustomComboBox->blockSignals(
true );
70 mCustomComboBox->clear();
71 for (
const auto &choice : choices )
73 mCustomComboBox->addItem( choice.first, choice.second );
75 mCustomComboBox->blockSignals(
false );
81 if ( expression != mExpressionText )
83 mExpressionText = expression;
91 if ( feature != mExpressionContext.
feature() )
101 mUseGeomCalculator =
true;
106 mExpressionContext = context;
109void QgsExpressionPreviewWidget::refreshPreview()
113 if ( mExpressionText.isEmpty() )
115 mPreviewLabel->clear();
116 mPreviewLabel->setStyleSheet( QString() );
117 mCopyPreviewAction->setEnabled(
false );
118 setExpressionToolTip( QString() );
126 if ( mUseGeomCalculator )
132 const QVariant value = mExpression.
evaluate( &mExpressionContext );
136 mPreviewLabel->setText( preview );
137 mCopyPreviewAction->setEnabled(
true );
149 mPreviewLabel->setText( tr(
"No feature was found on this layer to evaluate the expression." ) );
150 mPreviewLabel->setStyleSheet( QStringLiteral(
"color: rgba(220, 125, 0, 255);" ) );
152 setParserError(
false );
153 setEvalError(
false );
158 const QString errorString = mExpression.
parserErrorString().replace( QLatin1String(
"\n" ), QLatin1String(
"<br>" ) );
161 tooltip = QStringLiteral(
"<b>%1:</b>"
162 "%2" ).arg( tr(
"Parser Errors" ), errorString );
165 tooltip += QStringLiteral(
"<b>%1:</b> %2" ).arg( tr(
"Eval Error" ), mExpression.
evalErrorString() );
167 mPreviewLabel->setText( tr(
"Expression is invalid <a href=""more"">(more info)</a>" ) );
168 mPreviewLabel->setStyleSheet( QStringLiteral(
"color: rgba(255, 6, 10, 255);" ) );
169 setExpressionToolTip( tooltip );
173 mCopyPreviewAction->setEnabled(
false );
177 mPreviewLabel->setStyleSheet( QString() );
179 if ( longerPreview != preview )
180 setExpressionToolTip( longerPreview );
182 setExpressionToolTip( QString() );
184 setParserError(
false );
185 setEvalError(
false );
186 mCopyPreviewAction->setEnabled(
true );
191void QgsExpressionPreviewWidget::linkActivated(
const QString & )
194 mv.setWindowTitle( tr(
"More Info on Expression Error" ) );
195 mv.setMessageAsHtml( mToolTip );
199void QgsExpressionPreviewWidget::setExpressionToolTip(
const QString &toolTip )
201 if ( toolTip == mToolTip )
205 if ( toolTip.isEmpty() )
207 mPreviewLabel->setToolTip( tr(
"Right-click to copy" ) );
211 mPreviewLabel->setToolTip( tr(
"%1 (right-click to copy)" ).arg( mToolTip ) );
216void QgsExpressionPreviewWidget::setParserError(
bool parserError )
231 return mPreviewLabel->text();
234void QgsExpressionPreviewWidget::setEvalError(
bool evalError )
248void QgsExpressionPreviewWidget::copyFullExpressionValue()
250 QClipboard *clipboard = QApplication::clipboard();
251 const QVariant value = mExpression.
evaluate( &mExpressionContext );
253 QgsDebugMsgLevel( QStringLiteral(
"set clipboard: %1" ).arg( copiedValue ), 4 );
254 clipboard->setText( copiedValue );
257void QgsExpressionPreviewWidget::setCustomChoice(
int )
259 const QVariant selectedValue = mCustomComboBox->currentData();
261 mCustomButtonPrev->setEnabled( mCustomComboBox->currentIndex() > 0 && mCustomComboBox->count() > 0 );
262 mCustomButtonNext->setEnabled( mCustomComboBox->currentIndex() < ( mCustomComboBox->count() - 1 ) && mCustomComboBox->count() > 0 );
264 mExpressionContext = mCustomPreviewGeneratorFunction( selectedValue );
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)