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