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