QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsexpressionlineedit.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexpressionlineedit.cpp
3  ------------------------
4  Date : 18.08.2016
5  Copyright : (C) 2016 Nyall Dawson
6  Email : nyall dot dawson 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 
16 #include "qgsexpressionlineedit.h"
17 #include "qgsfilterlineedit.h"
18 #include "qgsexpressioncontext.h"
19 #include "qgsapplication.h"
22 #include "qgscodeeditorsql.h"
23 #include "qgsproject.h"
24 #include "qgsvectorlayer.h"
25 #include <QHBoxLayout>
26 #include <QVBoxLayout>
27 #include <QToolButton>
28 
29 
31  : QWidget( parent )
32  , mExpressionDialogTitle( tr( "Expression Dialog" ) )
33 {
34  mButton = new QToolButton();
35  mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
36  mButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpression.svg" ) ) );
37  connect( mButton, &QAbstractButton::clicked, this, &QgsExpressionLineEdit::editExpression );
38 
39  //sets up layout
40  setMultiLine( false );
41 
42  mExpressionContext = QgsExpressionContext();
43  mExpressionContext << QgsExpressionContextUtils::globalScope()
45 }
46 
48 {
49  mExpressionDialogTitle = title;
50 }
51 
53 {
54  QString exp = expression();
55 
56  if ( multiLine && !mCodeEditor )
57  {
58  mCodeEditor = new QgsCodeEditorExpression();
59  mCodeEditor->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
60  delete mLineEdit;
61  mLineEdit = nullptr;
62 
63  QHBoxLayout *newLayout = new QHBoxLayout();
64  newLayout->setContentsMargins( 0, 0, 0, 0 );
65  newLayout->addWidget( mCodeEditor );
66 
67  QVBoxLayout *vLayout = new QVBoxLayout();
68  vLayout->addWidget( mButton );
69  vLayout->addStretch();
70  newLayout->addLayout( vLayout );
71 
72  delete layout();
73  setLayout( newLayout );
74 
75  setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
76 
77  setFocusProxy( mCodeEditor );
78  connect( mCodeEditor, &QsciScintilla::textChanged, this, static_cast < void ( QgsExpressionLineEdit::* )() > ( &QgsExpressionLineEdit::expressionEdited ) );
79 
80  setExpression( exp );
81  }
82  else if ( !multiLine && !mLineEdit )
83  {
84  delete mCodeEditor;
85  mCodeEditor = nullptr;
86  mLineEdit = new QgsFilterLineEdit();
87  mLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
88 
89  QHBoxLayout *newLayout = new QHBoxLayout();
90  newLayout->setContentsMargins( 0, 0, 0, 0 );
91  newLayout->addWidget( mLineEdit );
92  newLayout->addWidget( mButton );
93 
94  delete layout();
95  setLayout( newLayout );
96 
97  setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
98 
99  setFocusProxy( mLineEdit );
100  connect( mLineEdit, &QLineEdit::textChanged, this, static_cast < void ( QgsExpressionLineEdit::* )( const QString & ) > ( &QgsExpressionLineEdit::expressionEdited ) );
101 
102  setExpression( exp );
103  }
104 }
105 
107 {
108  return mExpectedOutputFormat;
109 }
110 
111 void QgsExpressionLineEdit::setExpectedOutputFormat( const QString &expected )
112 {
113  mExpectedOutputFormat = expected;
114 }
115 
117 {
118  mDa.reset( new QgsDistanceArea( da ) );
119 }
120 
122 {
123  if ( !mExpressionContextGenerator || mExpressionContextGenerator == mLayer )
124  mExpressionContextGenerator = layer;
125  mLayer = layer;
126 }
127 
129 {
130  if ( mLineEdit )
131  return mLineEdit->text();
132  else if ( mCodeEditor )
133  return mCodeEditor->text();
134 
135  return QString();
136 }
137 
138 bool QgsExpressionLineEdit::isValidExpression( QString *expressionError ) const
139 {
140  QString temp;
141  return QgsExpression::checkExpression( expression(), &mExpressionContext, expressionError ? *expressionError : temp );
142 }
143 
145 {
146  mExpressionContextGenerator = generator;
147 }
148 
149 void QgsExpressionLineEdit::setExpression( const QString &newExpression )
150 {
151  if ( mLineEdit )
152  mLineEdit->setText( newExpression );
153  else if ( mCodeEditor )
154  mCodeEditor->setText( newExpression );
155 }
156 
157 void QgsExpressionLineEdit::editExpression()
158 {
159  QString currentExpression = expression();
160 
161  QgsExpressionContext context = mExpressionContextGenerator ? mExpressionContextGenerator->createExpressionContext() : mExpressionContext;
162 
163  QgsExpressionBuilderDialog dlg( mLayer, currentExpression, this, QStringLiteral( "generic" ), context );
164  dlg.setExpectedOutputFormat( mExpectedOutputFormat );
165  if ( mDa )
166  {
167  dlg.setGeomCalculator( *mDa );
168  }
169  dlg.setWindowTitle( mExpressionDialogTitle );
170 
171  if ( dlg.exec() )
172  {
173  QString newExpression = dlg.expressionText();
174  setExpression( newExpression );
175  }
176 }
177 
178 void QgsExpressionLineEdit::expressionEdited()
179 {
180  emit expressionChanged( expression() );
181 }
182 
183 void QgsExpressionLineEdit::expressionEdited( const QString &expression )
184 {
185  updateLineEditStyle( expression );
186  emit expressionChanged( expression );
187 }
188 
190 {
191  if ( event->type() == QEvent::EnabledChange )
192  {
193  updateLineEditStyle( expression() );
194  }
195 }
196 
197 void QgsExpressionLineEdit::updateLineEditStyle( const QString &expression )
198 {
199  if ( !mLineEdit )
200  return;
201 
202  QPalette palette = mLineEdit->palette();
203  if ( !isEnabled() )
204  {
205  palette.setColor( QPalette::Text, Qt::gray );
206  }
207  else
208  {
209  bool isValid = true;
210  if ( !expression.isEmpty() )
211  {
212  isValid = isExpressionValid( expression );
213  }
214  if ( !isValid )
215  {
216  palette.setColor( QPalette::Text, Qt::red );
217  }
218  else
219  {
220  palette.setColor( QPalette::Text, Qt::black );
221  }
222  }
223  mLineEdit->setPalette( palette );
224 }
225 
226 bool QgsExpressionLineEdit::isExpressionValid( const QString &expressionStr )
227 {
228  QgsExpression expression( expressionStr );
229  expression.prepare( &mExpressionContext );
230  return !expression.hasParserError();
231 }
Class for parsing and evaluation of expressions (formerly called "search strings").
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
void setExpectedOutputFormat(const QString &expected)
Set the expected format string, which is shown in the dialog.
QString expectedOutputFormat() const
Returns the expected format string, which is shown in the expression builder dialog for the widget...
A QGIS expression editor based on QScintilla2.
void setMultiLine(bool multiLine)
Sets whether the widget should show a multiline text editor.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
QString expression() const
Returns the current expression shown in the widget.
static bool checkExpression(const QString &text, const QgsExpressionContext *context, QString &errorMessage)
Tests whether a string is a valid expression.
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
void setExpression(const QString &expression)
Sets the current expression to show in the widget.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setGeomCalculator(const QgsDistanceArea &da)
Sets geometry calculator used in distance/area calculations.
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
void setGeomCalculator(const QgsDistanceArea &distanceArea)
Set the geometry calculator used in the expression dialog.
Abstract interface for generating an expression context.
void expressionChanged(const QString &expression)
Emitted when the expression is changed.
void changeEvent(QEvent *event) override
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
void setExpressionDialogTitle(const QString &title)
Sets the title used in the expression builder dialog.
void setLayer(QgsVectorLayer *layer)
Sets a layer associated with the widget.
The QgsExpressionLineEdit widget includes a line edit for entering expressions together with a button...
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
void registerExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:411
void setExpectedOutputFormat(const QString &expected)
Set the expected format string, which is shown in the expression builder dialog for the widget...
bool isValidExpression(QString *expressionError=nullptr) const
Returns true if the current expression is valid.
QgsExpressionLineEdit(QWidget *parent=nullptr)
Constructor for QgsExpressionLineEdit.
Represents a vector layer which manages a vector based data sets.
A generic dialog for building expression strings.