17#include "qclipboard.h"
21#include "moc_qgsexpressionpreviewwidget.cpp"
32 mPreviewLabel->clear();
33 mPreviewLabel->setContextMenuPolicy( Qt::ActionsContextMenu );
34 mCopyPreviewAction =
new QAction(
QgsApplication::getThemeIcon( QStringLiteral(
"/mActionEditCopy.svg" ) ), tr(
"Copy Expression Value" ),
this );
35 mPreviewLabel->addAction( mCopyPreviewAction );
36 mFeaturePickerWidget->setShowBrowserButtons(
true );
38 mStackedWidget->setCurrentWidget( mPageFeaturePicker );
40 mCustomButtonNext->setEnabled(
false );
41 mCustomButtonPrev->setEnabled(
false );
43 connect( mCustomComboBox, qOverload< int >( &QComboBox::currentIndexChanged ),
this, &QgsExpressionPreviewWidget::setCustomChoice );
44 connect( mPreviewLabel, &QLabel::linkActivated,
this, &QgsExpressionPreviewWidget::linkActivated );
45 connect( mCopyPreviewAction, &QAction::triggered,
this, &QgsExpressionPreviewWidget::copyFullExpressionValue );
46 connect( mCustomButtonPrev, &QToolButton::clicked,
this, [
this]
48 mCustomComboBox->setCurrentIndex( std::max( 0, mCustomComboBox->currentIndex() - 1 ) );
50 connect( mCustomButtonNext, &QToolButton::clicked,
this, [
this]
52 mCustomComboBox->setCurrentIndex( std::min( mCustomComboBox->count() - 1, mCustomComboBox->currentIndex() + 1 ) );
58 if ( layer != mLayer )
61 mFeaturePickerWidget->setLayer( layer );
67 mCustomPreviewGeneratorFunction = previewContextGenerator;
68 mStackedWidget->setCurrentWidget( mPageCustomPicker );
69 mCustomLabel->setText( label );
70 mCustomComboBox->blockSignals(
true );
71 mCustomComboBox->clear();
72 for (
const auto &choice : choices )
74 mCustomComboBox->addItem( choice.first, choice.second );
76 mCustomComboBox->blockSignals(
false );
82 if ( expression != mExpressionText )
84 mExpressionText = expression;
92 if ( feature != mExpressionContext.
feature() )
102 mUseGeomCalculator =
true;
107 mExpressionContext = context;
110void QgsExpressionPreviewWidget::refreshPreview()
114 if ( mExpressionText.isEmpty() )
116 mPreviewLabel->clear();
117 mPreviewLabel->setStyleSheet( QString() );
118 mCopyPreviewAction->setEnabled(
false );
119 setExpressionToolTip( QString() );
127 if ( mUseGeomCalculator )
133 const QVariant value = mExpression.
evaluate( &mExpressionContext );
137 mPreviewLabel->setText( preview );
138 mCopyPreviewAction->setEnabled(
true );
150 mPreviewLabel->setText( tr(
"No feature was found on this layer to evaluate the expression." ) );
151 mPreviewLabel->setStyleSheet( QStringLiteral(
"color: rgba(220, 125, 0, 255);" ) );
153 setParserError(
false );
154 setEvalError(
false );
159 const QString errorString = mExpression.
parserErrorString().replace( QLatin1String(
"\n" ), QLatin1String(
"<br>" ) );
162 tooltip = QStringLiteral(
"<b>%1:</b>"
163 "%2" ).arg( tr(
"Parser Errors" ), errorString );
166 tooltip += QStringLiteral(
"<b>%1:</b> %2" ).arg( tr(
"Eval Error" ), mExpression.
evalErrorString() );
168 mPreviewLabel->setText( tr(
"Expression is invalid <a href=""more"">(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)