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