17#include "qclipboard.h"
21#include "moc_qgsexpressionpreviewwidget.cpp"
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] {
46 mCustomComboBox->setCurrentIndex( std::max( 0, mCustomComboBox->currentIndex() - 1 ) );
48 connect( mCustomButtonNext, &QToolButton::clicked,
this, [
this] {
49 mCustomComboBox->setCurrentIndex( std::min( mCustomComboBox->count() - 1, mCustomComboBox->currentIndex() + 1 ) );
55 if ( layer != mLayer )
58 mFeaturePickerWidget->setLayer( layer );
64 mCustomPreviewGeneratorFunction = previewContextGenerator;
65 mStackedWidget->setCurrentWidget( mPageCustomPicker );
66 mCustomLabel->setText( label );
67 mCustomComboBox->blockSignals(
true );
68 mCustomComboBox->clear();
69 for (
const auto &choice : choices )
71 mCustomComboBox->addItem( choice.first, choice.second );
73 mCustomComboBox->blockSignals(
false );
79 if ( expression != mExpressionText )
81 mExpressionText = expression;
89 if ( feature != mExpressionContext.
feature() )
99 mUseGeomCalculator =
true;
104 mExpressionContext = context;
107void QgsExpressionPreviewWidget::refreshPreview()
111 if ( mExpressionText.isEmpty() )
113 mPreviewLabel->clear();
114 mPreviewLabel->setStyleSheet( QString() );
115 mCopyPreviewAction->setEnabled(
false );
116 setExpressionToolTip( QString() );
124 if ( mUseGeomCalculator )
130 const QVariant value = mExpression.
evaluate( &mExpressionContext );
134 mPreviewLabel->setText( preview );
135 mCopyPreviewAction->setEnabled(
true );
147 mPreviewLabel->setText( tr(
"No feature was found on this layer to evaluate the expression." ) );
148 mPreviewLabel->setStyleSheet( QStringLiteral(
"color: rgba(220, 125, 0, 255);" ) );
150 setParserError(
false );
151 setEvalError(
false );
156 const QString errorString = mExpression.
parserErrorString().replace( QLatin1String(
"\n" ), QLatin1String(
"<br>" ) );
159 tooltip = QStringLiteral(
"<b>%1:</b>"
161 .arg( tr(
"Parser Errors" ), errorString );
164 tooltip += QStringLiteral(
"<b>%1:</b> %2" ).arg( tr(
"Eval Error" ), mExpression.
evalErrorString() );
166 mPreviewLabel->setText( tr(
"Expression is invalid <a href="
168 ">(more info)</a>" ) );
169 mPreviewLabel->setStyleSheet( QStringLiteral(
"color: rgba(255, 6, 10, 255);" ) );
170 setExpressionToolTip( tooltip );
174 mCopyPreviewAction->setEnabled(
false );
178 mPreviewLabel->setStyleSheet( QString() );
180 if ( longerPreview != preview )
181 setExpressionToolTip( longerPreview );
183 setExpressionToolTip( QString() );
185 setParserError(
false );
186 setEvalError(
false );
187 mCopyPreviewAction->setEnabled(
true );
192void QgsExpressionPreviewWidget::linkActivated(
const QString & )
195 mv.setWindowTitle( tr(
"More Info on Expression Error" ) );
196 mv.setMessageAsHtml( mToolTip );
200void QgsExpressionPreviewWidget::setExpressionToolTip(
const QString &toolTip )
202 if ( toolTip == mToolTip )
206 if ( toolTip.isEmpty() )
208 mPreviewLabel->setToolTip( tr(
"Right-click to copy" ) );
212 mPreviewLabel->setToolTip( tr(
"%1 (right-click to copy)" ).arg( mToolTip ) );
217void QgsExpressionPreviewWidget::setParserError(
bool parserError )
232 return mPreviewLabel->text();
235void QgsExpressionPreviewWidget::setEvalError(
bool evalError )
249void QgsExpressionPreviewWidget::copyFullExpressionValue()
251 QClipboard *clipboard = QApplication::clipboard();
252 const QVariant value = mExpression.
evaluate( &mExpressionContext );
254 QgsDebugMsgLevel( QStringLiteral(
"set clipboard: %1" ).arg( copiedValue ), 4 );
255 clipboard->setText( copiedValue );
258void QgsExpressionPreviewWidget::setCustomChoice(
int )
260 const QVariant selectedValue = mCustomComboBox->currentData();
262 mCustomButtonPrev->setEnabled( mCustomComboBox->currentIndex() > 0 && mCustomComboBox->count() > 0 );
263 mCustomButtonNext->setEnabled( mCustomComboBox->currentIndex() < ( mCustomComboBox->count() - 1 ) && mCustomComboBox->count() > 0 );
265 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)