highlighter.cpp
author insilmaril
Mon, 15 Mar 2010 11:49:42 +0000
changeset 835 31841b366d5e
parent 831 25a950c2eb98
child 845 b98c1793bb8b
permissions -rw-r--r--
Fixes for autoLayout (later)
     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 					<< "\\bexportXHTML\\b"
    55 					<< "\\bexportXML\\b"
    56 					<< "\\bimportDir\\b"
    57 					<< "\\blinkTo\\b" 
    58 					<< "\\bloadImage\\b"
    59 					<< "\\bloadNote\\b"
    60 					<< "\\bmoveBranchUp\\b" 
    61 					<< "\\bmoveBranchDown\\b"
    62 					<< "\\bmove\\b" 
    63 					<< "\\bmoveRel\\b"
    64 					<< "\\bnop\\b"
    65 					<< "\\bpaste\\b" 
    66 					<< "\\bqa\\b" 
    67 					<< "\\bsaveImage\\b" 
    68 					<< "\\bsaveNote\\b" 
    69 					<< "\\bscroll\\b" 
    70 					<< "\\bselect\\b" 
    71 					<< "\\bselectLastBranch\\b" 
    72 					<< "\\bselectLastImage\\b"
    73 					<< "\\bselectLatestAdded\\b"
    74 					<< "\\bsetFrameType\\b" 
    75 					<< "\\bsetFramePenColor\\b" 
    76 					<< "\\bsetFrameBrushColor\\b" 
    77 					<< "\\bsetFramePadding\\b" 
    78 					<< "\\bsetFrameBorderWidth\\b" 
    79 					<< "\\bsetHideLinkUnselected\\b" 
    80 					<< "\\bsetMapAuthor\\b" 
    81 					<< "\\bsetMapComment\\b" 
    82 					<< "\\bsetMapBackgroundColor\\b" 
    83 					<< "\\bsetMapDefLinkColor\\b" 
    84 					<< "\\bsetMapDefLinkStyle\\b" 
    85 					<< "\\bsetNote\\b" 
    86 					<< "\\bsetHeading\\b" 
    87 					<< "\\bsetHideExport\\b" 
    88 					<< "\\bsetIncludeImagesHorizontally\\b" 
    89 					<< "\\bsetIncludeImagesVertically\\b" 
    90 					<< "\\bsetURL\\b" 
    91 					<< "\\bsetVymLink\\b" 
    92 					<< "\\bsetFlag\\b" 
    93 					<< "\\bsortChildren\\b" 
    94 					<< "\\btoggleFlag\\b" 
    95 					<< "\\bunscroll\\b" 
    96 					<< "\\bunscrollChilds\\b" 
    97 					<< "\\bunsetFlag\\b" 
    98 					;
    99     foreach (QString pattern, keywordPatterns) {
   100         rule.pattern = QRegExp(pattern);
   101         rule.format = keywordFormat;
   102         highlightingRules.append(rule);
   103     }
   104 
   105 	// QT keywords
   106 	/*
   107     classFormat.setFontWeight(QFont::Bold);
   108     classFormat.setForeground(Qt::darkMagenta);
   109     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
   110     rule.format = classFormat;
   111     highlightingRules.append(rule);
   112 	*/
   113 
   114 	// Single line comments
   115     singleLineCommentFormat.setForeground(Qt::red);
   116     rule.pattern = QRegExp("#[^\n]*");
   117     rule.format = singleLineCommentFormat;
   118     highlightingRules.append(rule);
   119 
   120 	// multiline comments
   121     multiLineCommentFormat.setForeground(Qt::red);
   122     commentStartExpression = QRegExp("/\\*");
   123     commentEndExpression = QRegExp("\\*/");
   124 
   125 	// Quotations
   126     quotationFormat.setForeground(Qt::darkGreen);
   127     rule.pattern = QRegExp("\".*\"");
   128     rule.format = quotationFormat;
   129     highlightingRules.append(rule);
   130 
   131     QStringList valuePatterns;
   132     valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
   133     foreach (QString pattern, valuePatterns) {
   134         rule.pattern = QRegExp(pattern);
   135         rule.format = quotationFormat;
   136         highlightingRules.append(rule);
   137     }
   138 
   139 
   140 
   141 	// Funtions
   142 	/*
   143     functionFormat.setFontItalic(true);
   144     functionFormat.setForeground(Qt::blue);
   145     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
   146     rule.format = functionFormat;
   147     highlightingRules.append(rule);
   148 	*/
   149 
   150 }
   151 
   152 void Highlighter::highlightBlock(const QString &text)
   153 {
   154     foreach (HighlightingRule rule, highlightingRules) {
   155         QRegExp expression(rule.pattern);
   156         int index = text.indexOf(expression);
   157         while (index >= 0) {
   158             int length = expression.matchedLength();
   159             setFormat(index, length, rule.format);
   160             index = text.indexOf(expression, index + length);
   161         }
   162     }
   163     setCurrentBlockState(0);
   164 
   165     int startIndex = 0;
   166     if (previousBlockState() != 1)
   167         startIndex = text.indexOf(commentStartExpression);
   168 
   169     while (startIndex >= 0) {
   170         int endIndex = text.indexOf(commentEndExpression, startIndex);
   171         int commentLength;
   172         if (endIndex == -1) {
   173             setCurrentBlockState(1);
   174             commentLength = text.length() - startIndex;
   175         } else {
   176             commentLength = endIndex - startIndex
   177                             + commentEndExpression.matchedLength();
   178         }
   179         setFormat(startIndex, commentLength, multiLineCommentFormat);
   180         startIndex = text.indexOf(commentStartExpression,
   181                                                 startIndex + commentLength);
   182     }
   183 }