FreeCAD

Форк
0
/
SyntaxHighlighter.cpp 
175 строк · 6.1 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
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.      *
10
 *                                                                         *
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.                  *
15
 *                                                                         *
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                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24

25
#include <QApplication>
26
#include <QPalette>
27

28
#include "SyntaxHighlighter.h"
29

30

31
using namespace Gui;
32

33
namespace Gui {
34
class SyntaxHighlighterP
35
{
36
public:
37
    SyntaxHighlighterP()
38
    {
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);
50
    }
51

52
    QColor cNormalText, cComment, cBlockcomment, cLiteral, cNumber,
53
    cOperator, cKeyword, cClassName, cDefineName, cOutput, cError;
54
};
55
} // namespace Gui
56

57
/**
58
 * Constructs a syntax highlighter.
59
 */
60
SyntaxHighlighter::SyntaxHighlighter(QObject* parent)
61
    : QSyntaxHighlighter(parent)
62
{
63
    d = new SyntaxHighlighterP;
64
}
65

66
/** Destroys this object. */
67
SyntaxHighlighter::~SyntaxHighlighter()
68
{
69
    delete d;
70
}
71

72
/** Sets the color \a col to the paragraph type \a type.
73
 * This method is provided for convenience to specify the paragraph type
74
 * by its name.
75
 */
76
void SyntaxHighlighter::setColor(const QString& type, const QColor& col)
77
{
78
    // Rehighlighting is very expensive, thus avoid it if this color is already set
79
    QColor old = color(type);
80
    if (!old.isValid())
81
        return; // no such type
82
    if (old == col)
83
        return;
84
    if (type == QLatin1String("Text"))
85
        d->cNormalText = col;
86
    else if (type == QLatin1String("Comment"))
87
        d->cComment = col;
88
    else if (type == QLatin1String("Block comment"))
89
        d->cBlockcomment = col;
90
    else if (type == QLatin1String("Number"))
91
        d->cNumber = col;
92
    else if (type == QLatin1String("String"))
93
        d->cLiteral = col;
94
    else if (type == QLatin1String("Keyword"))
95
        d->cKeyword = col;
96
    else if (type == QLatin1String("Class name"))
97
        d->cClassName = col;
98
    else if (type == QLatin1String("Define name"))
99
        d->cDefineName = col;
100
    else if (type == QLatin1String("Operator"))
101
        d->cOperator = col;
102
    else if (type == QLatin1String("Python output"))
103
        d->cOutput = col;
104
    else if (type == QLatin1String("Python error"))
105
        d->cError = col;
106
    colorChanged(type, col);
107
}
108

109
QColor SyntaxHighlighter::color(const QString& type)
110
{
111
    if (type == QLatin1String("Text"))
112
        return d->cNormalText;
113
    else if (type == QLatin1String("Comment"))
114
        return d->cComment;
115
    else if (type == QLatin1String("Block comment"))
116
        return d->cBlockcomment;
117
    else if (type == QLatin1String("Number"))
118
        return d->cNumber;
119
    else if (type == QLatin1String("String"))
120
        return d->cLiteral;
121
    else if (type == QLatin1String("Keyword"))
122
        return d->cKeyword;
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"))
128
        return d->cOperator;
129
    else if (type == QLatin1String("Python output"))
130
        return d->cOutput;
131
    else if (type == QLatin1String("Python error"))
132
        return d->cError;
133
    else
134
        return {}; // not found
135
}
136

137
QColor SyntaxHighlighter::colorByType(SyntaxHighlighter::TColor type)
138
{
139
    if (type == SyntaxHighlighter::Text)
140
        return d->cNormalText;
141
    else if (type == SyntaxHighlighter::Comment)
142
        return d->cComment;
143
    else if (type == SyntaxHighlighter::BlockComment)
144
        return d->cBlockcomment;
145
    else if (type == SyntaxHighlighter::Number)
146
        return d->cNumber;
147
    else if (type == SyntaxHighlighter::String)
148
        return d->cLiteral;
149
    else if (type == SyntaxHighlighter::Keyword)
150
        return d->cKeyword;
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)
156
        return d->cOperator;
157
    else if (type == SyntaxHighlighter::Output)
158
        return d->cOutput;
159
    else if (type == SyntaxHighlighter::Error)
160
        return d->cError;
161
    else
162
        return {}; // not found
163
}
164

165
void SyntaxHighlighter::colorChanged(const QString& type, const QColor& col)
166
{
167
    Q_UNUSED(type);
168
    Q_UNUSED(col);
169
    rehighlight();
170
}
171

172
int SyntaxHighlighter::maximumUserState() const
173
{
174
    return 8;
175
}
176

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.