23#include "moc_qgscodeeditorexpression.cpp"
25using namespace Qt::StringLiterals;
32 setTitle( tr(
"Expression Editor" ) );
34 setAutoCompletionCaseSensitivity(
false );
58 for (
const QString &var : variableNames )
60 mVariables <<
'@' + var;
65 mVariables << u
"@feature"_s;
66 mVariables << u
"@id"_s;
67 mVariables << u
"@geometry"_s;
75 const int count = functions->size();
76 for (
int i = 0; i < count; i++ )
79 if ( func->isDeprecated() )
81 if ( func->isContextual() )
88 QString signature = func->name();
89 if ( !signature.startsWith(
'$' ) )
93 QStringList paramNames;
97 paramNames << param.name();
101 if ( parameters.isEmpty() && func->params() )
102 signature += QChar( 0x2026 );
104 signature += paramNames.join(
", " );
108 mFunctions << signature;
118 for (
const QgsField &field : fields )
120 mFieldNames << field.name();
131 mSqlLexer =
new QgsLexerExpression(
this );
132 mSqlLexer->setDefaultFont( font );
135 mSqlLexer->setFont( font, -1 );
136 font.setBold(
true );
137 mSqlLexer->setFont( font, QsciLexerSQL::Keyword );
139 font.setBold(
false );
140 font.setItalic(
true );
141 mSqlLexer->setFont( font, QsciLexerSQL::Comment );
142 mSqlLexer->setFont( font, QsciLexerSQL::CommentLine );
144 mSqlLexer->setColor( Qt::darkYellow, QsciLexerSQL::DoubleQuotedString );
146 mSqlLexer->setColor(
defaultColor, QsciLexerSQL::Default );
158 setLexer( mSqlLexer );
162void QgsCodeEditorExpression::updateApis()
164 mApis =
new QgsSciApisExpression( mSqlLexer );
166 for (
const QString &var : std::as_const( mVariables ) )
171 for (
const QString &function : std::as_const( mContextFunctions ) )
173 mApis->add( function );
176 for (
const QString &function : std::as_const( mFunctions ) )
178 mApis->add( function );
181 for (
const QString &fieldName : std::as_const( mFieldNames ) )
183 mApis->add( fieldName );
186 mApis->add( QString(
"NULL" ) );
188 mSqlLexer->setAPIs( mApis );
192QgsLexerExpression::QgsLexerExpression( QObject *parent )
193 : QsciLexerSQL( parent )
195 setBackslashEscapes(
true );
198const char *QgsLexerExpression::language()
const
200 return "QGIS Expression";
203bool QgsLexerExpression::caseSensitive()
const
208const char *QgsLexerExpression::wordCharacters()
const
210 return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@";
213QgsSciApisExpression::QgsSciApisExpression( QsciLexer *lexer )
217QStringList QgsSciApisExpression::callTips(
const QStringList &context,
int commas, QsciScintilla::CallTipsStyle style, QList<int> &shifts )
219 const QStringList originalTips = QsciAPIs::callTips( context, commas, style, shifts );
220 QStringList lowercaseTips;
221 for (
const QString &tip : originalTips )
222 lowercaseTips << tip.toLower();
224 return lowercaseTips;
@ ToggleComment
Language supports comment toggling.
ScriptLanguage
Scripting languages.
@ QgisExpression
QGIS expressions.
QFlags< ScriptLanguageCapability > ScriptLanguageCapabilities
Script language capabilities.
@ QuotedOperator
Quoted operator color.
@ Identifier
Identifier color.
@ DoubleQuote
Double quote color.
@ QuotedIdentifier
Quoted identifier color.
@ CommentLine
Line comment color.
@ Default
Default text color.
@ Background
Background color.
@ SingleQuote
Single quote color.
@ Operator
Operator 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.
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.
Container of fields for a vector layer.