stylesheet.h
Go to the documentation of this file.
1 #ifndef STYLESHEET_H
2 #define STYLESHEET_H
3 
4 #include <QString>
5 #include <map>
6 #include <QColor>
7 #include <QSettings>
8 #include <QApplication>
9 
10 inline QString SetApplicationStyleSheet(QString style)
11 {
12  QString out;
13  QStringList lines = style.split("\n");
14 
15  std::map<QString, QString> palette;
16 
17  int i = 0;
18 
19  while( i < lines.size() )
20  {
21  if( lines[i++].contains("PALETTE START") )
22  {
23  break;
24  }
25  }
26 
27  while( i < lines.size() )
28  {
29  auto parts = lines[i].split(":");
30  if( parts.size() == 2 )
31  {
32  palette.insert( {parts[0].remove(" "), parts[1].remove(" ")} );
33  }
34 
35  if( lines[i++].contains("PALETTE END") )
36  {
37  break;
38  }
39  }
40 
41 
42  while( i < lines.size() )
43  {
44  QString line = lines[i];
45 
46  int pos_start = line.indexOf("${", 0 );
47  if( pos_start != -1 )
48  {
49  int pos_end = line.indexOf("}",pos_start );
50  if( pos_end == -1 )
51  {
52  throw std::runtime_error("problem loading stylesheet. Unclosed ${}");
53  }
54  int mid_length = pos_end -( pos_start+2 );
55  QString id = line.mid(pos_start+2, mid_length);
56 
57  if (palette.count(id) == 0)
58  {
59  std::string msg = "Problem loading stylesheet: can't find palette id: ";
60  msg += id.toStdString();
61  throw std::runtime_error(msg);
62  }
63  QString color = palette[id];
64  line = line.left(pos_start) + color + line.right(line.size() - pos_end -1);
65  }
66 
67  out += line + "\n";
68  i++;
69  }
70 
71  QSettings settings;
72  settings.setValue("StyleSheet::theme", palette["theme"]);
73 
74  dynamic_cast<QApplication*>(QCoreApplication::instance())->setStyleSheet(out);
75 
76  return palette["theme"];
77 }
78 
79 #endif // STYLESHEET_H
QString SetApplicationStyleSheet(QString style)
Definition: stylesheet.h:10


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 04:02:47