QGIS API Documentation 4.3.0-Master (35968c35f7e)
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
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
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
74
75 const int count = functions->size();
76 for ( int i = 0; i < count; i++ )
77 {
78 QgsExpression::SharedFunctionPtr func = functions->getFunction( i );
79 if ( func->isDeprecated() ) // don't show deprecated functions
80 continue;
81 if ( func->isContextual() )
82 {
83 //don't show contextual functions by default - it's up the the QgsExpressionContext
84 //object to provide them if supported
85 continue;
86 }
87
88 QString signature = func->name();
89 if ( !signature.startsWith( '$' ) )
90 {
91 signature += '(';
92
93 QStringList paramNames;
94 const QgsExpressionFunction::ParameterList parameters = func->parameters();
95 for ( const QgsExpressionFunction::Parameter &param : parameters )
96 {
97 paramNames << param.name();
98 }
99
100 // No named parameters but there should be parameteres? Show an ellipsis at least
101 if ( parameters.isEmpty() && func->params() )
102 signature += QChar( 0x2026 );
103
104 signature += paramNames.join( ", " );
105
106 signature += ')';
107 }
108 mFunctions << signature;
109 }
110
111 updateApis();
112}
113
115{
116 mFieldNames.clear();
117
118 for ( const QgsField &field : fields )
119 {
120 mFieldNames << field.name();
121 }
122
123 updateApis();
124}
125
127{
128 QFont font = lexerFont();
130
131 mSqlLexer = new QgsLexerExpression( this );
132 mSqlLexer->setDefaultFont( font );
133 mSqlLexer->setDefaultColor( defaultColor );
134 mSqlLexer->setDefaultPaper( lexerColor( QgsCodeEditorColorScheme::ColorRole::Background ) );
135 mSqlLexer->setFont( font, -1 );
136 font.setBold( true );
137 mSqlLexer->setFont( font, QsciLexerSQL::Keyword );
138
139 font.setBold( false );
140 font.setItalic( true );
141 mSqlLexer->setFont( font, QsciLexerSQL::Comment );
142 mSqlLexer->setFont( font, QsciLexerSQL::CommentLine );
143
144 mSqlLexer->setColor( Qt::darkYellow, QsciLexerSQL::DoubleQuotedString ); // fields
145
146 mSqlLexer->setColor( defaultColor, QsciLexerSQL::Default );
147 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Comment ), QsciLexerSQL::Comment );
148 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentLine ), QsciLexerSQL::CommentLine );
149 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Number ), QsciLexerSQL::Number );
150 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Keyword ), QsciLexerSQL::Keyword );
151 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::SingleQuote ), QsciLexerSQL::SingleQuotedString );
152 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QsciLexerSQL::DoubleQuotedString );
153 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QsciLexerSQL::Operator );
154 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Identifier ), QsciLexerSQL::Identifier );
155 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedIdentifier ), QsciLexerSQL::QuotedIdentifier );
156 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedOperator ), QsciLexerSQL::QuotedOperator );
157
158 setLexer( mSqlLexer );
160}
161
162void QgsCodeEditorExpression::updateApis()
163{
164 mApis = new QgsSciApisExpression( mSqlLexer );
165
166 for ( const QString &var : std::as_const( mVariables ) )
167 {
168 mApis->add( var );
169 }
170
171 for ( const QString &function : std::as_const( mContextFunctions ) )
172 {
173 mApis->add( function );
174 }
175
176 for ( const QString &function : std::as_const( mFunctions ) )
177 {
178 mApis->add( function );
179 }
180
181 for ( const QString &fieldName : std::as_const( mFieldNames ) )
182 {
183 mApis->add( fieldName );
184 }
185
186 mApis->add( QString( "NULL" ) );
187 mApis->prepare();
188 mSqlLexer->setAPIs( mApis );
189}
190
192QgsLexerExpression::QgsLexerExpression( QObject *parent )
193 : QsciLexerSQL( parent )
194{
195 setBackslashEscapes( true );
196}
197
198const char *QgsLexerExpression::language() const
199{
200 return "QGIS Expression";
201}
202
203bool QgsLexerExpression::caseSensitive() const
204{
205 return false;
206}
207
208const char *QgsLexerExpression::wordCharacters() const
209{
210 return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@";
211}
212
213QgsSciApisExpression::QgsSciApisExpression( QsciLexer *lexer )
214 : QsciAPIs( lexer )
215{}
216
217QStringList QgsSciApisExpression::callTips( const QStringList &context, int commas, QsciScintilla::CallTipsStyle style, QList<int> &shifts )
218{
219 const QStringList originalTips = QsciAPIs::callTips( context, commas, style, shifts );
220 QStringList lowercaseTips;
221 for ( const QString &tip : originalTips )
222 lowercaseTips << tip.toLower();
223
224 return lowercaseTips;
225}
@ ToggleComment
Language supports comment toggling.
Definition qgis.h:5004
ScriptLanguage
Scripting languages.
Definition qgis.h:4978
@ QgisExpression
QGIS expressions.
Definition qgis.h:4980
QFlags< ScriptLanguageCapability > ScriptLanguageCapabilities
Script language capabilities.
Definition qgis.h:5013
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.
QList< QgsExpressionFunction::Parameter > ParameterList
List of parameters, used for function definition.
std::shared_ptr< const SharedFunctionPtrList > FunctionListSnapshot
static const FunctionListSnapshot getFunctionsSnapshot()
Returns a thread-safe snapshot of all registered functions.
std::shared_ptr< QgsExpressionFunction > SharedFunctionPtr
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:45