| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtGui>
|
|---|
| 43 |
|
|---|
| 44 | #include "highlighter.h"
|
|---|
| 45 |
|
|---|
| 46 | //! [0]
|
|---|
| 47 | Highlighter::Highlighter(QTextDocument *parent)
|
|---|
| 48 | : QSyntaxHighlighter(parent)
|
|---|
| 49 | {
|
|---|
| 50 | HighlightingRule rule;
|
|---|
| 51 |
|
|---|
| 52 | keywordFormat.setForeground(Qt::darkBlue);
|
|---|
| 53 | keywordFormat.setFontWeight(QFont::Bold);
|
|---|
| 54 | QStringList keywordPatterns;
|
|---|
| 55 | keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
|
|---|
| 56 | << "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
|
|---|
| 57 | << "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
|
|---|
| 58 | << "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
|
|---|
| 59 | << "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
|
|---|
| 60 | << "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
|
|---|
| 61 | << "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
|
|---|
| 62 | << "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
|
|---|
| 63 | << "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
|
|---|
| 64 | << "\\bvoid\\b" << "\\bvolatile\\b";
|
|---|
| 65 | foreach (const QString &pattern, keywordPatterns) {
|
|---|
| 66 | rule.pattern = QRegExp(pattern);
|
|---|
| 67 | rule.format = keywordFormat;
|
|---|
| 68 | highlightingRules.append(rule);
|
|---|
| 69 | //! [0] //! [1]
|
|---|
| 70 | }
|
|---|
| 71 | //! [1]
|
|---|
| 72 |
|
|---|
| 73 | //! [2]
|
|---|
| 74 | classFormat.setFontWeight(QFont::Bold);
|
|---|
| 75 | classFormat.setForeground(Qt::darkMagenta);
|
|---|
| 76 | rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
|
|---|
| 77 | rule.format = classFormat;
|
|---|
| 78 | highlightingRules.append(rule);
|
|---|
| 79 | //! [2]
|
|---|
| 80 |
|
|---|
| 81 | //! [3]
|
|---|
| 82 | singleLineCommentFormat.setForeground(Qt::red);
|
|---|
| 83 | rule.pattern = QRegExp("//[^\n]*");
|
|---|
| 84 | rule.format = singleLineCommentFormat;
|
|---|
| 85 | highlightingRules.append(rule);
|
|---|
| 86 |
|
|---|
| 87 | multiLineCommentFormat.setForeground(Qt::red);
|
|---|
| 88 | //! [3]
|
|---|
| 89 |
|
|---|
| 90 | //! [4]
|
|---|
| 91 | quotationFormat.setForeground(Qt::darkGreen);
|
|---|
| 92 | rule.pattern = QRegExp("\".*\"");
|
|---|
| 93 | rule.format = quotationFormat;
|
|---|
| 94 | highlightingRules.append(rule);
|
|---|
| 95 | //! [4]
|
|---|
| 96 |
|
|---|
| 97 | //! [5]
|
|---|
| 98 | functionFormat.setFontItalic(true);
|
|---|
| 99 | functionFormat.setForeground(Qt::blue);
|
|---|
| 100 | rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
|
|---|
| 101 | rule.format = functionFormat;
|
|---|
| 102 | highlightingRules.append(rule);
|
|---|
| 103 | //! [5]
|
|---|
| 104 |
|
|---|
| 105 | //! [6]
|
|---|
| 106 | commentStartExpression = QRegExp("/\\*");
|
|---|
| 107 | commentEndExpression = QRegExp("\\*/");
|
|---|
| 108 | }
|
|---|
| 109 | //! [6]
|
|---|
| 110 |
|
|---|
| 111 | //! [7]
|
|---|
| 112 | void Highlighter::highlightBlock(const QString &text)
|
|---|
| 113 | {
|
|---|
| 114 | foreach (const HighlightingRule &rule, highlightingRules) {
|
|---|
| 115 | QRegExp expression(rule.pattern);
|
|---|
| 116 | int index = expression.indexIn(text);
|
|---|
| 117 | while (index >= 0) {
|
|---|
| 118 | int length = expression.matchedLength();
|
|---|
| 119 | setFormat(index, length, rule.format);
|
|---|
| 120 | index = expression.indexIn(text, index + length);
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 | //! [7] //! [8]
|
|---|
| 124 | setCurrentBlockState(0);
|
|---|
| 125 | //! [8]
|
|---|
| 126 |
|
|---|
| 127 | //! [9]
|
|---|
| 128 | int startIndex = 0;
|
|---|
| 129 | if (previousBlockState() != 1)
|
|---|
| 130 | startIndex = commentStartExpression.indexIn(text);
|
|---|
| 131 |
|
|---|
| 132 | //! [9] //! [10]
|
|---|
| 133 | while (startIndex >= 0) {
|
|---|
| 134 | //! [10] //! [11]
|
|---|
| 135 | int endIndex = commentEndExpression.indexIn(text, startIndex);
|
|---|
| 136 | int commentLength;
|
|---|
| 137 | if (endIndex == -1) {
|
|---|
| 138 | setCurrentBlockState(1);
|
|---|
| 139 | commentLength = text.length() - startIndex;
|
|---|
| 140 | } else {
|
|---|
| 141 | commentLength = endIndex - startIndex
|
|---|
| 142 | + commentEndExpression.matchedLength();
|
|---|
| 143 | }
|
|---|
| 144 | setFormat(startIndex, commentLength, multiLineCommentFormat);
|
|---|
| 145 | startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 | //! [11]
|
|---|