Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable users to configure their monospace font specifically #497

Merged
merged 5 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move under anonymous namespace?

{
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 @@ -118,6 +118,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 @@ -215,11 +246,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 @@ -427,8 +463,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 @@ -454,6 +490,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 @@ -586,11 +639,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