From c48d2038d4aa1666d76a04dc84f68bb5ab3eb1cc Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Mon, 12 Apr 2021 12:02:48 +1000 Subject: [PATCH] reimplemented saving keyboard shortcuts Fixes #1412 --- app/window/mainwindow/mainwindow.cpp | 105 +++++++++++++++++++++++++++ app/window/mainwindow/mainwindow.h | 6 ++ 2 files changed, 111 insertions(+) diff --git a/app/window/mainwindow/mainwindow.cpp b/app/window/mainwindow/mainwindow.cpp index 0cb3a4128..8cc4c3e1e 100644 --- a/app/window/mainwindow/mainwindow.cpp +++ b/app/window/mainwindow/mainwindow.cpp @@ -68,6 +68,8 @@ MainWindow::MainWindow(QWidget *parent) : MainMenu* main_menu = new MainMenu(this); setMenuBar(main_menu); + LoadCustomShortcuts(); + // Create and set status bar MainStatusBar* status_bar = new MainStatusBar(this); status_bar->ConnectTaskManager(TaskManager::instance()); @@ -406,6 +408,8 @@ void MainWindow::closeEvent(QCloseEvent *e) PanelManager::instance()->DeleteAllPanels(); + SaveCustomShortcuts(); + QMainWindow::closeEvent(e); } @@ -547,6 +551,107 @@ void MainWindow::TimelineFocused(ViewerOutput* viewer) curve_panel_->ConnectViewerNode(viewer); } +QString MainWindow::GetCustomShortcutsFile() +{ + return QDir(FileFunctions::GetConfigurationLocation()).filePath(QStringLiteral("shortcuts")); +} + +void LoadCustomShortcutsInternal(QMenu* menu, const QMap& shortcuts) +{ + QList actions = menu->actions(); + + foreach (QAction* a, actions) { + if (a->menu()) { + LoadCustomShortcutsInternal(a->menu(), shortcuts); + } else if (!a->isSeparator()) { + QString action_id = a->property("id").toString(); + + if (shortcuts.contains(action_id)) { + a->setShortcut(shortcuts.value(action_id)); + } + } + } +} + +void MainWindow::LoadCustomShortcuts() +{ + QFile shortcut_file(GetCustomShortcutsFile()); + if (shortcut_file.exists() && shortcut_file.open(QFile::ReadOnly)) { + QMap shortcuts; + + QString shortcut_str = QString::fromUtf8(shortcut_file.readAll()); + + QStringList shortcut_list = shortcut_str.split(QStringLiteral("\n")); + + foreach (const QString& s, shortcut_list) { + QStringList shortcut_line = s.split(QStringLiteral("\t")); + if (shortcut_line.size() >= 2) { + shortcuts.insert(shortcut_line.at(0), shortcut_line.at(1)); + } + } + + shortcut_file.close(); + + if (!shortcuts.isEmpty()) { + QList menus = menuBar()->actions(); + + foreach (QAction* menu, menus) { + LoadCustomShortcutsInternal(menu->menu(), shortcuts); + } + } + } +} + +void SaveCustomShortcutsInternal(QMenu* menu, QMap* shortcuts) +{ + QList actions = menu->actions(); + + foreach (QAction* a, actions) { + if (a->menu()) { + SaveCustomShortcutsInternal(a->menu(), shortcuts); + } else if (!a->isSeparator()) { + QString default_shortcut = a->property("keydefault").toString(); + QString current_shortcut = a->shortcut().toString(); + if (current_shortcut != default_shortcut) { + QString action_id = a->property("id").toString(); + shortcuts->insert(action_id, current_shortcut); + } + } + } +} + +void MainWindow::SaveCustomShortcuts() +{ + QMap shortcuts; + QList menus = menuBar()->actions(); + + foreach (QAction* menu, menus) { + SaveCustomShortcutsInternal(menu->menu(), &shortcuts); + } + + QFile shortcut_file(GetCustomShortcutsFile()); + if (shortcuts.isEmpty()) { + if (shortcut_file.exists()) { + // No custom shortcuts, remove any existing file + shortcut_file.remove(); + } + } else if (shortcut_file.open(QFile::WriteOnly)) { + for (auto it=shortcuts.cbegin(); it!=shortcuts.cend(); it++) { + if (it != shortcuts.cbegin()) { + shortcut_file.write(QStringLiteral("\n").toUtf8()); + } + + shortcut_file.write(it.key().toUtf8()); + shortcut_file.write(QStringLiteral("\t").toUtf8()); + shortcut_file.write(it.value().toUtf8()); + } + shortcut_file.close(); + } else { + qCritical() << "Failed to save custom keyboard shortcuts"; + } + +} + void MainWindow::FocusedPanelChanged(PanelWidget *panel) { TimelinePanel* timeline = dynamic_cast(panel); diff --git a/app/window/mainwindow/mainwindow.h b/app/window/mainwindow/mainwindow.h index a31cc6b30..35db59aee 100644 --- a/app/window/mainwindow/mainwindow.h +++ b/app/window/mainwindow/mainwindow.h @@ -132,6 +132,12 @@ private: void TimelineFocused(ViewerOutput *viewer); + static QString GetCustomShortcutsFile(); + + void LoadCustomShortcuts(); + + void SaveCustomShortcuts(); + QByteArray premaximized_state_; // Standard panels