Debian Codegraph @

src/internal/QSyntaxStyle.cpp

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