QGIS API Documentation 3.41.0-Master (3440c17df1d)
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
93 if ( builder->expressionText() != mInitialText )
94 {
95
96 QgsSettings settings;
97 const bool askToDiscardEditedExpression = settings.value( QStringLiteral( "askToDiscardEditedExpression" ), true, QgsSettings::Gui ).toBool();
98
99 if ( askToDiscardEditedExpression )
100 {
101 QMessageBox confirmMessage( QMessageBox::Question,
102 tr( "Expression was Edited" ),
103 tr( "The changes to the expression will be discarded. Would you like to continue?" ),
104 QMessageBox::Yes | QMessageBox::No,
105 this );
106 confirmMessage.setCheckBox( new QCheckBox( tr( "Don't show this message again" ) ) );
107 confirmMessage.checkBox()->setChecked( false );
108 confirmMessage.button( QMessageBox::Yes )->setText( tr( "Discard changes" ) );
109
110 int res = confirmMessage.exec();
111
112 if ( confirmMessage.checkBox()->isChecked() )
113 {
114 settings.setValue( QStringLiteral( "askToDiscardEditedExpression" ), false, QgsSettings::Gui );
115 }
116
117 if ( res != QMessageBox::Yes )
118 return;
119 }
120 }
121
122 QDialog::reject();
123}
124
126{
127 // Store in child widget only.
128 builder->setGeomCalculator( da );
129}
130
132{
133 return mAllowEvalErrors;
134}
135
137{
138 if ( allowEvalErrors == mAllowEvalErrors )
139 return;
140
141 mAllowEvalErrors = allowEvalErrors;
142 syncOkButtonEnabledState();
144}
145
146void QgsExpressionBuilderDialog::showHelp()
147{
148 QgsHelp::openHelp( QStringLiteral( "working_with_vector/expression.html" ) );
149}
150
151void QgsExpressionBuilderDialog::syncOkButtonEnabledState()
152{
153 QPushButton *okButton = buttonBox->button( QDialogButtonBox::Ok );
154
155 if ( builder->parserError() )
156 okButton->setEnabled( false );
157 else
158 okButton->setEnabled( true );
159}
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:209
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.