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