Skip to content

Commit a17fd33

Browse files
committed
GUI: OptionsDialog: Replace verbose two-option font selector with simple combobox with Custom... choice
1 parent 98e9ac5 commit a17fd33

File tree

4 files changed

+92
-127
lines changed

4 files changed

+92
-127
lines changed

src/qt/forms/optionsdialog.ui

Lines changed: 23 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -772,104 +772,29 @@
772772
</layout>
773773
</item>
774774
<item>
775-
<widget class="QGroupBox" name="font_groupBox">
776-
<property name="title">
777-
<string>Monospaced font in the Overview tab:</string>
778-
</property>
779-
<layout class="QVBoxLayout" name="font_verticalLayout">
780-
<item>
781-
<layout class="QHBoxLayout" name="embeddedFont_horizontalLayout">
782-
<item>
783-
<widget class="QRadioButton" name="embeddedFont_radioButton">
784-
<property name="text">
785-
<string>embedded &quot;%1&quot;</string>
786-
</property>
787-
</widget>
788-
</item>
789-
<item>
790-
<spacer name="embeddedFont_horizontalSpacer">
791-
<property name="orientation">
792-
<enum>Qt::Horizontal</enum>
793-
</property>
794-
<property name="sizeHint" stdset="0">
795-
<size>
796-
<width>40</width>
797-
<height>20</height>
798-
</size>
799-
</property>
800-
</spacer>
801-
</item>
802-
<item>
803-
<layout class="QVBoxLayout" name="embeddedFont_verticalLayout">
804-
<item>
805-
<widget class="QLabel" name="embeddedFont_label_1">
806-
<property name="text">
807-
<string notr="true">111.11111111 BTC</string>
808-
</property>
809-
</widget>
810-
</item>
811-
<item>
812-
<widget class="QLabel" name="embeddedFont_label_9">
813-
<property name="text">
814-
<string notr="true">909.09090909 BTC</string>
815-
</property>
816-
</widget>
817-
</item>
818-
</layout>
819-
</item>
820-
</layout>
821-
</item>
822-
<item>
823-
<widget class="Line" name="font_line">
824-
<property name="orientation">
825-
<enum>Qt::Horizontal</enum>
826-
</property>
827-
</widget>
828-
</item>
829-
<item>
830-
<layout class="QHBoxLayout" name="systemFont_horizontalLayout">
831-
<item>
832-
<widget class="QRadioButton" name="systemFont_radioButton">
833-
<property name="text">
834-
<string>closest matching &quot;%1&quot;</string>
835-
</property>
836-
</widget>
837-
</item>
838-
<item>
839-
<spacer name="systemFont_horizontalSpacer">
840-
<property name="orientation">
841-
<enum>Qt::Horizontal</enum>
842-
</property>
843-
<property name="sizeHint" stdset="0">
844-
<size>
845-
<width>40</width>
846-
<height>20</height>
847-
</size>
848-
</property>
849-
</spacer>
850-
</item>
851-
<item>
852-
<layout class="QVBoxLayout" name="systemFont_verticalLayout">
853-
<item>
854-
<widget class="QLabel" name="systemFont_label_1">
855-
<property name="text">
856-
<string notr="true">111.11111111 BTC</string>
857-
</property>
858-
</widget>
859-
</item>
860-
<item>
861-
<widget class="QLabel" name="systemFont_label_9">
862-
<property name="text">
863-
<string notr="true">909.09090909 BTC</string>
864-
</property>
865-
</widget>
866-
</item>
867-
</layout>
868-
</item>
869-
</layout>
870-
</item>
871-
</layout>
872-
</widget>
775+
<layout class="QHBoxLayout" name="horizontalLayout_4_Display">
776+
<item>
777+
<widget class="QLabel" name="moneyFontLabel">
778+
<property name="text">
779+
<string>Font in the Overview tab: </string>
780+
</property>
781+
<property name="buddy">
782+
<cstring>moneyFont</cstring>
783+
</property>
784+
</widget>
785+
</item>
786+
<item>
787+
<widget class="QComboBox" name="moneyFont"/>
788+
</item>
789+
<item>
790+
<widget class="QLabel" name="moneyFont_preview">
791+
<property name="text">
792+
<string notr="true">111.11111111 BTC
793+
909.09090909 BTC</string>
794+
</property>
795+
</widget>
796+
</item>
797+
</layout>
873798
</item>
874799
<item>
875800
<spacer name="verticalSpacer_Display">

src/qt/optionsdialog.cpp

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,70 @@
2323

2424
#include <chrono>
2525

