Merge pull request #292 from olive-editor/exportkeys

import/export keys
This commit is contained in:
itsmattkc
2019-01-10 11:26:45 +11:00
committed by GitHub
4 changed files with 178 additions and 74 deletions
+81 -8
View File
@@ -19,6 +19,8 @@
#include <QTreeWidgetItem>
#include <QList>
#include <QDoubleSpinBox>
#include <QFileDialog>
#include <QMessageBox>
#include "debug.h"
@@ -32,6 +34,22 @@ void KeySequenceEditor::set_action_shortcut() {
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) :
QDialog(parent)
{
@@ -51,7 +69,7 @@ void PreferencesDialog::setup_kbd_shortcut_worker(QMenu* menu, QTreeWidgetItem*
for (int i=0;i<actions.size();i++) {
QAction* a = actions.at(i);
if (!a->isSeparator()) {
if (!a->isSeparator() && a->property("keyignore").isNull()) {
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setText(0, a->text().replace("&", ""));
@@ -61,7 +79,6 @@ void PreferencesDialog::setup_kbd_shortcut_worker(QMenu* menu, QTreeWidgetItem*
item->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
setup_kbd_shortcut_worker(a->menu(), item);
} else {
item->setData(0, Qt::UserRole + 1, reinterpret_cast<quintptr>(a));
key_shortcut_items.append(item);
key_shortcut_actions.append(a);
}
@@ -112,12 +129,7 @@ void PreferencesDialog::reset_default_shortcut() {
QList<QTreeWidgetItem*> items = keyboard_tree->selectedItems();
for (int i=0;i<items.size();i++) {
QTreeWidgetItem* item = keyboard_tree->selectedItems().at(i);
const QVariant& data = item->data(0, Qt::UserRole + 1);
if (!data.isNull()) {
QAction* a = reinterpret_cast<QAction*>(data.value<quintptr>());
QKeySequence ks(a->property("default").toString());
static_cast<QKeySequenceEdit*>(keyboard_tree->itemWidget(item, 1))->setKeySequence(ks);
}
static_cast<KeySequenceEditor*>(keyboard_tree->itemWidget(item, 1))->reset_to_default();
}
}
@@ -159,6 +171,58 @@ bool PreferencesDialog::refine_shortcut_list(const QString &s, QTreeWidgetItem*
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;i<key_shortcut_fields.size();i++) {
int index = ba.indexOf(key_shortcut_fields.at(i)->action_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;i<key_shortcut_fields.size();i++) {
QString s = key_shortcut_fields.at(i)->export_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() {
@@ -250,6 +314,15 @@ void PreferencesDialog::setup_ui() {
shortcut_layout->addWidget(keyboard_tree);
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");
+5
View File
@@ -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();
+88 -66
View File
@@ -248,6 +248,84 @@ void MainWindow::make_inout_menu(QMenu *parent) {
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<QAction*> actions = menu->actions();
for (int i=0;i<actions.size();i++) {
QAction* a = actions.at(i);
if (a->menu() != 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<QAction*> menus = menuBar()->actions();
for (int i=0;i<menus.size();i++) {
QMenu* menu = menus.at(i)->menu();
kbd_shortcut_processor(shortcut_bytes, menu, false, first);
}
}
void MainWindow::save_shortcuts(const QString& fn) {
// save main menu actions
QList<QAction*> menus = menuBar()->actions();
QByteArray shortcut_file;
for (int i=0;i<menus.size();i++) {
QMenu* menu = menus.at(i)->menu();
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() {
AboutDialog a(this);
a.exec();
@@ -442,47 +520,6 @@ bool MainWindow::can_close_project() {
return true;
}
void kbd_shortcut_processor(QByteArray& file, QMenu* menu, bool save) {
QList<QAction*> actions = menu->actions();
for (int i=0;i<actions.size();i++) {
QAction* a = actions.at(i);
if (a->menu() != 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);
@@ -497,8 +534,13 @@ void MainWindow::setup_menus() {
file_menu->addAction("&Open Project", this, SLOT(open_project()), QKeySequence("Ctrl+O"));
clear_open_recent_action = new QAction("Clear Recent List");
connect(clear_open_recent_action, SIGNAL(triggered()), panel_project, SLOT(clear_recent_projects()));
open_recent = file_menu->addMenu("Open Recent");
open_recent->addAction(clear_open_recent_action);
file_menu->addAction("&Save Project", this, SLOT(save_project()), QKeySequence("Ctrl+S"));
file_menu->addAction("Save Project &As", this, SLOT(save_project_as()), QKeySequence("Ctrl+Shift+S"));
@@ -812,15 +854,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<QAction*> menus = menuBar->actions();
QByteArray shortcut_bytes = shortcut_path.readAll();
for (int i=0;i<menus.size();i++) {
QMenu* menu = menus.at(i)->menu();
kbd_shortcut_processor(shortcut_bytes, menu, false);
}
}
load_shortcuts(get_config_path() + "/shortcuts", true);
}
void MainWindow::set_bool_action_checked(QAction *a) {
@@ -873,20 +907,7 @@ void MainWindow::closeEvent(QCloseEvent *e) {
dout << "[ERROR] Failed to save layout";
}
// save main menu actions
QList<QAction*> menus = menuBar()->actions();
QByteArray shortcut_file;
for (int i=0;i<menus.size();i++) {
QMenu* menu = menus.at(i)->menu();
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();
@@ -1138,12 +1159,13 @@ void MainWindow::fileMenu_About_To_Be_Shown() {
open_recent->setEnabled(true);
for (int i=0;i<recent_projects.size();i++) {
QAction* action = open_recent->addAction(recent_projects.at(i));
action->setProperty("keyignore", true);
action->setData(i);
connect(action, SIGNAL(triggered()), this, SLOT(load_recent_project()));
}
open_recent->addSeparator();
QAction* clear_action = open_recent->addAction("Clear Recent List");
connect(clear_action, SIGNAL(triggered()), panel_project, SLOT(clear_recent_projects()));
open_recent->addAction(clear_open_recent_action);
} else {
open_recent->setEnabled(false);
}
+4
View File
@@ -20,6 +20,9 @@ public:
void make_new_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;
public slots:
@@ -133,6 +136,7 @@ private:
// file menu actions
QMenu* open_recent;
QAction* clear_open_recent_action;
// view menu actions
QAction* track_lines;