-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor(qt): simplify GUIUtill[fonts] API #7336
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
Changes from all commits
0ca353f
e68ad62
aa9cf24
5961bb5
fb8da28
25759c6
635480d
99a2277
fdffbf9
cae89c0
8963dce
6d15b12
50c0ff1
945bf8d
7517f7b
d4badd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,17 +9,23 @@ | |
| #include <qt/forms/ui_appearancewidget.h> | ||
|
|
||
| #include <qt/appearancewidget.h> | ||
| #include <qt/guiutil.h> | ||
| #include <qt/optionsmodel.h> | ||
|
|
||
| #include <util/system.h> | ||
|
|
||
| #include <QComboBox> | ||
| #include <QDataWidgetMapper> | ||
| #include <QDialogButtonBox> | ||
| #include <QFontDialog> | ||
| #include <QFontInfo> | ||
| #include <QLabel> | ||
| #include <QSettings> | ||
| #include <QSlider> | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdlib> | ||
|
|
||
| int setFontChoice(QComboBox* cb, const OptionsModel::FontChoice& fc) | ||
| { | ||
| int i; | ||
|
|
@@ -76,19 +82,20 @@ AppearanceWidget::AppearanceWidget(QWidget* parent) : | |
| QWidget(parent), | ||
| ui{new Ui::AppearanceWidget()}, | ||
| prevTheme{GUIUtil::getActiveTheme()}, | ||
| prevScale{GUIUtil::g_font_registry.GetFontScale()}, | ||
| prevFontFamily{GUIUtil::g_font_registry.GetFont()}, | ||
| prevWeightNormal{GUIUtil::g_font_registry.GetWeightNormal()}, | ||
| prevWeightBold{GUIUtil::g_font_registry.GetWeightBold()} | ||
| prevScale{GUIUtil::fontScale()}, | ||
| prevFontFamily{GUIUtil::activeFont()}, | ||
| prevWeightNormalArg{GUIUtil::currentWeightArg(GUIUtil::FontWeight::Normal)}, | ||
| prevWeightBoldArg{GUIUtil::currentWeightArg(GUIUtil::FontWeight::Bold)} | ||
| { | ||
| ui->setupUi(this); | ||
|
|
||
| for (const QString& entry : GUIUtil::listThemes()) { | ||
| ui->theme->addItem(entry, QVariant(entry)); | ||
| } | ||
|
|
||
| for (size_t idx{0}; idx < GUIUtil::g_fonts_known.size(); idx++) { | ||
| const auto& [font, selectable] = GUIUtil::g_fonts_known[idx]; | ||
| const auto& known = GUIUtil::knownFonts(); | ||
| for (size_t idx{0}; idx < known.size(); idx++) { | ||
| const auto& [font, selectable] = known[idx]; | ||
| if (selectable) { ui->fontFamily->addItem(font, QVariant((uint16_t)idx)); } | ||
| } | ||
|
|
||
|
|
@@ -127,19 +134,19 @@ AppearanceWidget::~AppearanceWidget() | |
| if (prevTheme != GUIUtil::getActiveTheme()) { | ||
| updateTheme(prevTheme); | ||
| } | ||
| if (prevFontFamily != GUIUtil::g_font_registry.GetFont()) { | ||
| const bool setfont_ret{GUIUtil::g_font_registry.SetFont(prevFontFamily)}; | ||
| if (prevFontFamily != GUIUtil::activeFont()) { | ||
| const bool setfont_ret{GUIUtil::setActiveFont(prevFontFamily)}; | ||
| assert(setfont_ret); | ||
| GUIUtil::setApplicationFont(); | ||
| } | ||
| if (prevScale != GUIUtil::g_font_registry.GetFontScale()) { | ||
| GUIUtil::g_font_registry.SetFontScale(prevScale); | ||
| if (prevScale != GUIUtil::fontScale()) { | ||
| GUIUtil::setFontScale(prevScale); | ||
| } | ||
| if (prevWeightNormal != GUIUtil::g_font_registry.GetWeightNormal()) { | ||
| GUIUtil::g_font_registry.SetWeightNormal(prevWeightNormal); | ||
| if (prevWeightNormalArg != GUIUtil::currentWeightArg(GUIUtil::FontWeight::Normal)) { | ||
| GUIUtil::setWeightFromArg(GUIUtil::FontWeight::Normal, prevWeightNormalArg); | ||
| } | ||
| if (prevWeightBold != GUIUtil::g_font_registry.GetWeightBold()) { | ||
| GUIUtil::g_font_registry.SetWeightBold(prevWeightBold); | ||
| if (prevWeightBoldArg != GUIUtil::currentWeightArg(GUIUtil::FontWeight::Bold)) { | ||
| GUIUtil::setWeightFromArg(GUIUtil::FontWeight::Bold, prevWeightBoldArg); | ||
| } | ||
| // Restore monospace font if cancelled | ||
| if (model) { | ||
|
|
@@ -180,32 +187,28 @@ void AppearanceWidget::setModel(OptionsModel* _model) | |
| const bool override_family{_model->isOptionOverridden("-font-family")}; | ||
| if (override_family) { | ||
| ui->fontFamily->setEnabled(false); | ||
| if (const auto idx{ui->fontFamily->findText(GUIUtil::g_font_registry.GetFont())}; idx != -1) { | ||
| if (const auto idx{ui->fontFamily->findText(GUIUtil::activeFont())}; idx != -1) { | ||
| ui->fontFamily->setCurrentIndex(idx); | ||
| } | ||
| } | ||
|
|
||
| if (_model->isOptionOverridden("-font-scale")) { | ||
| ui->fontScaleSlider->setEnabled(false); | ||
| ui->fontScaleSlider->setValue(GUIUtil::g_font_registry.GetFontScale()); | ||
| ui->fontScaleSlider->setValue(GUIUtil::fontScale()); | ||
| } | ||
|
|
||
| if (bool is_overridden{_model->isOptionOverridden("-font-weight-normal")}; is_overridden || override_family) { | ||
| if (is_overridden) { | ||
| ui->fontWeightNormalSlider->setEnabled(false); | ||
| } | ||
| if (const auto idx{GUIUtil::g_font_registry.WeightToIdx(GUIUtil::g_font_registry.GetWeightNormal())}; idx != -1) { | ||
| ui->fontWeightNormalSlider->setValue(idx); | ||
| } | ||
| ui->fontWeightNormalSlider->setValue(GUIUtil::currentWeightArg(GUIUtil::FontWeight::Normal)); | ||
| } | ||
|
|
||
| if (bool is_overridden{_model->isOptionOverridden("-font-weight-bold")}; is_overridden || override_family) { | ||
| if (is_overridden) { | ||
| ui->fontWeightBoldSlider->setEnabled(false); | ||
| } | ||
| if (const auto idx{GUIUtil::g_font_registry.WeightToIdx(GUIUtil::g_font_registry.GetWeightBold())}; idx != -1) { | ||
| ui->fontWeightBoldSlider->setValue(idx); | ||
| } | ||
| ui->fontWeightBoldSlider->setValue(GUIUtil::currentWeightArg(GUIUtil::FontWeight::Bold)); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -229,7 +232,7 @@ void AppearanceWidget::updateTheme(const QString& theme) | |
|
|
||
| void AppearanceWidget::updateFontFamily(int index) | ||
| { | ||
| const bool setfont_ret{GUIUtil::g_font_registry.SetFont(GUIUtil::g_fonts_known[ui->fontFamily->itemData(index).toInt()].first)}; | ||
| const bool setfont_ret{GUIUtil::setActiveFont(GUIUtil::knownFonts()[ui->fontFamily->itemData(index).toInt()].first)}; | ||
| assert(setfont_ret); | ||
| GUIUtil::setApplicationFont(); | ||
| GUIUtil::updateFonts(); | ||
|
|
@@ -238,7 +241,7 @@ void AppearanceWidget::updateFontFamily(int index) | |
|
|
||
| void AppearanceWidget::updateFontScale(int nScale) | ||
| { | ||
| GUIUtil::g_font_registry.SetFontScale(nScale); | ||
| GUIUtil::setFontScale(nScale); | ||
| GUIUtil::updateFonts(); | ||
| } | ||
|
|
||
|
|
@@ -248,9 +251,11 @@ void AppearanceWidget::updateFontWeightNormal(int nValue, bool fForce) | |
| if (nValue > ui->fontWeightBoldSlider->value() && !fForce) { | ||
| nSliderValue = ui->fontWeightBoldSlider->value(); | ||
| } | ||
| nSliderValue = std::ranges::min(GUIUtil::supportedWeightArgs(), {}, | ||
| [nSliderValue](int x) { return std::abs(x - nSliderValue); }); | ||
| const QSignalBlocker blocker(ui->fontWeightNormalSlider); | ||
| ui->fontWeightNormalSlider->setValue(nSliderValue); | ||
| GUIUtil::g_font_registry.SetWeightNormal(GUIUtil::g_font_registry.IdxToWeight(ui->fontWeightNormalSlider->value())); | ||
| GUIUtil::setWeightFromArg(GUIUtil::FontWeight::Normal, nSliderValue); | ||
| GUIUtil::setApplicationFont(); | ||
| GUIUtil::updateFonts(); | ||
| } | ||
|
|
@@ -261,9 +266,11 @@ void AppearanceWidget::updateFontWeightBold(int nValue, bool fForce) | |
| if (nValue < ui->fontWeightNormalSlider->value() && !fForce) { | ||
| nSliderValue = ui->fontWeightNormalSlider->value(); | ||
| } | ||
| nSliderValue = std::ranges::min(GUIUtil::supportedWeightArgs(), {}, | ||
| [nSliderValue](int x) { return std::abs(x - nSliderValue); }); | ||
| const QSignalBlocker blocker(ui->fontWeightBoldSlider); | ||
| ui->fontWeightBoldSlider->setValue(nSliderValue); | ||
| GUIUtil::g_font_registry.SetWeightBold(GUIUtil::g_font_registry.IdxToWeight(ui->fontWeightBoldSlider->value())); | ||
| GUIUtil::setWeightFromArg(GUIUtil::FontWeight::Bold, nSliderValue); | ||
| GUIUtil::setApplicationFont(); | ||
| GUIUtil::updateFonts(); | ||
| } | ||
|
|
@@ -283,19 +290,60 @@ void AppearanceWidget::updateMoneyFont(int index) | |
|
|
||
| void AppearanceWidget::updateWeightSlider(const bool fForce) | ||
| { | ||
| int nMaximum = GUIUtil::g_font_registry.GetSupportedWeights().size() - 1; | ||
| const auto supported = GUIUtil::supportedWeightArgs(); | ||
| const int nMin = supported.front(); | ||
| const int nMax = supported.back(); | ||
|
|
||
| ui->fontWeightNormalSlider->setMinimum(0); | ||
| ui->fontWeightNormalSlider->setMaximum(nMaximum); | ||
| ui->fontWeightNormalSlider->setMinimum(nMin); | ||
| ui->fontWeightNormalSlider->setMaximum(nMax); | ||
|
|
||
| ui->fontWeightBoldSlider->setMinimum(0); | ||
| ui->fontWeightBoldSlider->setMaximum(nMaximum); | ||
| ui->fontWeightBoldSlider->setMinimum(nMin); | ||
| ui->fontWeightBoldSlider->setMaximum(nMax); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
|
|
||
| if (fForce || !GUIUtil::g_font_registry.IsValidWeight(prevWeightNormal) || !GUIUtil::g_font_registry.IsValidWeight(prevWeightBold)) { | ||
| int nIndexNormal = GUIUtil::g_font_registry.WeightToIdx(GUIUtil::g_font_registry.GetWeightNormalDefault()); | ||
| int nIndexBold = GUIUtil::g_font_registry.WeightToIdx(GUIUtil::g_font_registry.GetWeightBoldDefault()); | ||
| assert(nIndexNormal != -1 && nIndexBold != -1); | ||
| updateFontWeightNormal(nIndexNormal, true); | ||
| updateFontWeightBold(nIndexBold, true); | ||
| if (fForce) { | ||
| updateFontWeightNormal(GUIUtil::defaultWeightArg(GUIUtil::FontWeight::Normal), true); | ||
| updateFontWeightBold(GUIUtil::defaultWeightArg(GUIUtil::FontWeight::Bold), true); | ||
|
Comment on lines
+303
to
+305
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't wipe a font family's cached weights when switching families.
Suggested fix- if (fForce) {
- updateFontWeightNormal(GUIUtil::defaultWeightArg(GUIUtil::FontWeight::Normal), true);
- updateFontWeightBold(GUIUtil::defaultWeightArg(GUIUtil::FontWeight::Bold), true);
- }
+ if (fForce) {
+ updateFontWeightNormal(GUIUtil::currentWeightArg(GUIUtil::FontWeight::Normal), true);
+ updateFontWeightBold(GUIUtil::currentWeightArg(GUIUtil::FontWeight::Bold), true);
+ }🤖 Prompt for AI Agents |
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| void AppearanceWidget::setupAppearance(QWidget* parent, OptionsModel* model) | ||
| { | ||
| if (!QSettings().value("fAppearanceSetupDone", false).toBool()) { | ||
| // Create the dialog | ||
| QDialog dlg(parent); | ||
| dlg.setObjectName("AppearanceSetup"); | ||
| dlg.setWindowTitle(QObject::tr("Appearance Setup")); | ||
| dlg.setWindowIcon(QIcon(":icons/dash")); | ||
| // And the widgets we add to it | ||
| QLabel lblHeading(QObject::tr("Please choose your preferred settings for the appearance of %1").arg(PACKAGE_NAME), &dlg); | ||
| lblHeading.setObjectName("lblHeading"); | ||
| lblHeading.setWordWrap(true); | ||
| QLabel lblSubHeading(QObject::tr("This can also be adjusted later in the \"Appearance\" tab of the preferences."), &dlg); | ||
| lblSubHeading.setObjectName("lblSubHeading"); | ||
| lblSubHeading.setWordWrap(true); | ||
| AppearanceWidget appearance(&dlg); | ||
| appearance.setModel(model); | ||
| QFrame line(&dlg); | ||
| line.setFrameShape(QFrame::HLine); | ||
| QDialogButtonBox buttonBox(QDialogButtonBox::Save); | ||
| // Put them into a vbox and add the vbox to the dialog | ||
| QVBoxLayout layout; | ||
| layout.addWidget(&lblHeading); | ||
| layout.addWidget(&lblSubHeading); | ||
| layout.addWidget(&line); | ||
| layout.addWidget(&appearance); | ||
| layout.addWidget(&buttonBox); | ||
| dlg.setLayout(&layout); | ||
| // Adjust the headings | ||
| GUIUtil::setFont({&lblHeading}, GUIUtil::FontWeight::Bold, 16); | ||
| GUIUtil::setFont({&lblSubHeading}, GUIUtil::FontWeight::Normal, 14, true); | ||
| // Make sure the dialog closes and accepts the settings if save has been pressed | ||
| QObject::connect(&buttonBox, &QDialogButtonBox::accepted, [&]() { | ||
| QSettings().setValue("fAppearanceSetupDone", true); | ||
| appearance.accept(); | ||
| dlg.accept(); | ||
| }); | ||
| // And fire it! | ||
| dlg.exec(); | ||
|
Comment on lines
+337
to
+347
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apply the heading styles before showing the dialog.
Suggested fix // Adjust the headings
GUIUtil::setFont({&lblHeading}, GUIUtil::FontWeight::Bold, 16);
GUIUtil::setFont({&lblSubHeading}, GUIUtil::FontWeight::Normal, 14, true);
+ GUIUtil::updateFonts();
// Make sure the dialog closes and accepts the settings if save has been pressed
QObject::connect(&buttonBox, &QDialogButtonBox::accepted, [&]() {🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updateWeightSlider()now sets each slider to a continuous[min,max]range fromsupportedWeightArgs(), but supported weights are often sparse (for example, a font may support only a subset like 3 and 6). That lets users pick intermediate values that are unsupported;updateFontWeightNormal/Bold()then callssetWeightFromArg(...)and silently ignores failure, so the UI value can diverge from the applied font and an invalid weight can be persisted. Previously the slider operated on dense indices over only supported weights, so this regression is new.Useful? React with 👍 / 👎.