QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgscodeeditorexpression.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscodeeditorexpressoin.cpp - An expression editor based on QScintilla
3 --------------------------------------
4 Date : 8.9.2018
5 Copyright : (C) 2018 by Matthias Kuhn
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 "qgsapplication.h"
18#include "qgssymbollayerutils.h"
19
20#include <QString>
21#include <QFont>
22
24 : QgsCodeEditor( parent )
25{
26 if ( !parent )
27 {
28 setTitle( tr( "Expression Editor" ) );
29 }
30 setAutoCompletionCaseSensitivity( false );
31 QgsCodeEditorExpression::initializeLexer(); // avoid cppcheck warning by explicitly specifying namespace
32}
33
35{
36 mVariables.clear();
37
38 const QStringList variableNames = context.filteredVariableNames();
39 for ( const QString &var : variableNames )
40 {
41 mVariables << '@' + var;
42 }
43
44 // always show feature variables in autocomplete -- they may not be available in the context
45 // at time of showing an expression builder, but they'll generally be available at evaluation time.
46 mVariables << QStringLiteral( "@feature" );
47 mVariables << QStringLiteral( "@id" );
48 mVariables << QStringLiteral( "@geometry" );
49
50 mContextFunctions = context.functionNames();
51
52 mFunctions.clear();
53
54 const int count = QgsExpression::functionCount();
55 for ( int i = 0; i < count; i++ )
56 {
58 if ( func->isDeprecated() ) // don't show deprecated functions
59 continue;
60 if ( func->isContextual() )
61 {
62 //don't show contextual functions by default - it's up the the QgsExpressionContext
63 //object to provide them if supported
64 continue;
65 }
66
67 QString signature = func->name();
68 if ( !signature.startsWith( '$' ) )
69 {
70 signature += '(';
71
72 QStringList paramNames;
73 const QgsExpressionFunction::ParameterList parameters = func->parameters();
74 for ( const QgsExpressionFunction::Parameter &param : parameters )
75 {
76 paramNames << param.name();
77 }
78
79 // No named parameters but there should be parameteres? Show an ellipsis at least
80 if ( parameters.isEmpty() && func->params() )
81 signature += QChar( 0x2026 );
82
83 signature += paramNames.join( ", " );
84
85 signature += ')';
86 }
87 mFunctions << signature;
88 }
89
90 updateApis();
91}
92
94{
95 mFieldNames.clear();
96
97 for ( const QgsField &field : fields )
98 {
99 mFieldNames << field.name();
100 }
101
102 updateApis();
103}
104
106{
107 QFont font = lexerFont();
109
110 mSqlLexer = new QgsLexerExpression( this );
111 mSqlLexer->setDefaultFont( font );
112 mSqlLexer->setDefaultColor( defaultColor );
113 mSqlLexer->setDefaultPaper( lexerColor( QgsCodeEditorColorScheme::ColorRole::Background ) );
114 mSqlLexer->setFont( font, -1 );
115 font.setBold( true );
116 mSqlLexer->setFont( font, QsciLexerSQL::Keyword );
117
118 font.setBold( false );
119 font.setItalic( true );
120 mSqlLexer->setFont( font, QsciLexerSQL::Comment );
121 mSqlLexer->setFont( font, QsciLexerSQL::CommentLine );
122
123 mSqlLexer->setColor( Qt::darkYellow, QsciLexerSQL::DoubleQuotedString ); // fields
124
125 mSqlLexer->setColor( defaultColor, QsciLexerSQL::Default );
126 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Comment ), QsciLexerSQL::Comment );
127 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentLine ), QsciLexerSQL::CommentLine );
128 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Number ), QsciLexerSQL::Number );
129 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Keyword ), QsciLexerSQL::Keyword );
130 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::SingleQuote ), QsciLexerSQL::SingleQuotedString );
131 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QsciLexerSQL::DoubleQuotedString );
132 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QsciLexerSQL::Operator );
133 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Identifier ), QsciLexerSQL::Identifier );
134 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedIdentifier ), QsciLexerSQL::QuotedIdentifier );
135 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedOperator ), QsciLexerSQL::QuotedOperator );
136
137 setLexer( mSqlLexer );
139}
140
141void QgsCodeEditorExpression::updateApis()
142{
143 mApis = new QgsSciApisExpression( mSqlLexer );
144
145 for ( const QString &var : std::as_const( mVariables ) )
146 {
147 mApis->add( var );
148 }
149
150 for ( const QString &function : std::as_const( mContextFunctions ) )
151 {
152 mApis->add( function );
153 }
154
155 for ( const QString &function : std::as_const( mFunctions ) )
156 {
157 mApis->add( function );
158 }
159
160 for ( const QString &fieldName : std::as_const( mFieldNames ) )
161 {
162 mApis->add( fieldName );
163 }
164
165 mApis->add( QString( "NULL" ) );
166 mApis->prepare();
167 mSqlLexer->setAPIs( mApis );
168}
169
171QgsLexerExpression::QgsLexerExpression( QObject *parent )
172 : QsciLexerSQL( parent )
173{
174 setBackslashEscapes( true );
175}
176
177const char *QgsLexerExpression::language() const
178{
179 return "QGIS Expression";
180}
181
182bool QgsLexerExpression::caseSensitive() const
183{
184 return false;
185}
186
187const char *QgsLexerExpression::wordCharacters() const
188{
189 return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@";
190}
191
192QgsSciApisExpression::QgsSciApisExpression( QsciLexer *lexer )
193 : QsciAPIs( lexer )
194{
195
196}
197
198QStringList QgsSciApisExpression::callTips( const QStringList &context, int commas, QsciScintilla::CallTipsStyle style, QList<int> &shifts )
199{
200 const QStringList originalTips = QsciAPIs::callTips( context, commas, style, shifts );
201 QStringList lowercaseTips;
202 for ( const QString &tip : originalTips )
203 lowercaseTips << tip.toLower();
204
205 return lowercaseTips;
206}
@ QuotedOperator
Quoted operator color.
@ DoubleQuote
Double quote color.
@ QuotedIdentifier
Quoted identifier color.
@ CommentLine
Line comment color.
@ SingleQuote
Single quote color.
void initializeLexer() override
Called when the dialect specific code lexer needs to be initialized (or reinitialized).
QgsCodeEditorExpression(QWidget *parent=nullptr)
Constructor for QgsCodeEditorExpression.
void setExpressionContext(const QgsExpressionContext &context)
Variables and functions from this expression context will be added to the API.
void setFields(const QgsFields &fields)
Field names will be added to the API.
A text editor based on QScintilla2.
Definition: qgscodeeditor.h:42
void runPostLexerConfigurationTasks()
Performs tasks which must be run after a lexer has been set for the widget.
void setTitle(const QString &title)
Set the widget title.
QFont lexerFont() const
Returns the font to use in the lexer.
QColor lexerColor(QgsCodeEditorColorScheme::ColorRole role) const
Returns the color to use in the lexer for the specified role.
static QColor defaultColor(QgsCodeEditorColorScheme::ColorRole role, const QString &theme=QString())
Returns the default color for the specified role.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QStringList functionNames() const
Retrieves a list of function names contained in the context.
QStringList filteredVariableNames() const
Returns a filtered list of variables names set by all scopes in the context.
Represents a single parameter passed to a function.
A abstract base class for defining QgsExpression functions.
QList< QgsExpressionFunction::Parameter > ParameterList
List of parameters, used for function definition.
bool isContextual() const
Returns whether the function is only available if provided by a QgsExpressionContext object.
int params() const
The number of parameters this function takes.
virtual bool isDeprecated() const
Returns true if the function is deprecated and should not be presented as a valid option to users in ...
QString name() const
The name of the function.
const QgsExpressionFunction::ParameterList & parameters() const
Returns the list of named parameters for the function, if set.
static const QList< QgsExpressionFunction * > & Functions()
static int functionCount()
Returns the number of functions defined in the parser.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
QString name
Definition: qgsfield.h:60
Container of fields for a vector layer.
Definition: qgsfields.h:45
const QgsField & field
Definition: qgsfield.h:463