Changeset View
Changeset View
Standalone View
Standalone View
kcms/fonts/fonts.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | Copyright 1997 Mark Donohoe | 2 | Copyright 1997 Mark Donohoe | ||
3 | Copyright 1999 Lars Knoll | 3 | Copyright 1999 Lars Knoll | ||
4 | Copyright 2000 Rik Hemsley | 4 | Copyright 2000 Rik Hemsley | ||
5 | Copyright 2015 Antonis Tsiapaliokas <antonis.tsiapaliokas@kde.org> | 5 | Copyright 2015 Antonis Tsiapaliokas <antonis.tsiapaliokas@kde.org> | ||
6 | Copyright 2017 Marco Martin <mart@kde.org> | 6 | Copyright 2017 Marco Martin <mart@kde.org> | ||
7 | Copyright 2019 Benjamin Port <benjamin.port@enioka.com> | ||||
7 | 8 | | |||
8 | Ported to kcontrol2 by Geert Jansen. | 9 | Ported to kcontrol2 by Geert Jansen. | ||
9 | 10 | | |||
10 | This library is free software; you can redistribute it and/or | 11 | This library is free software; you can redistribute it and/or | ||
11 | modify it under the terms of the GNU Library General Public | 12 | modify it under the terms of the GNU Library General Public | ||
12 | License as published by the Free Software Foundation; either | 13 | License as published by the Free Software Foundation; either | ||
13 | version 2 of the License, or (at your option) any later version. | 14 | version 2 of the License, or (at your option) any later version. | ||
14 | 15 | | |||
Show All 29 Lines | |||||
44 | #include <KPluginFactory> | 45 | #include <KPluginFactory> | ||
45 | #include <KFontDialog> | 46 | #include <KFontDialog> | ||
46 | #include <KWindowSystem> | 47 | #include <KWindowSystem> | ||
47 | #include <KMessageBox> | 48 | #include <KMessageBox> | ||
48 | 49 | | |||
49 | #include "../krdb/krdb.h" | 50 | #include "../krdb/krdb.h" | ||
50 | #include "previewimageprovider.h" | 51 | #include "previewimageprovider.h" | ||
51 | 52 | | |||
53 | #include "fontssettings.h" | ||||
54 | | ||||
52 | /**** DLL Interface ****/ | 55 | /**** DLL Interface ****/ | ||
53 | K_PLUGIN_FACTORY_WITH_JSON(KFontsFactory, "kcm_fonts.json", registerPlugin<KFonts>();) | 56 | K_PLUGIN_FACTORY_WITH_JSON(KFontsFactory, "kcm_fonts.json", registerPlugin<KFonts>();) | ||
54 | 57 | | |||
55 | //from KFontRequester | 58 | //from KFontRequester | ||
56 | // Determine if the font with given properties is available on the system, | 59 | // Determine if the font with given properties is available on the system, | ||
57 | // otherwise find and return the best fitting combination. | 60 | // otherwise find and return the best fitting combination. | ||
58 | static QFont nearestExistingFont(const QFont &font) | 61 | static QFont nearestExistingFont(const QFont &font) | ||
59 | { | 62 | { | ||
▲ Show 20 Lines • Show All 402 Lines • ▼ Show 20 Line(s) | 464 | { | |||
462 | return m_state != m_originalState; | 465 | return m_state != m_originalState; | ||
463 | } | 466 | } | ||
464 | 467 | | |||
465 | 468 | | |||
466 | /**** KFonts ****/ | 469 | /**** KFonts ****/ | ||
467 | 470 | | |||
468 | KFonts::KFonts(QObject *parent, const QVariantList &args) | 471 | KFonts::KFonts(QObject *parent, const QVariantList &args) | ||
469 | : KQuickAddons::ConfigModule(parent, args) | 472 | : KQuickAddons::ConfigModule(parent, args) | ||
473 | , m_settings(new FontsSettings(this)) | ||||
470 | , m_fontAASettings(new FontAASettings(this)) | 474 | , m_fontAASettings(new FontAASettings(this)) | ||
471 | { | 475 | { | ||
472 | KAboutData* about = new KAboutData("kcm_fonts", i18n("Fonts"), | 476 | KAboutData* about = new KAboutData("kcm_fonts", i18n("Fonts"), | ||
473 | "0.1", QString(), KAboutLicense::LGPL); | 477 | "0.1", QString(), KAboutLicense::LGPL); | ||
474 | about->addAuthor(i18n("Antonis Tsiapaliokas"), QString(), "antonis.tsiapaliokas@kde.org"); | 478 | about->addAuthor(i18n("Antonis Tsiapaliokas"), QString(), "antonis.tsiapaliokas@kde.org"); | ||
475 | setAboutData(about); | 479 | setAboutData(about); | ||
476 | qmlRegisterType<QStandardItemModel>(); | 480 | qmlRegisterType<QStandardItemModel>(); | ||
481 | qmlRegisterType<FontsSettings>(); | ||||
482 | | ||||
477 | setButtons(Apply | Default | Help); | 483 | setButtons(Apply | Default | Help); | ||
478 | 484 | | |||
479 | auto updateState = [this]() { | 485 | auto updateState = [this]() { | ||
480 | setNeedsSave(m_fontAASettings->needsSave()); | 486 | setNeedsSave(m_fontAASettings->needsSave()); | ||
481 | }; | 487 | }; | ||
482 | 488 | | |||
489 | connect(m_settings, &FontsSettings::configChanged, this, &KFonts::updateNeedsSave); | ||||
490 | connect(m_settings, &FontsSettings::fontChanged, this, &KFonts::updateNeedsSave); | ||||
491 | connect(m_settings, &FontsSettings::fixedChanged, this, &KFonts::updateNeedsSave); | ||||
492 | connect(m_settings, &FontsSettings::smallestReadableFont, this, &KFonts::updateNeedsSave); | ||||
493 | connect(m_settings, &FontsSettings::toolBarFont, this, &KFonts::updateNeedsSave); | ||||
494 | connect(m_settings, &FontsSettings::menuFont, this, &KFonts::updateNeedsSave); | ||||
495 | connect(m_settings, &FontsSettings::activeFont, this, &KFonts::updateNeedsSave); | ||||
496 | | ||||
483 | connect(m_fontAASettings, &FontAASettings::subPixelCurrentIndexChanged, this, updateState); | 497 | connect(m_fontAASettings, &FontAASettings::subPixelCurrentIndexChanged, this, updateState); | ||
484 | connect(m_fontAASettings, &FontAASettings::hintingCurrentIndexChanged, this, updateState); | 498 | connect(m_fontAASettings, &FontAASettings::hintingCurrentIndexChanged, this, updateState); | ||
485 | connect(m_fontAASettings, &FontAASettings::excludeChanged, this, updateState); | 499 | connect(m_fontAASettings, &FontAASettings::excludeChanged, this, updateState); | ||
486 | connect(m_fontAASettings, &FontAASettings::excludeFromChanged, this, updateState); | 500 | connect(m_fontAASettings, &FontAASettings::excludeFromChanged, this, updateState); | ||
487 | connect(m_fontAASettings, &FontAASettings::excludeToChanged, this, updateState); | 501 | connect(m_fontAASettings, &FontAASettings::excludeToChanged, this, updateState); | ||
488 | connect(m_fontAASettings, &FontAASettings::antiAliasingChanged, this, updateState); | 502 | connect(m_fontAASettings, &FontAASettings::antiAliasingChanged, this, updateState); | ||
489 | connect(m_fontAASettings, &FontAASettings::aliasingChanged, this, updateState); | 503 | connect(m_fontAASettings, &FontAASettings::aliasingChanged, this, updateState); | ||
490 | connect(m_fontAASettings, &FontAASettings::dpiChanged, this, updateState); | 504 | connect(m_fontAASettings, &FontAASettings::dpiChanged, this, updateState); | ||
491 | } | 505 | } | ||
492 | 506 | | |||
493 | KFonts::~KFonts() | 507 | KFonts::~KFonts() | ||
494 | { | 508 | { | ||
495 | } | 509 | } | ||
496 | 510 | | |||
497 | void KFonts::defaults() | 511 | FontsSettings *KFonts::fontsSettings() const | ||
498 | { | 512 | { | ||
499 | #ifdef Q_OS_MAC | 513 | return m_settings; | ||
500 | setGeneralFont(QFont("Lucida Grande", 13)); | 514 | } | ||
501 | setMenuFont(QFont("Lucida Grande", 13)); | | |||
502 | setFixedWidthFont(QFont("Monaco", 10)); | | |||
503 | setToolbarFont(QFont("Lucida Grande", 11)); | | |||
504 | setSmallFont(QFont("Lucida Grande", 9)); | | |||
505 | setWindowTitleFont(QFont("Lucida Grande", 14)); | | |||
506 | #else | | |||
507 | setGeneralFont(QFont("Noto Sans", 10)); | | |||
508 | setMenuFont(QFont("Noto Sans", 10)); | | |||
509 | setFixedWidthFont(QFont("Hack", 9)); | | |||
510 | setToolbarFont(QFont("Noto Sans", 10)); | | |||
511 | setSmallFont(QFont("Noto Sans", 8)); | | |||
512 | setWindowTitleFont(QFont("Noto Sans", 10)); | | |||
513 | #endif | | |||
514 | 515 | | |||
516 | void KFonts::defaults() | ||||
517 | { | ||||
518 | m_settings->setDefaults(); | ||||
515 | m_fontAASettings->defaults(); | 519 | m_fontAASettings->defaults(); | ||
516 | } | 520 | } | ||
517 | 521 | | |||
518 | void KFonts::load() | 522 | void KFonts::setNearestExistingFonts() | ||
519 | { | 523 | { | ||
520 | KSharedConfig::Ptr config = KSharedConfig::openConfig("kdeglobals"); | 524 | m_settings->setFont(nearestExistingFont(m_settings->font())); | ||
521 | 525 | m_settings->setFixed(nearestExistingFont(m_settings->fixed())); | |||
522 | KConfigGroup cg(config, "General"); | 526 | m_settings->setSmallestReadableFont(nearestExistingFont(m_settings->smallestReadableFont())); | ||
523 | m_generalFont = m_generalFontOriginal = nearestExistingFont(cg.readEntry("font", m_defaultFont)); | 527 | m_settings->setToolBarFont(nearestExistingFont(m_settings->toolBarFont())); | ||
524 | 528 | m_settings->setMenuFont(nearestExistingFont(m_settings->menuFont())); | |||
525 | 529 | m_settings->setActiveFont(nearestExistingFont(m_settings->activeFont())); | |||
526 | m_fixedWidthFont = m_fixedWidthFontOriginal = nearestExistingFont(cg.readEntry("fixed", QFont("Hack", 9))); | 530 | } | ||
527 | | ||||
528 | m_smallFont = m_smallFontOriginal = nearestExistingFont(cg.readEntry("smallestReadableFont", m_defaultFont)); | | |||
529 | | ||||
530 | m_toolbarFont = m_toolbarFontOriginal = nearestExistingFont(cg.readEntry("toolBarFont", m_defaultFont)); | | |||
531 | | ||||
532 | m_menuFont = m_menuFontOriginal = nearestExistingFont(cg.readEntry("menuFont", m_defaultFont)); | | |||
533 | | ||||
534 | cg = KConfigGroup(config, "WM"); | | |||
535 | m_windowTitleFont = m_windowTitleFontOriginal = nearestExistingFont(cg.readEntry("activeFont", m_defaultFont)); | | |||
536 | | ||||
537 | engine()->addImageProvider("preview", new PreviewImageProvider(generalFont())); | | |||
538 | 531 | | |||
539 | emit generalFontChanged(); | 532 | void KFonts::load() | ||
540 | emit fixedWidthFontChanged(); | 533 | { | ||
541 | emit smallFontChanged(); | 534 | m_settings->load(); | ||
542 | emit toolbarFontChanged(); | 535 | setNearestExistingFonts(); | ||
543 | emit menuFontChanged(); | 536 | engine()->addImageProvider("preview", new PreviewImageProvider(m_settings->font())); | ||
544 | emit windowTitleFontChanged(); | | |||
545 | 537 | | |||
546 | m_fontAASettings->load(); | 538 | m_fontAASettings->load(); | ||
547 | setNeedsSave(false); | 539 | setNeedsSave(false); | ||
548 | } | 540 | } | ||
549 | 541 | | |||
550 | void KFonts::save() | 542 | void KFonts::save() | ||
551 | { | 543 | { | ||
552 | KSharedConfig::Ptr config = KSharedConfig::openConfig("kdeglobals"); | 544 | m_settings->save(); | ||
553 | | ||||
554 | KConfigGroup cg(config, "General"); | | |||
555 | cg.writeEntry("font", m_generalFont.toString()); | | |||
556 | cg.writeEntry("fixed", m_fixedWidthFont.toString()); | | |||
557 | cg.writeEntry("smallestReadableFont", m_smallFont.toString()); | | |||
558 | cg.writeEntry("toolBarFont", m_toolbarFont.toString()); | | |||
559 | cg.writeEntry("menuFont", m_menuFont.toString()); | | |||
560 | cg.sync(); | | |||
561 | cg = KConfigGroup(config, "WM"); | | |||
562 | cg.writeEntry("activeFont", m_windowTitleFont.toString()); | | |||
563 | cg.sync(); | | |||
564 | | ||||
565 | m_defaultFontOriginal = m_defaultFont; | | |||
566 | m_generalFontOriginal = m_generalFont; | | |||
567 | m_fixedWidthFontOriginal = m_fixedWidthFont; | | |||
568 | m_smallFontOriginal = m_smallFont; | | |||
569 | m_toolbarFontOriginal = m_toolbarFont; | | |||
570 | m_menuFontOriginal = m_menuFont; | | |||
571 | m_windowTitleFontOriginal = m_windowTitleFont; | | |||
572 | 545 | | |||
573 | KConfig _cfgfonts("kcmfonts"); | 546 | KConfig _cfgfonts("kcmfonts"); | ||
574 | KConfigGroup cfgfonts(&_cfgfonts, "General"); | 547 | KConfigGroup cfgfonts(&_cfgfonts, "General"); | ||
575 | 548 | | |||
576 | FontAASettings::AASetting aaSetting = FontAASettings::AASystem; | 549 | FontAASettings::AASetting aaSetting = FontAASettings::AASystem; | ||
577 | if (m_fontAASettings->antiAliasingNeedsSave()) { | 550 | if (m_fontAASettings->antiAliasingNeedsSave()) { | ||
578 | aaSetting = m_fontAASettings->antiAliasing() ? FontAASettings::AAEnabled : FontAASettings::AADisabled; | 551 | aaSetting = m_fontAASettings->antiAliasing() ? FontAASettings::AAEnabled : FontAASettings::AADisabled; | ||
579 | } | 552 | } | ||
Show All 12 Lines | |||||
592 | runRdb(KRdbExportXftSettings | KRdbExportGtkTheme); | 565 | runRdb(KRdbExportXftSettings | KRdbExportGtkTheme); | ||
593 | 566 | | |||
594 | emit fontsHaveChanged(); | 567 | emit fontsHaveChanged(); | ||
595 | setNeedsSave(false); | 568 | setNeedsSave(false); | ||
596 | } | 569 | } | ||
597 | 570 | | |||
598 | void KFonts::updateNeedsSave() | 571 | void KFonts::updateNeedsSave() | ||
599 | { | 572 | { | ||
600 | setNeedsSave(m_defaultFontOriginal != m_defaultFont || | 573 | setNeedsSave(m_settings->isSaveNeeded() || m_fontAASettings->needsSave()); | ||
601 | m_generalFontOriginal != m_generalFont || | | |||
602 | m_fixedWidthFontOriginal != m_fixedWidthFont || | | |||
603 | m_smallFontOriginal != m_smallFont || | | |||
604 | m_toolbarFontOriginal != m_toolbarFont || | | |||
605 | m_menuFontOriginal != m_menuFont || | | |||
606 | m_windowTitleFontOriginal != m_windowTitleFont || | | |||
607 | m_fontAASettings->needsSave()); | | |||
608 | } | | |||
609 | | ||||
610 | void KFonts::setGeneralFont(const QFont &font) | | |||
611 | { | | |||
612 | if (m_generalFont == font) { | | |||
613 | return; | | |||
614 | } | | |||
615 | | ||||
616 | m_generalFont = font; | | |||
617 | emit generalFontChanged(); | | |||
618 | updateNeedsSave(); | | |||
619 | } | | |||
620 | | ||||
621 | QFont KFonts::generalFont() const | | |||
622 | { | | |||
623 | return m_generalFont; | | |||
624 | } | | |||
625 | | ||||
626 | void KFonts::setFixedWidthFont(const QFont &font) | | |||
627 | { | | |||
628 | if (m_fixedWidthFont == font) { | | |||
629 | return; | | |||
630 | } | | |||
631 | | ||||
632 | m_fixedWidthFont = font; | | |||
633 | emit fixedWidthFontChanged(); | | |||
634 | updateNeedsSave(); | | |||
635 | } | | |||
636 | | ||||
637 | QFont KFonts::fixedWidthFont() const | | |||
638 | { | | |||
639 | return m_fixedWidthFont; | | |||
640 | } | | |||
641 | | ||||
642 | void KFonts::setSmallFont(const QFont &font) | | |||
643 | { | | |||
644 | if (m_smallFont == font) { | | |||
645 | return; | | |||
646 | } | | |||
647 | | ||||
648 | m_smallFont = font; | | |||
649 | emit smallFontChanged(); | | |||
650 | updateNeedsSave(); | | |||
651 | } | | |||
652 | | ||||
653 | QFont KFonts::smallFont() const | | |||
654 | { | | |||
655 | return m_smallFont; | | |||
656 | } | | |||
657 | | ||||
658 | void KFonts::setToolbarFont(const QFont &font) | | |||
659 | { | | |||
660 | if (m_toolbarFont == font) { | | |||
661 | return; | | |||
662 | } | | |||
663 | | ||||
664 | m_toolbarFont = font; | | |||
665 | emit toolbarFontChanged(); | | |||
666 | updateNeedsSave(); | | |||
667 | } | | |||
668 | | ||||
669 | QFont KFonts::toolbarFont() const | | |||
670 | { | | |||
671 | return m_toolbarFont; | | |||
672 | } | | |||
673 | | ||||
674 | void KFonts::setMenuFont(const QFont &font) | | |||
675 | { | | |||
676 | if (m_menuFont == font) { | | |||
677 | return; | | |||
678 | } | | |||
679 | | ||||
680 | m_menuFont = font; | | |||
681 | emit menuFontChanged(); | | |||
682 | updateNeedsSave(); | | |||
683 | } | | |||
684 | | ||||
685 | QFont KFonts::menuFont() const | | |||
686 | { | | |||
687 | return m_menuFont; | | |||
688 | } | | |||
689 | | ||||
690 | void KFonts::setWindowTitleFont(const QFont &font) | | |||
691 | { | | |||
692 | if (m_windowTitleFont == font) { | | |||
693 | return; | | |||
694 | } | | |||
695 | | ||||
696 | m_windowTitleFont = font; | | |||
697 | emit windowTitleFontChanged(); | | |||
698 | updateNeedsSave(); | | |||
699 | } | | |||
700 | | ||||
701 | QFont KFonts::windowTitleFont() const | | |||
702 | { | | |||
703 | return m_windowTitleFont; | | |||
704 | } | 574 | } | ||
705 | 575 | | |||
706 | void KFonts::adjustAllFonts() | 576 | void KFonts::adjustAllFonts() | ||
707 | { | 577 | { | ||
708 | QFont font = m_generalFont; | 578 | QFont font = m_settings->font(); | ||
709 | KFontChooser::FontDiffFlags fontDiffFlags; | 579 | KFontChooser::FontDiffFlags fontDiffFlags; | ||
710 | int ret = KFontDialog::getFontDiff(font, fontDiffFlags, KFontChooser::NoDisplayFlags); | 580 | int ret = KFontDialog::getFontDiff(font, fontDiffFlags, KFontChooser::NoDisplayFlags); | ||
711 | 581 | | |||
712 | if (ret == KDialog::Accepted && fontDiffFlags) { | 582 | if (ret == KDialog::Accepted && fontDiffFlags) { | ||
713 | setGeneralFont(applyFontDiff(m_generalFont, font, fontDiffFlags)); | 583 | m_settings->setFont(applyFontDiff(m_settings->font(), font, fontDiffFlags)); | ||
714 | setMenuFont(applyFontDiff(m_menuFont, font, fontDiffFlags)); | 584 | m_settings->setMenuFont(applyFontDiff(m_settings->menuFont(), font, fontDiffFlags)); | ||
715 | { | 585 | m_settings->setToolBarFont(applyFontDiff(m_settings->toolBarFont(), font, fontDiffFlags)); | ||
716 | const QFont adjustedFont = applyFontDiff(m_fixedWidthFont, font, fontDiffFlags); | 586 | m_settings->setActiveFont(applyFontDiff(m_settings->activeFont(), font, fontDiffFlags)); | ||
587 | m_settings->setSmallestReadableFont(applyFontDiff(m_settings->smallestReadableFont(), font, fontDiffFlags)); | ||||
588 | const QFont adjustedFont = applyFontDiff(m_settings->fixed(), font, fontDiffFlags); | ||||
717 | if (QFontInfo(adjustedFont).fixedPitch()) { | 589 | if (QFontInfo(adjustedFont).fixedPitch()) { | ||
718 | setFixedWidthFont(adjustedFont); | 590 | m_settings->setFixed(adjustedFont); | ||
719 | } | | |||
720 | } | 591 | } | ||
721 | setToolbarFont(applyFontDiff(m_toolbarFont, font, fontDiffFlags)); | | |||
722 | setSmallFont(applyFontDiff(m_smallFont, font, fontDiffFlags)); | | |||
723 | setWindowTitleFont(applyFontDiff(m_windowTitleFont, font, fontDiffFlags)); | | |||
724 | } | 592 | } | ||
725 | } | 593 | } | ||
726 | 594 | | |||
727 | QFont KFonts::applyFontDiff(const QFont &fnt, const QFont &newFont, int fontDiffFlags) | 595 | QFont KFonts::applyFontDiff(const QFont &fnt, const QFont &newFont, int fontDiffFlags) | ||
728 | { | 596 | { | ||
729 | QFont font(fnt); | 597 | QFont font(fnt); | ||
730 | 598 | | |||
731 | if (fontDiffFlags & KFontChooser::FontDiffSize) { | 599 | if (fontDiffFlags & KFontChooser::FontDiffSize) { | ||
▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines |