Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

appearancewidget.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 The Dash Core developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #if defined(HAVE_CONFIG_H)
6 #include <config/dash-config.h>
7 #endif
8 
9 #include <qt/forms/ui_appearancewidget.h>
10 
11 #include <qt/appearancewidget.h>
12 #include <qt/optionsmodel.h>
13 
14 #include <util.h>
15 
16 #include <QComboBox>
17 #include <QDataWidgetMapper>
18 #include <QSettings>
19 #include <QSlider>
20 
22  QWidget(parent),
23  ui(new Ui::AppearanceWidget),
24  fAcceptChanges(false),
25  prevTheme(GUIUtil::getActiveTheme()),
26  prevFontFamily(GUIUtil::getFontFamily()),
27  prevScale(GUIUtil::getFontScale()),
28  prevWeightNormal(GUIUtil::getFontWeightNormal()),
29  prevWeightBold(GUIUtil::getFontWeightBold())
30 {
31  ui->setupUi(this);
32 
33  for (const QString& entry : GUIUtil::listThemes()) {
34  ui->theme->addItem(entry, QVariant(entry));
35  }
36 
39 
40  ui->fontFamily->addItem(GUIUtil::fontFamilyToString(fontSystem), QVariant(static_cast<int>(fontSystem)));
41  ui->fontFamily->addItem(GUIUtil::fontFamilyToString(fontMontserrat), QVariant(static_cast<int>(fontMontserrat)));
42 
44 
45  mapper = new QDataWidgetMapper(this);
46  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
47  mapper->setOrientation(Qt::Vertical);
48 
49  connect(ui->theme, SIGNAL(currentTextChanged(const QString&)), this, SLOT(updateTheme(const QString&)));
50  connect(ui->fontFamily, SIGNAL(activated(int)), this, SLOT(updateFontFamily(int)));
51  connect(ui->fontScaleSlider, SIGNAL(valueChanged(int)), this, SLOT(updateFontScale(int)));
52  connect(ui->fontWeightNormalSlider, SIGNAL(valueChanged(int)), this, SLOT(updateFontWeightNormal(int)));
53  connect(ui->fontWeightBoldSlider, SIGNAL(valueChanged(int)), this, SLOT(updateFontWeightBold(int)));
54 
55  connect(ui->theme, &QComboBox::currentTextChanged, [=]() { Q_EMIT appearanceChanged(); });
56  connect(ui->fontFamily, &QComboBox::currentTextChanged, [=]() { Q_EMIT appearanceChanged(); });
57  connect(ui->fontScaleSlider, &QSlider::sliderReleased, [=]() { Q_EMIT appearanceChanged(); });
58  connect(ui->fontWeightNormalSlider, &QSlider::sliderReleased, [=]() { Q_EMIT appearanceChanged(); });
59  connect(ui->fontWeightBoldSlider, &QSlider::sliderReleased, [=]() { Q_EMIT appearanceChanged(); });
60 }
61 
63 {
64  if (fAcceptChanges) {
65  mapper->submit();
66  } else {
69  }
72  }
75  }
78  }
81  }
82  }
83  delete ui;
84 }
85 
87 {
88  this->model = _model;
89 
90  if (_model) {
91  mapper->setModel(_model);
92  mapper->addMapping(ui->theme, OptionsModel::Theme);
93  mapper->addMapping(ui->fontFamily, OptionsModel::FontFamily);
94  mapper->addMapping(ui->fontScaleSlider, OptionsModel::FontScale);
95  mapper->addMapping(ui->fontWeightNormalSlider, OptionsModel::FontWeightNormal);
96  mapper->addMapping(ui->fontWeightBoldSlider, OptionsModel::FontWeightBold);
97  mapper->toFirst();
98  }
99 }
100 
102 {
103  fAcceptChanges = true;
104 }
105 
106 void AppearanceWidget::updateTheme(const QString& theme)
107 {
108  QString newValue = theme.isEmpty() ? ui->theme->currentData().toString() : theme;
109  if (GUIUtil::getActiveTheme() != newValue) {
110  QSettings().setValue("theme", newValue);
111  // Force loading the theme
112  GUIUtil::loadTheme(nullptr, true);
113  }
114 }
115 
117 {
118  GUIUtil::setFontFamily(static_cast<GUIUtil::FontFamily>(ui->fontFamily->itemData(index).toInt()));
120 }
121 
123 {
124  GUIUtil::setFontScale(nScale);
125 }
126 
127 void AppearanceWidget::updateFontWeightNormal(int nValue, bool fForce)
128 {
129  int nSliderValue = nValue;
130  if (nValue > ui->fontWeightBoldSlider->value() && !fForce) {
131  nSliderValue = ui->fontWeightBoldSlider->value();
132  }
133  const QSignalBlocker blocker(ui->fontWeightNormalSlider);
134  ui->fontWeightNormalSlider->setValue(nSliderValue);
135  GUIUtil::setFontWeightNormal(GUIUtil::supportedWeightFromIndex(ui->fontWeightNormalSlider->value()));
136 }
137 
138 void AppearanceWidget::updateFontWeightBold(int nValue, bool fForce)
139 {
140  int nSliderValue = nValue;
141  if (nValue < ui->fontWeightNormalSlider->value() && !fForce) {
142  nSliderValue = ui->fontWeightNormalSlider->value();
143  }
144  const QSignalBlocker blocker(ui->fontWeightBoldSlider);
145  ui->fontWeightBoldSlider->setValue(nSliderValue);
146  GUIUtil::setFontWeightBold(GUIUtil::supportedWeightFromIndex(ui->fontWeightBoldSlider->value()));
147 }
148 
150 {
151  int nMaximum = GUIUtil::getSupportedWeights().size() - 1;
152 
153  ui->fontWeightNormalSlider->setMinimum(0);
154  ui->fontWeightNormalSlider->setMaximum(nMaximum);
155 
156  ui->fontWeightBoldSlider->setMinimum(0);
157  ui->fontWeightBoldSlider->setMaximum(nMaximum);
158 
159  if (nMaximum < 4) {
160  updateFontWeightNormal(0, true);
161  updateFontWeightBold(nMaximum, true);
162  } else {
163  updateFontWeightNormal(1, true);
164  updateFontWeightBold(4, true);
165  }
166 }
FontFamily
Definition: guiutil.h:277
const std::vector< QString > listThemes()
Return a list of all theme css files.
Definition: guiutil.cpp:1139
QFont::Weight getFontWeightNormal()
Definition: guiutil.cpp:1361
void setFontWeightBold(QFont::Weight weight)
Definition: guiutil.cpp:1382
Utility functions used by the Dash Qt UI.
Definition: guiutil.cpp:95
OptionsModel * model
AppearanceWidget(QWidget *parent=0)
QFont::Weight supportedWeightFromIndex(int nIndex)
Convert an index to a weight in the supported weights vector.
Definition: guiutil.cpp:1751
GUIUtil::FontFamily prevFontFamily
AddressTableEntry * index(int idx)
void setFontWeightNormal(QFont::Weight weight)
Definition: guiutil.cpp:1366
void loadTheme(QWidget *widget, bool fForce)
Load the theme and update all UI elements according to the appearance settings.
Definition: guiutil.cpp:1782
FontFamily getFontFamily()
Definition: guiutil.cpp:1308
QFont::Weight getFontWeightBold()
Definition: guiutil.cpp:1377
false
Definition: bls_dkg.cpp:168
QFont::Weight prevWeightBold
void updateFontFamily(int index)
QString fontFamilyToString(FontFamily family)
Definition: guiutil.cpp:1284
void updateTheme(const QString &toTheme=QString())
QFont::Weight prevWeightNormal
void updateFontWeightBold(int nValue, bool fForce=false)
AddressTableModel * parent
std::vector< QFont::Weight > getSupportedWeights()
Return supported weights for the current font family.
Definition: guiutil.cpp:1745
Ui::AppearanceWidget * ui
QString getActiveTheme()
Return the name of the currently active theme.
Definition: guiutil.cpp:1769
QDataWidgetMapper * mapper
void setModel(OptionsModel *model)
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:25
void setFontScale(int nScale)
Definition: guiutil.cpp:1398
int getFontScale()
Definition: guiutil.cpp:1393
void updateFontWeightNormal(int nValue, bool fForce=false)
void setFontFamily(FontFamily family)
Definition: guiutil.cpp:1296
void updateFontScale(int nScale)
Released under the MIT license