QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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 "qgssettings.h"
18#include "qgsguiutils.h"
19#include "qgsgui.h"
20#include <QMessageBox>
21
22QgsExpressionBuilderDialog::QgsExpressionBuilderDialog( QgsVectorLayer *layer, const QString &startText, QWidget *parent, const QString &key, const QgsExpressionContext &context )
23 : QDialog( parent )
24 , mInitialText( startText )
25 , mRecentKey( key )
26{
27 setupUi( this );
29
30 connect( builder, &QgsExpressionBuilderWidget::parserErrorChanged, this, &QgsExpressionBuilderDialog::syncOkButtonEnabledState );
31 connect( builder, &QgsExpressionBuilderWidget::evalErrorChanged, this, &QgsExpressionBuilderDialog::syncOkButtonEnabledState );
32
33 builder->setExpressionContext( context );
34 builder->setLayer( layer );
35 builder->setExpressionText( startText );
36 builder->expressionTree()->loadRecent( mRecentKey );
37 builder->expressionTree()->loadUserExpressions( );
38
39
40 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionBuilderDialog::showHelp );
41}
42
44{
45 return builder;
46}
47
49{
50 builder->setExpressionText( text );
51}
52
54{
55 return builder->expressionText();
56}
57
59{
60 return builder->expectedOutputFormat();
61}
62
64{
65 builder->setExpectedOutputFormat( expected );
66}
67
69{
70 return builder->expressionContext();
71}
72
74{
75 builder->setExpressionContext( context );
76}
77
79{
80 QDialog::done( r );
81}
82
84{
85 builder->expressionTree()->saveToRecent( builder->expressionText(), mRecentKey );
86 QDialog::accept();
87}
88
90{
91
92 if ( builder->expressionText() != mInitialText )
93 {
94
95 QgsSettings settings;
96 const bool askToDiscardEditedExpression = settings.value( QStringLiteral( "askToDiscardEditedExpression" ), true, QgsSettings::Gui ).toBool();
97
98 if ( askToDiscardEditedExpression )
99 {
100 QMessageBox confirmMessage( QMessageBox::Question,
101 tr( "Expression was Edited" ),
102 tr( "The changes to the expression will be discarded. Would you like to continue?" ),
103 QMessageBox::Yes | QMessageBox::No,
104 this );
105 confirmMessage.setCheckBox( new QCheckBox( tr( "Don't show this message again" ) ) );
106 confirmMessage.checkBox()->setChecked( false );
107 confirmMessage.button( QMessageBox::Yes )->setText( tr( "Discard changes" ) );
108
109 int res = confirmMessage.exec();
110
111 if ( confirmMessage.checkBox()->isChecked() )
112 {
113 settings.setValue( QStringLiteral( "askToDiscardEditedExpression" ), false, QgsSettings::Gui );
114 }
115
116 if ( res != QMessageBox::Yes )
117 return;
118 }
119 }
120
121 QDialog::reject();
122}
123
125{
126 // Store in child widget only.
127 builder->setGeomCalculator( da );
128}
129
131{
132 return mAllowEvalErrors;
133}
134
136{
137 if ( allowEvalErrors == mAllowEvalErrors )
138 return;
139
140 mAllowEvalErrors = allowEvalErrors;
141 syncOkButtonEnabledState();
143}
144
145void QgsExpressionBuilderDialog::showHelp()
146{
147 QgsHelp::openHelp( QStringLiteral( "working_with_vector/expression.html" ) );
148}
149
150void QgsExpressionBuilderDialog::syncOkButtonEnabledState()
151{
152 QPushButton *okButton = buttonBox->button( QDialogButtonBox::Ok );
153
154 if ( builder->parserError() )
155 okButton->setEnabled( false );
156 else
157 okButton->setEnabled( true );
158}
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.
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:194
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.