-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheme.cpp
More file actions
117 lines (99 loc) · 3.11 KB
/
Theme.cpp
File metadata and controls
117 lines (99 loc) · 3.11 KB
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
#include "Theme.h"
#include <QFile>
#include <QFileInfo>
#include <QSettings>
#include <QDebug>
Theme::Theme(Config* config) {
_config = config;
qDebug() << "Current theme: " << _config->theme();
_loadStyle();
_loadIni();
_loadImages();
}
void Theme::apply(QWidget* widget) {
widget->setStyleSheet(_style);
}
QString Theme::_basePath() {
QString postfix = "/themes/" + _config->theme() + "/";
QString themePath = _config->userFolder() + postfix;
QFileInfo info(themePath);
if (info.exists(themePath)) {
return themePath;
}
themePath = _config->appFolder() + postfix;
return themePath;
}
QString Theme::realPath(QString path) {
QString realPath = path.replace(":/", _basePath());
QFileInfo info(realPath);
QString result = info.exists() ? realPath : path;
return result;
}
QPixmap* Theme::pixmap(QString name) {
return _pixmaps[name];
}
QIcon* Theme::icon(QString name) {
return _icons[name];
}
QColor Theme::color(QString name, QColor defaultValue) {
QColor result(defaultValue);
if (_values.contains(name)) {
qDebug() << _values.value(name).toString();
result.setNamedColor(_values.value(name).toString());
qDebug() << result;
}
return result;
}
void Theme::_loadStyle() {
if (_config->theme() == "default") {
_style = "";
return;
}
QString path = realPath(":/style.qss");
qDebug() << "Loading style: " << path;
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return;
}
auto ba = file.readAll();
_style = QString::fromUtf8(ba);
_style.replace("url(\":/", "url(\"" + _basePath());
file.close();
}
void Theme::_loadIni() {
if (_config->theme() == "default") {
_values.clear();
return;
}
QString path = realPath(":/style.ini");
qDebug() << "Loading theme config: " << path;
QSettings settings(path, QSettings::IniFormat);
settings.sync();
foreach (auto group, settings.childGroups()) {
settings.beginGroup(group);
foreach (auto key, settings.childKeys()) {
QString fullKey = group + "." + key;
QVariant value = settings.value(key);
_values.insert(fullKey, value);
qDebug() << "Key/Value added: " << fullKey << "/" << value.toString();
}
settings.endGroup();
}
}
void Theme::_loadImages() {
_pixmaps["back"] = _createPixmap(":/icons/back-big.png");
_pixmaps["file"] = _createPixmap(":/icons/file-big.png");
_pixmaps["folder"] = _createPixmap(":/icons/folder-big.png");
_pixmaps["image"] = _createPixmap(":/icons/image-big.png");
_pixmaps["image-error"] = _createPixmap(":/icons/image-error-big.png");
_icons["folder"] = _createIcon(":/icons/folder.png");
}
QPixmap* Theme::_createPixmap(QString path) {
int size = _config->thumbnailSize();
QPixmap pixmap(realPath(path));
auto result = new QPixmap(pixmap.scaled(size - 8, size - 8, Qt::KeepAspectRatio, Qt::SmoothTransformation));
return result;
}
QIcon* Theme::_createIcon(QString path) {
return new QIcon(realPath(path));
}