Skip to content

Commit 98e9ac5

Browse files
committed
GUI: Use FontChoice type in OptionsModel settings abstraction
1 parent 3a6757e commit 98e9ac5

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

src/qt/optionsdialog.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ void OptionsDialog::setModel(OptionsModel *_model)
198198
setMapper();
199199
mapper->toFirst();
200200

201+
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+
}
209+
201210
updateDefaultProxyNets();
202211
}
203212

@@ -275,7 +284,6 @@ void OptionsDialog::setMapper()
275284
mapper->addMapping(ui->lang, OptionsModel::Language);
276285
mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
277286
mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
278-
mapper->addMapping(ui->embeddedFont_radioButton, OptionsModel::UseEmbeddedMonospacedFont);
279287
}
280288

281289
void OptionsDialog::setOkButtonState(bool fState)
@@ -337,6 +345,12 @@ void OptionsDialog::on_openBitcoinConfButton_clicked()
337345

338346
void OptionsDialog::on_okButton_clicked()
339347
{
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+
}
353+
340354
mapper->submit();
341355
accept();
342356
updateDefaultProxyNets();

src/qt/optionsmodel.cpp

+19-15
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ static const QLatin1String fontchoice_str_embedded{"embedded"};
122122
static const QLatin1String fontchoice_str_best_system{"best_system"};
123123
static const QString fontchoice_str_custom_prefix{QStringLiteral("custom, ")};
124124

125+
QString OptionsModel::FontChoiceToString(const OptionsModel::FontChoice& f)
126+
{
127+
if (std::holds_alternative<FontChoiceAbstract>(f)) {
128+
if (f == UseBestSystemFont) {
129+
return fontchoice_str_best_system;
130+
} else {
131+
return fontchoice_str_embedded;
132+
}
133+
}
134+
return fontchoice_str_custom_prefix + std::get<QFont>(f).toString();
135+
}
136+
125137
OptionsModel::FontChoice OptionsModel::FontChoiceFromString(const QString& s)
126138
{
127139
if (s == fontchoice_str_best_system) {
@@ -451,8 +463,8 @@ QVariant OptionsModel::getOption(OptionID option, const std::string& suffix) con
451463
return strThirdPartyTxUrls;
452464
case Language:
453465
return QString::fromStdString(SettingToString(setting(), ""));
454-
case UseEmbeddedMonospacedFont:
455-
return (m_font_money != UseBestSystemFont);
466+
case FontForMoney:
467+
return QVariant::fromValue(m_font_money);
456468
case CoinControlFeatures:
457469
return fCoinControlFeatures;
458470
case EnablePSBTControls:
@@ -622,20 +634,12 @@ bool OptionsModel::setOption(OptionID option, const QVariant& value, const std::
622634
setRestartRequired(true);
623635
}
624636
break;
625-
case UseEmbeddedMonospacedFont:
637+
case FontForMoney:
626638
{
627-
const bool use_embedded_monospaced_font = value.toBool();
628-
if (use_embedded_monospaced_font) {
629-
if (m_font_money != UseBestSystemFont) {
630-
// Leave it as-is
631-
break;
632-
}
633-
m_font_money = FontChoiceAbstract::EmbeddedFont;
634-
} else {
635-
m_font_money = FontChoiceAbstract::BestSystemFont;
636-
}
637-
settings.setValue("UseEmbeddedMonospacedFont", use_embedded_monospaced_font);
638-
settings.remove("FontForMoney");
639+
const auto& new_font = value.value<FontChoice>();
640+
if (m_font_money == new_font) break;
641+
settings.setValue("FontForMoney", FontChoiceToString(new_font));
642+
m_font_money = new_font;
639643
Q_EMIT fontForMoneyChanged(getFontForMoney());
640644
break;
641645
}

src/qt/optionsmodel.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class OptionsModel : public QAbstractListModel
6262
DisplayUnit, // BitcoinUnit
6363
ThirdPartyTxUrls, // QString
6464
Language, // QString
65-
UseEmbeddedMonospacedFont, // bool
65+
FontForMoney, // FontChoice
6666
CoinControlFeatures, // bool
6767
SubFeeFromAmount, // bool
6868
ThreadsScriptVerif, // int
@@ -138,6 +138,7 @@ class OptionsModel : public QAbstractListModel
138138
/* settings that were overridden by command-line */
139139
QString strOverriddenByCommandLine;
140140

141+
static QString FontChoiceToString(const OptionsModel::FontChoice&);
141142
static FontChoice FontChoiceFromString(const QString&);
142143

143144
// Add option to list of GUI options overridden through command line/config file
@@ -153,4 +154,6 @@ class OptionsModel : public QAbstractListModel
153154
void fontForMoneyChanged(const QFont&);
154155
};
155156

157+
Q_DECLARE_METATYPE(OptionsModel::FontChoice)
158+
156159
#endif // BITCOIN_QT_OPTIONSMODEL_H

0 commit comments

Comments
 (0)