-
Notifications
You must be signed in to change notification settings - Fork 6
/
settings.hpp
160 lines (148 loc) · 5.25 KB
/
settings.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*This class can r/w settings, set themes*/
/************created by m231**************/
/**************[email protected]*************/
#ifndef SETTINGS_HPP
#define SETTINGS_HPP
#include <QApplication>
#include <QDesktopWidget>
#include <QPalette>
#include <QSettings>
#include <QStyle>
#include <QStyleFactory>
#include <QToolTip>
#include <QTimer>
class Settings : public QObject
{
Q_OBJECT
QSettings *m_settingsObj = nullptr;
QSettings p_settings{
"HKEY_CURRENT_"
"USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
QSettings::NativeFormat};
QTimer mTimer;
bool islight;
public:
enum Palette { light, dark };
enum Theme { vista, classic, lightFusion, darkFusion, autoFusion };
enum Format { regFormat, iniFormat };
//Construct a Settings object
Settings(bool format, const QString name)
{
(format) ? m_settingsObj = new QSettings(name, QSettings::IniFormat)
: m_settingsObj = new QSettings(name, QApplication::applicationName());
mTimer.setInterval(500);
mTimer.connect(&mTimer, &QTimer::timeout, [&]{
auto const temp=p_settings.value("AppsUseLightTheme", true).toBool();
if(temp==islight){
qApp->setStyle(QStyleFactory::create("Fusion"));
changePalette(!temp);
emit isLight(temp);
}
islight=temp;
});
}
~Settings() { delete m_settingsObj; }
/*!
\fn int loadStyle
\brief Loads current application style from settings file or registry
*/
int loadStyle()
{
int val;
m_settingsObj->beginGroup("Style");
val = m_settingsObj->value("Theme", autoFusion).toInt(); //default theme is lightFusion
m_settingsObj->endGroup();
return val;
}
/*!
\fn void setStyle
\brief Apply a given style to application
*/
void setStyle(const int val)
{
setAutoPalette(val==autoFusion);
switch (val) {
case vista:
qApp->setStyle(QStyleFactory::create("windowsvista"));
changePalette(Settings::light);
break;
case classic:
qApp->setStyle(QStyleFactory::create("windows"));
changePalette(Settings::light);
break;
case lightFusion:
qApp->setStyle(QStyleFactory::create("Fusion"));
changePalette(Settings::light);
break;
case darkFusion:
qApp->setStyle(QStyleFactory::create("Fusion"));
changePalette(Settings::dark);
break;
default:
break;
}
}
void setAutoPalette(bool b)
{
b ? mTimer.start() : mTimer.stop();
}
/*!
\fn QVariant readSettings
\brief Read a value stored in settings given group and key
*/
QVariant readSettings(const QString group, const QString key)
{
QVariant val;
m_settingsObj->beginGroup(group);
val = m_settingsObj->value(key);
m_settingsObj->endGroup();
return val;
}
template<class T>
/*!
\fn void writeSettings
\brief Write a value in settings given group and key
*/
void writeSettings(const QString group, const QString key, const T option)
{
m_settingsObj->beginGroup(group);
m_settingsObj->setValue(key, option);
m_settingsObj->endGroup();
}
/*!
\fn void changePalette
\brief Change the color palette between light or dark
*/
void changePalette(bool _palette)
{
static QPalette Palette;
if (_palette) {
Palette.setColor(QPalette::Window, QColor(53, 53, 53));
Palette.setColor(QPalette::WindowText, Qt::white);
Palette.setColor(QPalette::Base, QColor(25, 25, 25));
Palette.setColor(QPalette::AlternateBase, QColor(53, 53, 53));
Palette.setColor(QPalette::ToolTipBase, QColor(53, 53, 53));
Palette.setColor(QPalette::ToolTipText, Qt::white);
Palette.setColor(QPalette::Text, Qt::white);
Palette.setColor(QPalette::Button, QColor(53, 53, 53));
Palette.setColor(QPalette::ButtonText, Qt::white);
Palette.setColor(QPalette::BrightText, Qt::red);
Palette.setColor(QPalette::Link, QColor(42, 130, 218));
Palette.setColor(QPalette::Highlight, QColor(42, 130, 218));
Palette.setColor(QPalette::HighlightedText, Qt::black);
Palette.setColor(QPalette::Disabled, QPalette::Text, QColor(164, 166, 168));
Palette.setColor(QPalette::Disabled, QPalette::WindowText, QColor(164, 166, 168));
Palette.setColor(QPalette::Disabled, QPalette::ButtonText, QColor(164, 166, 168));
Palette.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(164, 166, 168));
Palette.setColor(QPalette::Disabled, QPalette::Base, QColor(68, 68, 68));
Palette.setColor(QPalette::Disabled, QPalette::Window, QColor(68, 68, 68));
Palette.setColor(QPalette::Disabled, QPalette::Highlight, QColor(68, 68, 68));
} else
Palette = qApp->style()->standardPalette();
QToolTip::setPalette(Palette);
qApp->setPalette(Palette);
}
signals:
void isLight(bool b);
};
#endif // SETTINGS_HPP