stylesheet.h
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #ifndef STYLESHEET_H
8 #define STYLESHEET_H
9 
10 #include <QString>
11 #include <map>
12 #include <QColor>
13 #include <QSettings>
14 #include <QApplication>
15 #include <QFile>
16 
17 #include "QSyntaxStyle"
18 
19 inline QString SetApplicationStyleSheet(QString style)
20 {
21  QString out;
22  QStringList lines = style.split("\n");
23 
24  std::map<QString, QString> palette;
25 
26  int i = 0;
27 
28  while (i < lines.size())
29  {
30  if (lines[i++].contains("PALETTE START"))
31  {
32  break;
33  }
34  }
35 
36  while (i < lines.size())
37  {
38  auto parts = lines[i].split(":");
39  if (parts.size() == 2)
40  {
41  QString value = parts[1].remove(" ");
42  value.remove("\r");
43  palette.insert({ parts[0].remove(" "), value });
44  }
45 
46  if (lines[i++].contains("PALETTE END"))
47  {
48  break;
49  }
50  }
51 
52  while (i < lines.size())
53  {
54  QString line = lines[i];
55 
56  int pos_start = line.indexOf("${", 0);
57  if (pos_start != -1)
58  {
59  int pos_end = line.indexOf("}", pos_start);
60  if (pos_end == -1)
61  {
62  throw std::runtime_error("problem loading stylesheet. Unclosed ${}");
63  }
64  int mid_length = pos_end - (pos_start + 2);
65  QString id = line.mid(pos_start + 2, mid_length);
66 
67  if (palette.count(id) == 0)
68  {
69  std::string msg = "Problem loading stylesheet: can't find palette id: ";
70  msg += id.toStdString();
71  throw std::runtime_error(msg);
72  }
73  QString color = palette[id];
74  line = line.left(pos_start) + color + line.right(line.size() - pos_end - 1);
75  }
76 
77  out += line + "\n";
78  i++;
79  }
80 
81  QSettings settings;
82  settings.setValue("StyleSheet::theme", palette["theme"]);
83 
84  dynamic_cast<QApplication*>(QCoreApplication::instance())->setStyleSheet(out);
85 
86  return palette["theme"];
87 }
88 
89 inline QSyntaxStyle* GetLuaSyntaxStyle(QString theme)
90 {
91  auto loadStyle = [](const char* path) -> QSyntaxStyle* {
92  QFile fl(path);
93  QSyntaxStyle* style = nullptr;
94  if (fl.open(QIODevice::ReadOnly))
95  {
96  style = new QSyntaxStyle();
97  if (!style->load(fl.readAll()))
98  {
99  delete style;
100  }
101  }
102  return style;
103  };
104 
105  static QSyntaxStyle* style[2] = { loadStyle(":/resources/lua_style_light.xml"),
106  loadStyle(":/resources/lua_style_dark.xml") };
107 
108  return theme == "light" ? style[0] : style[1];
109 }
110 
111 #endif // STYLESHEET_H
color
color
Definition: color.h:16
QSyntaxStyle::load
bool load(QString fl)
Method for loading and parsing style.
Definition: QSyntaxStyle.cpp:18
GetLuaSyntaxStyle
QSyntaxStyle * GetLuaSyntaxStyle(QString theme)
Definition: stylesheet.h:89
SetApplicationStyleSheet
QString SetApplicationStyleSheet(QString style)
Definition: stylesheet.h:19
mqtt_test_proto.msg
msg
Definition: mqtt_test_proto.py:43
QSyntaxStyle
Class, that describes Qt style parser for QCodeEditor.
Definition: QSyntaxStyle.hpp:13


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