1
/***************************************************************************
2
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library 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 Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
#include <QApplication>
28
#include "SyntaxHighlighter.h"
34
class SyntaxHighlighterP
39
cNormalText = qApp->palette().windowText().color();
40
cComment.setRgb(0, 170, 0);
41
cBlockcomment.setRgb(160, 160, 164);
42
cLiteral.setRgb(255, 0, 0);
43
cNumber.setRgb(0, 0, 255);
44
cOperator.setRgb(160, 160, 164);
45
cKeyword.setRgb(0, 0, 255);
46
cClassName.setRgb(255, 170, 0);
47
cDefineName.setRgb(255, 170, 0);
48
cOutput.setRgb(170, 170, 127);
49
cError.setRgb(255, 0, 0);
52
QColor cNormalText, cComment, cBlockcomment, cLiteral, cNumber,
53
cOperator, cKeyword, cClassName, cDefineName, cOutput, cError;
58
* Constructs a syntax highlighter.
60
SyntaxHighlighter::SyntaxHighlighter(QObject* parent)
61
: QSyntaxHighlighter(parent)
63
d = new SyntaxHighlighterP;
66
/** Destroys this object. */
67
SyntaxHighlighter::~SyntaxHighlighter()
72
/** Sets the color \a col to the paragraph type \a type.
73
* This method is provided for convenience to specify the paragraph type
76
void SyntaxHighlighter::setColor(const QString& type, const QColor& col)
78
// Rehighlighting is very expensive, thus avoid it if this color is already set
79
QColor old = color(type);
81
return; // no such type
84
if (type == QLatin1String("Text"))
86
else if (type == QLatin1String("Comment"))
88
else if (type == QLatin1String("Block comment"))
89
d->cBlockcomment = col;
90
else if (type == QLatin1String("Number"))
92
else if (type == QLatin1String("String"))
94
else if (type == QLatin1String("Keyword"))
96
else if (type == QLatin1String("Class name"))
98
else if (type == QLatin1String("Define name"))
100
else if (type == QLatin1String("Operator"))
102
else if (type == QLatin1String("Python output"))
104
else if (type == QLatin1String("Python error"))
106
colorChanged(type, col);
109
QColor SyntaxHighlighter::color(const QString& type)
111
if (type == QLatin1String("Text"))
112
return d->cNormalText;
113
else if (type == QLatin1String("Comment"))
115
else if (type == QLatin1String("Block comment"))
116
return d->cBlockcomment;
117
else if (type == QLatin1String("Number"))
119
else if (type == QLatin1String("String"))
121
else if (type == QLatin1String("Keyword"))
123
else if (type == QLatin1String("Class name"))
124
return d->cClassName;
125
else if (type == QLatin1String("Define name"))
126
return d->cDefineName;
127
else if (type == QLatin1String("Operator"))
129
else if (type == QLatin1String("Python output"))
131
else if (type == QLatin1String("Python error"))
134
return {}; // not found
137
QColor SyntaxHighlighter::colorByType(SyntaxHighlighter::TColor type)
139
if (type == SyntaxHighlighter::Text)
140
return d->cNormalText;
141
else if (type == SyntaxHighlighter::Comment)
143
else if (type == SyntaxHighlighter::BlockComment)
144
return d->cBlockcomment;
145
else if (type == SyntaxHighlighter::Number)
147
else if (type == SyntaxHighlighter::String)
149
else if (type == SyntaxHighlighter::Keyword)
151
else if (type == SyntaxHighlighter::Classname)
152
return d->cClassName;
153
else if (type == SyntaxHighlighter::Defname)
154
return d->cDefineName;
155
else if (type == SyntaxHighlighter::Operator)
157
else if (type == SyntaxHighlighter::Output)
159
else if (type == SyntaxHighlighter::Error)
162
return {}; // not found
165
void SyntaxHighlighter::colorChanged(const QString& type, const QColor& col)
172
int SyntaxHighlighter::maximumUserState() const