| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved. |
| 4 | ** |
| 5 | ** This file is part of the example classes of the Qt Toolkit. |
| 6 | ** |
| 7 | ** This file may be used under the terms of the GNU General Public |
| 8 | ** License version 2.0 as published by the Free Software Foundation |
| 9 | ** and appearing in the file LICENSE.GPL included in the packaging of |
| 10 | ** this file. Please review the following information to ensure GNU |
| 11 | ** General Public Licensing requirements will be met: |
| 12 | ** http://trolltech.com/products/qt/licenses/licensing/opensource/ |
| 13 | ** |
| 14 | ** If you are unsure which license is appropriate for your use, please |
| 15 | ** review the following information: |
| 16 | ** http://trolltech.com/products/qt/licenses/licensing/licensingoverview |
| 17 | ** or contact the sales department at [email protected]. |
| 18 | ** |
| 19 | ** In addition, as a special exception, Trolltech gives you certain |
| 20 | ** additional rights. These rights are described in the Trolltech GPL |
| 21 | ** Exception version 1.0, which can be found at |
| 22 | ** http://www.trolltech.com/products/qt/gplexception/ and in the file |
| 23 | ** GPL_EXCEPTION.txt in this package. |
| 24 | ** |
| 25 | ** In addition, as a special exception, Trolltech, as the sole copyright |
| 26 | ** holder for Qt Designer, grants users of the Qt/Eclipse Integration |
| 27 | ** plug-in the right for the Qt/Eclipse Integration to link to |
| 28 | ** functionality provided by Qt Designer and its related libraries. |
| 29 | ** |
| 30 | ** Trolltech reserves all rights not expressly granted herein. |
| 31 | ** |
| 32 | ** Trolltech ASA (c) 2007 |
| 33 | ** |
| 34 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 35 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 36 | ** |
| 37 | ****************************************************************************/ |
| 38 | |
| 39 | #include <QtGui> |
| 40 | |
| 41 | #include "highlighter.h" |
| 42 | |
| 43 | Highlighter::Highlighter(QTextDocument *parent) |
| 44 | : QSyntaxHighlighter(parent){ |
| 45 | } |
| 46 | |
| 47 | Highlighter::Highlighter(QString FileName, QTextDocument *parent) |
| 48 | : QSyntaxHighlighter(parent){ |
| 49 | // setEditStyle(); |
| 50 | printf("Highlighter\n"); |
| 51 | loadHighlightPatterns(FileName); |
| 52 | } |
| 53 | |
| 54 | void Highlighter::highlightSearch(const QString &find){ |
| 55 | HighlightingRule rule; |
| 56 | rule.format.setBackground(Qt::yellow); |
| 57 | rule.format.setFontWeight(QFont::Black);// |
| 58 | findRule.clear(); |
| 59 | QString f=find; |
| 60 | f.replace(".*",".+"); |
| 61 | if ((f=="^")||(f=="$"))return; |
| 62 | if (!f.isEmpty()){ |
| 63 | rule.pattern = QRegularExpression(f, QRegularExpression::CaseInsensitiveOption); |
| 64 | |
| 65 | findRule.append( rule); |
| 66 | } |
| 67 | // printf("suchSuch\n"); |
| 68 | rehighlight(); |
| 69 | } |
| 70 | |
| 71 | void Highlighter::highlightError(const QStringList fnd){ |
| 72 | findRule.clear(); |
| 73 | HighlightingRule rule; |
| 74 | rule.format.setFontWeight(QFont::Black); |
| 75 | rule.format.setForeground(Qt::yellow); |
| 76 | rule.format.setFontUnderline(true); |
| 77 | rule.format.setUnderlineStyle(QTextCharFormat::WaveUnderline); |
| 78 | rule.format.setUnderlineColor(Qt::red); |
| 79 | rule.format.setBackground(QColor(Qt::red).lighter(100)); |
| 80 | QString f=""; |
| 81 | for (int i=0;i<fnd.size();i++){ |
| 82 | f=fnd.at(i); |
| 83 | if (!f.isEmpty()){ |
| 84 | rule.pattern = QRegularExpression(f, QRegularExpression::CaseInsensitiveOption);//fixedString was in QRegExp |
| 85 | |
| 86 | findRule.append( rule); |
| 87 | } |
| 88 | } |
| 89 | // printf("suchSuch\n"); |
| 90 | rehighlight(); |
| 91 | } |
| 92 | |
| 93 | void Highlighter::highlightDupl(const CEnvironment &dupls){ |
| 94 | findRule.clear(); |
| 95 | if (dupls.size()==0){rehighlight();return;} |
| 96 | HighlightingRule rule; |
| 97 | rule.format.setBackground(QColor("orange")); |
| 98 | rule.format.setForeground(Qt::blue); |
| 99 | rule.format.setFontWeight(QFont::Black);// |
| 100 | findRule.clear(); |
| 101 | QString f=""; |
| 102 | for (int i=0;i<dupls.size();i++){ |
| 103 | f=dupls.at(i).orginalLine; |
| 104 | if (!f.isEmpty()){ |
| 105 | rule.pattern = QRegularExpression(f, QRegularExpression::CaseInsensitiveOption); |
| 106 | |
| 107 | findRule.append( rule); |
| 108 | } |
| 109 | } |
| 110 | // printf("suchSuch\n"); |
| 111 | rehighlight(); |
| 112 | } |
| 113 | |
| 114 | void Highlighter::highlightBlock(const QString &text) { |
| 115 | //QElapsedTimer stech; |
| 116 | //stech.start(); |
| 117 | foreach (HighlightingRule rule, highlightingRules) { |
| 118 | if (rule.Comment) continue; |
| 119 | QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text); |
| 120 | while (matchIterator.hasNext()) { |
| 121 | QRegularExpressionMatch match = matchIterator.next(); |
| 122 | setFormat(match.capturedStart(), match.capturedLength(), rule.format); |
| 123 | } |
| 124 | } |
| 125 | foreach (HighlightingRule rule, highlightingRules) { |
| 126 | if (!rule.Comment) continue; |
| 127 | QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text); |
| 128 | while (matchIterator.hasNext()) { |
| 129 | QRegularExpressionMatch match = matchIterator.next(); |
| 130 | setFormat(match.capturedStart(), match.capturedLength(), rule.format); |
| 131 | } |
| 132 | } |
| 133 | setCurrentBlockState(0); |
| 134 | if (!outputstyle) { |
| 135 | int startIndex = 0; |
| 136 | if (previousBlockState() != 1){ |
| 137 | startIndex = text.indexOf(commentStartExpression); |
| 138 | } |
| 139 | QTextCharFormat fmt = format(startIndex); |
| 140 | if (fmt.foreground()==Qt::blue) startIndex=-1; |
| 141 | while (startIndex >= 0) { |
| 142 | QRegularExpressionMatch match = commentEndExpression.match(text, startIndex); |
| 143 | int endIndex = match.capturedStart(); |
| 144 | int commentLength = 0; |
| 145 | if (endIndex == -1) { |
| 146 | setCurrentBlockState(1); |
| 147 | commentLength = text.length() - startIndex; |
| 148 | } else { |
| 149 | commentLength = endIndex - startIndex |
| 150 | + match.capturedLength(); |
| 151 | } |
| 152 | setFormat(startIndex, commentLength, multiLineCommentFormat); |
| 153 | startIndex = text.indexOf(commentStartExpression, startIndex + commentLength); |
| 154 | fmt = format(startIndex); |
| 155 | if (fmt.foreground()==Qt::blue) startIndex=-1; |
| 156 | if (fmt.underlineStyle()) startIndex=-1; |
| 157 | } |
| 158 | foreach (HighlightingRule rule, highlightingRules) { |
| 159 | if (!rule.Comment) continue; |
| 160 | QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text); |
| 161 | while (matchIterator.hasNext()) { |
| 162 | QRegularExpressionMatch match = matchIterator.next(); |
| 163 | setFormat(match.capturedStart(), match.capturedLength(), rule.format); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | /* |
| 168 | rule.pattern = QRegularExpression("\\b[0-9]{2,}\\.[0-9]+\\b"); |
| 169 | rule.format = numberFormat; |
| 170 | |
| 171 | expression = QRegularExpression(rule.pattern); |
| 172 | index = text.indexOf(expression); |
| 173 | while (index >= 0) { |
| 174 | int length = expression.matchedLength(); |
| 175 | setFormat(index, length, rule.format); |
| 176 | index = text.indexOf(expression, index + length); |
| 177 | } */ |
| 178 | foreach (HighlightingRule rule, findRule) { |
| 179 | QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text); |
| 180 | while (matchIterator.hasNext()) { |
| 181 | QRegularExpressionMatch match = matchIterator.next(); |
| 182 | setFormat(match.capturedStart(), match.capturedLength(), rule.format); |
| 183 | } |
| 184 | } |
| 185 | // printf("hi blo\n"); |
| 186 | //fprintf(stderr,"hiliblo %dms\n",stech.elapsed()); |
| 187 | } |
| 188 | |
| 189 | void Highlighter::loadHighlightPatterns(QString FileName){ |
| 190 | QElapsedTimer stech; |
| 191 | stech.start(); |
| 192 | //int group=1; |
| 193 | highlightingRules.clear(); |
| 194 | outputstyle=true; |
| 195 | //fprintf(stderr,"loadHighlightPatterns %s\n",FileName.toStdString().c_str()); |
| 196 | QSettings *pat = new QSettings(FileName,QSettings::IniFormat); |
| 197 | /*foreach(HighlightingRule rule, highlightingRules){ |
| 198 | pat->beginGroup(QString("%1").arg(group)); |
| 199 | pat->setValue("Pattern" ,rule.pattern.pattern()); |
| 200 | pat->setValue("Color" ,rule.format.foreground().color().name()); |
| 201 | pat->setValue("Background" ,rule.format.background().color().name()); |
| 202 | pat->setValue("Weight" ,rule.format.fontWeight()); |
| 203 | pat->setValue("Underlined" ,rule.format.fontUnderline()); |
| 204 | pat->setValue("UnderlineStyle" ,rule.format.underlineStyle()); |
| 205 | pat->setValue("UnderlineColor" ,rule.format.underlineColor().name()); |
| 206 | pat->setValue("Overlined" ,rule.format.fontOverline()); |
| 207 | pat->setValue("CaseSensitive" ,rule.pattern.caseSensitivity()); |
| 208 | pat->endGroup(); |
| 209 | group++; |
| 210 | }*/ |
| 211 | static HighlightingRule rule; |
| 212 | QStringList groups = pat->childGroups(); |
| 213 | static QRegularExpression Dplus = QRegularExpression("\\D+"); |
| 214 | foreach (QString grp,groups){ |
| 215 | if (!grp.contains(Dplus)){ |
| 216 | pat->beginGroup(grp); |
| 217 | rule.pattern.setPattern(pat->value("Pattern","").toString()); |
| 218 | rule.pattern.setPatternOptions( QRegularExpression::CaseInsensitiveOption); |
| 219 | rule.format.setForeground(QColor(pat->value("Color" ,"#000000").toString())); |
| 220 | rule.format.setBackground(QColor(pat->value("Background" ,"#ffffff").toString())); |
| 221 | rule.format.setFontWeight(pat->value("Weight" ,50).toInt()); |
| 222 | rule.format.setFontUnderline(pat->value("Underlined" ,"false").toBool()); |
| 223 | rule.format.setUnderlineStyle((QTextCharFormat::UnderlineStyle) pat->value("UnderlineStyle" ,"1").toInt()); |
| 224 | rule.format.setUnderlineColor(QColor(pat->value("UnderlineColor" ,rule.format.foreground().color().name()).toString())); |
| 225 | rule.format.setFontOverline(pat->value("Overlined" ,"false").toBool()); |
| 226 | rule.Comment=pat->value("Comment","false").toBool(); |
| 227 | //rule.pattern.setPatternOptions(static_cast<QRegularExpression::PatternOptions>(pat->value("CaseSensitive" ,0).toInt())); |
| 228 | pat->endGroup(); |
| 229 | highlightingRules.append(rule); |
| 230 | }else{ |
| 231 | pat->beginGroup(grp); |
| 232 | multiLineCommentFormat.setForeground(QColor(pat->value("MultilineColor" ,"#000000").toString())); |
| 233 | multiLineCommentFormat.setBackground(QColor(pat->value("MultilineBackground" ,"#eefffe").toString())); |
| 234 | multiLineCommentFormat.setFontUnderline(pat->value("MultilineUnderlined" ,"false").toBool()); |
| 235 | commentStartExpression.setPattern(pat->value("MultilineStart","=\\s*$").toString()); |
| 236 | commentEndExpression.setPattern(pat->value("MultilineiEnd","^ [^=]*$").toString()); |
| 237 | outputstyle=false; |
| 238 | } |
| 239 | } |
| 240 | fprintf(stderr,"finished loadHighlightPatterns %s outputstyle=%s %dms\n",FileName.toStdString().c_str(),(outputstyle)?"true":"false",stech.elapsed()); |
| 241 | } |