Fabcoin Core  0.16.2
P2P Digital Currency
styleSheet.cpp
Go to the documentation of this file.
1 #include <styleSheet.h>
2 
3 #include <QFile>
4 #include <QWidget>
5 #include <QApplication>
6 #include <QStyleFactory>
7 #include <QProxyStyle>
8 #include <QListView>
9 #include <QComboBox>
10 #include <QMessageBox>
11 #include <QPushButton>
12 #include <QPainter>
13 #include <QLineEdit>
14 #include <QtGlobal>
15 
16 static const QString STYLE_FORMAT = ":/styles/%1";
17 static const QColor LINK_COLOR = "#2d9ad0";
18 
19 class FabcoinStyle : public QProxyStyle
20 {
21 public:
22 
23  void polish(QWidget *widget)
24  {
25  if(widget && widget->inherits("QComboBox"))
26  {
27  QComboBox* comboBox = (QComboBox*)widget;
28  if(comboBox->view() && comboBox->view()->inherits("QComboBoxListView"))
29  {
30  comboBox->setView(new QListView());
31  qApp->processEvents();
32  }
33 
34  if(comboBox->view() && comboBox->view()->parentWidget())
35  {
36  QWidget* parent = comboBox->view()->parentWidget();
37  parent->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
38  parent->setAttribute(Qt::WA_TranslucentBackground);
39  }
40  }
41  if(widget && widget->inherits("QMessageBox"))
42  {
43  QMessageBox* messageBox = (QMessageBox*)widget;
44  QPixmap iconPixmap;
45  QMessageBox::Icon icon = messageBox->icon();
46  switch (icon)
47  {
48  case QMessageBox::Information:
49  iconPixmap = QPixmap(":/styles/app-icons/message_info");
50  break;
51  case QMessageBox::Warning:
52  iconPixmap = QPixmap(":/styles/app-icons/message_warning");
53  break;
54  case QMessageBox::Critical:
55  iconPixmap = QPixmap(":/styles/app-icons/message_critical");
56  break;
57  case QMessageBox::Question:
58  iconPixmap = QPixmap(":/styles/app-icons/message_question");
59  break;
60  default:
61  QProxyStyle::polish(widget);
62  return;
63  }
64  messageBox->setIconPixmap(iconPixmap.scaled(45,49));
65  }
66  if(widget && widget->inherits("QPushButton"))
67  {
68  QPushButton* button = (QPushButton*)widget;
69  button->setText(button->text().toUpper());
70  }
71  if(widget && widget->inherits("QLineEdit"))
72  {
73  QLineEdit* lineEdit = (QLineEdit*)widget;
74  if(lineEdit->isReadOnly())
75  {
76  lineEdit->setFocusPolicy(Qt::ClickFocus);
77  }
78  }
79 
80  QProxyStyle::polish(widget);
81  }
82 };
83 
85 {
86  static StyleSheet inst;
87  return inst;
88 }
89 
91 {}
92 
93 void StyleSheet::setStyleSheet(QWidget *widget, const QString &style_name)
94 {
95  setObjectStyleSheet<QWidget>(widget, style_name);
96 }
97 
98 void StyleSheet::setStyleSheet(QApplication *app, const QString& style_name)
99 {
100  QStyle* mainStyle = QStyleFactory::create("fusion");
101  FabcoinStyle* fabcoinStyle = new FabcoinStyle;
102  fabcoinStyle->setBaseStyle(mainStyle);
103  app->setStyle(fabcoinStyle);
104 
105  QPalette mainPalette(app->palette());
106  mainPalette.setColor(QPalette::Link, LINK_COLOR);
107  app->setPalette(mainPalette);
108 
109  // Increase the font size slightly for Windows and MAC
110  QFont font = app->font();
111  qreal fontSize = font.pointSizeF();
112  qreal multiplier = 1;
113 #if defined(Q_OS_WIN) || defined(Q_OS_MAC)
114  multiplier = 1.1;
115 #endif
116  font.setPointSizeF(fontSize * multiplier);
117  app->setFont(font);
118 
119  setObjectStyleSheet<QApplication>(app, style_name);
120 }
121 
122 QString StyleSheet::getStyleSheet(const QString &style_name)
123 {
124  QString style;
125  QFile file(STYLE_FORMAT.arg(style_name));
126  if(file.open(QIODevice::ReadOnly))
127  {
128  style = file.readAll();
129  m_cacheStyles[style_name] = style;
130  }
131  return style;
132 }
133 
134 template<typename T>
135 void StyleSheet::setObjectStyleSheet(T *object, const QString &style_name)
136 {
137  QString style_value = m_cacheStyles.contains(style_name) ? m_cacheStyles[style_name] : getStyleSheet(style_name);
138  object->setStyleSheet(style_value);
139 }
static StyleSheet & instance()
Definition: styleSheet.cpp:84
QString getStyleSheet(const QString &style_name)
Definition: styleSheet.cpp:122
#define T(i, x)
void setStyleSheet(QWidget *widget, const QString &style_name)
Definition: styleSheet.cpp:93
Singleton class that manage the styles.
Definition: styleSheet.h:34
void setObjectStyleSheet(T *object, const QString &style_name)
Definition: styleSheet.cpp:135
void polish(QWidget *widget)
Definition: styleSheet.cpp:23