QSyntaxStyle.cpp
Go to the documentation of this file.
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 
135 {
136  return m_loaded;
137 }
138 
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 }
QSyntaxStyle::name
QString name() const
Method for getting style name.
Definition: QSyntaxStyle.cpp:117
QSyntaxStyle::m_loaded
bool m_loaded
Definition: QSyntaxStyle.hpp:68
QSyntaxStyle::load
bool load(QString fl)
Method for loading and parsing style.
Definition: QSyntaxStyle.cpp:18
QSyntaxStyle::QSyntaxStyle
QSyntaxStyle(QObject *parent=nullptr)
Constructor.
Definition: QSyntaxStyle.cpp:9
QSyntaxStyle::isLoaded
bool isLoaded() const
Method for checking is syntax style loaded.
Definition: QSyntaxStyle.cpp:134
s
XmlRpcServer s
emphasis::underline
@ underline
QSyntaxStyle::m_data
QMap< QString, QTextCharFormat > m_data
Definition: QSyntaxStyle.hpp:66
QSyntaxStyle::m_name
QString m_name
Definition: QSyntaxStyle.hpp:61
QSyntaxStyle::getFormat
QTextCharFormat getFormat(QString name) const
Method for getting format for property name.
Definition: QSyntaxStyle.cpp:122
format
auto format(const text_style &ts, const S &format_str, const Args &... args) -> std::basic_string< Char >
Definition: color.h:543
QSyntaxStyle::defaultStyle
static QSyntaxStyle * defaultStyle()
Static method for getting default style.
Definition: QSyntaxStyle.cpp:139
QSyntaxStyle
Class, that describes Qt style parser for QCodeEditor.
Definition: QSyntaxStyle.hpp:13
reader
static const char * reader(lua_State *L, void *ud, size_t *size)
Definition: luac.c:126


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45