Skip to content

Commit

Permalink
GUI: OptionsDialog: Replace verbose two-option font selector with sim…
Browse files Browse the repository at this point in the history
…ple combobox with Custom... choice
  • Loading branch information
luke-jr committed Dec 3, 2021
1 parent e54d7f2 commit 7740196
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 127 deletions.
121 changes: 23 additions & 98 deletions src/qt/forms/optionsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -762,104 +762,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
84 changes: 59 additions & 25 deletions src/qt/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,71 @@
#include <netbase.h>
#include <txdb.h> // for -dbcache defaults

#include <QApplication>
#include <QDataWidgetMapper>
#include <QDir>
#include <QFontDialog>
#include <QIntValidator>
#include <QLocale>
#include <QMessageBox>
#include <QSettings>
#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 @@ -149,19 +205,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 @@ -195,13 +239,7 @@ void OptionsDialog::setModel(OptionsModel *_model)
mapper->toFirst();

const auto& font_for_money = _model->data(_model->index(OptionsModel::FontForMoney, 0), Qt::EditRole).value<OptionsModel::FontChoice>();
if (std::holds_alternative<OptionsModel::FontChoiceAbstract>(font_for_money)) {
ui->embeddedFont_radioButton->setChecked(font_for_money != OptionsModel::UseBestSystemFont);
ui->systemFont_radioButton->setChecked(font_for_money == OptionsModel::UseBestSystemFont);
} else {
ui->embeddedFont_radioButton->setChecked(false);
ui->systemFont_radioButton->setChecked(false);
}
setFontChoice(ui->moneyFont, font_for_money);

updateDefaultProxyNets();
}
Expand Down Expand Up @@ -331,11 +369,7 @@ void OptionsDialog::on_openBitcoinConfButton_clicked()

void OptionsDialog::on_okButton_clicked()
{
if (ui->embeddedFont_radioButton->isChecked()) {
model->setData(model->index(OptionsModel::FontForMoney, 0), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::EmbeddedFont}));
} else if (ui->systemFont_radioButton->isChecked()) {
model->setData(model->index(OptionsModel::FontForMoney, 0), QVariant::fromValue(OptionsModel::FontChoice{OptionsModel::FontChoiceAbstract::BestSystemFont}));
}
model->setData(model->index(OptionsModel::FontForMoney, 0), ui->moneyFont->itemData(ui->moneyFont->currentIndex()));

mapper->submit();
accept();
Expand Down
13 changes: 9 additions & 4 deletions src/qt/optionsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,18 +605,23 @@ void OptionsModel::setDisplayUnit(const QVariant &value)
}
}

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

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

void OptionsModel::setRestartRequired(bool fRequired)
{
QSettings settings;
Expand Down
1 change: 1 addition & 0 deletions src/qt/optionsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class OptionsModel : public QAbstractListModel
};
typedef std::variant<FontChoiceAbstract, QFont> FontChoice;
static inline const FontChoice UseBestSystemFont{FontChoiceAbstract::BestSystemFont};
static QFont getFontForChoice(const FontChoice& fc);

void Init(bool resetSettings = false);
void Reset();
Expand Down

0 comments on commit 7740196

Please sign in to comment.