~ubuntu-branches/ubuntu/intrepid/kdesdk/intrepid-updates

« back to all changes in this revision

Viewing changes to lokalize/src/syntaxhighlighter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-05-28 10:11:43 UTC
  • mto: This revision was merged to the branch mainline in revision 37.
  • Revision ID: james.westby@ubuntu.com-20080528101143-gzc3styjz1b70zxu
Tags: upstream-4.0.80
ImportĀ upstreamĀ versionĀ 4.0.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ****************************************************************************
 
2
  This file is part of KAider
 
3
 
 
4
  Copyright (C) 2007 by Nick Shaforostoff <shafff@ukr.net>
 
5
 
 
6
  This program is free software; you can redistribute it and/or modify
 
7
  it under the terms of the GNU General Public License as published by
 
8
  the Free Software Foundation; either version 2 of the License, or
 
9
  (at your option) any later version.
 
10
 
 
11
  This program is distributed in the hope that it will be useful,
 
12
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
  GNU General Public License for more details.
 
15
 
 
16
  You should have received a copy of the GNU General Public License
 
17
  along with this program; if not, write to the Free Software
 
18
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 
 
20
  In addition, as a special exception, the copyright holders give
 
21
  permission to link the code of this program with any edition of
 
22
  the Qt library by Trolltech AS, Norway (or with modified versions
 
23
  of Qt that use the same license as Qt), and distribute linked
 
24
  combinations including the two.  You must obey the GNU General
 
25
  Public License in all respects for all of the code used other than
 
26
  Qt. If you modify this file, you may extend this exception to
 
27
  your version of the file, but you are not obligated to do so.  If
 
28
  you do not wish to do so, delete this exception statement from
 
29
  your version.
 
30
 
 
31
**************************************************************************** */
 
32
 
 
33
 
 
34
#include "syntaxhighlighter.h"
 
35
 
 
36
#include "project.h"
 
37
 
 
38
#include <kdebug.h>
 
39
 
 
40
#include <QApplication>
 
41
 
 
42
#define STATE_NORMAL 0
 
43
#define STATE_TAG 1
 
44
 
 
45
 
 
46
#define NUM_OF_RULES 4
 
47
 
 
48
SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent/*, bool docbook*/)
 
49
    : QSyntaxHighlighter(parent)
 
50
    , tagBrush(KColorScheme::View,KColorScheme::VisitedText)
 
51
//     , fuzzyState(false)
 
52
//     , fromDocbook(docbook)
 
53
{
 
54
    highlightingRules.reserve(NUM_OF_RULES);
 
55
    HighlightingRule rule;
 
56
    //rule.format.setFontItalic(true);
 
57
//     tagFormat.setForeground(tagBrush.brush(QApplication::palette()));
 
58
    tagFormat.setForeground(tagBrush.brush(QApplication::palette()));
 
59
    //QTextCharFormat format;
 
60
    //tagFormat.setForeground(Qt::darkBlue);
 
61
//     if (!docbook) //support multiline tags
 
62
//     {
 
63
//         rule.format = tagFormat;
 
64
//         rule.pattern = QRegExp("<.+>");
 
65
//         rule.pattern.setMinimal(true);
 
66
//         highlightingRules.append(rule);
 
67
//     }
 
68
 
 
69
    //entity
 
70
    rule.format.setForeground(Qt::darkMagenta);
 
71
    rule.pattern = QRegExp("(&[A-Za-z_:][A-Za-z0-9_\\.:-]*;)");
 
72
    highlightingRules.append(rule);
 
73
 
 
74
    rule.format.setForeground(Qt::darkMagenta);
 
75
    rule.pattern = QRegExp(Project::instance()->accel());
 
76
    //rule.pattern = QRegExp("&[^;]*;");
 
77
/*    QString accelRx=Project::instance()->accel();
 
78
    int pos=accelRx.indexOf('(')+1;
 
79
    rule.pattern = QRegExp(  accelRx.mid( pos,accelRx.indexOf(')',pos)-1 )  );*/
 
80
    highlightingRules.append(rule);
 
81
 
 
82
    //\n \t \"
 
83
    rule.format.setForeground(Qt::darkGreen);
 
84
    rule.pattern = QRegExp("(\\\\[abfnrtv'\"\?\\\\])|(\\\\\\d+)|(\\\\x[\\dabcdef]+)");
 
85
    highlightingRules.append(rule);
 
86
 
 
87
/*
 
88
    //spaces
 
89
    rule.format.clearForeground();
 
90
    rule.format.setBackground(Qt::Dense6Pattern);
 
91
    rule.pattern = QRegExp(" ");
 
92
    highlightingRules.append(rule);
 
93
*/
 
94
//     commentStartExpression = QRegExp("/\\*");
 
95
//     commentEndExpression = QRegExp("\\*/");
 
96
 
 
97
//         highlightingRulesFuzzy[i].format.setFontItalic(true);
 
98
}
 
99
/*
 
100
void SyntaxHighlighter::setFuzzyState(bool fuzzy)
 
101
{
 
102
    return;
 
103
    int i=NUM_OF_RULES;
 
104
    while(--i>=0)
 
105
        highlightingRules[i].format.setFontItalic(fuzzy);
 
106
 
 
107
    tagFormat.setFontItalic(fuzzy);
 
108
}*/
 
109
 
 
110
void SyntaxHighlighter::highlightBlock(const QString &text)
 
111
{
 
112
//     if (fromDocbook)
 
113
    {
 
114
        setCurrentBlockState(STATE_NORMAL);
 
115
 
 
116
        int startIndex = STATE_NORMAL;
 
117
        if (previousBlockState() != STATE_TAG)
 
118
            startIndex = text.indexOf('<');
 
119
 
 
120
        while (startIndex >= 0)
 
121
        {
 
122
            int endIndex = text.indexOf('>', startIndex);
 
123
            int commentLength;
 
124
            if (endIndex == -1)
 
125
            {
 
126
                setCurrentBlockState(STATE_TAG);
 
127
                commentLength = text.length() - startIndex;
 
128
            }
 
129
            else
 
130
            {
 
131
                commentLength = endIndex - startIndex
 
132
                                +1/*+ commentEndExpression.matchedLength()*/;
 
133
            }
 
134
            setFormat(startIndex, commentLength, tagFormat);
 
135
            startIndex = text.indexOf('<', startIndex + commentLength);
 
136
        }
 
137
    }
 
138
 
 
139
    foreach (HighlightingRule rule, highlightingRules)
 
140
    {
 
141
        QRegExp expression(rule.pattern);
 
142
        int index = text.indexOf(expression);
 
143
        while (index >= 0)
 
144
        {
 
145
            int length = expression.matchedLength();
 
146
            setFormat(index, length, rule.format);
 
147
            index = text.indexOf(expression, index + length);
 
148
        }
 
149
    }
 
150
}
 
151
 
 
152
 
 
153
#include "syntaxhighlighter.moc"
 
154