Skip to content

Commit

Permalink
Merge #497: Enable users to configure their monospace font specifically
Browse files Browse the repository at this point in the history
a17fd33 GUI: OptionsDialog: Replace verbose two-option font selector with simple combobox with Custom... choice (Luke Dashjr)
98e9ac5 GUI: Use FontChoice type in OptionsModel settings abstraction (Luke Dashjr)
3a6757e GUI: Load custom FontForMoney from QSettings (Luke Dashjr)
49eb97e GUI: Add possibility for an explicit QFont for FontForMoney in OptionsModel (Luke Dashjr)
f2dfde8 GUI: Move "embedded font or not" decision into new OptionsModel::getFontForMoney method (Luke Dashjr)

Pull request description:

  This replaces the overly-verbose radio-button font setting (which only allows embedded or autodetected from system) with a simple combobox providing both existing options as well as a custom option to allow the user to select any font of their choice/style.

ACKs for top commit:
  pablomartin4btc:
    tested ACK  a17fd33
  hebasto:
    ACK a17fd33, I have reviewed the code and tested it on Ubuntu 22.04. This is a UX improvement. #497 (comment) might be addressed in a follow-up.

Tree-SHA512: 2f0a8bc1242a374c4b7dc6e34014400428b6d36063fa0b01c9f62a8bd6078adfbbca93d95c87e4ccb580d982fe10173e1d9a28bcec586591dd3f966c7b90fc5d
  • Loading branch information
hebasto committed Feb 7, 2024
2 parents 7b39702 + a17fd33 commit 60ac503
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 132 deletions.
121 changes: 23 additions & 98 deletions src/qt/forms/optionsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -772,104 +772,29 @@
</layout>
</item>
<item>
<widget class="QGroupBox" name="font_groupBox">
<property name="title">
<string>Monospaced font in the Overview tab:</string>
</property>
<layout class="QVBoxLayout" name="font_verticalLayout">
<item>
<layout class="QHBoxLayout" name="embeddedFont_horizontalLayout">
<item>
<widget class="QRadioButton" name="embeddedFont_radioButton">
<property name="text">
<string>embedded &quot;%1&quot;</string>
</property>
</widget>
</item>
<item>
<spacer name="embeddedFont_horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QVBoxLayout" name="embeddedFont_verticalLayout">
<item>
<widget class="QLabel" name="embeddedFont_label_1">
<property name="text">
<string notr="true">111.11111111 BTC</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="embeddedFont_label_9">
<property name="text">
<string notr="true">909.09090909 BTC</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<widget class="Line" name="font_line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="systemFont_horizontalLayout">
<item>
<widget class="QRadioButton" name="systemFont_radioButton">
<property name="text">
<string>closest matching &quot;%1&quot;</string>
</property>
</widget>
</item>
<item>
<spacer name="systemFont_horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QVBoxLayout" name="systemFont_verticalLayout">
<item>
<widget class="QLabel" name="systemFont_label_1">
<property name="text">
<string notr="true">111.11111111 BTC</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="systemFont_label_9">
<property name="text">
<string notr="true">909.09090909 BTC</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout_4_Display">
<item>
<widget class="QLabel" name="moneyFontLabel">
<property name="text">
<string>Font in the Overview tab: </string>
</property>
<property name="buddy">
<cstring>moneyFont</cstring>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="moneyFont"/>
</item>
<item>
<widget class="QLabel" name="moneyFont_preview">
<property name="text">
<string notr="true">111.11111111 BTC
909.09090909 BTC</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_Display">
Expand Down
76 changes: 62 additions & 14 deletions src/qt/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,70 @@

#include <chrono>

#include <QApplication>
#include <QDataWidgetMapper>
#include <QDir>
#include <QFontDialog>
#include <QIntValidator>
#include <QLocale>
#include <QMessageBox>
#include <QSystemTrayIcon>
#include <QTimer>

int setFontChoice(QComboBox* cb, const OptionsModel::FontChoice& fc)
{
int i;
for (i = cb->count(); --i >= 0; ) {
QVariant item_data = cb->itemData(i);
if (!item_data.canConvert<OptionsModel::FontChoice>()) continue;
if (item_data.value<OptionsModel::FontChoice>() == fc) {
break;
}
}
if (i == -1) {
// New item needed
QFont chosen_font = OptionsModel::getFontForChoice(fc);
QSignalBlocker block_currentindexchanged_signal(cb); // avoid triggering QFontDialog
cb->insertItem(0, QFontInfo(chosen_font).family(), QVariant::fromValue(fc));
i = 0;
}

cb->setCurrentIndex(i);
return i;
}

