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