QGIS API Documentation 3.43.0-Master (32433f7016e)
qgscodeeditorexpression.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscodeeditorexpression.cpp - An expression editor based on QScintilla
3 --------------------------------------
4 Date : 8.9.2018
5 Copyright : (C) 2018 by Matthias Kuhn
6 Email : matthias@opengis.ch
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 "moc_qgscodeeditorexpression.cpp"
18#include "qgsexpression.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
38
43
45{
46 toggleLineComments( QStringLiteral( "--" ) );
47}
48
50{
51 mVariables.clear();
52
53 const QStringList variableNames = context.filteredVariableNames();
54 for ( const QString &var : variableNames )
55 {
56 mVariables << '@' + var;
57 }
58
59 // always show feature variables in autocomplete -- they may not be available in the context
60 // at time of showing an expression builder, but they'll generally be available at evaluation time.
61 mVariables << QStringLiteral( "@feature" );
62 mVariables << QStringLiteral( "@id" );
63 mVariables << QStringLiteral( "@geometry" );
64
65 mContextFunctions = context.functionNames();
66
67 mFunctions.clear();
68
69 const int count = QgsExpression::functionCount();
70 for ( int i = 0; i < count; i++ )
71 {
73 if ( func->isDeprecated() ) // don't show deprecated functions
74 continue;
75 if ( func->isContextual() )
76 {
77 //don't show contextual functions by default - it's up the the QgsExpressionContext
78 //object to provide them if supported
79 continue;
80 }
81
82 QString signature = func->name();
83 if ( !signature.startsWith( '$' ) )
84 {
85 signature += '(';
86
87 QStringList paramNames;
88 const QgsExpressionFunction::ParameterList parameters = func->parameters();
89 for ( const QgsExpressionFunction::Parameter &param : parameters )
90 {
91 paramNames << param.name();
92 }
93
94 // No named parameters but there should be parameteres? Show an ellipsis at least
95 if ( parameters.isEmpty() && func->params() )
96 signature += QChar( 0x2026 );
97
98 signature += paramNames.join( ", " );
99
100 signature += ')';
101 }
102 mFunctions << signature;
103 }
104
105 updateApis();
106}
107
109{
110 mFieldNames.clear();
111
112 for ( const QgsField &field : fields )
113 {
114 mFieldNames << field.name();
115 }
116
117 updateApis();
118}
119
121{
122 QFont font = lexerFont();
124
125 mSqlLexer = new QgsLexerExpression( this );
126 mSqlLexer->setDefaultFont( font );
127 mSqlLexer->setDefaultColor( defaultColor );
128 mSqlLexer->setDefaultPaper( lexerColor( QgsCodeEditorColorScheme::ColorRole::Background ) );
129 mSqlLexer->setFont( font, -1 );
130 font.setBold( true );
131 mSqlLexer->setFont( font, QsciLexerSQL::Keyword );
132
133 font.setBold( false );
134 font.setItalic( true );
135 mSqlLexer->setFont( font, QsciLexerSQL::Comment );
136 mSqlLexer->setFont( font, QsciLexerSQL::CommentLine );
137
138 mSqlLexer->setColor( Qt::darkYellow, QsciLexerSQL::DoubleQuotedString ); // fields
139
140 mSqlLexer->setColor( defaultColor, QsciLexerSQL::Default );
141 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Comment ), QsciLexerSQL::Comment );
142 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentLine ), QsciLexerSQL::CommentLine );
143 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Number ), QsciLexerSQL::Number );
144 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Keyword ), QsciLexerSQL::Keyword );
145 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::SingleQuote ), QsciLexerSQL::SingleQuotedString );
146 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QsciLexerSQL::DoubleQuotedString );
147 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QsciLexerSQL::Operator );
148 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Identifier ), QsciLexerSQL::Identifier );
149 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedIdentifier ), QsciLexerSQL::QuotedIdentifier );
150 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedOperator ), QsciLexerSQL::QuotedOperator );
151
152 setLexer( mSqlLexer );
154}
155
156void QgsCodeEditorExpression::updateApis()
157{
158 mApis = new QgsSciApisExpression( mSqlLexer );
159
160 for ( const QString &var : std::as_const( mVariables ) )
161 {
162 mApis->add( var );
163 }
164
165 for ( const QString &function : std::as_const( mContextFunctions ) )
166 {
167 mApis->add( function );
168 }
169
170 for ( const QString &function : std::as_const( mFunctions ) )
171 {
172 mApis->add( function );
173 }
174
175 for ( const QString &fieldName : std::as_const( mFieldNames ) )
176 {
177 mApis->add( fieldName );
178 }
179
180 mApis->add( QString( "NULL" ) );
181 mApis->prepare();
182 mSqlLexer->setAPIs( mApis );
183}
184
186QgsLexerExpression::QgsLexerExpression( QObject *parent )
187 : QsciLexerSQL( parent )
188{
189 setBackslashEscapes( true );
190}
191
192const char *QgsLexerExpression::language() const
193{
194 return "QGIS Expression";
195}
196
197bool QgsLexerExpression::caseSensitive() const
198{
199 return false;
200}
201
202const char *QgsLexerExpression::wordCharacters() const
203{
204 return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@";
205}
206
207QgsSciApisExpression::QgsSciApisExpression( QsciLexer *lexer )
208 : QsciAPIs( lexer )
209{
210}
211
212QStringList QgsSciApisExpression::callTips( const QStringList &context, int commas, QsciScintilla::CallTipsStyle style, QList<int> &shifts )
213{
214 const QStringList originalTips = QsciAPIs::callTips( context, commas, style, shifts );
215 QStringList lowercaseTips;
216 for ( const QString &tip : originalTips )
217 lowercaseTips << tip.toLower();
218
219 return lowercaseTips;
220}
@ ToggleComment
Language supports comment toggling.
ScriptLanguage
Scripting languages.
Definition qgis.h:4328
@ QgisExpression
QGIS expressions.
QFlags< ScriptLanguageCapability > ScriptLanguageCapabilities
Script language capabilities.
Definition qgis.h:4363
@ 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.
Qgis::ScriptLanguageCapabilities languageCapabilities() const override
Returns the associated scripting language capabilities.
void setExpressionContext(const QgsExpressionContext &context)
Variables and functions from this expression context will be added to the API.
void toggleComment() override
Toggle comment for the selected text.
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.
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.
void toggleLineComments(const QString &commentPrefix)
Toggles comment for selected lines with the given comment prefix.
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.
An 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:53
Container of fields for a vector layer.
Definition qgsfields.h:46