| 27 | } |
| 28 | |
| 29 | void EquationHighlighter::highlightBlock(const QString& text) { |
| 30 | // TODO: m_parent->checkTextValidity(); |
| 31 | |
| 32 | if (text.isEmpty()) |
| 33 | return; |
| 34 | |
| 35 | QTextCharFormat number; |
| 36 | QTextCharFormat function; |
| 37 | QTextCharFormat variable; |
| 38 | QTextCharFormat constant; |
| 39 | QTextCharFormat matchedParenthesis; |
| 40 | |
| 41 | QPalette palette; |
| 42 | if (qGray(palette.color(QPalette::Base).rgb()) > 160) { // light |
| 43 | number.setForeground(QColor(0, 0, 127)); |
| 44 | function.setForeground(QColor(85, 0, 0)); |
| 45 | function.setFontWeight(QFont::Bold); |
| 46 | variable.setForeground(QColor(0, 85, 0)); |
| 47 | constant.setForeground(QColor(85, 0, 0)); |
| 48 | matchedParenthesis.setBackground(QColor(255, 255, 183)); |
| 49 | } else { // dark |
| 50 | number.setForeground(QColor(160, 160, 255)); |
| 51 | function.setForeground(QColor(255, 160, 160)); |
| 52 | function.setFontWeight(QFont::Bold); |
| 53 | variable.setForeground(QColor(160, 255, 160)); |
| 54 | constant.setForeground(QColor(255, 160, 160)); |
| 55 | matchedParenthesis.setBackground(QColor(85, 85, 0)); |
| 56 | } |
| 57 | |
| 58 | QTextCharFormat other; |
| 59 | |
| 60 | static const QStringList& functions = ExpressionParser::getInstance()->functions(); |
| 61 | static const QStringList& constants = ExpressionParser::getInstance()->constants(); |
| 62 | |
| 63 | for (int i = 0; i < text.length(); ++i) { |
| 64 | QString remaining = text.right(text.length() - i); |
| 65 | bool found = false; |
| 66 | |
| 67 | // variables |
| 68 | for (const QString& var : m_variables) { |
| 69 | if (remaining.startsWith(var)) { |
| 70 | QString nextChar = remaining.mid(var.length(), 1); |
| 71 | if (nextChar == QLatin1Char(' ') || nextChar == QLatin1Char(')') || nextChar == QLatin1Char('+') || nextChar == QLatin1Char('-') |
| 72 | || nextChar == QLatin1Char('*') || nextChar == QLatin1Char('/') || nextChar == QLatin1Char('^')) { |
| 73 | setFormat(i, var.length(), variable); |
| 74 | i += var.length() - 1; |
| 75 | found = true; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | if (found) |
| 81 | continue; |
| 82 | |
| 83 | // functions |
| 84 | for (const QString& f : functions) { |
| 85 | if (remaining.startsWith(f)) { |
| 86 | setFormat(i, f.length(), function); |