Changeset View
Changeset View
Standalone View
Standalone View
kcms/fonts/fonts.cpp
Show First 20 Lines • Show All 647 Lines • ▼ Show 20 Line(s) | 647 | { | |||
---|---|---|---|---|---|
648 | return m_fontAASettings->needsSave(); | 648 | return m_fontAASettings->needsSave(); | ||
649 | } | 649 | } | ||
650 | 650 | | |||
651 | bool KFonts::isDefaults() const | 651 | bool KFonts::isDefaults() const | ||
652 | { | 652 | { | ||
653 | return m_fontAASettings->isDefaults(); | 653 | return m_fontAASettings->isDefaults(); | ||
654 | } | 654 | } | ||
655 | 655 | | |||
656 | // if the styleName property is empty, QFontDialog will always select | ||||
657 | // the first style in the list; for more details see: | ||||
658 | // https://phabricator.kde.org/D27735 and https://phabricator.kde.org/D27785 | ||||
659 | QFont setRegularFontStyle(const QFont &font) | ||||
660 | { | ||||
661 | QFont f(font); | ||||
662 | QFontDatabase fdb; | ||||
663 | const QStringList styles = fdb.styles(f.family()); | ||||
664 | for (const QString &s : styles) { | ||||
665 | if (s == QLatin1String("Regular") | ||||
666 | || s == QLatin1String("Normal") | ||||
667 | || s == QLatin1String("Book") | ||||
668 | || s == QLatin1String("Roman")) { | ||||
669 | f.setStyleName(s); | ||||
670 | return f; | ||||
671 | } | ||||
672 | } | ||||
673 | return font; | ||||
674 | } | ||||
675 | | ||||
676 | void KFonts::adjustFont(const QFont &font, const QString &category) | ||||
677 | { | ||||
678 | QFont f(font); | ||||
679 | if (font.styleName().isEmpty() && font.weight() == QFont::Normal) { | ||||
680 | f = setRegularFontStyle(font); | ||||
681 | } | ||||
682 | | ||||
683 | bool ok = false; | ||||
684 | QFont selFont = QFontDialog::getFont(&ok, f, nullptr, i18n("Select Font")); | ||||
685 | | ||||
686 | if (ok && !m_settings->isImmutable(category)) { | ||||
687 | if (category == QLatin1String("font")) { | ||||
688 | m_settings->setFont(selFont); | ||||
689 | } else if (category == QLatin1String("menuFont")) { | ||||
690 | m_settings->setMenuFont(selFont); | ||||
691 | } else if (category == QLatin1String("toolBarFont")) { | ||||
692 | m_settings->setToolBarFont(selFont); | ||||
693 | } else if (category == QLatin1String("activeFont")) { | ||||
694 | m_settings->setActiveFont(selFont); | ||||
695 | } else if (category == QLatin1String("smallestReadableFont")) { | ||||
696 | m_settings->setSmallestReadableFont(selFont); | ||||
697 | } else if (category == QLatin1String("fixed")) { | ||||
698 | m_settings->setFixed(selFont); | ||||
699 | } | ||||
700 | } | ||||
701 | } | ||||
702 | | ||||
656 | void KFonts::adjustAllFonts() | 703 | void KFonts::adjustAllFonts() | ||
657 | { | 704 | { | ||
705 | QFont f(m_settings->font()); | ||||
706 | if (f.styleName().isEmpty() && f.weight() == QFont::Normal) { | ||||
707 | f = setRegularFontStyle(f); | ||||
708 | } | ||||
709 | | ||||
658 | bool ok = false; | 710 | bool ok = false; | ||
659 | QFont font = QFontDialog::getFont(&ok, m_settings->font(), nullptr, i18n("Select Font")); | 711 | QFont selFont = QFontDialog::getFont(&ok, f, nullptr, i18n("Select Font")); | ||
660 | 712 | | |||
661 | if (ok) { | 713 | if (ok) { | ||
662 | if (!m_settings->isImmutable("font")) { | 714 | if (!m_settings->isImmutable("font")) { | ||
663 | m_settings->setFont(font); | 715 | m_settings->setFont(selFont); | ||
664 | } | 716 | } | ||
665 | if (!m_settings->isImmutable("menuFont")) { | 717 | if (!m_settings->isImmutable("menuFont")) { | ||
666 | m_settings->setMenuFont(font); | 718 | m_settings->setMenuFont(selFont); | ||
667 | } | 719 | } | ||
668 | if (!m_settings->isImmutable("toolBarFont")) { | 720 | if (!m_settings->isImmutable("toolBarFont")) { | ||
669 | m_settings->setToolBarFont(font); | 721 | m_settings->setToolBarFont(selFont); | ||
670 | } | 722 | } | ||
671 | if (!m_settings->isImmutable("activeFont")) { | 723 | if (!m_settings->isImmutable("activeFont")) { | ||
672 | m_settings->setActiveFont(font); | 724 | m_settings->setActiveFont(selFont); | ||
673 | } | 725 | } | ||
674 | if (!m_settings->isImmutable("smallestReadableFont")) { | 726 | if (!m_settings->isImmutable("smallestReadableFont")) { | ||
675 | m_settings->setSmallestReadableFont(font); | 727 | m_settings->setSmallestReadableFont(selFont); | ||
676 | } | 728 | } | ||
677 | 729 | | |||
678 | // Try to find a fixed-width/monspace font from the font family the user selected, e.g.: | 730 | // Try to find a fixed-width/monospace font from the font family the user selected, e.g.: | ||
679 | // "Noto Sans" -> "Noto Sans Mono", "Ubuntu" -> "Ubuntu Mono" | 731 | // "Noto Sans" -> "Noto Sans Mono", "Ubuntu" -> "Ubuntu Mono" | ||
680 | // we take the first one that comes along, e.g. "Noto Sans Mono" _not_ "Noto Sans Mono Blk" | 732 | // we take the first one that comes along, e.g. "Noto Sans Mono" _not_ "Noto Sans Mono Blk" | ||
733 | // N.B. this a best effort kind of hack, the user can still select whatever fixed-width font | ||||
734 | // he prefers afterwards | ||||
681 | QFontDatabase fdb; | 735 | QFontDatabase fdb; | ||
682 | const QStringList fontFamilies = fdb.families(); | 736 | const QStringList fontFamilies = fdb.families(); | ||
683 | QString mono; | 737 | QString mono; | ||
684 | for (const QString &family : fontFamilies) { | 738 | for (const QString &family : fontFamilies) { | ||
685 | // QString::endsWith() is cheaper than contains(), so try it first | 739 | // QString::endsWith() is cheaper than contains(), so try it first | ||
686 | if (family.startsWith(font.family()) | 740 | if (family.startsWith(selFont.family()) | ||
687 | && (family.endsWith(QLatin1String("Mono")) | 741 | && (family.endsWith(QLatin1String("Mono")) | ||
688 | || family.contains(QLatin1String("Mono")))) { | 742 | || family.contains(QLatin1String("Mono")))) { | ||
689 | mono = family; | 743 | mono = family; | ||
690 | break; | 744 | break; | ||
691 | } | 745 | } | ||
692 | } | 746 | } | ||
693 | // fallback if we find nothing | 747 | // fallback if we find nothing | ||
694 | QFont f = mono.isEmpty() ? QFontDatabase::systemFont(QFontDatabase::FixedFont) : QFont(mono); | 748 | QFont f = mono.isEmpty() ? QFontDatabase::systemFont(QFontDatabase::FixedFont) : QFont(mono); | ||
695 | if (!m_settings->isImmutable("fixed")) { | 749 | if (!m_settings->isImmutable("fixed")) { | ||
696 | m_settings->setFixed(f); | 750 | m_settings->setFixed(f); | ||
697 | } | 751 | } | ||
698 | } | 752 | } | ||
699 | } | 753 | } | ||
700 | 754 | | |||
701 | #include "fonts.moc" | 755 | #include "fonts.moc" |