Skip to content

Commit e5a8d05

Browse files
committed
refactor: move monospace font selector to appearance widget
Dash already has an appearance widget for managing font parameters, the selector shouldn't be in "Display". Review with `git log -p -n1 --color-moved=dimmed_zebra`.
1 parent 41c69e9 commit e5a8d05

File tree

4 files changed

+90
-88
lines changed

4 files changed

+90
-88
lines changed

src/qt/appearancewidget.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,65 @@
1515

1616
#include <QComboBox>
1717
#include <QDataWidgetMapper>
18+
#include <QFontDialog>
19+
#include <QFontInfo>
1820
#include <QSettings>
1921
#include <QSlider>
2022

23+
int setFontChoice(QComboBox* cb, const OptionsModel::FontChoice& fc)
24+
{
25+
int i;
26+
for (i = cb->count(); --i >= 0; ) {
27+
QVariant item_data = cb->itemData(i);
28+
if (!item_data.canConvert<OptionsModel::FontChoice>()) continue;
29+
if (item_data.value<OptionsModel::FontChoice>() == fc) {
30+
break;
31+
}
32+
}
33+
if (i == -1) {
34+
// New item needed
35+
QFont chosen_font = OptionsModel::getFontForChoice(fc);
36+
QSignalBlocker block_currentindexchanged_signal(cb); // avoid triggering QFontDialog
37+
cb->insertItem(0, QFontInfo(chosen_font).family(), QVariant::fromValue(fc));
38+
i = 0;
39+
}
40+
41+
cb->setCurrentIndex(i);
42+
return i;
43+
}
44+
45+
void setupFontOptions(QComboBox* cb, QLabel* preview)
46+
{
47+
QFont embedded_font{GUIUtil::fixedPitchFont(true)};
48+
QFont system_font{GUIUtil::fixedPitchFont(false)};
49+
cb->addItem(QObject::tr("Embedded \"%1\"").arg(QFontInfo(embedded_font).family()), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::EmbeddedFont}));
50+
cb->addItem(QObject::tr("Default system font \"%1\"").arg(QFontInfo(system_font).family()), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::BestSystemFont}));
51+
cb->addItem(QObject::tr("Custom…"));
52+
53+
const auto& on_font_choice_changed = [cb, preview](int index) {
54+
static int previous_index = -1;
55+
QVariant item_data = cb->itemData(index);
56+
QFont f;
57+
if (item_data.canConvert<OptionsModel::FontChoice>()) {
58+
f = OptionsModel::getFontForChoice(item_data.value<OptionsModel::FontChoice>());
59+
} else {
60+
bool ok;
61+
f = QFontDialog::getFont(&ok, GUIUtil::fixedPitchFont(false), cb->parentWidget());
62+
if (!ok) {
63+
cb->setCurrentIndex(previous_index);
64+
return;
65+
}
66+
index = setFontChoice(cb, OptionsModel::FontChoice{f});
67+
}
68+
if (preview) {
69+
preview->setFont(f);
70+
}
71+
previous_index = index;
72+
};
73+
QObject::connect(cb, QOverload<int>::of(&QComboBox::currentIndexChanged), on_font_choice_changed);
74+
on_font_choice_changed(cb->currentIndex());
75+
}
76+
2177
AppearanceWidget::AppearanceWidget(QWidget* parent) :
2278
QWidget(parent),
2379
ui{new Ui::AppearanceWidget()},
@@ -55,6 +111,9 @@ AppearanceWidget::AppearanceWidget(QWidget* parent) :
55111
connect(ui->fontScaleSlider, &QSlider::sliderReleased, [this]() { Q_EMIT appearanceChanged(); });
56112
connect(ui->fontWeightNormalSlider, &QSlider::sliderReleased, [this]() { Q_EMIT appearanceChanged(); });
57113
connect(ui->fontWeightBoldSlider, &QSlider::sliderReleased, [this]() { Q_EMIT appearanceChanged(); });
114+
115+
setupFontOptions(ui->moneyFont, ui->moneyFont_preview);
116+
connect(ui->moneyFont, &QComboBox::currentTextChanged, [this]() { Q_EMIT appearanceChanged(); });
58117
}
59118

