From c340901419d9cbd4dfd470e17a45950b35437287 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Sat, 21 Jul 2018 14:21:57 +0100 Subject: [PATCH] customizable keyboard shortcuts --- dialogs/preferencesdialog.cpp | 59 +++++++++++++++++++++++- dialogs/preferencesdialog.h | 14 ++++++ dialogs/preferencesdialog.ui | 87 ++++++++++++++++++++++++++--------- mainwindow.cpp | 1 + 4 files changed, 137 insertions(+), 24 deletions(-) diff --git a/dialogs/preferencesdialog.cpp b/dialogs/preferencesdialog.cpp index 1c20a76b0..43ec2d9af 100644 --- a/dialogs/preferencesdialog.cpp +++ b/dialogs/preferencesdialog.cpp @@ -1,6 +1,20 @@ #include "preferencesdialog.h" #include "ui_preferencesdialog.h" +#include +#include +#include + +KeySequenceEditor::KeySequenceEditor(QAction* a) + : QKeySequenceEdit(0), action(a) { + setKeySequence(action->shortcut()); + connect(this, SIGNAL(editingFinished()), this, SLOT(set_action_shortcut())); +} + +void KeySequenceEditor::set_action_shortcut() { + action->setShortcut(keySequence()); +} + PreferencesDialog::PreferencesDialog(QWidget *parent) : QDialog(parent), ui(new Ui::PreferencesDialog) @@ -8,7 +22,48 @@ PreferencesDialog::PreferencesDialog(QWidget *parent) : ui->setupUi(this); } -PreferencesDialog::~PreferencesDialog() -{ +PreferencesDialog::~PreferencesDialog() { delete ui; } + +void PreferencesDialog::setup_kbd_shortcuts(QMenuBar* menubar) { + ui->treeWidget->clear(); + QList menus = menubar->findChildren(); + + // caching for parent-child pairing + QVector added_action_items; + QVector added_actions; + + for (int i=0;iparent() != menubar) { + for (int j=0;jmenu() == menu) { + item = added_action_items.at(j); + break; + } + } + } + if (menu->parent() == menubar || item == NULL) { + item = new QTreeWidgetItem(); + item->setText(0, menu->title().replace("&", "")); + ui->treeWidget->addTopLevelItem(item); + } + for (int j=0;jactions().size();j++) { + QAction* action = menu->actions().at(j); + if (!action->isSeparator()) { + QTreeWidgetItem* child = new QTreeWidgetItem(); + child->setText(0, action->text().replace("&", "")); + item->addChild(child); + + added_actions.append(action); + added_action_items.append(child); + } + } + } + for (int i=0;itreeWidget->setItemWidget(added_action_items.at(i), 1, editor); + } +} diff --git a/dialogs/preferencesdialog.h b/dialogs/preferencesdialog.h index 3e2a5ed2f..2d9471a8b 100644 --- a/dialogs/preferencesdialog.h +++ b/dialogs/preferencesdialog.h @@ -2,6 +2,8 @@ #define PREFERENCESDIALOG_H #include +#include +class QMenuBar; namespace Ui { class PreferencesDialog; @@ -15,8 +17,20 @@ public: explicit PreferencesDialog(QWidget *parent = 0); ~PreferencesDialog(); + void setup_kbd_shortcuts(QMenuBar* menu); + private: Ui::PreferencesDialog *ui; }; +class KeySequenceEditor : public QKeySequenceEdit { + Q_OBJECT +public: + KeySequenceEditor(QAction* a); +private slots: + void set_action_shortcut(); +private: + QAction* action; +}; + #endif // PREFERENCESDIALOG_H diff --git a/dialogs/preferencesdialog.ui b/dialogs/preferencesdialog.ui index ec7fea199..cfc822350 100644 --- a/dialogs/preferencesdialog.ui +++ b/dialogs/preferencesdialog.ui @@ -1,38 +1,81 @@ + - - - PreferencesDialog 0 0 - 400 - 300 + 693 + 491 Dialog - - - - 30 - 240 - 341 - 32 - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - + + + + + 2 + + + + General + + + + + Behavior + + + + + Keyboard + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + Action + + + + + Shortcut + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + - diff --git a/mainwindow.cpp b/mainwindow.cpp index 5a98eb94f..f0e281546 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -478,6 +478,7 @@ void MainWindow::on_actionGo_to_Next_Cut_triggered() void MainWindow::on_actionPreferences_triggered() { PreferencesDialog pd(this); + pd.setup_kbd_shortcuts(this->menuBar()); pd.exec(); }