highlighter.cpp
author insilmaril
Fri, 09 Apr 2010 14:24:04 +0000
changeset 846 f0fe7c36ec5c
parent 845 b98c1793bb8b
permissions -rw-r--r--
Version 1.13.3 and added treeeditor.png
     1 /****************************************************************************
     2 **
     3 ** Copyright (C) 2005-2006 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://www.trolltech.com/products/qt/opensource.html
    13 **
    14 ** If you are unsure which license is appropriate for your use, please
    15 ** review the following information:
    16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
    17 ** sales department at sales@trolltech.com.
    18 **
    19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    21 **
    22 ****************************************************************************/
    23 
    24 // highlighting rules have been adapted by Uwe Drechsel to match vym syntax
    25 
    26 
    27 #include <QtGui>
    28 
    29 #include "highlighter.h"
    30 
    31 Highlighter::Highlighter(QTextDocument *parent)
    32     : QSyntaxHighlighter(parent)
    33 {
    34     HighlightingRule rule;
    35 
    36     keywordFormat.setForeground(Qt::darkBlue);
    37     keywordFormat.setFontWeight(QFont::Bold);
    38     QStringList keywordPatterns;
    39     keywordPatterns << "\\baddBranch\\b" 
    40 					<< "\\baddBranchBefore\\b" 
    41                     << "\\baddMapCenter\\b" 
    42                     << "\\baddMapInsert\\b" 
    43 					<< "\\baddMapReplace\\b"
    44                     << "\\bcolorBranch\\b" 
    45 					<< "\\bcolorSubtree\\b"
    46 					<< "\\bcopy\\b"
    47                     << "\\bcut\\b" 
    48 					<< "\\bdelete\\b" 
    49 					<< "\\bdeleteKeepChilds\\b" 
    50 					<< "\\bdeleteChilds\\b"
    51 					<< "\\bexportAO\\b"
    52 					<< "\\bexportASCII\\b"
    53 					<< "\\bexportImage\\b"
    54 					<< "\\bexportXML\\b"
    55 					<< "\\bimportDir\\b"
    56 					<< "\\blinkTo\\b" 
    57 					<< "\\bloadImage\\b"
    58 					<< "\\bloadNote\\b"
    59 					<< "\\bmoveBranchUp\\b" 
    60 					<< "\\bmoveBranchDown\\b"
    61 					<< "\\bmove\\b" 
    62 					<< "\\bmoveRel\\b"
    63 					<< "\\bnop\\b"
    64 					<< "\\bpaste\\b" 
    65 					<< "\\bqa\\b" 
    66 					<< "\\bsaveImage\\b" 
    67 					<< "\\bsaveNote\\b" 
    68 					<< "\\bscroll\\b" 
    69 					<< "\\bselect\\b" 
    70 					<< "\\bselectLastBranch\\b" 
    71 					<< "\\bselectLastImage\\b"
    72 					<< "\\bselectLatestAdded\\b"
    73 					<< "\\bsetFrameType\\b" 
    74 					<< "\\bsetFramePenColor\\b" 
    75 					<< "\\bsetFrameBrushColor\\b" 
    76 					<< "\\bsetFramePadding\\b" 
    77 					<< "\\bsetFrameBorderWidth\\b" 
    78 					<< "\\bsetHideLinkUnselected\\b" 
    79 					<< "\\bsetMapAuthor\\b" 
    80 					<< "\\bsetMapComment\\b" 
    81 					<< "\\bsetMapBackgroundColor\\b" 
    82 					<< "\\bsetMapDefLinkColor\\b" 
    83 					<< "\\bsetMapDefLinkStyle\\b" 
    84 					<< "\\bsetNote\\b" 
    85 					<< "\\bsetHeading\\b" 
    86 					<< "\\bsetHideExport\\b" 
    87 					<< "\\bsetIncludeImagesHorizontally\\b" 
    88 					<< "\\bsetIncludeImagesVertically\\b" 
    89 					<< "\\bsetURL\\b" 
    90 					<< "\\bsetVymLink\\b" 
    91 					<< "\\bsetFlag\\b" 
    92 					<< "\\bsortChildren\\b" 
    93 					<< "\\btoggleFlag\\b" 
    94 					<< "\\bunscroll\\b" 
    95 					<< "\\bunscrollChilds\\b" 
    96 					<< "\\bunsetFlag\\b" 
    97 					;
    98     foreach (QString pattern, keywordPatterns) {
    99         rule.pattern = QRegExp(pattern);
   100         rule.format = keywordFormat;
   101         highlightingRules.append(rule);
   102     }
   103 
   104 	// QT keywords
   105 	/*
   106     classFormat.setFontWeight(QFont::Bold);
   107     classFormat.setForeground(Qt::darkMagenta);
   108     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
   109     rule.format = classFormat;
   110     highlightingRules.append(rule);
   111 	*/
   112 
   113 	// Single line comments
   114     singleLineCommentFormat.setForeground(Qt::red);
   115     rule.pattern = QRegExp("#[^\n]*");
   116     rule.format = singleLineCommentFormat;
   117     highlightingRules.append(rule);
   118 
   119 	// multiline comments
   120     multiLineCommentFormat.setForeground(Qt::red);
   121     commentStartExpression = QRegExp("/\\*");
   122     commentEndExpression = QRegExp("\\*/");
   123 
   124 	// Quotations
   125     quotationFormat.setForeground(Qt::darkGreen);
   126     rule.pattern = QRegExp("\".*\"");
   127     rule.format = quotationFormat;
   128     highlightingRules.append(rule);
   129 
   130     QStringList valuePatterns;
   131     valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
   132     foreach (QString pattern, valuePatterns) {
   133         rule.pattern = QRegExp(pattern);
   134         rule.format = quotationFormat;
   135         highlightingRules.append(rule);
   136     }
   137 
   138 
   139 
   140 	// Funtions
   141 	/*
   142     functionFormat.setFontItalic(true);
   143     functionFormat.setForeground(Qt::blue);
   144     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
   145     rule.format = functionFormat;
   146     highlightingRules.append(rule);
   147 	*/
   148 
   149 }
   150 
   151 void Highlighter::highlightBlock(const QString &text)
   152 {
   153     foreach (HighlightingRule rule, highlightingRules) {
   154         QRegExp expression(rule.pattern);
   155         int index = text.indexOf(expression);
   156         while (index >= 0) {
   157             int length = expression.matchedLength();
   158             setFormat(index, length, rule.format);
   159             index = text.indexOf(expression, index + length);
   160         }
   161     }
   162     setCurrentBlockState(0);
   163 
   164     int startIndex = 0;
   165     if (previousBlockState() != 1)
   166         startIndex = text.indexOf(commentStartExpression);
   167 
   168     while (startIndex >= 0) {
   169         int endIndex = text.indexOf(commentEndExpression, startIndex);
   170         int commentLength;
   171         if (endIndex == -1) {
   172             setCurrentBlockState(1);
   173             commentLength = text.length() - startIndex;
   174         } else {
   175             commentLength = endIndex - startIndex
   176                             + commentEndExpression.matchedLength();
   177         }
   178         setFormat(startIndex, commentLength, multiLineCommentFormat);
   179         startIndex = text.indexOf(commentStartExpression,
   180                                                 startIndex + commentLength);
   181     }
   182 }