void setupFontOptions(QComboBox* cb, QLabel* preview)
{
QFont embedded_font{GUIUtil::fixedPitchFont(true)};
QFont system_font{GUIUtil::fixedPitchFont(false)};
cb->addItem(QObject::tr("Embedded \"%1\"").arg(QFontInfo(embedded_font).family()), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::EmbeddedFont}));
cb->addItem(QObject::tr("Default system font \"%1\"").arg(QFontInfo(system_font).family()), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::BestSystemFont}));
cb->addItem(QObject::tr("Custom…"));

const auto& on_font_choice_changed = [cb, preview](int index) {
static int previous_index = -1;
QVariant item_data = cb->itemData(index);
QFont f;
if (item_data.canConvert<OptionsModel::FontChoice>()) {
f = OptionsModel::getFontForChoice(item_data.value<OptionsModel::FontChoice>());
} else {
bool ok;
f = QFontDialog::getFont(&ok, GUIUtil::fixedPitchFont(false), cb->parentWidget());
if (!ok) {
cb->setCurrentIndex(previous_index);
return;
}
index = setFontChoice(cb, OptionsModel::FontChoice{f});
}
if (preview) {
preview->setFont(f);
}
previous_index = index;
};
QObject::connect(cb, QOverload<int>::of(&QComboBox::currentIndexChanged), on_font_choice_changed);
on_font_choice_changed(cb->currentIndex());
}

OptionsDialog::OptionsDialog(QWidget* parent, bool enableWallet)
: QDialog(parent, GUIUtil::dialog_flags),
ui(new Ui::OptionsDialog)
Expand Down Expand Up @@ -148,19 +204,7 @@ OptionsDialog::OptionsDialog(QWidget* parent, bool enableWallet)
ui->minimizeToTray->setEnabled(false);
}

QFont embedded_font{GUIUtil::fixedPitchFont(true)};
ui->embeddedFont_radioButton->setText(ui->embeddedFont_radioButton->text().arg(QFontInfo(embedded_font).family()));
embedded_font.setWeight(QFont::Bold);
ui->embeddedFont_label_1->setFont(embedded_font);
ui->embeddedFont_label_9->setFont(embedded_font);

QFont system_font{GUIUtil::fixedPitchFont(false)};
ui->systemFont_radioButton->setText(ui->systemFont_radioButton->text().arg(QFontInfo(system_font).family()));
system_font.setWeight(QFont::Bold);
ui->systemFont_label_1->setFont(system_font);
ui->systemFont_label_9->setFont(system_font);
// Checking the embeddedFont_radioButton automatically unchecks the systemFont_radioButton.
ui->systemFont_radioButton->setChecked(true);
setupFontOptions(ui->moneyFont, ui->moneyFont_preview);

GUIUtil::handleCloseWindowShortcut(this);
}
Expand Down Expand Up @@ -198,6 +242,9 @@ void OptionsDialog::setModel(OptionsModel *_model)
setMapper();
mapper->toFirst();

const auto& font_for_money = _model->data(_model->index(OptionsModel::FontForMoney, 0), Qt::EditRole).value<OptionsModel::FontChoice>();
setFontChoice(ui->moneyFont, font_for_money);

updateDefaultProxyNets();
}

Expand Down Expand Up @@ -275,7 +322,6 @@ void OptionsDialog::setMapper()
mapper->addMapping(ui->lang, OptionsModel::Language);
mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
mapper->addMapping(ui->embeddedFont_radioButton, OptionsModel::UseEmbeddedMonospacedFont);
}

void OptionsDialog::setOkButtonState(bool fState)
Expand Down Expand Up @@ -337,6 +383,8 @@ void OptionsDialog::on_openBitcoinConfButton_clicked()

