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