-
-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathTheme.cpp
More file actions
246 lines (214 loc) · 12.6 KB
/
Copy pathTheme.cpp
File metadata and controls
246 lines (214 loc) · 12.6 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright (C) 2020 - 2025 Evan Teran <evan.teran@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "Theme.h"
#include "Configuration.h"
#include "edb.h"
#include <QApplication>
#include <QDir>
#include <QPalette>
#include <QSettings>
#include <QStandardPaths>
namespace {
/**
* @brief Returns the directory where themes are stored.
*
* @return The path to the theme directory.
*/
QString themeDirectory() {
static const QString configDir = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
return QStringLiteral("%1/%2/%3").arg(configDir, QApplication::organizationName(), QLatin1String("themes"));
}
/**
* @brief Reads a color from the settings.
*
* @param settings The settings object to read from.
* @param name The name of the color to read.
* @param defaultValue The default value to return if the color is not found.
* @return The color read from the settings or the default value if not found.
*/
QColor readColor(QSettings &settings, const QString &name, const QColor &defaultValue = QColor()) {
QVariant variant = settings.value(name);
auto color = variant.value<QColor>();
if (color.isValid()) {
return color;
}
return defaultValue;
}
/**
* @brief Reads a text character format from the settings.
*
* @param settings The settings object to read from.
* @param name The name of the format to read.
* @param defaultValue The default value to return if the format is not found.
* @return The format read from the settings or the default value if not found.
*/
QTextCharFormat readFormat(QSettings &settings, const QString &name, const QTextCharFormat &defaultValue = QTextCharFormat()) {
QTextCharFormat format;
format.setForeground(readColor(settings, QStringLiteral("%1.foreground").arg(name), defaultValue.foreground().color()));
format.setBackground(readColor(settings, QStringLiteral("%1.background").arg(name), defaultValue.background().color()));
format.setFontWeight(settings.value(QStringLiteral("%1.weight").arg(name), defaultValue.fontWeight()).toInt());
format.setFontItalic(settings.value(QStringLiteral("%1.italic").arg(name), defaultValue.fontItalic()).toBool());
format.setFontUnderline(settings.value(QStringLiteral("%1.underline").arg(name), defaultValue.fontUnderline()).toBool());
return format;
}
/**
* @brief Reads the theme from the settings.
*
* @param settings The settings object to read from.
* @param baseTheme The base theme to use as a fallback.
* @return The theme read from the settings or the base theme if not found.
*/
Theme readTheme(QSettings &settings, const Theme &baseTheme = Theme()) {
Theme theme;
settings.beginGroup("Theme");
// General application palette
theme.palette[Theme::Window] = readColor(settings, "palette.window", baseTheme.palette[Theme::Window]);
theme.palette[Theme::WindowDisabled] = readColor(settings, "palette.window.disabled", baseTheme.palette[Theme::WindowDisabled]);
theme.palette[Theme::WindowText] = readColor(settings, "palette.windowtext", baseTheme.palette[Theme::WindowText]);
theme.palette[Theme::WindowTextDisabled] = readColor(settings, "palette.windowtext.disabled", baseTheme.palette[Theme::WindowTextDisabled]);
theme.palette[Theme::Base] = readColor(settings, "palette.base", baseTheme.palette[Theme::Base]);
theme.palette[Theme::BaseDisabled] = readColor(settings, "palette.base.disabled", baseTheme.palette[Theme::BaseDisabled]);
theme.palette[Theme::AlternateBase] = readColor(settings, "palette.alternatebase", baseTheme.palette[Theme::AlternateBase]);
theme.palette[Theme::AlternateBaseDisabled] = readColor(settings, "palette.alternatebase.disabled", baseTheme.palette[Theme::AlternateBaseDisabled]);
theme.palette[Theme::ToolTipBase] = readColor(settings, "palette.tooltipbase", baseTheme.palette[Theme::ToolTipBase]);
theme.palette[Theme::ToolTipBaseDisabled] = readColor(settings, "palette.tooltipbase.disabled", baseTheme.palette[Theme::ToolTipBaseDisabled]);
theme.palette[Theme::ToolTipText] = readColor(settings, "palette.tooltiptext", baseTheme.palette[Theme::ToolTipText]);
theme.palette[Theme::ToolTipTextDisabled] = readColor(settings, "palette.tooltiptext.disabled", baseTheme.palette[Theme::ToolTipTextDisabled]);
theme.palette[Theme::Text] = readColor(settings, "palette.text", baseTheme.palette[Theme::Text]);
theme.palette[Theme::TextDisabled] = readColor(settings, "palette.text.disabled", baseTheme.palette[Theme::TextDisabled]);
theme.palette[Theme::Button] = readColor(settings, "palette.button", baseTheme.palette[Theme::Button]);
theme.palette[Theme::ButtonDisabled] = readColor(settings, "palette.button.disabled", baseTheme.palette[Theme::ButtonDisabled]);
theme.palette[Theme::ButtonText] = readColor(settings, "palette.buttontext", baseTheme.palette[Theme::ButtonText]);
theme.palette[Theme::ButtonTextDisabled] = readColor(settings, "palette.buttontext.disabled", baseTheme.palette[Theme::ButtonTextDisabled]);
theme.palette[Theme::BrightText] = readColor(settings, "palette.brighttext", baseTheme.palette[Theme::BrightText]);
theme.palette[Theme::BrightTextDisabled] = readColor(settings, "palette.brighttext.disabled", baseTheme.palette[Theme::BrightTextDisabled]);
theme.palette[Theme::Highlight] = readColor(settings, "palette.highlight", baseTheme.palette[Theme::Highlight]);
theme.palette[Theme::HighlightDisabled] = readColor(settings, "palette.highlight.disabled", baseTheme.palette[Theme::HighlightDisabled]);
theme.palette[Theme::HighlightedText] = readColor(settings, "palette.highlightedtext", baseTheme.palette[Theme::HighlightedText]);
theme.palette[Theme::HighlightedTextDisabled] = readColor(settings, "palette.highlightedtext.disabled", baseTheme.palette[Theme::HighlightedTextDisabled]);
theme.palette[Theme::Link] = readColor(settings, "palette.link", baseTheme.palette[Theme::Link]);
theme.palette[Theme::LinkDisabled] = readColor(settings, "palette.link.disabled", baseTheme.palette[Theme::LinkDisabled]);
theme.palette[Theme::LinkVisited] = readColor(settings, "palette.linkvisited", baseTheme.palette[Theme::LinkVisited]);
theme.palette[Theme::LinkVisitedDisabled] = readColor(settings, "palette.linkvisited.disabled", baseTheme.palette[Theme::LinkVisitedDisabled]);
theme.palette[Theme::Light] = readColor(settings, "palette.light", baseTheme.palette[Theme::Light]);
theme.palette[Theme::LightDisabled] = readColor(settings, "palette.light.disabled", baseTheme.palette[Theme::LightDisabled]);
theme.palette[Theme::Midlight] = readColor(settings, "palette.midlight", baseTheme.palette[Theme::Midlight]);
theme.palette[Theme::MidlightDisabled] = readColor(settings, "palette.midlight.disabled", baseTheme.palette[Theme::MidlightDisabled]);
theme.palette[Theme::Dark] = readColor(settings, "palette.dark", baseTheme.palette[Theme::Dark]);
theme.palette[Theme::DarkDisabled] = readColor(settings, "palette.dark.disabled", baseTheme.palette[Theme::DarkDisabled]);
theme.palette[Theme::Mid] = readColor(settings, "palette.mid", baseTheme.palette[Theme::Mid]);
theme.palette[Theme::MidDisabled] = readColor(settings, "palette.mid.disabled", baseTheme.palette[Theme::MidDisabled]);
theme.palette[Theme::Shadow] = readColor(settings, "palette.shadow", baseTheme.palette[Theme::Shadow]);
theme.palette[Theme::ShadowDisabled] = readColor(settings, "palette.shadow.disabled", baseTheme.palette[Theme::ShadowDisabled]);
// various text/syntax settings
theme.text[Theme::Address] = readFormat(settings, "address", baseTheme.text[Theme::Address]);
theme.text[Theme::AlternatingByte] = readFormat(settings, "alternating_byte", baseTheme.text[Theme::AlternatingByte]);
theme.text[Theme::Arithmetic] = readFormat(settings, "arithmetic", baseTheme.text[Theme::Arithmetic]);
theme.text[Theme::Brackets] = readFormat(settings, "brackets", baseTheme.text[Theme::Brackets]);
theme.text[Theme::Comma] = readFormat(settings, "comma", baseTheme.text[Theme::Comma]);
theme.text[Theme::Comparison] = readFormat(settings, "comparison", baseTheme.text[Theme::Comparison]);
theme.text[Theme::Constant] = readFormat(settings, "constant", baseTheme.text[Theme::Constant]);
theme.text[Theme::DataXfer] = readFormat(settings, "data_xfer", baseTheme.text[Theme::DataXfer]);
theme.text[Theme::Data] = readFormat(settings, "data", baseTheme.text[Theme::Data]);
theme.text[Theme::Filling] = readFormat(settings, "filling", baseTheme.text[Theme::Filling]);
theme.text[Theme::FlowCtrl] = readFormat(settings, "flow_ctrl", baseTheme.text[Theme::FlowCtrl]);
theme.text[Theme::Function] = readFormat(settings, "function", baseTheme.text[Theme::Function]);
theme.text[Theme::Logic] = readFormat(settings, "logic", baseTheme.text[Theme::Logic]);
theme.text[Theme::NonPrintingCharacter] = readFormat(settings, "non_printing_character", baseTheme.text[Theme::NonPrintingCharacter]);
theme.text[Theme::Operator] = readFormat(settings, "operator", baseTheme.text[Theme::Operator]);
theme.text[Theme::Prefix] = readFormat(settings, "prefix", baseTheme.text[Theme::Prefix]);
theme.text[Theme::Ptr] = readFormat(settings, "ptr", baseTheme.text[Theme::Ptr]);
theme.text[Theme::Register] = readFormat(settings, "register", baseTheme.text[Theme::Register]);
theme.text[Theme::Shift] = readFormat(settings, "shift", baseTheme.text[Theme::Shift]);
theme.text[Theme::Stack] = readFormat(settings, "stack", baseTheme.text[Theme::Stack]);
theme.text[Theme::System] = readFormat(settings, "system", baseTheme.text[Theme::System]);
theme.text[Theme::TakenJump] = readFormat(settings, "taken_jump", baseTheme.text[Theme::TakenJump]);
// misc settings
theme.misc[Theme::Badge] = readFormat(settings, "badge", baseTheme.misc[Theme::Badge]);
settings.endGroup();
return theme;
}
/**
* @brief Reads the system theme based on the current application palette.
*
* @return The system theme.
*/
Theme readSystemTheme() {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
if (QApplication::palette().window().color().lightnessF() >= 0.5f) {
QSettings settings(":/themes/system-light.ini", QSettings::IniFormat);
return readTheme(settings);
}
#else
if (QApplication::palette().window().color().lightnessF() >= 0.5) {
QSettings settings(":/themes/system-light.ini", QSettings::IniFormat);
return readTheme(settings);
}
#endif
QSettings settings(":/themes/system-dark.ini", QSettings::IniFormat);
return readTheme(settings);
}
/**
* @brief Reads the theme from the settings.
*
* @return The theme read from the settings.
*/
Theme readTheme() {
// Ensure that the directory exists, this will help users know where to put the theme files
QDir().mkpath(themeDirectory());
Theme system = readSystemTheme();
QString theme_name = edb::v1::config().theme_name;
// Handle the built-in themes
if (theme_name == "System") {
return system;
}
if (theme_name == "Dark [Built-in]") {
QSettings settings(":/themes/dark.ini", QSettings::IniFormat);
return readTheme(settings, system);
}
if (theme_name == "Light [Built-in]") {
QSettings settings(":/themes/light.ini", QSettings::IniFormat);
return readTheme(settings, system);
}
QString themeFile = themeDirectory() + QDir::separator() + theme_name;
QSettings settings(themeFile, QSettings::IniFormat);
return readTheme(settings, system);
}
}
/**
* @brief Loads the theme from the settings.
*
* @return The loaded theme.
*/
Theme Theme::load() {
// we do this function indirection to make it so the theme is
// read once even if this function is called several times
static Theme theme = readTheme();
return theme;
}
/**
* @brief Returns a list of all user-defined themes.
*
* @return A list of all user-defined themes.
*/
QStringList Theme::userThemes() {
QDir directory(themeDirectory());
return directory.entryList(QStringList() << "*.ini", QDir::Files);
}
/**
* @brief Returns the name of a theme file.
*
* @param theme_file The theme file to get the name of.
* @return The name of the theme.
*/
QString Theme::themeName(const QString &theme_file) {
QString themeFile = themeDirectory() + QDir::separator() + theme_file;
QSettings settings(themeFile, QSettings::IniFormat);
settings.beginGroup("Meta");
QString name = settings.value("name", theme_file).toString();
settings.endGroup();
return name;
}