Warning, file /education/kmplot/kmplot/equationhighlighter.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 KmPlot - a math. function plotter for the KDE-Desktop 0003 0004 SPDX-FileCopyrightText: 2006 David Saxton <david@bluehaze.org> 0005 0006 This file is part of the KDE Project. 0007 KmPlot is part of the KDE-EDU Project. 0008 0009 SPDX-License-Identifier: GPL-2.0-or-later 0010 0011 */ 0012 0013 #include "equationhighlighter.h" 0014 0015 #include <QLocale> 0016 0017 #include "equationedit.h" 0018 #include "equationeditwidget.h" 0019 #include "xparser.h" 0020 0021 EquationHighlighter::EquationHighlighter(EquationEdit *parent) 0022 : QSyntaxHighlighter(parent->m_equationEditWidget) 0023 , m_parent(parent) 0024 { 0025 m_errorPosition = -1; 0026 } 0027 0028 EquationHighlighter::~EquationHighlighter() 0029 { 0030 } 0031 0032 void EquationHighlighter::highlightBlock(const QString &text) 0033 { 0034 m_parent->checkTextValidity(); 0035 0036 if (text.isEmpty()) 0037 return; 0038 0039 QTextCharFormat number; 0040 QTextCharFormat function; 0041 QTextCharFormat variable; 0042 QTextCharFormat matchedParenthesis; 0043 0044 QPalette palette; 0045 if (qGray(palette.color(QPalette::Base).rgb()) > 160) { 0046 // Good color defaults borrowed from Abakus - thanks! :) 0047 number.setForeground(QColor(0, 0, 127)); 0048 function.setForeground(QColor(85, 0, 0)); 0049 variable.setForeground(QColor(0, 85, 0)); 0050 matchedParenthesis.setBackground(QColor(255, 255, 183)); 0051 } else { 0052 number.setForeground(QColor(160, 160, 255)); 0053 function.setForeground(QColor(255, 160, 160)); 0054 variable.setForeground(QColor(160, 255, 160)); 0055 matchedParenthesis.setBackground(QColor(85, 85, 0)); 0056 } 0057 0058 QTextCharFormat other; 0059 0060 const QStringList variables = m_parent->m_equation->variables(); 0061 const QStringList functions = XParser::self()->predefinedFunctions(true) + XParser::self()->userFunctions(); 0062 0063 for (int i = 0; i < text.length(); ++i) { 0064 QString remaining = text.right(text.length() - i); 0065 0066 bool found = false; 0067 0068 for (const QString &var : variables) { 0069 if (remaining.startsWith(var)) { 0070 setFormat(i, var.length(), variable); 0071 i += var.length() - 1; 0072 found = true; 0073 break; 0074 } 0075 } 0076 if (found) 0077 continue; 0078 0079 for (const QString &f : functions) { 0080 if (remaining.startsWith(f)) { 0081 setFormat(i, f.length(), function); 0082 i += f.length() - 1; 0083 found = true; 0084 break; 0085 } 0086 } 0087 if (found) 0088 continue; 0089 0090 ushort u = text[i].unicode(); 0091 bool isFraction = (u >= 0xbc && u <= 0xbe) || (u >= 0x2153 && u <= 0x215e); 0092 bool isPower = (u >= 0xb2 && u <= 0xb3) || (u == 0x2070) || (u >= 0x2074 && u <= 0x2079); 0093 bool isDigit = text[i].isDigit(); 0094 bool isDecimalPoint = text[i] == QLocale().decimalPoint(); 0095 0096 if (isFraction || isPower || isDigit || isDecimalPoint) 0097 setFormat(i, 1, number); 0098 else 0099 setFormat(i, 1, other); 0100 } 0101 0102 // BEGIN highlight matched brackets 0103 int cursorPos = m_parent->m_equationEditWidget->textCursor().position(); 0104 if (cursorPos < 0) 0105 cursorPos = 0; 0106 0107 // Adjust cursorpos to allow for a bracket before the cursor position 0108 if (cursorPos >= text.size()) 0109 cursorPos = text.size() - 1; 0110 else if (cursorPos > 0 && (text[cursorPos - 1] == '(' || text[cursorPos - 1] == ')')) 0111 cursorPos--; 0112 0113 bool haveOpen = text[cursorPos] == '('; 0114 bool haveClose = text[cursorPos] == ')'; 0115 0116 if ((haveOpen || haveClose) && m_parent->hasFocus()) { 0117 // Search for the other bracket 0118 0119 int inc = haveOpen ? 1 : -1; // which direction to search in 0120 0121 int level = 0; 0122 for (int i = cursorPos; i >= 0 && i < text.size(); i += inc) { 0123 if (text[i] == ')') 0124 level--; 0125 else if (text[i] == '(') 0126 level++; 0127 0128 if (level == 0) { 0129 // Matched! 0130 setFormat(cursorPos, 1, matchedParenthesis); 0131 setFormat(i, 1, matchedParenthesis); 0132 break; 0133 } 0134 } 0135 } 0136 // END highlight matched brackets 0137 0138 if (m_errorPosition != -1) { 0139 QTextCharFormat error; 0140 error.setForeground(Qt::red); 0141 0142 setFormat(m_errorPosition, 1, error); 0143 } 0144 } 0145 0146 void EquationHighlighter::setErrorPosition(int position) 0147 { 0148 m_errorPosition = position; 0149 }