customizable keyboard shortcuts

This commit is contained in:
itsmattkc
2018-07-21 14:21:57 +01:00
parent 43dc221438
commit c340901419
4 changed files with 137 additions and 24 deletions
+57 -2
View File
@@ -1,6 +1,20 @@
#include "preferencesdialog.h"
#include "ui_preferencesdialog.h"
#include <QMenuBar>
#include <QAction>
#include <QDebug>
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<QMenu*> menus = menubar->findChildren<QMenu*>();
// caching for parent-child pairing
QVector<QTreeWidgetItem*> added_action_items;
QVector<QAction*> added_actions;
for (int i=0;i<menus.size();i++) {
QMenu* menu = menus.at(i);
QTreeWidgetItem* item = NULL;
if (menu->parent() != menubar) {
for (int j=0;j<added_actions.size();j++) {
if (added_actions.at(j)->menu() == 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;j<menu->actions().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;i<added_action_items.size();i++) {
KeySequenceEditor* editor = new KeySequenceEditor(added_actions.at(i));
ui->treeWidget->setItemWidget(added_action_items.at(i), 1, editor);
}
}
+14
View File
@@ -2,6 +2,8 @@
#define PREFERENCESDIALOG_H
#include <QDialog>
#include <QKeySequenceEdit>
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
+65 -22
View File
@@ -1,38 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<author/>
<comment/>
<exportmacro/>
<class>PreferencesDialog</class>
<widget class="QDialog" name="PreferencesDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<width>693</width>
<height>491</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>30</x>
<y>240</y>
<width>341</width>
<height>32</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>2</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>General</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Behavior</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>Keyboard</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QTreeWidget" name="treeWidget">
<column>
<property name="text">
<string notr="true">Action</string>
</property>
</column>
<column>
<property name="text">
<string>Shortcut</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<pixmapfunction/>
<resources/>
<connections>
<connection>
+1
View File
@@ -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();
}