From c24a0876f712359d8be53b39027d638a3ba0bd41 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Thu, 7 Oct 2021 23:16:17 +0200 Subject: [PATCH] Optimise saving profile shortcuts - Call ProfileSettings::slotAccepted() when the parent ConfigurationDialog is accepted, so that ProfileManager::saveSettings() is called - don't delete the ProfileSettings dialog on accept, it'll be destroyed when the parent ConfigurationDialog is destroyed - save the profiles' shorcuts if they were actually changed With this commit and the previous one, there is no need for ~Part() or ~Application() to call saveSettings(), shorcuts settings are saved when the ProfileSettings dialog is accepted and saving the default profile is done in setDefaultProfile(). This fixes an issue where changing e.g. the default profile is only saved to the konsolerc file when the main window is closed. --- src/Application.cpp | 1 - src/MainWindow.cpp | 4 +++- src/Part.cpp | 5 +---- src/profile/ProfileManager.cpp | 22 ++++++++++++---------- src/profile/ProfileManager.h | 6 +++++- src/settings/ProfileSettings.cpp | 1 - src/settings/ProfileSettings.h | 3 ++- 7 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index e0d62368..caa9161d 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -109,7 +109,6 @@ QStringList Application::getCustomCommand(QStringList &args) Application::~Application() { SessionManager::instance()->closeAllSessions(); - ProfileManager::instance()->saveSettings(); } MainWindow *Application::newMainWindow() diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 0e826f47..e2955f9f 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -766,9 +766,11 @@ void MainWindow::showSettingsDialog(const bool showProfilePage) generalPage->setIcon(QIcon::fromTheme(QStringLiteral("utilities-terminal"))); confDialog->addPage(generalPage, true); - auto *profilePage = new KPageWidgetItem(new ProfileSettings(confDialog), profilePageName); + auto *profileSettings = new ProfileSettings(confDialog); + auto *profilePage = new KPageWidgetItem(profileSettings, profilePageName); profilePage->setIcon(QIcon::fromTheme(QStringLiteral("preferences-system-profiles"))); confDialog->addPage(profilePage, true); + connect(confDialog, &QDialog::accepted, profileSettings, &ProfileSettings::slotAccepted); const QString tabBarPageName = i18nc("@title Preferences page name", "Tab Bar / Splitters"); auto tabBarPage = new KPageWidgetItem(new TabBarSettings(confDialog), tabBarPageName); diff --git a/src/Part.cpp b/src/Part.cpp index b0c80888..59bfcd36 100644 --- a/src/Part.cpp +++ b/src/Part.cpp @@ -67,10 +67,7 @@ Part::Part(QWidget *parentWidget, QObject *parent, const QVariantList &) createSession(); } -Part::~Part() -{ - ProfileManager::instance()->saveSettings(); -} +Part::~Part() = default; bool Part::openFile() { diff --git a/src/profile/ProfileManager.cpp b/src/profile/ProfileManager.cpp index 83ffd61f..d833145b 100644 --- a/src/profile/ProfileManager.cpp +++ b/src/profile/ProfileManager.cpp @@ -215,12 +215,7 @@ void ProfileManager::loadAllProfiles(const QString &defaultProfileFileName) void ProfileManager::saveSettings() { - // save shortcuts saveShortcuts(); - - // ensure default/shortcuts settings are synced into disk - KSharedConfigPtr appConfig = KSharedConfig::openConfig(); - appConfig->sync(); } void ProfileManager::sortProfiles() @@ -467,17 +462,24 @@ void ProfileManager::loadShortcuts() void ProfileManager::saveShortcuts() { - KSharedConfigPtr appConfig = KSharedConfig::openConfig(); - KConfigGroup shortcutGroup = appConfig->group("Profile Shortcuts"); - shortcutGroup.deleteGroup(); + if (_profileShortcutsChanged) { + _profileShortcutsChanged = false; + + KSharedConfigPtr appConfig = KSharedConfig::openConfig(); + KConfigGroup shortcutGroup = appConfig->group("Profile Shortcuts"); + shortcutGroup.deleteGroup(); + + for (const auto &[profile, keySeq] : _shortcuts) { + shortcutGroup.writeEntry(keySeq.toString(), profile->name()); + } - for (const auto &[profile, keySeq] : _shortcuts) { - shortcutGroup.writeEntry(keySeq.toString(), profile->name()); + appConfig->sync(); } } void ProfileManager::setShortcut(Profile::Ptr profile, const QKeySequence &keySequence) { + _profileShortcutsChanged = true; QKeySequence existingShortcut = shortcut(profile); auto profileIt = std::find_if(_shortcuts.begin(), _shortcuts.end(), [&profile](const ShortcutData &data) { diff --git a/src/profile/ProfileManager.h b/src/profile/ProfileManager.h index 3d597195..5f1343a7 100644 --- a/src/profile/ProfileManager.h +++ b/src/profile/ProfileManager.h @@ -181,7 +181,7 @@ Q_SIGNALS: void shortcutChanged(const Profile::Ptr &profile, const QKeySequence &newShortcut); public Q_SLOTS: - /** Saves settings (shortcuts, default profile etc.) to disk. */ + /** Saves settings (shortcuts) to disk. */ void saveSettings(); protected Q_SLOTS: @@ -233,6 +233,10 @@ private: QKeySequence keySeq; }; std::vector _shortcuts; + + // Set to true when setShortcut() is called so that when the ProfileSettings + // dialog is accepted the profiles shorcut changes are saved + bool _profileShortcutsChanged = false; }; } diff --git a/src/settings/ProfileSettings.cpp b/src/settings/ProfileSettings.cpp index 781269ea..6b9f9003 100644 --- a/src/settings/ProfileSettings.cpp +++ b/src/settings/ProfileSettings.cpp @@ -57,7 +57,6 @@ ProfileSettings::~ProfileSettings() = default; void ProfileSettings::slotAccepted() { ProfileManager::instance()->saveSettings(); - deleteLater(); } void ProfileSettings::doubleClicked(const QModelIndex &idx) diff --git a/src/settings/ProfileSettings.h b/src/settings/ProfileSettings.h index 0ecf2ef7..b660deb1 100644 --- a/src/settings/ProfileSettings.h +++ b/src/settings/ProfileSettings.h @@ -54,9 +54,10 @@ public: */ void setShortcutEditorVisible(bool visible); -protected: private Q_SLOTS: + friend class MainWindow; void slotAccepted(); + void deleteSelected(); void setSelectedAsDefault(); void createProfile();