QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
qgsexpressionbuilderdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgisexpressionbuilderdialog.h - A generic expression builder dialog.
3 --------------------------------------
4 Date : 29-May-2011
5 Copyright : (C) 2011 by Nathan Woodrow
6 Email : woodrow.nathan at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgsgui.h"
19#include "qgsguiutils.h"
20#include "qgssettings.h"
21
22#include <QMessageBox>
23#include <QString>
24
25#include "moc_qgsexpressionbuilderdialog.cpp"
26
27using namespace Qt::StringLiterals;
28
29QgsExpressionBuilderDialog::QgsExpressionBuilderDialog( QgsVectorLayer *layer, const QString &startText, QWidget *parent, const QString &key, const QgsExpressionContext &context )
30 : QDialog( parent )
31 , mInitialText( startText )
32 , mRecentKey( key )
33{
34 setupUi( this );
36
37 connect( builder, &QgsExpressionBuilderWidget::parserErrorChanged, this, &QgsExpressionBuilderDialog::syncOkButtonEnabledState );
38 connect( builder, &QgsExpressionBuilderWidget::evalErrorChanged, this, &QgsExpressionBuilderDialog::syncOkButtonEnabledState );
39
40 builder->setExpressionContext( context );
41 builder->setLayer( layer );
42 builder->setExpressionText( startText );
43 builder->expressionTree()->loadRecent( mRecentKey );
44 builder->expressionTree()->loadUserExpressions();
45
46
47 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionBuilderDialog::showHelp );
48}
49
54
56{
57 builder->setExpressionText( text );
58}
59
61{
62 return builder->expressionText();
63}
64
66{
67 return builder->expectedOutputFormat();
68}
69
71{
72 builder->setExpectedOutputFormat( expected );
73}
74
76{
77 return builder->expressionContext();
78}
79
81{
82 builder->setExpressionContext( context );
83}
84
86{
87 QDialog::done( r );
88}
89
91{
92 builder->expressionTree()->saveToRecent( builder->expressionText(), mRecentKey );
93 QDialog::accept();
94}
95
97{
98 if ( builder->expressionText() != mInitialText )
99 {
100 QgsSettings settings;
101 const bool askToDiscardEditedExpression = settings.value( u"askToDiscardEditedExpression"_s, true, QgsSettings::Gui ).toBool();
102
103 if ( askToDiscardEditedExpression )
104 {
105 QMessageBox
106 confirmMessage( QMessageBox::Question, tr( "Expression was Edited" ), tr( "The changes to the expression will be discarded. Would you like to continue?" ), QMessageBox::Yes | QMessageBox::No, this );
107 confirmMessage.setCheckBox( new QCheckBox( tr( "Don't show this message again" ) ) );
108 confirmMessage.checkBox()->setChecked( false );
109 confirmMessage.button( QMessageBox::Yes )->setText( tr( "Discard changes" ) );
110
111 int res = confirmMessage.exec();
112
113 if ( confirmMessage.checkBox()->isChecked() )
114 {
115 settings.setValue( u"askToDiscardEditedExpression"_s, false, QgsSettings::Gui );
116 }
117
118 if ( res != QMessageBox::Yes )
119 return;
120 }
121 }
122
123 QDialog::reject();
124}
125
127{
128 // Store in child widget only.
129 builder->setGeomCalculator( da );
130}
131
133{
134 return mAllowEvalErrors;
135}
136
138{
139 if ( allowEvalErrors == mAllowEvalErrors )
140 return;
141
142 mAllowEvalErrors = allowEvalErrors;
143 syncOkButtonEnabledState();
145}
146
147void QgsExpressionBuilderDialog::showHelp()
148{
149 QgsHelp::openHelp( u"working_with_vector/expression.html"_s );
150}
151
152void QgsExpressionBuilderDialog::syncOkButtonEnabledState()
153{
154 QPushButton *okButton = buttonBox->button( QDialogButtonBox::Ok );
155
156 if ( builder->parserError() )
157 okButton->setEnabled( false );
158 else
159 okButton->setEnabled( true );
160}
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
void done(int r) override
Is called when the dialog get accepted or rejected Used to save geometry.
void setGeomCalculator(const QgsDistanceArea &da)
Sets geometry calculator used in distance/area calculations.
QgsExpressionBuilderDialog(QgsVectorLayer *layer, const QString &startText=QString(), QWidget *parent SIP_TRANSFERTHIS=nullptr, const QString &key="generic", const QgsExpressionContext &context=QgsExpressionContext())
void setExpressionText(const QString &text)
void setExpectedOutputFormat(const QString &expected)
Set the expected format string, which is shown in the dialog.
QgsExpressionBuilderWidget * expressionBuilder()
The builder widget that is used by the dialog.
void allowEvalErrorsChanged()
Allow accepting expressions with evaluation errors.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context for the dialog.
void setAllowEvalErrors(bool allowEvalErrors)
Allow accepting expressions with evaluation errors.
QString expectedOutputFormat()
Returns the expected format string, which is shown in the dialog.
QgsExpressionContext expressionContext() const
Returns the expression context for the dialog.
A reusable widget that can be used to build an expression string.
void parserErrorChanged()
Will be set to true if the current expression text reported a parser error with the context.
void evalErrorChanged()
Will be set to true if the current expression text reported an eval error with the context.
void setExpressionText(const QString &expression)
Sets the expression string for the widget.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:224
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:41
Stores settings for use within QGIS.
Definition qgssettings.h:68
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Represents a vector layer which manages a vector based dataset.