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;
74 for (
int i = 0; i < count; i++ )
86 QString signature = func->
name();
87 if ( !signature.startsWith(
'$' ) )
91 QStringList paramNames;
95 paramNames << param.name();
99 if ( parameters.isEmpty() && func->
params() )
100 signature += QChar( 0x2026 );
102 signature += paramNames.join(
", " );
106 mFunctions << signature;
116 for (
const QgsField &field : fields )
118 mFieldNames << field.name();
129 mSqlLexer =
new QgsLexerExpression(
this );
130 mSqlLexer->setDefaultFont( font );
133 mSqlLexer->setFont( font, -1 );
134 font.setBold(
true );
135 mSqlLexer->setFont( font, QsciLexerSQL::Keyword );
137 font.setBold(
false );
138 font.setItalic(
true );
139 mSqlLexer->setFont( font, QsciLexerSQL::Comment );
140 mSqlLexer->setFont( font, QsciLexerSQL::CommentLine );
142 mSqlLexer->setColor( Qt::darkYellow, QsciLexerSQL::DoubleQuotedString );
144 mSqlLexer->setColor(
defaultColor, QsciLexerSQL::Default );
156 setLexer( mSqlLexer );
160void QgsCodeEditorExpression::updateApis()
162 mApis =
new QgsSciApisExpression( mSqlLexer );
164 for (
const QString &var : std::as_const( mVariables ) )
169 for (
const QString &function : std::as_const( mContextFunctions ) )
171 mApis->add( function );
174 for (
const QString &function : std::as_const( mFunctions ) )
176 mApis->add( function );
179 for (
const QString &fieldName : std::as_const( mFieldNames ) )
181 mApis->add( fieldName );
184 mApis->add( QString(
"NULL" ) );
186 mSqlLexer->setAPIs( mApis );
190QgsLexerExpression::QgsLexerExpression( QObject *parent )
191 : QsciLexerSQL( parent )
193 setBackslashEscapes(
true );
196const char *QgsLexerExpression::language()
const
198 return "QGIS Expression";
201bool QgsLexerExpression::caseSensitive()
const
206const char *QgsLexerExpression::wordCharacters()
const
208 return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@";
211QgsSciApisExpression::QgsSciApisExpression( QsciLexer *lexer )
216QStringList QgsSciApisExpression::callTips(
const QStringList &context,
int commas, QsciScintilla::CallTipsStyle style, QList<int> &shifts )
218 const QStringList originalTips = QsciAPIs::callTips( context, commas, style, shifts );
219 QStringList lowercaseTips;
220 for (
const QString &tip : originalTips )
221 lowercaseTips << tip.toLower();
223 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.
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.
Container of fields for a vector layer.