QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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 <QString>
32#include <QToolButton>
33#include <QVBoxLayout>
34
35#include "moc_qgsexpressionlineedit.cpp"
36
37using namespace Qt::StringLiterals;
38
40 : QWidget( parent )
41 , mExpressionDialogTitle( tr( "Expression Builder" ) )
42{
43 mButton = new QToolButton();
44 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
45 mButton->setIcon( QgsApplication::getThemeIcon( u"/mIconExpression.svg"_s ) );
46 connect( mButton, &QAbstractButton::clicked, this, &QgsExpressionLineEdit::editExpression );
47
48 //sets up layout
49 setMultiLine( false );
50
51 mExpressionContext = QgsExpressionContext();
53}
54
56
58{
59 mExpressionDialogTitle = title;
60}
61
63{
64 const QString exp = expression();
65
66 if ( multiLine && !mCodeEditor )
67 {
68 mCodeEditor = new QgsCodeEditorExpression();
69 mCodeEditor->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
70 delete mLineEdit;
71 mLineEdit = nullptr;
72
73 QHBoxLayout *newLayout = new QHBoxLayout();
74 newLayout->setContentsMargins( 0, 0, 0, 0 );
75 newLayout->addWidget( mCodeEditor );
76
77 QVBoxLayout *vLayout = new QVBoxLayout();
78 vLayout->addWidget( mButton );
79 vLayout->addStretch();
80 newLayout->addLayout( vLayout );
81
82 delete layout();
83 setLayout( newLayout );
84
85 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
86
87 setFocusProxy( mCodeEditor );
88 connect( mCodeEditor, &QsciScintilla::textChanged, this, static_cast<void ( QgsExpressionLineEdit::* )()>( &QgsExpressionLineEdit::expressionEdited ) );
89
90 setExpression( exp );
91 }
92 else if ( !multiLine && !mLineEdit )
93 {
94 delete mCodeEditor;
95 mCodeEditor = nullptr;
96 mLineEdit = new QgsFilterLineEdit();
97 mLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
98
99 QHBoxLayout *newLayout = new QHBoxLayout();
100 newLayout->setContentsMargins( 0, 0, 0, 0 );
101 newLayout->addWidget( mLineEdit );
102 newLayout->addWidget( mButton );
103
104 delete layout();
105 setLayout( newLayout );
106
107 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
108
109 setFocusProxy( mLineEdit );
110 connect( mLineEdit, &QLineEdit::textChanged, this, static_cast<void ( QgsExpressionLineEdit::* )( const QString & )>( &QgsExpressionLineEdit::expressionEdited ) );
111
112 setExpression( exp );
113 }
114}
115
117{
118 return mExpectedOutputFormat;
119}
120
122{
123 mExpectedOutputFormat = expected;
124}
125
127{
128 mDa = std::make_unique<QgsDistanceArea>( da );
129}
130
132{
133 if ( !mExpressionContextGenerator || mExpressionContextGenerator == mLayer )
134 mExpressionContextGenerator = layer;
135 mLayer = layer;
136}
137
139{
140 if ( mLineEdit )
141 return mLineEdit->text();
142 else if ( mCodeEditor )
143 return mCodeEditor->text();
144
145 return QString();
146}
147
148bool QgsExpressionLineEdit::isValidExpression( QString *expressionError ) const
149{
150 QString temp;
151 const QgsExpressionContext context = mExpressionContextGenerator ? mExpressionContextGenerator->createExpressionContext() : mExpressionContext;
152 return QgsExpression::checkExpression( expression(), &context, expressionError ? *expressionError : temp );
153}
154
156{
157 mExpressionContextGenerator = generator;
158}
159
160void QgsExpressionLineEdit::setExpression( const QString &newExpression )
161{
162 if ( mLineEdit )
163 mLineEdit->setText( newExpression );
164 else if ( mCodeEditor )
165 mCodeEditor->setText( newExpression );
166}
167
168void QgsExpressionLineEdit::editExpression()
169{
170 const QString currentExpression = expression();
171
172 const QgsExpressionContext context = mExpressionContextGenerator ? mExpressionContextGenerator->createExpressionContext() : mExpressionContext;
173
174 QgsExpressionBuilderDialog dlg( mLayer, currentExpression, this, u"generic"_s, context );
175 dlg.setExpectedOutputFormat( mExpectedOutputFormat );
176 if ( mDa )
177 {
178 dlg.setGeomCalculator( *mDa );
179 }
180 dlg.setWindowTitle( mExpressionDialogTitle );
181
182 if ( dlg.exec() )
183 {
184 const QString newExpression = dlg.expressionText();
185 setExpression( newExpression );
186 }
187}
188
189void QgsExpressionLineEdit::expressionEdited()
190{
192}
193
194void QgsExpressionLineEdit::expressionEdited( const QString &expression )
195{
196 updateLineEditStyle( expression );
198}
199
201{
202 if ( event->type() == QEvent::EnabledChange )
203 {
204 updateLineEditStyle( expression() );
205 }
206}
207
208void QgsExpressionLineEdit::updateLineEditStyle( const QString &expression )
209{
210 if ( !mLineEdit )
211 return;
212
213 QPalette appPalette = qApp->palette();
214 QPalette palette = mLineEdit->palette();
215 if ( !isEnabled() )
216 {
217 palette.setColor( QPalette::Text, appPalette.color( QPalette::Disabled, QPalette::Text ) );
218 }
219 else
220 {
221 bool isValid = true;
222 if ( !expression.isEmpty() )
223 {
224 isValid = isExpressionValid( expression );
225 }
226 if ( !isValid )
227 {
228 palette.setColor( QPalette::Text, Qt::red );
229 }
230 else
231 {
232 palette.setColor( QPalette::Text, appPalette.color( QPalette::Text ) );
233 }
234 }
235 mLineEdit->setPalette( palette );
236}
237
238bool QgsExpressionLineEdit::isExpressionValid( const QString &expressionStr )
239{
240 QgsExpression expression( expressionStr );
241
242 const QgsExpressionContext context = mExpressionContextGenerator ? mExpressionContextGenerator->createExpressionContext() : mExpressionContext;
243 expression.prepare( &mExpressionContext );
244 return !expression.hasParserError();
245}
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.