From 1c7ca419215abd0e81fbe970eed4c0d2e985bc54 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sun, 24 Apr 2022 12:59:05 +0200 Subject: [PATCH] Let KColorSchemeManager handle loading/saving the widget color scheme KColorSchemeManager (from KConfigWidgets) was changed in 5.89 to handle loading/saving the selected color scheme automatically if KColorSchemeManager::setAutosaveChanges(true) is used (it auto saves by default, but calling setAutosaveChanges(true) makes it easier to understand the code). However this is conditional on KConfigWidgets >= 5.93 because that's where the KColorSchemeManager::createSchemeSelectionMenu() overload used here was changed to load the previously saved color scheme. Since KColorSchemeManager uses "ColorScheme" as the config entry name, migrate the old config entry when KF >= 5.93. --- src/AppColorSchemeChooser.cpp | 10 +++++++--- src/AppColorSchemeChooser.h | 7 ++++++- src/main.cpp | 11 +++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/AppColorSchemeChooser.cpp b/src/AppColorSchemeChooser.cpp index 52800ecf..eaaaf186 100644 --- a/src/AppColorSchemeChooser.cpp +++ b/src/AppColorSchemeChooser.cpp @@ -22,20 +22,23 @@ AppColorSchemeChooser::AppColorSchemeChooser(QObject *parent) { auto *manager = new KColorSchemeManager(parent); +#if KCONFIGWIDGETS_VERSION < QT_VERSION_CHECK(5, 93, 0) const QString scheme(currentSchemeName()); qCDebug(KonsoleDebug) << "Color scheme : " << scheme; - auto *selectionMenu = manager->createSchemeSelectionMenu(scheme, this); - connect(selectionMenu->menu(), &QMenu::triggered, this, &AppColorSchemeChooser::slotSchemeChanged); - manager->activateScheme(manager->indexForScheme(scheme)); +#else + manager->setAutosaveChanges(true); + KActionMenu *selectionMenu = manager->createSchemeSelectionMenu(this); +#endif setMenu(selectionMenu->menu()); menu()->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-color"))); menu()->setTitle(i18n("&Window Color Scheme")); } +#if KCONFIGWIDGETS_VERSION < QT_VERSION_CHECK(5, 93, 0) QString AppColorSchemeChooser::currentSchemeName() const { if (!menu()) { @@ -58,3 +61,4 @@ void AppColorSchemeChooser::slotSchemeChanged(QAction *triggeredAction) cg.writeEntry("WindowColorScheme", KLocalizedString::removeAcceleratorMarker(triggeredAction->text())); cg.sync(); } +#endif diff --git a/src/AppColorSchemeChooser.h b/src/AppColorSchemeChooser.h index 9186b54b..7eb12195 100644 --- a/src/AppColorSchemeChooser.h +++ b/src/AppColorSchemeChooser.h @@ -12,6 +12,7 @@ #include #include +#include // Konsole #include "konsoleprivate_export.h" @@ -23,15 +24,19 @@ class KActionCollection; * * Furthermore, it will save the selection in the user configuration. */ +// TODO: Once the minimum KF version is changed to >= 5.93, remove this whole +// class and move the couple of lines of code from the constructor to MainWindow class KONSOLEPRIVATE_EXPORT AppColorSchemeChooser : public QAction { public: explicit AppColorSchemeChooser(QObject *parent); + // Not needed with KF >= 5.93 +#if KCONFIGWIDGETS_VERSION < QT_VERSION_CHECK(5, 93, 0) QString currentSchemeName() const; - private Q_SLOTS: void slotSchemeChanged(QAction *triggeredAction); +#endif }; #endif diff --git a/src/main.cpp b/src/main.cpp index b1d931cf..2f9d6a1d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,6 +32,7 @@ #include #include #endif +#include #include using Konsole::Application; @@ -98,6 +99,16 @@ static void migrateRenamedConfigKeys() } } + // With 5.93 KColorSchemeManager from KConfigWidgets, handles the loading + // and saving of the widget color scheme, and uses "ColorScheme" as the + // entry name, so clean-up here +#if KCONFIGWIDGETS_VERSION >= QT_VERSION_CHECK(5, 93, 0) + KConfigGroup cg(konsoleConfig, "UiSettings"); + const QString schemeName = cg.readEntry("WindowColorScheme"); + cg.deleteEntry("WindowColorScheme"); + cg.writeEntry("ColorScheme", schemeName); +#endif + konsoleConfig->sync(); }