Changeset View
Changeset View
Standalone View
Standalone View
kcms/fonts/fonts.cpp
Show All 36 Lines | |||||
37 | 37 | | |||
38 | #include <KAcceleratorManager> | 38 | #include <KAcceleratorManager> | ||
39 | #include <KGlobalSettings> | 39 | #include <KGlobalSettings> | ||
40 | #include <KConfigGroup> | 40 | #include <KConfigGroup> | ||
41 | #include <KConfig> | 41 | #include <KConfig> | ||
42 | #include <KAboutData> | 42 | #include <KAboutData> | ||
43 | #include <KLocalizedString> | 43 | #include <KLocalizedString> | ||
44 | #include <KPluginFactory> | 44 | #include <KPluginFactory> | ||
45 | #include <KFontDialog> | | |||
46 | #include <KWindowSystem> | 45 | #include <KWindowSystem> | ||
47 | 46 | | |||
48 | #include "../krdb/krdb.h" | 47 | #include "../krdb/krdb.h" | ||
49 | #include "previewimageprovider.h" | 48 | #include "previewimageprovider.h" | ||
50 | 49 | | |||
51 | #include "fontssettings.h" | 50 | #include "fontssettings.h" | ||
52 | 51 | | |||
53 | /**** DLL Interface ****/ | 52 | /**** DLL Interface ****/ | ||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Line(s) | 84 | if (!dbase.isSmoothlyScalable(family, style)) { | |||
98 | } | 97 | } | ||
99 | } | 98 | } | ||
100 | 99 | | |||
101 | // Select the font with confirmed properties. | 100 | // Select the font with confirmed properties. | ||
102 | QFont result = dbase.font(family, style, int(size)); | 101 | QFont result = dbase.font(family, style, int(size)); | ||
103 | if (dbase.isSmoothlyScalable(family, style) && result.pointSize() == floor(size)) { | 102 | if (dbase.isSmoothlyScalable(family, style) && result.pointSize() == floor(size)) { | ||
104 | result.setPointSizeF(size); | 103 | result.setPointSizeF(size); | ||
105 | } | 104 | } | ||
106 | return result; | 105 | | ||
106 | bool sameFont = font.toString() == result.toString(); | ||||
107 | // A "Regular" font will have an empty styleName e.g. "DejaVu Sans,12,-1,5,50,0,0,0,0,0" | ||||
108 | // so that setBold(true) can work (we deliberately clear the styleName for "Regular"-like | ||||
109 | // font styles when saving via KConfig, see: writeEntryGui() from kconfiggroupgui.cpp). | ||||
110 | // Here in this method we could end up with a "result" font that's using a Regular style | ||||
111 | // and that has the styleName set, so the the original "font" param. and "result" are | ||||
112 | // actually the same font | ||||
113 | if (!sameFont) { | ||||
114 | if (font.weight() == QFont::Normal && font.styleName().isEmpty() | ||||
bport: You can use && to have only one if there | |||||
ahmadsamir: OK. | |||||
115 | && result.weight() == QFont::Normal | ||||
116 | && (result.styleName() == QLatin1String("Regular") | ||||
117 | || result.styleName() == QLatin1String("Normal") | ||||
118 | || result.styleName() == QLatin1String("Book") | ||||
119 | || result.styleName() == QLatin1String("Roman"))) { | ||||
120 | result.setStyleName(QString()); | ||||
121 | sameFont = font.toString() == result.toString(); | ||||
122 | } | ||||
123 | } | ||||
124 | | ||||
125 | return sameFont ? font : result; | ||||
107 | } | 126 | } | ||
108 | 127 | | |||
109 | /**** FontAASettings ****/ | 128 | /**** FontAASettings ****/ | ||
110 | #if defined(HAVE_FONTCONFIG) && HAVE_X11 | 129 | #if defined(HAVE_FONTCONFIG) && HAVE_X11 | ||
111 | FontAASettings::FontAASettings(QObject *parent) | 130 | FontAASettings::FontAASettings(QObject *parent) | ||
112 | : QObject(parent) | 131 | : QObject(parent) | ||
113 | , m_state() | 132 | , m_state() | ||
114 | , m_originalState() | 133 | , m_originalState() | ||
▲ Show 20 Lines • Show All 429 Lines • ▼ Show 20 Line(s) | |||||
544 | void KFonts::defaults() | 563 | void KFonts::defaults() | ||
545 | { | 564 | { | ||
546 | ManagedConfigModule::defaults(); | 565 | ManagedConfigModule::defaults(); | ||
547 | m_fontAASettings->defaults(); | 566 | m_fontAASettings->defaults(); | ||
548 | } | 567 | } | ||
549 | 568 | | |||
550 | void KFonts::setNearestExistingFonts() | 569 | void KFonts::setNearestExistingFonts() | ||
551 | { | 570 | { | ||
552 | m_settings->setFont(nearestExistingFont(m_settings->font())); | 571 | const QFont font = nearestExistingFont(m_settings->font()); | ||
Those change will compare font twice, here and on setters so I will keep old code there bport: Those change will compare font twice, here and on setters so I will keep old code there | |||||
Indeed; looking at the code generated by kconfig compiler in <builddir>/kcms/fonts/fontsettings.h: /** Set General font */ void setFont( const QFont & v ) { if (v != mFont && !isFontImmutable()) { mFont = v; Q_EMIT fontChanged(); } } I will revert. And another point is that the code in fonts.cpp doesn't need to check for immutability since it's checked by the setters. ahmadsamir: Indeed; looking at the code generated by kconfig compiler in <builddir>/kcms/fonts/fontsettings. | |||||
553 | m_settings->setFixed(nearestExistingFont(m_settings->fixed())); | 572 | if (m_settings->font() != font) { | ||
554 | m_settings->setSmallestReadableFont(nearestExistingFont(m_settings->smallestReadableFont())); | 573 | m_settings->setFont(font); | ||
555 | m_settings->setToolBarFont(nearestExistingFont(m_settings->toolBarFont())); | 574 | } | ||
556 | m_settings->setMenuFont(nearestExistingFont(m_settings->menuFont())); | 575 | | ||
557 | m_settings->setActiveFont(nearestExistingFont(m_settings->activeFont())); | 576 | const QFont fixed = nearestExistingFont(m_settings->fixed()); | ||
577 | if (m_settings->fixed() != fixed) { | ||||
578 | m_settings->setFixed(fixed); | ||||
579 | } | ||||
580 | | ||||
581 | const QFont smallest = nearestExistingFont(m_settings->smallestReadableFont()); | ||||
582 | if (m_settings->smallestReadableFont() != smallest) { | ||||
583 | m_settings->setSmallestReadableFont(smallest); | ||||
584 | } | ||||
585 | | ||||
586 | const QFont toolBar = nearestExistingFont(m_settings->toolBarFont()); | ||||
587 | if (m_settings->toolBarFont() != toolBar) { | ||||
588 | m_settings->setToolBarFont(toolBar); | ||||
589 | } | ||||
590 | | ||||
591 | const QFont menu = nearestExistingFont(m_settings->menuFont()); | ||||
592 | if (m_settings->menuFont() != menu) { | ||||
593 | m_settings->setMenuFont(menu); | ||||
594 | } | ||||
595 | | ||||
596 | const QFont active = nearestExistingFont(m_settings->activeFont()); | ||||
597 | if (m_settings->activeFont() != active) { | ||||
598 | m_settings->setActiveFont(active); | ||||
599 | } | ||||
558 | } | 600 | } | ||
559 | 601 | | |||
560 | void KFonts::load() | 602 | void KFonts::load() | ||
561 | { | 603 | { | ||
562 | // first load all the settings | 604 | // first load all the settings | ||
563 | ManagedConfigModule::load(); | 605 | ManagedConfigModule::load(); | ||
564 | m_fontAASettings->load(); | 606 | m_fontAASettings->load(); | ||
565 | 607 | | |||
566 | // Then set the existing fonts based on those settings | 608 | // Then set the existing fonts based on those settings | ||
567 | setNearestExistingFonts(); | 609 | setNearestExistingFonts(); | ||
568 | 610 | | |||
569 | // Load preview | 611 | // Load preview | ||
570 | // NOTE: This needs to be done AFTER AA settings is loaded | 612 | // NOTE: This needs to be done AFTER AA settings is loaded | ||
571 | // otherwise AA settings will be reset in process of loading | 613 | // otherwise AA settings will be reset in process of loading | ||
572 | // previews | 614 | // previews | ||
573 | engine()->addImageProvider("preview", new PreviewImageProvider(m_settings->font())); | 615 | engine()->addImageProvider("preview", new PreviewImageProvider(m_settings->font())); | ||
574 | | ||||
575 | // KCM expect save state to be false at this point (can be true because of setNearestExistingFonts | | |||
576 | setNeedsSave(false); | | |||
I will keep that until we have a proper tested fix for https://phabricator.kde.org/D27452 bport: I will keep that until we have a proper tested fix for https://phabricator.kde.org/D27452
can… | |||||
ahmadsamir: OK, fair point, I will revert that bit. | |||||
577 | } | 616 | } | ||
578 | 617 | | |||
579 | void KFonts::save() | 618 | void KFonts::save() | ||
580 | { | 619 | { | ||
581 | ManagedConfigModule::save(); | 620 | ManagedConfigModule::save(); | ||
582 | 621 | | |||
583 | KConfig _cfgfonts("kcmfonts"); | 622 | KConfig _cfgfonts("kcmfonts"); | ||
584 | KConfigGroup cfgfonts(&_cfgfonts, "General"); | 623 | KConfigGroup cfgfonts(&_cfgfonts, "General"); | ||
Show All 26 Lines | |||||
611 | 650 | | |||
612 | bool KFonts::isDefaults() const | 651 | bool KFonts::isDefaults() const | ||
613 | { | 652 | { | ||
614 | return m_fontAASettings->isDefaults(); | 653 | return m_fontAASettings->isDefaults(); | ||
615 | } | 654 | } | ||
616 | 655 | | |||
617 | void KFonts::adjustAllFonts() | 656 | void KFonts::adjustAllFonts() | ||
618 | { | 657 | { | ||
619 | QFont font = m_settings->font(); | 658 | bool ok = false; | ||
620 | KFontChooser::FontDiffFlags fontDiffFlags; | 659 | QFont font = QFontDialog::getFont(&ok, m_settings->font(), nullptr, i18n("Select Font")); | ||
621 | int ret = KFontDialog::getFontDiff(font, fontDiffFlags, KFontChooser::NoDisplayFlags); | | |||
622 | 660 | | |||
623 | if (ret == KDialog::Accepted && fontDiffFlags) { | 661 | if (ok) { | ||
624 | if (!m_settings->isImmutable("font")) { | 662 | if (!m_settings->isImmutable("font")) { | ||
625 | m_settings->setFont(applyFontDiff(m_settings->font(), font, fontDiffFlags)); | 663 | m_settings->setFont(font); | ||
626 | } | 664 | } | ||
627 | if (!m_settings->isImmutable("menuFont")) { | 665 | if (!m_settings->isImmutable("menuFont")) { | ||
628 | m_settings->setMenuFont(applyFontDiff(m_settings->menuFont(), font, fontDiffFlags)); | 666 | m_settings->setMenuFont(font); | ||
629 | } | 667 | } | ||
630 | if (!m_settings->isImmutable("toolBarFont")) { | 668 | if (!m_settings->isImmutable("toolBarFont")) { | ||
631 | m_settings->setToolBarFont(applyFontDiff(m_settings->toolBarFont(), font, fontDiffFlags)); | 669 | m_settings->setToolBarFont(font); | ||
632 | } | 670 | } | ||
633 | if (!m_settings->isImmutable("activeFont")) { | 671 | if (!m_settings->isImmutable("activeFont")) { | ||
634 | m_settings->setActiveFont(applyFontDiff(m_settings->activeFont(), font, fontDiffFlags)); | 672 | m_settings->setActiveFont(font); | ||
635 | } | 673 | } | ||
636 | if (!m_settings->isImmutable("smallestReadableFont")) { | 674 | if (!m_settings->isImmutable("smallestReadableFont")) { | ||
637 | m_settings->setSmallestReadableFont(applyFontDiff(m_settings->smallestReadableFont(), font, fontDiffFlags)); | 675 | m_settings->setSmallestReadableFont(font); | ||
638 | } | | |||
639 | const QFont adjustedFont = applyFontDiff(m_settings->fixed(), font, fontDiffFlags); | | |||
640 | if (QFontInfo(adjustedFont).fixedPitch() && !m_settings->isImmutable("fixed")) { | | |||
641 | m_settings->setFixed(adjustedFont); | | |||
642 | } | | |||
643 | } | 676 | } | ||
644 | } | | |||
645 | | ||||
646 | QFont KFonts::applyFontDiff(const QFont &fnt, const QFont &newFont, int fontDiffFlags) | | |||
647 | { | | |||
648 | QFont font(fnt); | | |||
649 | 677 | | |||
650 | if (fontDiffFlags & KFontChooser::FontDiffSize) { | 678 | // Try to find a fixed-width/monspace font from the font family the user selected, e.g.: | ||
651 | font.setPointSizeF(newFont.pointSizeF()); | 679 | // "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" | ||||
681 | QFontDatabase fdb; | ||||
682 | const QStringList fontFamilies = fdb.families(); | ||||
683 | QString mono; | ||||
684 | for (const QString &family : fontFamilies) { | ||||
685 | // QString::endsWith() is cheaper than contains(), so try it first | ||||
686 | if (family.startsWith(font.family()) | ||||
687 | && (family.endsWith(QLatin1String("Mono")) | ||||
688 | || family.contains(QLatin1String("Mono")))) { | ||||
689 | mono = family; | ||||
690 | break; | ||||
691 | } | ||||
692 | } | ||||
693 | // fallback if we find nothing | ||||
694 | QFont f = mono.isEmpty() ? QFontDatabase::systemFont(QFontDatabase::FixedFont) : QFont(mono); | ||||
695 | if (!m_settings->isImmutable("fixed")) { | ||||
696 | m_settings->setFixed(f); | ||||
652 | } | 697 | } | ||
653 | if ((fontDiffFlags & KFontChooser::FontDiffFamily)) { | | |||
654 | font.setFamily(newFont.family()); | | |||
655 | } | 698 | } | ||
656 | if (fontDiffFlags & KFontChooser::FontDiffStyle) { | | |||
657 | font.setWeight(newFont.weight()); | | |||
658 | font.setStyle(newFont.style()); | | |||
659 | font.setUnderline(newFont.underline()); | | |||
660 | font.setStyleName(newFont.styleName()); | | |||
661 | } | | |||
662 | | ||||
663 | return font; | | |||
664 | } | 699 | } | ||
665 | 700 | | |||
666 | #include "fonts.moc" | 701 | #include "fonts.moc" | ||
667 | |
You can use && to have only one if there