void OptionsDialog::on_okButton_clicked()
{
model->setData(model->index(OptionsModel::FontForMoney, 0), ui->moneyFont->itemData(ui->moneyFont->currentIndex()));

mapper->submit();
accept();
updateDefaultProxyNets();
Expand Down
77 changes: 67 additions & 10 deletions src/qt/optionsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,37 @@ struct ProxySetting {
static ProxySetting ParseProxyString(const std::string& proxy);
static std::string ProxyString(bool is_set, QString ip, QString port);

static const QLatin1String fontchoice_str_embedded{"embedded"};
static const QLatin1String fontchoice_str_best_system{"best_system"};
static const QString fontchoice_str_custom_prefix{QStringLiteral("custom, ")};

QString OptionsModel::FontChoiceToString(const OptionsModel::FontChoice& f)
{
if (std::holds_alternative<FontChoiceAbstract>(f)) {
if (f == UseBestSystemFont) {
return fontchoice_str_best_system;
} else {
return fontchoice_str_embedded;
}
}
return fontchoice_str_custom_prefix + std::get<QFont>(f).toString();
}

OptionsModel::FontChoice OptionsModel::FontChoiceFromString(const QString& s)
{
if (s == fontchoice_str_best_system) {
return FontChoiceAbstract::BestSystemFont;
} else if (s == fontchoice_str_embedded) {
return FontChoiceAbstract::EmbeddedFont;
} else if (s.startsWith(fontchoice_str_custom_prefix)) {
QFont f;
f.fromString(s.mid(fontchoice_str_custom_prefix.size()));
return f;
} else {
return FontChoiceAbstract::EmbeddedFont; // default
}
}

OptionsModel::OptionsModel(interfaces::Node& node, QObject *parent) :
QAbstractListModel(parent), m_node{node}
{
Expand Down Expand Up @@ -216,11 +247,16 @@ bool OptionsModel::Init(bilingual_str& error)
#endif

// Display
if (!settings.contains("UseEmbeddedMonospacedFont")) {
settings.setValue("UseEmbeddedMonospacedFont", "true");
if (settings.contains("FontForMoney")) {
m_font_money = FontChoiceFromString(settings.value("FontForMoney").toString());
} else if (settings.contains("UseEmbeddedMonospacedFont")) {
if (settings.value("UseEmbeddedMonospacedFont").toBool()) {
m_font_money = FontChoiceAbstract::EmbeddedFont;
} else {
m_font_money = FontChoiceAbstract::BestSystemFont;
}
}
m_use_embedded_monospaced_font = settings.value("UseEmbeddedMonospacedFont").toBool();
Q_EMIT useEmbeddedMonospacedFontChanged(m_use_embedded_monospaced_font);
Q_EMIT fontForMoneyChanged(getFontForMoney());

m_mask_values = settings.value("mask_values", false).toBool();

Expand Down Expand Up @@ -428,8 +464,8 @@ QVariant OptionsModel::getOption(OptionID option, const std::string& suffix) con
return strThirdPartyTxUrls;
case Language:
return QString::fromStdString(SettingToString(setting(), ""));
case UseEmbeddedMonospacedFont:
return m_use_embedded_monospaced_font;
case FontForMoney:
return QVariant::fromValue(m_font_money);
case CoinControlFeatures:
return fCoinControlFeatures;
case EnablePSBTControls:
Expand All @@ -455,6 +491,23 @@ QVariant OptionsModel::getOption(OptionID option, const std::string& suffix) con
}
}

QFont OptionsModel::getFontForChoice(const FontChoice& fc)
{
QFont f;
if (std::holds_alternative<FontChoiceAbstract>(fc)) {
f = GUIUtil::fixedPitchFont(fc != UseBestSystemFont);
f.setWeight(QFont::Bold);
} else {
f = std::get<QFont>(fc);
}
return f;
}

QFont OptionsModel::getFontForMoney() const
{
return getFontForChoice(m_font_money);
}

bool OptionsModel::setOption(OptionID option, const QVariant& value, const std::string& suffix)
{
auto changed = [&] { return value.isValid() && value != getOption(option, suffix); };
Expand Down Expand Up @@ -587,11 +640,15 @@ bool OptionsModel::setOption(OptionID option, const QVariant& value, const std::
setRestartRequired(true);
}
break;
case UseEmbeddedMonospacedFont:
m_use_embedded_monospaced_font = value.toBool();
settings.setValue("UseEmbeddedMonospacedFont", m_use_embedded_monospaced_font);
Q_EMIT useEmbeddedMonospacedFontChanged(m_use_embedded_monospaced_font);
case FontForMoney:
{
const auto& new_font = value.value<FontChoice>();
if (m_font_money == new_font) break;
settings.setValue("FontForMoney", FontChoiceToString(new_font));
m_font_money = new_font;
Q_EMIT fontForMoneyChanged(getFontForMoney());
break;
}
case CoinControlFeatures:
fCoinControlFeatures = value.toBool();
settings.setValue("fCoinControlFeatures", fCoinControlFeatures);
Expand Down
Loading

0 comments on commit 60ac503

Please sign in to comment.