-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathQSyntaxStyle.cpp
More file actions
155 lines (126 loc) · 4.14 KB
/
QSyntaxStyle.cpp
File metadata and controls
155 lines (126 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// QCodeEditor
#include <QSyntaxStyle>
// Qt
#include <QDebug>
#include <QXmlStreamReader>
#include <QFile>
QSyntaxStyle::QSyntaxStyle(QObject* parent) :
QObject(parent),
m_name(),
m_data(),
m_loaded(false)
{
}
bool QSyntaxStyle::load(QString fl)
{
QXmlStreamReader reader(fl);
while (!reader.atEnd() && !reader.hasError())
{
auto token = reader.readNext();
if(token == QXmlStreamReader::StartElement)
{
if (reader.name().toString() == "style-scheme") {
if (reader.attributes().hasAttribute("name"))
{
m_name = reader.attributes().value("name").toString();
}
} else if (reader.name().toString() == "style") {
auto attributes = reader.attributes();
auto name = attributes.value("name");
QTextCharFormat format;
if (attributes.hasAttribute("background"))
{
format.setBackground(QColor(attributes.value("background").toString()));
}
if (attributes.hasAttribute("foreground"))
{
format.setForeground(QColor(attributes.value("foreground").toString()));
}
if (attributes.hasAttribute("bold")
&& attributes.value("bold").toString() == "true") {
format.setFontWeight(QFont::Weight::Bold);
}
if (attributes.hasAttribute("italic")
&& attributes.value("italic").toString() == "true") {
format.setFontItalic(true);
}
if (attributes.hasAttribute("underlineStyle"))
{
auto underline = attributes.value("underlineStyle").toString();
auto s = QTextCharFormat::UnderlineStyle::NoUnderline;
if (underline == "SingleUnderline")
{
s = QTextCharFormat::UnderlineStyle::SingleUnderline;
}
else if (underline == "DashUnderline")
{
s = QTextCharFormat::UnderlineStyle::DashUnderline;
}
else if (underline == "DotLine")
{
s = QTextCharFormat::UnderlineStyle::DotLine;
}
else if (underline == "DashDotLine")
{
s = QTextCharFormat::DashDotLine;
}
else if (underline == "DashDotDotLine")
{
s = QTextCharFormat::DashDotDotLine;
}
else if (underline == "WaveUnderline")
{
s = QTextCharFormat::WaveUnderline;
}
else if (underline == "SpellCheckUnderline")
{
s = QTextCharFormat::SpellCheckUnderline;
}
else
{
qDebug() << "Unknown underline value " << underline;
}
format.setUnderlineStyle(s);
}
m_data[name.toString()] = format;
}
}
}
m_loaded = !reader.hasError();
return m_loaded;
}
QString QSyntaxStyle::name() const
{
return m_name;
}
QTextCharFormat QSyntaxStyle::getFormat(QString name) const
{
auto result = m_data.find(name);
if (result == m_data.end())
{
return QTextCharFormat();
}
return result.value();
}
bool QSyntaxStyle::isLoaded() const
{
return m_loaded;
}
QSyntaxStyle* QSyntaxStyle::defaultStyle()
{
static QSyntaxStyle style;
if (!style.isLoaded())
{
Q_INIT_RESOURCE(qcodeeditor_resources);
QFile fl(":/default_style.xml");
if (!fl.open(QIODevice::ReadOnly))
{
return &style;
}
if (!style.load(fl.readAll()))
{
qDebug() << "Can't load default style.";
}
}
return &style;
}