OSDN Git Service

Initial checkin for denncoCreator
[dennco/denncoCreator.git] / Source / codeeditor / dccodehighlighter.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the examples of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 //  Copyright (c) 2012 Dennco Project
41 //
42 // This program is free software: you can redistribute it and/or modify
43 // it under the terms of the GNU General Public License as published by
44 // the Free Software Foundation, either version 3 of the License, or
45 // (at your option) any later version.
46 //
47 // This program is distributed in the hope that it will be useful,
48 // but WITHOUT ANY WARRANTY; without even the implied warranty of
49 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
50 // GNU General Public License for more details.
51 //
52 // You should have received a copy of the GNU General Public License
53 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
54
55 //
56 //  Created by tkawata on Sep-30, 2012.
57 //
58
59 #include "dccodehighlighter.h"
60
61 #include <QtGui>
62
63 DCCodeHighlighter::DCCodeHighlighter(QTextDocument *parent) :
64     QSyntaxHighlighter(parent)
65 {
66     HighlightingRule rule;
67
68     keywordFormat.setForeground(QColor(Qt::blue).lighter());
69     keywordFormat.setFontWeight(QFont::Bold);
70     QStringList keywordPatterns;
71     keywordPatterns << "\\bthis.cell.axonValue\\b" << "\\bthis.cell.receptors\\b"
72                     << "\\bfunction\\b";
73
74     foreach (const QString &pattern, keywordPatterns) {
75         rule.pattern = QRegExp(pattern);
76         rule.format = keywordFormat;
77         highlightingRules.append(rule);
78     }
79
80     classFormat.setFontWeight(QFont::Bold);
81     classFormat.setForeground(Qt::magenta);
82     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
83     rule.format = classFormat;
84     highlightingRules.append(rule);
85
86     singleLineCommentFormat.setForeground(Qt::red);
87     rule.pattern = QRegExp("//[^\n]*");
88     rule.format = singleLineCommentFormat;
89     highlightingRules.append(rule);
90
91     multiLineCommentFormat.setForeground(Qt::red);
92
93     quotationFormat.setForeground(QColor(Qt::green).lighter());
94     rule.pattern = QRegExp("\".*\"");
95     rule.format = quotationFormat;
96     highlightingRules.append(rule);
97
98     functionFormat.setFontItalic(true);
99     functionFormat.setForeground(QColor(Qt::blue).lighter(170));
100     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
101     rule.format = functionFormat;
102     highlightingRules.append(rule);
103
104     commentStartExpression = QRegExp("/\\*");
105     commentEndExpression = QRegExp("\\*/");
106 }
107
108
109 void DCCodeHighlighter::highlightBlock(const QString &text)
110 {
111     foreach (const HighlightingRule &rule, highlightingRules) {
112         QRegExp expression(rule.pattern);
113         int index = expression.indexIn(text);
114         while (index >= 0) {
115             int length = expression.matchedLength();
116             setFormat(index, length, rule.format);
117             index = expression.indexIn(text, index + length);
118         }
119     }
120     setCurrentBlockState(0);
121
122     int startIndex = 0;
123     if (previousBlockState() != 1)
124         startIndex = commentStartExpression.indexIn(text);
125
126     while (startIndex >= 0) {
127         int endIndex = commentEndExpression.indexIn(text, startIndex);
128         int commentLength;
129         if (endIndex == -1) {
130             setCurrentBlockState(1);
131             commentLength = text.length() - startIndex;
132         } else {
133             commentLength = endIndex - startIndex
134                             + commentEndExpression.matchedLength();
135         }
136         setFormat(startIndex, commentLength, multiLineCommentFormat);
137         startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
138     }
139 }