QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsexpressionbuilderdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgisexpressionbuilderdialog.h - A generic expression string 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 
21 QgsExpressionBuilderDialog::QgsExpressionBuilderDialog( QgsVectorLayer *layer, const QString &startText, QWidget *parent, const QString &key, const QgsExpressionContext &context )
22  : QDialog( parent )
23  , mRecentKey( key )
24 {
25  setupUi( this );
27 
28  connect( builder, &QgsExpressionBuilderWidget::parserErrorChanged, this, &QgsExpressionBuilderDialog::syncOkButtonEnabledState );
29  connect( builder, &QgsExpressionBuilderWidget::evalErrorChanged, this, &QgsExpressionBuilderDialog::syncOkButtonEnabledState );
30 
31  builder->setExpressionContext( context );
32  builder->setLayer( layer );
33  builder->setExpressionText( startText );
34  builder->expressionTree()->loadRecent( mRecentKey );
35  builder->expressionTree()->loadUserExpressions( );
36 
37  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionBuilderDialog::showHelp );
38 }
39 
41 {
42  return builder;
43 }
44 
46 {
47  builder->setExpressionText( text );
48 }
49 
51 {
52  return builder->expressionText();
53 }
54 
56 {
57  return builder->expectedOutputFormat();
58 }
59 
61 {
62  builder->setExpectedOutputFormat( expected );
63 }
64 
66 {
67  return builder->expressionContext();
68 }
69 
71 {
72  builder->setExpressionContext( context );
73 }
74 
76 {
77  QDialog::done( r );
78 }
79 
81 {
82  builder->expressionTree()->saveToRecent( builder->expressionText(), mRecentKey );
83  QDialog::accept();
84 }
85 
87 {
88  // Store in child widget only.
89  builder->setGeomCalculator( da );
90 }
91 
93 {
94  return mAllowEvalErrors;
95 }
96 
98 {
99  if ( allowEvalErrors == mAllowEvalErrors )
100  return;
101 
102  mAllowEvalErrors = allowEvalErrors;
103  syncOkButtonEnabledState();
104  emit allowEvalErrorsChanged();
105 }
106 
107 void QgsExpressionBuilderDialog::showHelp()
108 {
109  QgsHelp::openHelp( QStringLiteral( "working_with_vector/expression.html" ) );
110 }
111 
112 void QgsExpressionBuilderDialog::syncOkButtonEnabledState()
113 {
114  QPushButton *okButton = buttonBox->button( QDialogButtonBox::Ok );
115 
116  if ( builder->parserError() )
117  okButton->setEnabled( false );
118  else if ( !builder->evalError() || mAllowEvalErrors )
119  okButton->setEnabled( true );
120  else
121  okButton->setEnabled( true );
122 }
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsExpressionBuilderWidget
A reusable widget that can be used to build a expression string. See QgsExpressionBuilderDialog for e...
Definition: qgsexpressionbuilderwidget.h:42
QgsExpressionBuilderDialog::allowEvalErrors
bool allowEvalErrors
Definition: qgsexpressionbuilderdialog.h:47
qgsgui.h
QgsExpressionBuilderDialog::allowEvalErrorsChanged
void allowEvalErrorsChanged()
Allow accepting expressions with evaluation errors.
QgsGui::enableAutoGeometryRestore
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:180
QgsExpressionBuilderDialog::setExpressionContext
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context for the dialog.
Definition: qgsexpressionbuilderdialog.cpp:70
QgsExpressionBuilderDialog::expressionBuilder
QgsExpressionBuilderWidget * expressionBuilder()
The builder widget that is used by the dialog.
Definition: qgsexpressionbuilderdialog.cpp:40
QgsExpressionBuilderDialog::expectedOutputFormat
QString expectedOutputFormat()
Returns the expected format string, which is shown in the dialog.
Definition: qgsexpressionbuilderdialog.cpp:55
QgsExpressionBuilderDialog::accept
void accept() override
Definition: qgsexpressionbuilderdialog.cpp:80
QgsExpressionBuilderDialog::setAllowEvalErrors
void setAllowEvalErrors(bool allowEvalErrors)
Allow accepting expressions with evaluation errors.
Definition: qgsexpressionbuilderdialog.cpp:97
QgsExpressionBuilderDialog::setExpressionText
void setExpressionText(const QString &text)
Definition: qgsexpressionbuilderdialog.cpp:45
QgsExpressionBuilderDialog::setGeomCalculator
void setGeomCalculator(const QgsDistanceArea &da)
Sets geometry calculator used in distance/area calculations.
Definition: qgsexpressionbuilderdialog.cpp:86
QgsExpressionBuilderWidget::evalErrorChanged
void evalErrorChanged()
Will be set to true if the current expression text reported an eval error with the context.
QgsExpressionBuilderDialog::expressionText
QString expressionText()
Definition: qgsexpressionbuilderdialog.cpp:50
QgsHelp::openHelp
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsExpressionBuilderDialog::expressionContext
QgsExpressionContext expressionContext() const
Returns the expression context for the dialog.
Definition: qgsexpressionbuilderdialog.cpp:65
qgssettings.h
QgsDistanceArea
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Definition: qgsdistancearea.h:52
QgsExpressionBuilderDialog::setExpectedOutputFormat
void setExpectedOutputFormat(const QString &expected)
Set the expected format string, which is shown in the dialog.
Definition: qgsexpressionbuilderdialog.cpp:60
qgsguiutils.h
QgsExpressionBuilderWidget::parserErrorChanged
void parserErrorChanged()
Will be set to true if the current expression text reported a parser error with the context.
QgsExpressionBuilderDialog::QgsExpressionBuilderDialog
QgsExpressionBuilderDialog(QgsVectorLayer *layer, const QString &startText=QString(), QWidget *parent SIP_TRANSFERTHIS=nullptr, const QString &key="generic", const QgsExpressionContext &context=QgsExpressionContext())
Definition: qgsexpressionbuilderdialog.cpp:21
QgsExpressionBuilderDialog::done
void done(int r) override
Is called when the dialog get accepted or rejected Used to save geometry.
Definition: qgsexpressionbuilderdialog.cpp:75
qgsexpressionbuilderdialog.h