Skip to content

Commit e54d7f2

Browse files
committed
GUI: Use FontChoice type in OptionsModel settings abstraction
1 parent bada5f3 commit e54d7f2

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
@@ -194,6 +194,15 @@ void OptionsDialog::setModel(OptionsModel *_model)
194194
setMapper();
195195
mapper->toFirst();
196196

197+
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+
}
205+
197206
updateDefaultProxyNets();
198207
}
199208

@@ -270,7 +279,6 @@ void OptionsDialog::setMapper()
270279
mapper->addMapping(ui->lang, OptionsModel::Language);
271280
mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
272281
mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
273-
mapper->addMapping(ui->embeddedFont_radioButton, OptionsModel::UseEmbeddedMonospacedFont);
274282
}
275283

276284
void OptionsDialog::setOkButtonState(bool fState)
@@ -323,6 +331,12 @@ void OptionsDialog::on_openBitcoinConfButton_clicked()
323331

324332
void OptionsDialog::on_okButton_clicked()
325333
{
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+
}
339+
326340
mapper->submit();
327341
accept();
328342
updateDefaultProxyNets();

src/qt/optionsmodel.cpp

+19-15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ static const QLatin1String fontchoice_str_embedded{"embedded"};
3333
static const QLatin1String fontchoice_str_best_system{"best_system"};
3434
static const QString fontchoice_str_custom_prefix{QStringLiteral("custom, ")};
3535

36+
QString OptionsModel::FontChoiceToString(const OptionsModel::FontChoice& f)
37+
{
38+
if (std::holds_alternative<FontChoiceAbstract>(f)) {
39+
if (f == UseBestSystemFont) {
40+
return fontchoice_str_best_system;
41+
} else {
42+
return fontchoice_str_embedded;
43+
}
44+
}
45+
return fontchoice_str_custom_prefix + std::get<QFont>(f).toString();
46+
}
47+
3648
OptionsModel::FontChoice OptionsModel::FontChoiceFromString(const QString& s)
3749
{
3850
if (s == fontchoice_str_best_system) {
@@ -380,8 +392,8 @@ QVariant OptionsModel::data(const QModelIndex & index, int role) const
380392
return strThirdPartyTxUrls;
381393
case Language:
382394
return settings.value("language");
383-
case UseEmbeddedMonospacedFont:
384-
return (m_font_money != UseBestSystemFont);
395+
case FontForMoney:
396+
return QVariant::fromValue(m_font_money);
385397
case CoinControlFeatures:
386398
return fCoinControlFeatures;
387399
case Prune:
@@ -521,20 +533,12 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
521533
setRestartRequired(true);
522534
}
523535
break;
524-
case UseEmbeddedMonospacedFont:
536+
case FontForMoney:
525537
{
526-
const bool use_embedded_monospaced_font = value.toBool();
527-
if (use_embedded_monospaced_font) {
528-
if (m_font_money != UseBestSystemFont) {
529-
// Leave it as-is
530-
break;
531-
}
532-
m_font_money = FontChoiceAbstract::EmbeddedFont;
533-
} else {
534-
m_font_money = FontChoiceAbstract::BestSystemFont;
535-
}
536-
settings.setValue("UseEmbeddedMonospacedFont", use_embedded_monospaced_font);
537-
settings.remove("FontForMoney");
538+
const auto& new_font = value.value<FontChoice>();
539+
if (m_font_money == new_font) break;
540+
settings.setValue("FontForMoney", FontChoiceToString(new_font));
541+
m_font_money = new_font;
538542
Q_EMIT fontForMoneyChanged(getFontForMoney());
539543
break;
540544
}

src/qt/optionsmodel.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class OptionsModel : public QAbstractListModel
6060
DisplayUnit, // BitcoinUnits::Unit
6161
ThirdPartyTxUrls, // QString
6262
Language, // QString
63-
UseEmbeddedMonospacedFont, // bool
63+
FontForMoney, // FontChoice
6464
CoinControlFeatures, // bool
6565
SubFeeFromAmount, // bool
6666
ThreadsScriptVerif, // int
@@ -127,6 +127,7 @@ class OptionsModel : public QAbstractListModel
127127
/* settings that were overridden by command-line */
128128
QString strOverriddenByCommandLine;
129129

130+
static QString FontChoiceToString(const OptionsModel::FontChoice&);
130131
static FontChoice FontChoiceFromString(const QString&);
131132

132133
// Add option to list of GUI options overridden through command line/config file
@@ -141,4 +142,6 @@ class OptionsModel : public QAbstractListModel
141142
void fontForMoneyChanged(const QFont&);
142143
};
143144

145+
Q_DECLARE_METATYPE(OptionsModel::FontChoice)
146+
144147
#endif // BITCOIN_QT_OPTIONSMODEL_H

0 commit comments

Comments
 (0)