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;
73 void 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 );
155 void QgsExpressionPreviewWidget::linkActivated(
const QString & )
157 QgsMessageViewer mv(
this, QgsGuiUtils::ModalDialogFlags,
false );
158 mv.setWindowTitle( tr(
"More Info on Expression Error" ) );
159 mv.setMessageAsHtml( mToolTip );
163 void 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 ) );
180 void QgsExpressionPreviewWidget::setParserError(
bool parserError )
193 void QgsExpressionPreviewWidget::setEvalError(
bool evalError )
207 void 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 );