highlighter.cpp
author insilmaril
Wed, 20 Jun 2007 11:58:47 +0000
changeset 523 3e73719e1613
parent 517 b525fdd445c4
child 524 7692a97d4def
permissions -rw-r--r--
Added center function
     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                     << "\\baddMapInsert\\b" 
    42 					<< "\\baddMapReplace\\b"
    43                     << "\\bcolorBranch\\b" 
    44 					<< "\\bcolorSubtree\\b"
    45 					<< "\\bcopy\\b"
    46                     << "\\bcut\\b" 
    47 					<< "\\bdelete\\b" 
    48 					<< "\\bdeleteKeepChilds\\b" 
    49 					<< "\\bdeleteChilds\\b"
    50 					<< "\\bexportASCII\\b"
    51 					<< "\\bexportImage\\b"
    52 					<< "\\bimportDir\\b"
    53 					<< "\\blinkTo\\b" 
    54 					<< "\\bloadImage\\b"
    55 					<< "\\bmoveBranchUp\\b" 
    56 					<< "\\bmoveBranchDown\\b"
    57 					<< "\\bmove\\b" 
    58 					<< "\\bmoveRel\\b"
    59 					<< "\\bnop\\b"
    60 					<< "\\bpaste\\b" 
    61 					<< "\\bqa\\b" 
    62 					<< "\\bsaveImage\\b" 
    63 					<< "\\bscroll\\b" 
    64 					<< "\\bselect\\b" 
    65 					<< "\\bselectLastBranch\\b" 
    66 					<< "\\bselectLastImage\\b"
    67 					<< "\\bsetFrameType\\b" 
    68 					<< "\\bsetFramePenColor\\b" 
    69 					<< "\\bsetFrameBrushColor\\b" 
    70 					<< "\\bsetFramePadding\\b" 
    71 					<< "\\bsetFrameBorderWidth\\b" 
    72 					<< "\\bsetHideLinkUnselected\\b" 
    73 					<< "\\bsetMapAuthor\\b" 
    74 					<< "\\bsetMapComment\\b" 
    75 					<< "\\bsetMapBackgroundColor\\b" 
    76 					<< "\\bsetMapDefLinkColor\\b" 
    77 					<< "\\bsetMapDefLinkStyle\\b" 
    78 					<< "\\bsetHeading\\b" 
    79 					<< "\\bsetHideExport\\b" 
    80 					<< "\\bsetIncludeImagesHorizontally\\b" 
    81 					<< "\\bsetIncludeImagesVertically\\b" 
    82 					<< "\\bsetURL\\b" 
    83 					<< "\\bsetVymLink\\b" 
    84 					<< "\\bsetFlag\\b" 
    85 					<< "\\btoggleFlag\\b" 
    86 					<< "\\bunscroll\\b" 
    87 					<< "\\bunscrollChilds\\b" 
    88 					<< "\\bunsetFlag\\b" 
    89 					;
    90     foreach (QString pattern, keywordPatterns) {
    91         rule.pattern = QRegExp(pattern);
    92         rule.format = keywordFormat;
    93         highlightingRules.append(rule);
    94     }
    95 
    96 	// QT keywords
    97 	/*
    98     classFormat.setFontWeight(QFont::Bold);
    99     classFormat.setForeground(Qt::darkMagenta);
   100     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
   101     rule.format = classFormat;
   102     highlightingRules.append(rule);
   103 	*/
   104 
   105 	// Single line comments
   106     singleLineCommentFormat.setForeground(Qt::red);
   107     rule.pattern = QRegExp("#[^\n]*");
   108     rule.format = singleLineCommentFormat;
   109     highlightingRules.append(rule);
   110 
   111 	// multiline comments
   112     multiLineCommentFormat.setForeground(Qt::red);
   113     commentStartExpression = QRegExp("/\\*");
   114     commentEndExpression = QRegExp("\\*/");
   115 
   116 	// Quotations
   117     quotationFormat.setForeground(Qt::darkGreen);
   118     rule.pattern = QRegExp("\".*\"");
   119     rule.format = quotationFormat;
   120     highlightingRules.append(rule);
   121 
   122     QStringList valuePatterns;
   123     valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
   124     foreach (QString pattern, valuePatterns) {
   125         rule.pattern = QRegExp(pattern);
   126         rule.format = quotationFormat;
   127         highlightingRules.append(rule);
   128     }
   129 
   130 
   131 
   132 	// Funtions
   133 	/*
   134     functionFormat.setFontItalic(true);
   135     functionFormat.setForeground(Qt::blue);
   136     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
   137     rule.format = functionFormat;
   138     highlightingRules.append(rule);
   139 	*/
   140 
   141 }
   142 
   143 void Highlighter::highlightBlock(const QString &text)
   144 {
   145     foreach (HighlightingRule rule, highlightingRules) {
   146         QRegExp expression(rule.pattern);
   147         int index = text.indexOf(expression);
   148         while (index >= 0) {
   149             int length = expression.matchedLength();
   150             setFormat(index, length, rule.format);
   151             index = text.indexOf(expression, index + length);
   152         }
   153     }
   154     setCurrentBlockState(0);
   155 
   156     int startIndex = 0;
   157     if (previousBlockState() != 1)
   158         startIndex = text.indexOf(commentStartExpression);
   159 
   160     while (startIndex >= 0) {
   161         int endIndex = text.indexOf(commentEndExpression, startIndex);
   162         int commentLength;
   163         if (endIndex == -1) {
   164             setCurrentBlockState(1);
   165             commentLength = text.length() - startIndex;
   166         } else {
   167             commentLength = endIndex - startIndex
   168                             + commentEndExpression.matchedLength();
   169         }
   170         setFormat(startIndex, commentLength, multiLineCommentFormat);
   171         startIndex = text.indexOf(commentStartExpression,
   172                                                 startIndex + commentLength);
   173     }
   174 }