QGIS API Documentation 3.41.0-Master (57ec4277f5e)
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#include "moc_qgsexpressionbuilderdialog.cpp"
18#include "qgssettings.h"
19#include "qgsguiutils.h"
20#include "qgsgui.h"
21#include <QMessageBox>
22
23QgsExpressionBuilderDialog::QgsExpressionBuilderDialog( QgsVectorLayer *layer, const QString &startText, QWidget *parent, const QString &key, const QgsExpressionContext &context )
24 : QDialog( parent )
25 , mInitialText( startText )
26 , mRecentKey( key )
27{
28 setupUi( this );
30
31 connect( builder, &QgsExpressionBuilderWidget::parserErrorChanged, this, &QgsExpressionBuilderDialog::syncOkButtonEnabledState );
32 connect( builder, &QgsExpressionBuilderWidget::evalErrorChanged, this, &QgsExpressionBuilderDialog::syncOkButtonEnabledState );
33
34 builder->setExpressionContext( context );
35 builder->setLayer( layer );
36 builder->setExpressionText( startText );
37 builder->expressionTree()->loadRecent( mRecentKey );
38 builder->expressionTree()->loadUserExpressions();
39
40
41 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionBuilderDialog::showHelp );
42}
43
48
50{
51 builder->setExpressionText( text );
52}
53
55{
56 return builder->expressionText();
57}
58
60{
61 return builder->expectedOutputFormat();
62}
63
65{
66 builder->setExpectedOutputFormat( expected );
67}
68
70{
71 return builder->expressionContext();
72}
73
75{
76 builder->setExpressionContext( context );
77}
78
80{
81 QDialog::done( r );
82}
83
85{
86 builder->expressionTree()->saveToRecent( builder->expressionText(), mRecentKey );
87 QDialog::accept();
88}
89
91{
92 if ( builder->expressionText() != mInitialText )
93 {
94 QgsSettings settings;
95 const bool askToDiscardEditedExpression = settings.value( QStringLiteral( "askToDiscardEditedExpression" ), true, QgsSettings::Gui ).toBool();
96
97 if ( askToDiscardEditedExpression )
98 {
99 QMessageBox 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 );
100 confirmMessage.setCheckBox( new QCheckBox( tr( "Don't show this message again" ) ) );
101 confirmMessage.checkBox()->setChecked( false );
102 confirmMessage.button( QMessageBox::Yes )->setText( tr( "Discard changes" ) );
103
104 int res = confirmMessage.exec();
105
106 if ( confirmMessage.checkBox()->isChecked() )
107 {
108 settings.setValue( QStringLiteral( "askToDiscardEditedExpression" ), false, QgsSettings::Gui );
109 }
110
111 if ( res != QMessageBox::Yes )
112 return;
113 }
114 }
115
116 QDialog::reject();
117}
118
120{
121 // Store in child widget only.
122 builder->setGeomCalculator( da );
123}
124
126{
127 return mAllowEvalErrors;
128}
129
131{
132 if ( allowEvalErrors == mAllowEvalErrors )
133 return;
134
135 mAllowEvalErrors = allowEvalErrors;
136 syncOkButtonEnabledState();
138}
139
140void QgsExpressionBuilderDialog::showHelp()
141{
142 QgsHelp::openHelp( QStringLiteral( "working_with_vector/expression.html" ) );
143}
144
145void QgsExpressionBuilderDialog::syncOkButtonEnabledState()
146{
147 QPushButton *okButton = buttonBox->button( QDialogButtonBox::Ok );
148
149 if ( builder->parserError() )
150 okButton->setEnabled( false );
151 else
152 okButton->setEnabled( true );
153}
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 a 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:210
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
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 data sets.