26+
#include <QApplication>
2627
#include <QDataWidgetMapper>
2728
#include <QDir>
29+
#include <QFontDialog>
2830
#include <QIntValidator>
2931
#include <QLocale>
3032
#include <QMessageBox>
3133
#include <QSystemTrayIcon>
3234
#include <QTimer>
3335

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

151-
QFont embedded_font{GUIUtil::fixedPitchFont(true)};
152-
ui->embeddedFont_radioButton->setText(ui->embeddedFont_radioButton->text().arg(QFontInfo(embedded_font).family()));
153-
embedded_font.setWeight(QFont::Bold);
154-
ui->embeddedFont_label_1->setFont(embedded_font);
155-
ui->embeddedFont_label_9->setFont(embedded_font);
156-
157-
QFont system_font{GUIUtil::fixedPitchFont(false)};
158-
ui->systemFont_radioButton->setText(ui->systemFont_radioButton->text().arg(QFontInfo(system_font).family()));
159-
system_font.setWeight(QFont::Bold);
160-
ui->systemFont_label_1->setFont(system_font);
161-
ui->systemFont_label_9->setFont(system_font);
162-
// Checking the embeddedFont_radioButton automatically unchecks the systemFont_radioButton.
163-
ui->systemFont_radioButton->setChecked(true);
207+
setupFontOptions(ui->moneyFont, ui->moneyFont_preview);
164208

165209
GUIUtil::handleCloseWindowShortcut(this);
166210
}
@@ -199,13 +243,7 @@ void OptionsDialog::setModel(OptionsModel *_model)
199243
mapper->toFirst();
200244

201245
const auto& font_for_money = _model->data(_model->index(OptionsModel::FontForMoney, 0), Qt::EditRole).value<OptionsModel::FontChoice>();
202-
if (std::holds_alternative<OptionsModel::FontChoiceAbstract>(font_for_money)) {
203-
ui->embeddedFont_radioButton->setChecked(font_for_money != OptionsModel::UseBestSystemFont);
204-
ui->systemFont_radioButton->setChecked(font_for_money == OptionsModel::UseBestSystemFont);
205-
} else {
206-
ui->embeddedFont_radioButton->setChecked(false);
207-
ui->systemFont_radioButton->setChecked(false);
208-
}
246+
setFontChoice(ui->moneyFont, font_for_money);
209247

210248
updateDefaultProxyNets();
211249
}
@@ -345,11 +383,7 @@ void OptionsDialog::on_openBitcoinConfButton_clicked()
345383

346384
void OptionsDialog::on_okButton_clicked()
347385
{
348-
if (ui->embeddedFont_radioButton->isChecked()) {
349-
model->setData(model->index(OptionsModel::FontForMoney, 0), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::EmbeddedFont}));
350-
} else if (ui->systemFont_radioButton->isChecked()) {
351-
model->setData(model->index(OptionsModel::FontForMoney, 0), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::BestSystemFont}));
352-
}
386+
model->setData(model->index(OptionsModel::FontForMoney, 0), ui->moneyFont->itemData(ui->moneyFont->currentIndex()));
353387

354388
mapper->submit();
355389
accept();

src/qt/optionsmodel.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,18 +490,23 @@ QVariant OptionsModel::getOption(OptionID option, const std::string& suffix) con
490490
}
491491
}
492492

493-
QFont OptionsModel::getFontForMoney() const
493+
QFont OptionsModel::getFontForChoice(const FontChoice& fc)
494494
{
495495
QFont f;
496-
if (std::holds_alternative<FontChoiceAbstract>(m_font_money)) {
497-
f = GUIUtil::fixedPitchFont(m_font_money != UseBestSystemFont);
496+
if (std::holds_alternative<FontChoiceAbstract>(fc)) {
497+
f = GUIUtil::fixedPitchFont(fc != UseBestSystemFont);
498498
f.setWeight(QFont::Bold);
499499
} else {
500-
f = std::get<QFont>(m_font_money);
500+
f = std::get<QFont>(fc);
501501
}
502502
return f;
503503
}
504504

505+
QFont OptionsModel::getFontForMoney() const
506+
{
507+
return getFontForChoice(m_font_money);
508+
}
509+
505510
bool OptionsModel::setOption(OptionID option, const QVariant& value, const std::string& suffix)
506511
{
507512
auto changed = [&] { return value.isValid() && value != getOption(option, suffix); };

src/qt/optionsmodel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class OptionsModel : public QAbstractListModel
8484
};
8585
typedef std::variant<FontChoiceAbstract, QFont> FontChoice;
8686
static inline const FontChoice UseBestSystemFont{FontChoiceAbstract::BestSystemFont};
87+
static QFont getFontForChoice(const FontChoice& fc);
8788

8889
bool Init(bilingual_str& error);
8990
void Reset();

0 commit comments

Comments
 (0)