60119
AppearanceWidget::~AppearanceWidget()
@@ -104,6 +163,9 @@ void AppearanceWidget::setModel(OptionsModel* _model)
104163

105164
mapper->toFirst();
106165

166+
const auto& font_for_money = _model->data(_model->index(OptionsModel::FontForMoney, 0), Qt::EditRole).value<OptionsModel::FontChoice>();
167+
setFontChoice(ui->moneyFont, font_for_money);
168+
107169
const bool override_family{_model->isOptionOverridden("-font-family")};
108170
if (override_family) {
109171
ui->fontFamily->setEnabled(false);
@@ -139,6 +201,9 @@ void AppearanceWidget::setModel(OptionsModel* _model)
139201
void AppearanceWidget::accept()
140202
{
141203
fAcceptChanges = true;
204+
if (model) {
205+
model->setData(model->index(OptionsModel::FontForMoney, 0), ui->moneyFont->itemData(ui->moneyFont->currentIndex()));
206+
}
142207
}
143208

144209
void AppearanceWidget::updateTheme(const QString& theme)

src/qt/forms/appearancewidget.ui

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,31 @@
479479
</item>
480480
</layout>
481481
</item>
482+
<item>
483+
<layout class="QHBoxLayout" name="hLayoutMoneyFont">
484+
<item>
485+
<widget class="QLabel" name="moneyFontLabel">
486+
<property name="text">
487+
<string>Font in the Overview tab: </string>
488+
</property>
489+
<property name="buddy">
490+
<cstring>moneyFont</cstring>
491+
</property>
492+
</widget>
493+
</item>
494+
<item>
495+
<widget class="QComboBox" name="moneyFont"/>
496+
</item>
497+
<item>
498+
<widget class="QLabel" name="moneyFont_preview">
499+
<property name="text">
500+
<string notr="true">111.11111111 DASH
501+
909.09090909 DASH</string>
502+
</property>
503+
</widget>
504+
</item>
505+
</layout>
506+
</item>
482507
</layout>
483508
</widget>
484509
<resources/>

src/qt/forms/optionsdialog.ui

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,31 +1095,6 @@ https://explore.transifex.com/dash/dash/</string>
10951095
</item>
10961096
</layout>
10971097
</item>
1098-
<item>
1099-
<layout class="QHBoxLayout" name="horizontalLayout_4_Display">
1100-
<item>
1101-
<widget class="QLabel" name="moneyFontLabel">
1102-
<property name="text">
1103-
<string>Font in the Overview tab: </string>
1104-
</property>
1105-
<property name="buddy">
1106-
<cstring>moneyFont</cstring>
1107-
</property>
1108-
</widget>
1109-
</item>
1110-
<item>
1111-
<widget class="QComboBox" name="moneyFont"/>
1112-
</item>
1113-
<item>
1114-
<widget class="QLabel" name="moneyFont_preview">
1115-
<property name="text">
1116-
<string notr="true">111.11111111 DASH
1117-
909.09090909 DASH</string>
1118-
</property>
1119-
</widget>
1120-
</item>
1121-
</layout>
1122-
</item>
11231098
<item>
11241099
<spacer name="verticalSpacer">
11251100
<property name="orientation">

src/qt/optionsdialog.cpp

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@
2929

3030
#include <chrono>
3131

32-
#include <QApplication>
3332
#include <QButtonGroup>
3433
#include <QDataWidgetMapper>
3534
#include <QDir>
36-
#include <QFontDialog>
3735
#include <QIntValidator>
3836
#include <QLocale>
3937
#include <QMessageBox>
@@ -42,60 +40,6 @@
4240
#include <QSystemTrayIcon>
4341
#include <QTimer>
4442

45-
int setFontChoice(QComboBox* cb, const OptionsModel::FontChoice& fc)
46-
{
47-
int i;
48-
for (i = cb->count(); --i >= 0; ) {
49-
QVariant item_data = cb->itemData(i);
50-
if (!item_data.canConvert<OptionsModel::FontChoice>()) continue;
51-
if (item_data.value<OptionsModel::FontChoice>() == fc) {
52-
break;
53-
}
54-
}
55-
if (i == -1) {
56-
// New item needed
57-
QFont chosen_font = OptionsModel::getFontForChoice(fc);
58-
QSignalBlocker block_currentindexchanged_signal(cb); // avoid triggering QFontDialog
59-
cb->insertItem(0, QFontInfo(chosen_font).family(), QVariant::fromValue(fc));
60-
i = 0;
61-
}
62-
63-
cb->setCurrentIndex(i);
64-
return i;
65-
}
66-
67-
void setupFontOptions(QComboBox* cb, QLabel* preview)
68-
{
69-
QFont embedded_font{GUIUtil::fixedPitchFont(true)};
70-
QFont system_font{GUIUtil::fixedPitchFont(false)};
71-
cb->addItem(QObject::tr("Embedded \"%1\"").arg(QFontInfo(embedded_font).family()), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::EmbeddedFont}));
72-
cb->addItem(QObject::tr("Default system font \"%1\"").arg(QFontInfo(system_font).family()), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::BestSystemFont}));
73-
cb->addItem(QObject::tr("Custom…"));
74-
75-
const auto& on_font_choice_changed = [cb, preview](int index) {
76-
static int previous_index = -1;
77-
QVariant item_data = cb->itemData(index);
78-
QFont f;
79-
if (item_data.canConvert<OptionsModel::FontChoice>()) {
80-
f = OptionsModel::getFontForChoice(item_data.value<OptionsModel::FontChoice>());
81-
} else {
82-
bool ok;
83-
f = QFontDialog::getFont(&ok, GUIUtil::fixedPitchFont(false), cb->parentWidget());
84-
if (!ok) {
85-
cb->setCurrentIndex(previous_index);
86-
return;
87-
}
88-
index = setFontChoice(cb, OptionsModel::FontChoice{f});
89-
}
90-
if (preview) {
91-
preview->setFont(f);
92-
}
93-
previous_index = index;
94-
};
95-
QObject::connect(cb, QOverload<int>::of(&QComboBox::currentIndexChanged), on_font_choice_changed);
96-
on_font_choice_changed(cb->currentIndex());
97-
}
98-
9943
OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
10044
QDialog(parent, GUIUtil::dialog_flags),
10145
ui(new Ui::OptionsDialog),
@@ -273,8 +217,6 @@ OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
273217
ui->minimizeToTray->setEnabled(false);
274218
}
275219

276-
setupFontOptions(ui->moneyFont, ui->moneyFont_preview);
277-
278220
GUIUtil::handleCloseWindowShortcut(this);
279221
}
280222

@@ -326,9 +268,6 @@ void OptionsDialog::setModel(OptionsModel *_model)
326268

327269
appearance->setModel(_model);
328270

329-
const auto& font_for_money = _model->data(_model->index(OptionsModel::FontForMoney, 0), Qt::EditRole).value<OptionsModel::FontChoice>();
330-
setFontChoice(ui->moneyFont, font_for_money);
331-
332271
updateDefaultProxyNets();
333272
}
334273

@@ -502,8 +441,6 @@ void OptionsDialog::on_resetButton_clicked()
502441

503442
void OptionsDialog::on_okButton_clicked()
504443
{
505-
model->setData(model->index(OptionsModel::FontForMoney, 0), ui->moneyFont->itemData(ui->moneyFont->currentIndex()));
506-
507444
mapper->submit();
508445
appearance->accept();
509446
#ifdef ENABLE_WALLET

0 commit comments

Comments
 (0)