Skip to content

Commit ca6e29d

Browse files
committed
GUI: OptionsDialog: Replace verbose two-option font selector with simple combobox with Custom... choice
1 parent 9af6268 commit ca6e29d

File tree

4 files changed

+92
-127
lines changed

4 files changed

+92
-127
lines changed

src/qt/forms/optionsdialog.ui

+23-98
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

+59-25
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,71 @@
2121

2222
#include <chrono>
2323

24+
#include <QApplication>
2425
#include <QDataWidgetMapper>
2526
#include <QDir>
27+
#include <QFontDialog>
2628
#include <QIntValidator>
2729
#include <QLocale>
2830
#include <QMessageBox>
2931
#include <QSettings>
3032
#include <QSystemTrayIcon>
3133
#include <QTimer>
3234

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

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

168212
GUIUtil::handleCloseWindowShortcut(this);
169213
}
@@ -197,13 +241,7 @@ void OptionsDialog::setModel(OptionsModel *_model)
197241
mapper->toFirst();
198242

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

208246
updateDefaultProxyNets();
209247
}
@@ -334,11 +372,7 @@ void OptionsDialog::on_openBitcoinConfButton_clicked()
334372

335373
void OptionsDialog::on_okButton_clicked()
336374
{
337-
if (ui->embeddedFont_radioButton->isChecked()) {
338-
model->setData(model->index(OptionsModel::FontForMoney, 0), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::EmbeddedFont}));
339-
} else if (ui->systemFont_radioButton->isChecked()) {
340-
model->setData(model->index(OptionsModel::FontForMoney, 0), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::BestSystemFont}));
341-
}
375+
model->setData(model->index(OptionsModel::FontForMoney, 0), ui->moneyFont->itemData(ui->moneyFont->currentIndex()));
342376

343377
mapper->submit();
344378
accept();

src/qt/optionsmodel.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -616,18 +616,23 @@ void OptionsModel::setDisplayUnit(const QVariant &value)
616616
}
617617
}
618618

619-
QFont OptionsModel::getFontForMoney() const
619+
QFont OptionsModel::getFontForChoice(const FontChoice& fc)
620620
{
621621
QFont f;
622-
if (std::holds_alternative<FontChoiceAbstract>(m_font_money)) {
623-
f = GUIUtil::fixedPitchFont(m_font_money != UseBestSystemFont);
622+
if (std::holds_alternative<FontChoiceAbstract>(fc)) {
623+
f = GUIUtil::fixedPitchFont(fc != UseBestSystemFont);
624624
f.setWeight(QFont::Bold);
625625
} else {
626-
f = std::get<QFont>(m_font_money);
626+
f = std::get<QFont>(fc);
627627
}
628628
return f;
629629
}
630630

631+
QFont OptionsModel::getFontForMoney() const
632+
{
633+
return getFontForChoice(m_font_money);
634+
}
635+
631636
void OptionsModel::setRestartRequired(bool fRequired)
632637
{
633638
QSettings settings;

src/qt/optionsmodel.h

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class OptionsModel : public QAbstractListModel
8181
};
8282
typedef std::variant<FontChoiceAbstract, QFont> FontChoice;
8383
static inline const FontChoice UseBestSystemFont{FontChoiceAbstract::BestSystemFont};
84+
static QFont getFontForChoice(const FontChoice& fc);
8485

8586
void Init(bool resetSettings = false);
8687
void Reset();

0 commit comments

Comments
 (0)