QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
17#include "qgsexpression.h"
18
19#include <QString>
20#include <QFont>
21
23 : QgsCodeEditor( parent )
24{
25 if ( !parent )
26 {
27 setTitle( tr( "Expression Editor" ) );
28 }
29 setAutoCompletionCaseSensitivity( false );
30 QgsCodeEditorExpression::initializeLexer(); // avoid cppcheck warning by explicitly specifying namespace
31}
32
34{
36}
37
39{
40 mVariables.clear();
41
42 const QStringList variableNames = context.filteredVariableNames();
43 for ( const QString &var : variableNames )
44 {
45 mVariables << '@' + var;
46 }
47
48 // always show feature variables in autocomplete -- they may not be available in the context
49 // at time of showing an expression builder, but they'll generally be available at evaluation time.
50 mVariables << QStringLiteral( "@feature" );
51 mVariables << QStringLiteral( "@id" );
52 mVariables << QStringLiteral( "@geometry" );
53
54 mContextFunctions = context.functionNames();
55
56 mFunctions.clear();
57
58 const int count = QgsExpression::functionCount();
59 for ( int i = 0; i < count; i++ )
60 {
62 if ( func->isDeprecated() ) // don't show deprecated functions
63 continue;
64 if ( func->isContextual() )
65 {
66 //don't show contextual functions by default - it's up the the QgsExpressionContext
67 //object to provide them if supported
68 continue;
69 }
70
71 QString signature = func->name();
72 if ( !signature.startsWith( '$' ) )
73 {
74 signature += '(';
75
76 QStringList paramNames;
77 const QgsExpressionFunction::ParameterList parameters = func->parameters();
78 for ( const QgsExpressionFunction::Parameter &param : parameters )
79 {
80 paramNames << param.name();
81 }
82
83 // No named parameters but there should be parameteres? Show an ellipsis at least
84 if ( parameters.isEmpty() && func->params() )
85 signature += QChar( 0x2026 );
86
87 signature += paramNames.join( ", " );
88
89 signature += ')';
90 }
91 mFunctions << signature;
92 }
93
94 updateApis();
95}
96
98{
99 mFieldNames.clear();
100
101 for ( const QgsField &field : fields )
102 {
103 mFieldNames << field.name();
104 }
105
106 updateApis();
107}
108
110{
111 QFont font = lexerFont();
113
114 mSqlLexer = new QgsLexerExpression( this );
115 mSqlLexer->setDefaultFont( font );
116 mSqlLexer->setDefaultColor( defaultColor );
117 mSqlLexer->setDefaultPaper( lexerColor( QgsCodeEditorColorScheme::ColorRole::Background ) );
118 mSqlLexer->setFont( font, -1 );
119 font.setBold( true );
120 mSqlLexer->setFont( font, QsciLexerSQL::Keyword );
121
122 font.setBold( false );
123 font.setItalic( true );
124 mSqlLexer->setFont( font, QsciLexerSQL::Comment );
125 mSqlLexer->setFont( font, QsciLexerSQL::CommentLine );
126
127 mSqlLexer->setColor( Qt::darkYellow, QsciLexerSQL::DoubleQuotedString ); // fields
128
129 mSqlLexer->setColor( defaultColor, QsciLexerSQL::Default );
130 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Comment ), QsciLexerSQL::Comment );
131 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentLine ), QsciLexerSQL::CommentLine );
132 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Number ), QsciLexerSQL::Number );
133 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Keyword ), QsciLexerSQL::Keyword );
134 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::SingleQuote ), QsciLexerSQL::SingleQuotedString );
135 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QsciLexerSQL::DoubleQuotedString );
136 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QsciLexerSQL::Operator );
137 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Identifier ), QsciLexerSQL::Identifier );
138 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedIdentifier ), QsciLexerSQL::QuotedIdentifier );
139 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedOperator ), QsciLexerSQL::QuotedOperator );
140
141 setLexer( mSqlLexer );
143}
144
145void QgsCodeEditorExpression::updateApis()
146{
147 mApis = new QgsSciApisExpression( mSqlLexer );
148
149 for ( const QString &var : std::as_const( mVariables ) )
150 {
151 mApis->add( var );
152 }
153
154 for ( const QString &function : std::as_const( mContextFunctions ) )
155 {
156 mApis->add( function );
157 }
158
159 for ( const QString &function : std::as_const( mFunctions ) )
160 {
161 mApis->add( function );
162 }
163
164 for ( const QString &fieldName : std::as_const( mFieldNames ) )
165 {
166 mApis->add( fieldName );
167 }
168
169 mApis->add( QString( "NULL" ) );
170 mApis->prepare();
171 mSqlLexer->setAPIs( mApis );
172}
173
175QgsLexerExpression::QgsLexerExpression( QObject *parent )
176 : QsciLexerSQL( parent )
177{
178 setBackslashEscapes( true );
179}
180
181const char *QgsLexerExpression::language() const
182{
183 return "QGIS Expression";
184}
185
186bool QgsLexerExpression::caseSensitive() const
187{
188 return false;
189}
190
191const char *QgsLexerExpression::wordCharacters() const
192{
193 return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@";
194}
195
196QgsSciApisExpression::QgsSciApisExpression( QsciLexer *lexer )
197 : QsciAPIs( lexer )
198{
199
200}
201
202QStringList QgsSciApisExpression::callTips( const QStringList &context, int commas, QsciScintilla::CallTipsStyle style, QList<int> &shifts )
203{
204 const QStringList originalTips = QsciAPIs::callTips( context, commas, style, shifts );
205 QStringList lowercaseTips;
206 for ( const QString &tip : originalTips )
207 lowercaseTips << tip.toLower();
208
209 return lowercaseTips;
210}
ScriptLanguage
Scripting languages.
Definition: qgis.h:2715
@ QgisExpression
QGIS expressions.
@ 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.
Qgis::ScriptLanguage language() const override
Returns the associated scripting language.
A text editor based on QScintilla2.
Definition: qgscodeeditor.h:93
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:52
QString name
Definition: qgsfield.h:61
Container of fields for a vector layer.
Definition: qgsfields.h:45
const QgsField & field
Definition: qgsfield.h:501