diff --git a/kcms/fonts/fonts.h b/kcms/fonts/fonts.h --- a/kcms/fonts/fonts.h +++ b/kcms/fonts/fonts.h @@ -153,6 +153,7 @@ void save() override; void defaults() override; Q_INVOKABLE void adjustAllFonts(); + Q_INVOKABLE void adjustFont(const QFont &font, const QString &category); Q_SIGNALS: void fontsHaveChanged(); diff --git a/kcms/fonts/fonts.cpp b/kcms/fonts/fonts.cpp --- a/kcms/fonts/fonts.cpp +++ b/kcms/fonts/fonts.cpp @@ -653,37 +653,91 @@ return m_fontAASettings->isDefaults(); } +// if the styleName property is empty, QFontDialog will always select +// the first style in the list; for more details see: +// https://phabricator.kde.org/D27735 and https://phabricator.kde.org/D27785 +QFont setRegularFontStyle(const QFont &font) +{ + QFont f(font); + QFontDatabase fdb; + const QStringList styles = fdb.styles(f.family()); + for (const QString &s : styles) { + if (s == QLatin1String("Regular") + || s == QLatin1String("Normal") + || s == QLatin1String("Book") + || s == QLatin1String("Roman")) { + f.setStyleName(s); + return f; + } + } + return font; +} + +void KFonts::adjustFont(const QFont &font, const QString &category) +{ + QFont f(font); + if (font.styleName().isEmpty() && font.weight() == QFont::Normal) { + f = setRegularFontStyle(font); + } + + bool ok = false; + QFont selFont = QFontDialog::getFont(&ok, f, nullptr, i18n("Select Font")); + + if (ok && !m_settings->isImmutable(category)) { + if (category == QLatin1String("font")) { + m_settings->setFont(selFont); + } else if (category == QLatin1String("menuFont")) { + m_settings->setMenuFont(selFont); + } else if (category == QLatin1String("toolBarFont")) { + m_settings->setToolBarFont(selFont); + } else if (category == QLatin1String("activeFont")) { + m_settings->setActiveFont(selFont); + } else if (category == QLatin1String("smallestReadableFont")) { + m_settings->setSmallestReadableFont(selFont); + } else if (category == QLatin1String("fixed")) { + m_settings->setFixed(selFont); + } + } +} + void KFonts::adjustAllFonts() { + QFont f(m_settings->font()); + if (f.styleName().isEmpty() && f.weight() == QFont::Normal) { + f = setRegularFontStyle(f); + } + bool ok = false; - QFont font = QFontDialog::getFont(&ok, m_settings->font(), nullptr, i18n("Select Font")); + QFont selFont = QFontDialog::getFont(&ok, f, nullptr, i18n("Select Font")); if (ok) { if (!m_settings->isImmutable("font")) { - m_settings->setFont(font); + m_settings->setFont(selFont); } if (!m_settings->isImmutable("menuFont")) { - m_settings->setMenuFont(font); + m_settings->setMenuFont(selFont); } if (!m_settings->isImmutable("toolBarFont")) { - m_settings->setToolBarFont(font); + m_settings->setToolBarFont(selFont); } if (!m_settings->isImmutable("activeFont")) { - m_settings->setActiveFont(font); + m_settings->setActiveFont(selFont); } if (!m_settings->isImmutable("smallestReadableFont")) { - m_settings->setSmallestReadableFont(font); + m_settings->setSmallestReadableFont(selFont); } - // Try to find a fixed-width/monspace font from the font family the user selected, e.g.: + // Try to find a fixed-width/monospace font from the font family the user selected, e.g.: // "Noto Sans" -> "Noto Sans Mono", "Ubuntu" -> "Ubuntu Mono" // we take the first one that comes along, e.g. "Noto Sans Mono" _not_ "Noto Sans Mono Blk" + // N.B. this a best effort kind of hack, the user can still select whatever fixed-width font + // he prefers afterwards QFontDatabase fdb; const QStringList fontFamilies = fdb.families(); QString mono; for (const QString &family : fontFamilies) { // QString::endsWith() is cheaper than contains(), so try it first - if (family.startsWith(font.family()) + if (family.startsWith(selFont.family()) && (family.endsWith(QLatin1String("Mono")) || family.contains(QLatin1String("Mono")))) { mono = family; diff --git a/kcms/fonts/package/contents/ui/FontWidget.qml b/kcms/fonts/package/contents/ui/FontWidget.qml --- a/kcms/fonts/package/contents/ui/FontWidget.qml +++ b/kcms/fonts/package/contents/ui/FontWidget.qml @@ -57,11 +57,8 @@ Kirigami.MnemonicData.enabled: false focus: true onClicked: { - fontDialog.adjustAllFonts = false; - fontDialog.currentCategory = root.category - fontDialog.font = root.font; - fontDialog.currentFont = root.font; - fontDialog.open() + fontDialog.adjustAllFonts = false + kcm.adjustFont(root.font, root.category) } QtControls.ToolTip { visible: parent.hovered diff --git a/kcms/fonts/package/contents/ui/main.qml b/kcms/fonts/package/contents/ui/main.qml --- a/kcms/fonts/package/contents/ui/main.qml +++ b/kcms/fonts/package/contents/ui/main.qml @@ -264,9 +264,9 @@ property bool adjustAllFonts: false onAccepted: { if (adjustAllFonts) { - kcm.adjustAllFonts(font); + kcm.adjustAllFonts() } else { - kcm.fontsSettings[currentCategory] = font; + kcm.adjustFont(font, currentCategory) } } }