|
| 1 | +/*************************************************************************** |
| 2 | + qgscodeeditorexpressoin.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 | + |
| 16 | +#include "qgsapplication.h" |
| 17 | +#include "qgscodeeditorexpression.h" |
| 18 | + |
| 19 | +#include <QString> |
| 20 | +#include <QFont> |
| 21 | +#include <QLabel> |
| 22 | + |
| 23 | +QgsCodeEditorExpression::QgsCodeEditorExpression( QWidget *parent ) |
| 24 | + : QgsCodeEditor( parent ) |
| 25 | +{ |
| 26 | + if ( !parent ) |
| 27 | + { |
| 28 | + setTitle( tr( "Expression Editor" ) ); |
| 29 | + } |
| 30 | + setMarginVisible( false ); |
| 31 | + setFoldingVisible( true ); |
| 32 | + setAutoCompletionCaseSensitivity( false ); |
| 33 | + initializeLexer(); |
| 34 | +} |
| 35 | + |
| 36 | +void QgsCodeEditorExpression::setExpressionContext( const QgsExpressionContext &context ) |
| 37 | +{ |
| 38 | + mVariables.clear(); |
| 39 | + |
| 40 | + const QStringList variableNames = context.filteredVariableNames(); |
| 41 | + for ( const QString &var : variableNames ) |
| 42 | + { |
| 43 | + mVariables << '@' + var; |
| 44 | + } |
| 45 | + |
| 46 | + mContextFunctions = context.functionNames(); |
| 47 | + |
| 48 | + mFunctions.clear(); |
| 49 | + |
| 50 | + const int count = QgsExpression::functionCount(); |
| 51 | + for ( int i = 0; i < count; i++ ) |
| 52 | + { |
| 53 | + QgsExpressionFunction *func = QgsExpression::Functions()[i]; |
| 54 | + if ( func->isDeprecated() ) // don't show deprecated functions |
| 55 | + continue; |
| 56 | + if ( func->isContextual() ) |
| 57 | + { |
| 58 | + //don't show contextual functions by default - it's up the the QgsExpressionContext |
| 59 | + //object to provide them if supported |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + QString signature = func->name(); |
| 64 | + if ( !signature.startsWith( '$' ) ) |
| 65 | + { |
| 66 | + signature += '('; |
| 67 | + |
| 68 | + QStringList paramNames; |
| 69 | + const auto ¶meters = func->parameters(); |
| 70 | + for ( const auto ¶m : parameters ) |
| 71 | + { |
| 72 | + paramNames << param.name(); |
| 73 | + } |
| 74 | + |
| 75 | + // No named parameters but there should be parameteres? Show an ellipsis at least |
| 76 | + if ( parameters.isEmpty() && func->params() ) |
| 77 | + signature += QChar( 0x2026 ); |
| 78 | + |
| 79 | + signature += paramNames.join( ", " ); |
| 80 | + |
| 81 | + signature += ')'; |
| 82 | + } |
| 83 | + mFunctions << signature; |
| 84 | + } |
| 85 | + |
| 86 | + updateApis(); |
| 87 | +} |
| 88 | + |
| 89 | +void QgsCodeEditorExpression::setFields( const QgsFields &fields ) |
| 90 | +{ |
| 91 | + mFieldNames.clear(); |
| 92 | + |
| 93 | + for ( const QgsField &field : fields ) |
| 94 | + { |
| 95 | + mFieldNames << field.name(); |
| 96 | + } |
| 97 | + |
| 98 | + updateApis(); |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +void QgsCodeEditorExpression::initializeLexer() |
| 103 | +{ |
| 104 | + QFont font = getMonospaceFont(); |
| 105 | +#ifdef Q_OS_MAC |
| 106 | + // The font size gotten from getMonospaceFont() is too small on Mac |
| 107 | + font.setPointSize( QLabel().font().pointSize() ); |
| 108 | +#endif |
| 109 | + mSqlLexer = new QgsLexerExpression( this ); |
| 110 | + mSqlLexer->setDefaultFont( font ); |
| 111 | + mSqlLexer->setFont( font, -1 ); |
| 112 | + font.setBold( true ); |
| 113 | + mSqlLexer->setFont( font, QsciLexerSQL::Keyword ); |
| 114 | + mSqlLexer->setColor( Qt::darkYellow, QsciLexerSQL::DoubleQuotedString ); // fields |
| 115 | + |
| 116 | + setLexer( mSqlLexer ); |
| 117 | +} |
| 118 | + |
| 119 | +void QgsCodeEditorExpression::updateApis() |
| 120 | +{ |
| 121 | + mApis = new QgsSciApisExpression( mSqlLexer ); |
| 122 | + |
| 123 | + for ( const QString &var : qgis::as_const( mVariables ) ) |
| 124 | + { |
| 125 | + mApis->add( var ); |
| 126 | + } |
| 127 | + |
| 128 | + for ( const QString &function : qgis::as_const( mContextFunctions ) ) |
| 129 | + { |
| 130 | + mApis->add( function ); |
| 131 | + } |
| 132 | + |
| 133 | + for ( const QString &function : qgis::as_const( mFunctions ) ) |
| 134 | + { |
| 135 | + mApis->add( function ); |
| 136 | + } |
| 137 | + |
| 138 | + for ( const QString &fieldName : qgis::as_const( mFieldNames ) ) |
| 139 | + { |
| 140 | + mApis->add( fieldName ); |
| 141 | + } |
| 142 | + |
| 143 | + mApis->prepare(); |
| 144 | + mSqlLexer->setAPIs( mApis ); |
| 145 | +} |
| 146 | + |
| 147 | +///@cond PRIVATE |
| 148 | +QgsLexerExpression::QgsLexerExpression( QObject *parent ) |
| 149 | + : QsciLexerSQL( parent ) |
| 150 | +{ |
| 151 | +} |
| 152 | + |
| 153 | +const char *QgsLexerExpression::language() const |
| 154 | +{ |
| 155 | + return "QGIS Expression"; |
| 156 | +} |
| 157 | + |
| 158 | +bool QgsLexerExpression::caseSensitive() const |
| 159 | +{ |
| 160 | + return false; |
| 161 | +} |
| 162 | + |
| 163 | +const char *QgsLexerExpression::wordCharacters() const |
| 164 | +{ |
| 165 | + return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@"; |
| 166 | +} |
| 167 | + |
| 168 | +QgsSciApisExpression::QgsSciApisExpression( QsciLexer *lexer ) |
| 169 | + : QsciAPIs( lexer ) |
| 170 | +{ |
| 171 | + |
| 172 | +} |
| 173 | + |
| 174 | +QStringList QgsSciApisExpression::callTips( const QStringList &context, int commas, QsciScintilla::CallTipsStyle style, QList<int> &shifts ) |
| 175 | +{ |
| 176 | + const QStringList originalTips = QsciAPIs::callTips( context, commas, style, shifts ); |
| 177 | + QStringList lowercaseTips; |
| 178 | + for ( const QString &tip : originalTips ) |
| 179 | + lowercaseTips << tip.toLower(); |
| 180 | + |
| 181 | + return lowercaseTips; |
| 182 | +} |
| 183 | +///@endcond |
0 commit comments