QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
24#include "moc_qgsexpressionbuilderdialog.cpp"
25
26QgsExpressionBuilderDialog::QgsExpressionBuilderDialog( QgsVectorLayer *layer, const QString &startText, QWidget *parent, const QString &key, const QgsExpressionContext &context )
27 : QDialog( parent )
28 , mInitialText( startText )
29 , mRecentKey( key )
30{
31 setupUi( this );
33
34 connect( builder, &QgsExpressionBuilderWidget::parserErrorChanged, this, &QgsExpressionBuilderDialog::syncOkButtonEnabledState );
35 connect( builder, &QgsExpressionBuilderWidget::evalErrorChanged, this, &QgsExpressionBuilderDialog::syncOkButtonEnabledState );
36
37 builder->setExpressionContext( context );
38 builder->setLayer( layer );
39 builder->setExpressionText( startText );
40 builder->expressionTree()->loadRecent( mRecentKey );
41 builder->expressionTree()->loadUserExpressions();
42
43
44 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionBuilderDialog::showHelp );
45}
46
51
53{
54 builder->setExpressionText( text );
55}
56
58{
59 return builder->expressionText();
60}
61
63{
64 return builder->expectedOutputFormat();
65}
66
68{
69 builder->setExpectedOutputFormat( expected );
70}
71
73{
74 return builder->expressionContext();
75}
76
78{
79 builder->setExpressionContext( context );
80}
81
83{
84 QDialog::done( r );
85}
86
88{
89 builder->expressionTree()->saveToRecent( builder->expressionText(), mRecentKey );
90 QDialog::accept();
91}
92
94{
95 if ( builder->expressionText() != mInitialText )
96 {
97 QgsSettings settings;
98 const bool askToDiscardEditedExpression = settings.value( QStringLiteral( "askToDiscardEditedExpression" ), true, QgsSettings::Gui ).toBool();
99
100 if ( askToDiscardEditedExpression )
101 {
102 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 );
103 confirmMessage.setCheckBox( new QCheckBox( tr( "Don't show this message again" ) ) );
104 confirmMessage.checkBox()->setChecked( false );
105 confirmMessage.button( QMessageBox::Yes )->setText( tr( "Discard changes" ) );
106
107 int res = confirmMessage.exec();
108
109 if ( confirmMessage.checkBox()->isChecked() )
110 {
111 settings.setValue( QStringLiteral( "askToDiscardEditedExpression" ), false, QgsSettings::Gui );
112 }
113
114 if ( res != QMessageBox::Yes )
115 return;
116 }
117 }
118
119 QDialog::reject();
120}
121
123{
124 // Store in child widget only.
125 builder->setGeomCalculator( da );
126}
127
129{
130 return mAllowEvalErrors;
131}
132
134{
135 if ( allowEvalErrors == mAllowEvalErrors )
136 return;
137
138 mAllowEvalErrors = allowEvalErrors;
139 syncOkButtonEnabledState();
141}
142
143void QgsExpressionBuilderDialog::showHelp()
144{
145 QgsHelp::openHelp( QStringLiteral( "working_with_vector/expression.html" ) );
146}
147
148void QgsExpressionBuilderDialog::syncOkButtonEnabledState()
149{
150 QPushButton *okButton = buttonBox->button( QDialogButtonBox::Ok );
151
152 if ( builder->parserError() )
153 okButton->setEnabled( false );
154 else
155 okButton->setEnabled( true );
156}
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:221
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:38
Stores settings for use within QGIS.
Definition qgssettings.h:65
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.