From 5ee3df312f7e167c817d1d36502b67045d94593f Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Thu, 10 Jan 2019 10:26:55 +1100 Subject: [PATCH] import/export keyboard shortcuts --- dialogs/preferencesdialog.cpp | 95 +++++++++++++++++++--- dialogs/preferencesdialog.h | 5 ++ mainwindow.cpp | 146 +++++++++++++++++++--------------- mainwindow.h | 5 +- 4 files changed, 174 insertions(+), 77 deletions(-) diff --git a/dialogs/preferencesdialog.cpp b/dialogs/preferencesdialog.cpp index 044ffd895..c85724086 100644 --- a/dialogs/preferencesdialog.cpp +++ b/dialogs/preferencesdialog.cpp @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include "debug.h" @@ -29,7 +31,23 @@ KeySequenceEditor::KeySequenceEditor(QWidget* parent, QAction* a) } void KeySequenceEditor::set_action_shortcut() { - action->setShortcut(keySequence()); + action->setShortcut(keySequence()); +} + +void KeySequenceEditor::reset_to_default() { + setKeySequence(action->property("default").toString()); +} + +QString KeySequenceEditor::action_name() { + return action->text().replace("&", ""); +} + +QString KeySequenceEditor::export_shortcut() { + QString ks = keySequence().toString(); + if (ks != action->property("default")) { + return action->text().replace("&", "") + "\t" + keySequence().toString(); + } + return 0; } PreferencesDialog::PreferencesDialog(QWidget *parent) : @@ -60,8 +78,7 @@ void PreferencesDialog::setup_kbd_shortcut_worker(QMenu* menu, QTreeWidgetItem* if (a->menu() != NULL) { item->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator); setup_kbd_shortcut_worker(a->menu(), item); - } else { - item->setData(0, Qt::UserRole + 1, reinterpret_cast(a)); + } else { key_shortcut_items.append(item); key_shortcut_actions.append(a); } @@ -112,12 +129,7 @@ void PreferencesDialog::reset_default_shortcut() { QList items = keyboard_tree->selectedItems(); for (int i=0;iselectedItems().at(i); - const QVariant& data = item->data(0, Qt::UserRole + 1); - if (!data.isNull()) { - QAction* a = reinterpret_cast(data.value()); - QKeySequence ks(a->property("default").toString()); - static_cast(keyboard_tree->itemWidget(item, 1))->setKeySequence(ks); - } + static_cast(keyboard_tree->itemWidget(item, 1))->reset_to_default(); } } @@ -158,7 +170,59 @@ bool PreferencesDialog::refine_shortcut_list(const QString &s, QTreeWidgetItem* parent->setHidden(all_children_are_hidden); return all_children_are_hidden; - } + } + return true; +} + +void PreferencesDialog::load_shortcut_file() { + QString fn = QFileDialog::getOpenFileName(this, "Import Keyboard Shortcuts"); + if (!fn.isEmpty()) { + QFile f(fn); + if (f.exists() && f.open(QFile::ReadOnly)) { + QByteArray ba = f.readAll(); + f.close(); + for (int i=0;iaction_name()); + if (index == 0 || (index > 0 && ba.at(index-1) == '\n')) { + while (index < ba.size() && ba.at(index) != '\t') index++; + QString ks; + index++; + while (index < ba.size() && ba.at(index) != '\n') { + ks.append(ba.at(index)); + index++; + } + dout << "set" << key_shortcut_fields.at(i)->action_name() << "to" << ks; + key_shortcut_fields.at(i)->setKeySequence(ks); + } else { + key_shortcut_fields.at(i)->reset_to_default(); + } + } + } else { + QMessageBox::critical(this, "Error saving shortcuts", "Failed to open file for reading"); + } + } +} + +void PreferencesDialog::save_shortcut_file() { + QString fn = QFileDialog::getSaveFileName(this, "Export Keyboard Shortcuts"); + if (!fn.isEmpty()) { + QFile f(fn); + if (f.open(QFile::WriteOnly)) { + bool start = true; + for (int i=0;iexport_shortcut(); + if (!s.isEmpty()) { + if (!start) f.write("\n"); + f.write(s.toUtf8()); + start = false; + } + } + QMessageBox::information(this, "Export Shortcuts", "Shortcuts exported successfully"); + f.close(); + } else { + QMessageBox::critical(this, "Error saving shortcuts", "Failed to open file for writing"); + } + } } void PreferencesDialog::setup_ui() { @@ -249,7 +313,16 @@ void PreferencesDialog::setup_ui() { tree_header->setText(1, "Shortcut"); shortcut_layout->addWidget(keyboard_tree); - QHBoxLayout* reset_shortcut_layout = new QHBoxLayout(); + QHBoxLayout* reset_shortcut_layout = new QHBoxLayout(); + + QPushButton* import_shortcut_button = new QPushButton("Import"); + reset_shortcut_layout->addWidget(import_shortcut_button); + connect(import_shortcut_button, SIGNAL(clicked(bool)), this, SLOT(load_shortcut_file())); + + QPushButton* export_shortcut_button = new QPushButton("Export"); + reset_shortcut_layout->addWidget(export_shortcut_button); + connect(export_shortcut_button, SIGNAL(clicked(bool)), this, SLOT(save_shortcut_file())); + reset_shortcut_layout->addStretch(); reset_shortcut_button = new QPushButton("Reset to Default"); diff --git a/dialogs/preferencesdialog.h b/dialogs/preferencesdialog.h index 0e6d575d4..1bdb37b12 100644 --- a/dialogs/preferencesdialog.h +++ b/dialogs/preferencesdialog.h @@ -18,6 +18,9 @@ class KeySequenceEditor : public QKeySequenceEdit { public: KeySequenceEditor(QWidget *parent, QAction* a); void set_action_shortcut(); + void reset_to_default(); + QString action_name(); + QString export_shortcut(); private: QAction* action; }; @@ -36,6 +39,8 @@ private slots: void save(); void reset_default_shortcut(); bool refine_shortcut_list(const QString &, QTreeWidgetItem* parent = NULL); + void load_shortcut_file(); + void save_shortcut_file(); private: void setup_ui(); diff --git a/mainwindow.cpp b/mainwindow.cpp index 17e06f0c4..590a652f3 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -245,7 +245,85 @@ void MainWindow::make_new_menu(QMenu *parent) { void MainWindow::make_inout_menu(QMenu *parent) { parent->addAction("Set In Point", this, SLOT(set_in_point()), QKeySequence("I")); parent->addAction("Set Out Point", this, SLOT(set_out_point()), QKeySequence("O")); - parent->addAction("Clear In/Out Point", this, SLOT(clear_inout()), QKeySequence("G")); + parent->addAction("Clear In/Out Point", this, SLOT(clear_inout()), QKeySequence("G")); +} + +void kbd_shortcut_processor(QByteArray& file, QMenu* menu, bool save, bool first) { + QList actions = menu->actions(); + for (int i=0;imenu() != NULL) { + kbd_shortcut_processor(file, a->menu(), save, first); + } else if (!a->isSeparator()) { + if (save) { + // saving custom shortcuts + if (!a->property("default").isNull()) { + QKeySequence defks(a->property("default").toString()); + if (a->shortcut() != defks) { + // custom shortcut + if (!file.isEmpty()) file.append('\n'); + file.append(a->text().replace("&", "")); + file.append('\t'); + file.append(a->shortcut().toString()); + } + } + } else { + // loading custom shortcuts + if (first) { + // store default shortcut + a->setProperty("default", a->shortcut().toString()); + } else { + // restore default shortcut + a->setShortcut(a->property("default").toString()); + } + QString comp_str = a->text().replace("&", ""); + int shortcut_index = file.indexOf(comp_str); + if (shortcut_index == 0 || (shortcut_index > 0 && file.at(shortcut_index-1) == '\n')) { + shortcut_index += comp_str.size() + 1; + QString shortcut; + while (shortcut_index < file.size() && file.at(shortcut_index) != '\n') { + shortcut.append(file.at(shortcut_index)); + shortcut_index++; + } + QKeySequence ks(shortcut); + if (!ks.isEmpty()) { + a->setShortcut(ks); + } + } + } + } + } +} + +void MainWindow::load_shortcuts(const QString& fn, bool first) { + QByteArray shortcut_bytes; + QFile shortcut_path(fn); + if (shortcut_path.exists() && shortcut_path.open(QFile::ReadOnly)) { + shortcut_bytes = shortcut_path.readAll(); + shortcut_path.close(); + } + QList menus = menuBar()->actions(); + for (int i=0;imenu(); + kbd_shortcut_processor(shortcut_bytes, menu, false, first); + } +} + +void MainWindow::save_shortcuts(const QString& fn) { + // save main menu actions + QList menus = menuBar()->actions(); + QByteArray shortcut_file; + for (int i=0;imenu(); + kbd_shortcut_processor(shortcut_file, menu, true, false); + } + QFile shortcut_file_io(fn); + if (shortcut_file_io.open(QFile::WriteOnly)) { + shortcut_file_io.write(shortcut_file); + shortcut_file_io.close(); + } else { + dout << "[ERROR] Failed to save shortcut file"; + } } void MainWindow::show_about() { @@ -442,47 +520,6 @@ bool MainWindow::can_close_project() { return true; } -void kbd_shortcut_processor(QByteArray& file, QMenu* menu, bool save) { - QList actions = menu->actions(); - for (int i=0;imenu() != NULL) { - kbd_shortcut_processor(file, a->menu(), save); - } else if (!a->isSeparator()) { - if (save) { - // saving custom shortcuts - if (!a->property("default").isNull()) { - QKeySequence defks(a->property("default").toString()); - if (a->shortcut() != defks) { - // custom shortcut - if (!file.isEmpty()) file.append('\n'); - file.append(a->text().replace("&", "")); - file.append('\t'); - file.append(a->shortcut().toString()); - } - } - } else { - // loading custom shortcuts - a->setProperty("default", a->shortcut().toString()); - QString comp_str = a->text().replace("&", ""); - int shortcut_index = file.indexOf(comp_str); - if (shortcut_index == 0 || (shortcut_index > 0 && file.at(shortcut_index-1) == '\n')) { - shortcut_index += comp_str.size() + 1; - QString shortcut; - while (shortcut_index < file.size() && file.at(shortcut_index) != '\n') { - shortcut.append(file.at(shortcut_index)); - shortcut_index++; - } - QKeySequence ks(shortcut); - if (!ks.isEmpty()) { - a->setShortcut(ks); - } - } - } - } - } -} - void MainWindow::setup_menus() { QMenuBar* menuBar = new QMenuBar(this); setMenuBar(menuBar); @@ -809,15 +846,7 @@ void MainWindow::setup_menus() { help_menu->addAction("&About...", this, SLOT(show_about())); - QFile shortcut_path(get_config_path() + "/shortcuts"); - if (shortcut_path.exists() && shortcut_path.open(QFile::ReadOnly)) { - QList menus = menuBar->actions(); - QByteArray shortcut_bytes = shortcut_path.readAll(); - for (int i=0;imenu(); - kbd_shortcut_processor(shortcut_bytes, menu, false); - } - } + load_shortcuts(get_config_path() + "/shortcuts", true); } void MainWindow::set_bool_action_checked(QAction *a) { @@ -870,20 +899,7 @@ void MainWindow::closeEvent(QCloseEvent *e) { dout << "[ERROR] Failed to save layout"; } - // save main menu actions - QList menus = menuBar()->actions(); - QByteArray shortcut_file; - for (int i=0;imenu(); - kbd_shortcut_processor(shortcut_file, menu, true); - } - QFile shortcut_file_io(config_dir + "/shortcuts"); - if (shortcut_file_io.open(QFile::WriteOnly)) { - shortcut_file_io.write(shortcut_file); - shortcut_file_io.close(); - } else { - dout << "[ERROR] Failed to save shortcut file"; - } + save_shortcuts(config_dir + "/shortcuts"); } stop_audio(); diff --git a/mainwindow.h b/mainwindow.h index da96851fe..b95f9f165 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -18,7 +18,10 @@ public: void launch_with_project(const QString& s); void make_new_menu(QMenu* parent); - void make_inout_menu(QMenu* parent); + void make_inout_menu(QMenu* parent); + + void load_shortcuts(const QString &fn, bool first = false); + void save_shortcuts(const QString &fn); QString appName;