29 setTitle( tr(
"Expression Editor" ) );
33 setAutoCompletionCaseSensitivity(
false );
42 for (
const QString &var : variableNames )
44 mVariables <<
'@' + var;
52 for (
int i = 0; i < count; i++ )
64 QString signature = func->
name();
65 if ( !signature.startsWith(
'$' ) )
69 QStringList paramNames;
71 for (
const auto ¶m : parameters )
73 paramNames << param.name();
77 if ( parameters.isEmpty() && func->
params() )
78 signature += QChar( 0x2026 );
80 signature += paramNames.join(
", " );
84 mFunctions << signature;
94 for (
const QgsField &field : fields )
96 mFieldNames << field.name();
103 void QgsCodeEditorExpression::initializeLexer()
105 QHash< QString, QColor > colors;
109 for (
const auto &key : ini.allKeys() )
118 font.setPointSize( QLabel().font().pointSize() );
120 QColor defaultColor = colors.value( QStringLiteral(
"sql/defaultFontColor" ), Qt::black );
122 mSqlLexer =
new QgsLexerExpression(
this );
123 mSqlLexer->setDefaultFont( font );
124 mSqlLexer->setDefaultColor( defaultColor );
125 mSqlLexer->setDefaultPaper( colors.value( QStringLiteral(
"sql/paperBackgroundColor" ), Qt::white ) );
126 mSqlLexer->setFont( font, -1 );
127 font.setBold(
true );
128 mSqlLexer->setFont( font, QsciLexerSQL::Keyword );
130 mSqlLexer->setColor( Qt::darkYellow, QsciLexerSQL::DoubleQuotedString );
132 mSqlLexer->setColor( defaultColor, QsciLexerSQL::Default );
133 mSqlLexer->setColor( colors.value( QStringLiteral(
"sql/commentFontColor" ), QColor( 142, 144, 140 ) ), QsciLexerSQL::Comment );
134 mSqlLexer->setColor( colors.value( QStringLiteral(
"sql/commentLineFontColor" ), QColor( 142, 144, 140 ) ), QsciLexerSQL::CommentLine );
135 mSqlLexer->setColor( colors.value( QStringLiteral(
"sql/numberFontColor" ), QColor( 200, 40, 41 ) ), QsciLexerSQL::Number );
136 mSqlLexer->setColor( colors.value( QStringLiteral(
"sql/keywordFontColor" ), QColor( 137, 89, 168 ) ), QsciLexerSQL::Keyword );
137 mSqlLexer->setColor( colors.value( QStringLiteral(
"sql/singleQuoteFontColor" ), QColor( 113, 140, 0 ) ), QsciLexerSQL::SingleQuotedString );
138 mSqlLexer->setColor( colors.value( QStringLiteral(
"sql/doubleQuoteFontColor" ), QColor( 234, 183, 0 ) ), QsciLexerSQL::DoubleQuotedString );
139 mSqlLexer->setColor( colors.value( QStringLiteral(
"sql/operatorFontColor" ), QColor( 66, 113, 174 ) ), QsciLexerSQL::Operator );
140 mSqlLexer->setColor( colors.value( QStringLiteral(
"sql/identifierFontColor" ), QColor( 62, 153, 159 ) ), QsciLexerSQL::Identifier );
141 mSqlLexer->setColor( colors.value( QStringLiteral(
"sql/QuotedIdentifierFontColor" ), Qt::black ), QsciLexerSQL::QuotedIdentifier );
142 mSqlLexer->setColor( colors.value( QStringLiteral(
"sql/QuotedOperatorFontColor" ), Qt::black ), QsciLexerSQL::QuotedOperator );
144 setLexer( mSqlLexer );
147 void QgsCodeEditorExpression::updateApis()
149 mApis =
new QgsSciApisExpression( mSqlLexer );
151 for (
const QString &var : qgis::as_const( mVariables ) )
156 for (
const QString &
function : qgis::as_const( mContextFunctions ) )
158 mApis->add(
function );
161 for (
const QString &
function : qgis::as_const( mFunctions ) )
163 mApis->add(
function );
166 for (
const QString &fieldName : qgis::as_const( mFieldNames ) )
168 mApis->add( fieldName );
172 mSqlLexer->setAPIs( mApis );
176 QgsLexerExpression::QgsLexerExpression( QObject *parent )
177 : QsciLexerSQL( parent )
181 const char *QgsLexerExpression::language()
const 183 return "QGIS Expression";
186 bool QgsLexerExpression::caseSensitive()
const 191 const char *QgsLexerExpression::wordCharacters()
const 193 return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@";
196 QgsSciApisExpression::QgsSciApisExpression( QsciLexer *lexer )
202 QStringList QgsSciApisExpression::callTips(
const QStringList &context,
int commas, QsciScintilla::CallTipsStyle style, QList<int> &shifts )
204 const QStringList originalTips = QsciAPIs::callTips( context, commas, style, shifts );
205 QStringList lowercaseTips;
206 for (
const QString &tip : originalTips )
207 lowercaseTips << tip.toLower();
209 return lowercaseTips;
A text editor based on QScintilla2.
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
void setFoldingVisible(bool folding)
Set folding visible state.
void setFields(const QgsFields &fields)
Field names will be added to the API.
Container of fields for a vector layer.
QString name() const
The name of the function.
void setExpressionContext(const QgsExpressionContext &context)
Variables and functions from this expression context will be added to the API.
static int functionCount()
Returns the number of functions defined in the parser.
QgsCodeEditorExpression(QWidget *parent=nullptr)
Constructor for QgsCodeEditorExpression.
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.
Encapsulate a field in an attribute table or data source.
static const QList< QgsExpressionFunction * > & Functions()
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 ...
void setMarginVisible(bool margin)
Set margin visible state.
A abstract base class for defining QgsExpression functions.
const QgsExpressionFunction::ParameterList & parameters() const
Returns the list of named parameters for the function, if set.
QStringList filteredVariableNames() const
Returns a filtered list of variables names set by all scopes in the context.
void setTitle(const QString &title)
Set the widget title.
bool isContextual() const
Returns whether the function is only available if provided by a QgsExpressionContext object...
static QColor decodeColor(const QString &str)