forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappearancewidget.cpp
162 lines (136 loc) · 5.92 KB
/
appearancewidget.cpp
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
// Copyright (c) 2020-2023 The Dash Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#if defined(HAVE_CONFIG_H)
#include <config/bitcoin-config.h>
#endif
#include <qt/forms/ui_appearancewidget.h>
#include <qt/appearancewidget.h>
#include <qt/optionsmodel.h>
#include <util/system.h>
#include <QComboBox>
#include <QDataWidgetMapper>
#include <QSettings>
#include <QSlider>
AppearanceWidget::AppearanceWidget(QWidget* parent) :
QWidget(parent),
ui{new Ui::AppearanceWidget()}
{
ui->setupUi(this);
for (const QString& entry : GUIUtil::listThemes()) {
ui->theme->addItem(entry, QVariant(entry));
}
GUIUtil::FontFamily fontSystem = GUIUtil::FontFamily::SystemDefault;
GUIUtil::FontFamily fontMontserrat = GUIUtil::FontFamily::Montserrat;
ui->fontFamily->addItem(GUIUtil::fontFamilyToString(fontSystem), QVariant(static_cast<int>(fontSystem)));
ui->fontFamily->addItem(GUIUtil::fontFamilyToString(fontMontserrat), QVariant(static_cast<int>(fontMontserrat)));
updateWeightSlider();
mapper = new QDataWidgetMapper(this);
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
mapper->setOrientation(Qt::Vertical);
connect(ui->theme, &QComboBox::currentTextChanged, this, &AppearanceWidget::updateTheme);
connect(ui->fontFamily, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &AppearanceWidget::updateFontFamily);
connect(ui->fontScaleSlider, &QSlider::valueChanged, this, &AppearanceWidget::updateFontScale);
connect(ui->fontWeightNormalSlider, &QSlider::valueChanged, [this](auto nValue) { updateFontWeightNormal(nValue); });
connect(ui->fontWeightBoldSlider, &QSlider::valueChanged, [this](auto nValue) { updateFontWeightBold(nValue); });
connect(ui->theme, &QComboBox::currentTextChanged, [=]() { Q_EMIT appearanceChanged(); });
connect(ui->fontFamily, &QComboBox::currentTextChanged, [=]() { Q_EMIT appearanceChanged(); });
connect(ui->fontScaleSlider, &QSlider::sliderReleased, [=]() { Q_EMIT appearanceChanged(); });
connect(ui->fontWeightNormalSlider, &QSlider::sliderReleased, [=]() { Q_EMIT appearanceChanged(); });
connect(ui->fontWeightBoldSlider, &QSlider::sliderReleased, [=]() { Q_EMIT appearanceChanged(); });
}
AppearanceWidget::~AppearanceWidget()
{
if (fAcceptChanges) {
mapper->submit();
} else {
if (prevTheme != GUIUtil::getActiveTheme()) {
updateTheme(prevTheme);
}
if (prevFontFamily != GUIUtil::getFontFamily()) {
GUIUtil::setFontFamily(prevFontFamily);
}
if (prevScale != GUIUtil::getFontScale()) {
GUIUtil::setFontScale(prevScale);
}
if (prevWeightNormal != GUIUtil::getFontWeightNormal()) {
GUIUtil::setFontWeightNormal(prevWeightNormal);
}
if (prevWeightBold != GUIUtil::getFontWeightBold()) {
GUIUtil::setFontWeightBold(prevWeightBold);
}
}
delete ui;
}
void AppearanceWidget::setModel(OptionsModel* _model)
{
this->model = _model;
if (_model) {
mapper->setModel(_model);
mapper->addMapping(ui->theme, OptionsModel::Theme);
mapper->addMapping(ui->fontFamily, OptionsModel::FontFamily);
mapper->addMapping(ui->fontScaleSlider, OptionsModel::FontScale);
mapper->addMapping(ui->fontWeightNormalSlider, OptionsModel::FontWeightNormal);
mapper->addMapping(ui->fontWeightBoldSlider, OptionsModel::FontWeightBold);
mapper->toFirst();
}
}
void AppearanceWidget::accept()
{
fAcceptChanges = true;
}
void AppearanceWidget::updateTheme(const QString& theme)
{
QString newValue = theme.isEmpty() ? ui->theme->currentData().toString() : theme;
if (GUIUtil::getActiveTheme() != newValue) {
QSettings().setValue("theme", newValue);
// Force loading the theme
if (model) {
GUIUtil::loadTheme(true);
}
}
}
void AppearanceWidget::updateFontFamily(int index)
{
GUIUtil::setFontFamily(static_cast<GUIUtil::FontFamily>(ui->fontFamily->itemData(index).toInt()));
updateWeightSlider(true);
}
void AppearanceWidget::updateFontScale(int nScale)
{
GUIUtil::setFontScale(nScale);
}
void AppearanceWidget::updateFontWeightNormal(int nValue, bool fForce)
{
int nSliderValue = nValue;
if (nValue > ui->fontWeightBoldSlider->value() && !fForce) {
nSliderValue = ui->fontWeightBoldSlider->value();
}
const QSignalBlocker blocker(ui->fontWeightNormalSlider);
ui->fontWeightNormalSlider->setValue(nSliderValue);
GUIUtil::setFontWeightNormal(GUIUtil::supportedWeightFromIndex(ui->fontWeightNormalSlider->value()));
}
void AppearanceWidget::updateFontWeightBold(int nValue, bool fForce)
{
int nSliderValue = nValue;
if (nValue < ui->fontWeightNormalSlider->value() && !fForce) {
nSliderValue = ui->fontWeightNormalSlider->value();
}
const QSignalBlocker blocker(ui->fontWeightBoldSlider);
ui->fontWeightBoldSlider->setValue(nSliderValue);
GUIUtil::setFontWeightBold(GUIUtil::supportedWeightFromIndex(ui->fontWeightBoldSlider->value()));
}
void AppearanceWidget::updateWeightSlider(const bool fForce)
{
int nMaximum = GUIUtil::getSupportedWeights().size() - 1;
ui->fontWeightNormalSlider->setMinimum(0);
ui->fontWeightNormalSlider->setMaximum(nMaximum);
ui->fontWeightBoldSlider->setMinimum(0);
ui->fontWeightBoldSlider->setMaximum(nMaximum);
if (fForce || !GUIUtil::isSupportedWeight(prevWeightNormal) || !GUIUtil::isSupportedWeight(prevWeightBold)) {
int nIndexNormal = GUIUtil::supportedWeightToIndex(GUIUtil::getSupportedFontWeightNormalDefault());
int nIndexBold = GUIUtil::supportedWeightToIndex(GUIUtil::getSupportedFontWeightBoldDefault());
assert(nIndexNormal != -1 && nIndexBold != -1);
updateFontWeightNormal(nIndexNormal, true);
updateFontWeightBold(nIndexBold, true);
}
}