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