Skip to content

Commit 7740196

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

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
@@ -762,104 +762,29 @@
762762
</layout>
763763
</item>
764764
<item>
765-
<widget class="QGroupBox" name="font_groupBox">
766-
<property name="title">
767-
<string>Monospaced font in the Overview tab:</string>
768-
</property>
769-
<layout class="QVBoxLayout" name="font_verticalLayout">
770-
<item>
771-
<layout class="QHBoxLayout" name="embeddedFont_horizontalLayout">
772-
<item>
773-
<widget class="QRadioButton" name="embeddedFont_radioButton">
774-
<property name="text">
775-
<string>embedded &quot;%1&quot;</string>
776-
</property>
777-
</widget>
778-
</item>
779-
<item>
780-
<spacer name="embeddedFont_horizontalSpacer">
781-
<property name="orientation">
782-
<enum>Qt::Horizontal</enum>
783-
</property>
784-
<property name="sizeHint" stdset="0">
785-
<size>
786-
<width>40</width>
787-
<height>20</height>
788-
</size>
789-
</property>
790-
</spacer>
791-
</item>
792-
<item>
793-
<layout class="QVBoxLayout" name="embeddedFont_verticalLayout">
794-
<item>
795-
<widget class="QLabel" name="embeddedFont_label_1">
796-
<property name="text">
797-
<string notr="true">111.11111111 BTC</string>
798-
</property>
799-
</widget>
800-
</item>
801-
<item>
802-
<widget class="QLabel" name="embeddedFont_label_9">
803-
<property name="text">
804-
<string notr="true">909.09090909 BTC</string>
805-
</property>
806-
</widget>
807-
</item>
808-
</layout>
809-
</item>
810-
</layout>
811-
</item>
812-
<item>
813-
<widget class="Line" name="font_line">
814-
<property name="orientation">
815-
<enum>Qt::Horizontal</enum>
816-
</property>
817-
</widget>
818-
</item>
819-
<item>
820-
<layout class="QHBoxLayout" name="systemFont_horizontalLayout">
821-
<item>
822-
<widget class="QRadioButton" name="systemFont_radioButton">
823-
<property name="text">
824-
<string>closest matching &quot;%1&quot;</string>
825-
</property>
826-
</widget>
827-
</item>
828-
<item>
829-
<spacer name="systemFont_horizontalSpacer">
830-
<property name="orientation">
831-
<enum>Qt::Horizontal</enum>
832-
</property>
833-
<property name="sizeHint" stdset="0">
834-
<size>
835-
<width>40</width>
836-
<height>20</height>
837-
</size>
838-
</property>
839-
</spacer>
840-
</item>
841-
<item>
842-
<layout class="QVBoxLayout" name="systemFont_verticalLayout">
843-
<item>
844-
<widget class="QLabel" name="systemFont_label_1">
845-
<property name="text">
846-
<string notr="true">111.11111111 BTC</string>
847-
</property>
848-
</widget>
849-
</item>
850-
<item>
851-
<widget class="QLabel" name="systemFont_label_9">
852-
<property name="text">
853-
<string notr="true">909.09090909 BTC</string>
854-
</property>
855-
</widget>
856-
</item>
857-
</layout>
858-
</item>
859-
</layout>
860-
</item>
861-
</layout>
862-
</widget>
765+
<layout class="QHBoxLayout" name="horizontalLayout_4_Display">
766+
<item>
767+
<widget class="QLabel" name="moneyFontLabel">
768+
<property name="text">
769+
<string>Font in the Overview tab: </string>
770+
</property>
771+
<property name="buddy">
772+
<cstring>moneyFont</cstring>
773+
</property>
774+
</widget>
775+
</item>
776+
<item>
777+
<widget class="QComboBox" name="moneyFont"/>
778+
</item>
779+
<item>
780+
<widget class="QLabel" name="moneyFont_preview">
781+
<property name="text">
782+
<string notr="true">111.11111111 BTC
783+
909.09090909 BTC</string>
784+
</property>
785+
</widget>
786+
</item>
787+
</layout>
863788
</item>
864789
<item>
865790
<spacer name="verticalSpacer_Display">

src/qt/optionsdialog.cpp

+59-25
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,71 @@
1919
#include <netbase.h>
2020
#include <txdb.h> // for -dbcache defaults
2121

22+
#include <QApplication>
2223
#include <QDataWidgetMapper>
2324
#include <QDir>
25+
#include <QFontDialog>
2426
#include <QIntValidator>
2527
#include <QLocale>
2628
#include <QMessageBox>
2729
#include <QSettings>
2830
#include <QSystemTrayIcon>
2931
#include <QTimer>
3032

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

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

166210
GUIUtil::handleCloseWindowShortcut(this);
167211
}
@@ -195,13 +239,7 @@ void OptionsDialog::setModel(OptionsModel *_model)
195239
mapper->toFirst();
196240

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

206244
updateDefaultProxyNets();
207245
}
@@ -331,11 +369,7 @@ void OptionsDialog::on_openBitcoinConfButton_clicked()
331369

332370
void OptionsDialog::on_okButton_clicked()
333371
{
334-
if (ui->embeddedFont_radioButton->isChecked()) {
335-
model->setData(model->index(OptionsModel::FontForMoney, 0), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::EmbeddedFont}));
336-
} else if (ui->systemFont_radioButton->isChecked()) {
337-
model->setData(model->index(OptionsModel::FontForMoney, 0), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::BestSystemFont}));
338-
}
372+
model->setData(model->index(OptionsModel::FontForMoney, 0), ui->moneyFont->itemData(ui->moneyFont->currentIndex()));
339373

340374
mapper->submit();
341375
accept();

src/qt/optionsmodel.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -605,18 +605,23 @@ void OptionsModel::setDisplayUnit(const QVariant &value)
605605
}
606606
}
607607

608-
QFont OptionsModel::getFontForMoney() const
608+
QFont OptionsModel::getFontForChoice(const FontChoice& fc)
609609
{
610610
QFont f;
611-
if (std::holds_alternative<FontChoiceAbstract>(m_font_money)) {
612-
f = GUIUtil::fixedPitchFont(m_font_money != UseBestSystemFont);
611+
if (std::holds_alternative<FontChoiceAbstract>(fc)) {
612+
f = GUIUtil::fixedPitchFont(fc != UseBestSystemFont);
613613
f.setWeight(QFont::Bold);
614614
} else {
615-
f = std::get<QFont>(m_font_money);
615+
f = std::get<QFont>(fc);
616616
}
617617
return f;
618618
}
619619

620+
QFont OptionsModel::getFontForMoney() const
621+
{
622+
return getFontForChoice(m_font_money);
623+
}
624+
620625
void OptionsModel::setRestartRequired(bool fRequired)
621626
{
622627
QSettings settings;

src/qt/optionsmodel.h

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

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

0 commit comments

Comments
 (0)