From 5fd8e72f52ec510ae79ebf1c7c8d41742f756d56 Mon Sep 17 00:00:00 2001 From: MrBesen Date: Sat, 26 Feb 2022 16:37:01 +0100 Subject: [PATCH] configure all shortcuts --- include/config.h | 10 ++++++++++ include/mainwindow.h | 3 +++ soundboard.json.example | 9 +++++++++ src/config_json.cpp | 22 +++++++++++++++++++++- src/mainwindow.cpp | 34 +++++++++++++++++++++++++--------- 5 files changed, 68 insertions(+), 10 deletions(-) diff --git a/include/config.h b/include/config.h index 9916574..cce674c 100644 --- a/include/config.h +++ b/include/config.h @@ -36,11 +36,21 @@ public: const static uint8_t DEFAULTWIDTH; }; + struct ShortcutConfig { + std::string up; + std::string down; + std::string left; + std::string right; + std::string play; + std::string stop; + }; + struct RootConfig { AudioConfig audio; std::vector> buttons; std::string audioPath; + ShortcutConfig shortcuts; } rootConfig; private: diff --git a/include/mainwindow.h b/include/mainwindow.h index 02430e3..3997519 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -44,12 +44,15 @@ signals: private: void loadSoundFromConfig(); + void loadShortcuts(); void removeAllButtons(); void loadButtonsFromConfig(); void reselectNext(std::function stepf); QLayoutItem* findNextButton(std::function stepf); + QxtGlobalShortcut* loadShortcut(const std::string& key); + Ui::Soundboard *ui; std::vector soundbuttons; QxtGlobalShortcut* stopGlobal = nullptr; diff --git a/soundboard.json.example b/soundboard.json.example index 80bf2d4..e16021a 100644 --- a/soundboard.json.example +++ b/soundboard.json.example @@ -5,6 +5,15 @@ "" ] }, + "shortcuts": { + "up": "Shift+Up", + "down": "Shift+Down", + "left": "Shift+Left", + "right": "Shift+Right", + "play": "Shift+Num+Enter", + "stop": "Shift+Num+0" + }, + "audioPath": "./neuesSB/", "buttons": [ [ { diff --git a/src/config_json.cpp b/src/config_json.cpp index 3ef6e3f..53ef05f 100644 --- a/src/config_json.cpp +++ b/src/config_json.cpp @@ -50,6 +50,15 @@ void from_json(const json& j, Config::ButtonConfig& bc) { } } +void from_json(const json& j, Config::ShortcutConfig& sc) { + sc.up = j.value("up", ""); + sc.down = j.value("down", ""); + sc.left = j.value("left", ""); + sc.right = j.value("right", ""); + sc.play = j.value("play", ""); + sc.stop = j.value("stop", ""); +} + void from_json(const json& j, Config::RootConfig& rc) { rc.audio = j.value("audio", {}); json barr = j.value("buttons", json::array()); @@ -62,6 +71,7 @@ void from_json(const json& j, Config::RootConfig& rc) { } } rc.audioPath = j.value("audioPath", ""); + rc.shortcuts = j.value("shortcuts", Config::ShortcutConfig()); } @@ -101,6 +111,15 @@ void to_json(json& j, const Config::ButtonConfig& bc) { } } +void to_json(json& j, const Config::ShortcutConfig& sc) { + j["up"] = sc.up; + j["down"] = sc.down; + j["left"] = sc.left; + j["right"] = sc.right; + j["play"] = sc.play; + j["stop"] = sc.stop; +} + void to_json(json& j, const Config::RootConfig& rc) { j["audio"] = rc.audio; j["audioPath"] = rc.audioPath; @@ -118,4 +137,5 @@ void to_json(json& j, const Config::RootConfig& rc) { } j["buttons"] = buttonarr; -} \ No newline at end of file + j["shortcuts"] = rc.shortcuts; +} diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index df0f36e..8296348 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -15,15 +15,6 @@ MainWindow::MainWindow(const std::string& binary, QWidget* parent) : QMainWindow(parent), ui(new Ui::Soundboard), config(binary) { ui->setupUi(this); - up = new QxtGlobalShortcut(QKeySequence("Shift+Up")); - down = new QxtGlobalShortcut(QKeySequence("Shift+Down")); - left = new QxtGlobalShortcut(QKeySequence("Shift+Left")); - right = new QxtGlobalShortcut(QKeySequence("Shift+Right")); - play = new QxtGlobalShortcut(QKeySequence("Shift+Num+Enter")); - - QKeySequence seq(QString::fromStdString("Shift+Num+0")); - stopGlobal = new QxtGlobalShortcut(seq); - reloadConfig(); QObject::connect(ui->reloadButton, SIGNAL(clicked()), this, SLOT(reloadConfig())); @@ -63,6 +54,7 @@ void MainWindow::reloadConfig() { config.load(); loadSoundFromConfig(); + loadShortcuts(); removeAllButtons(); @@ -170,6 +162,23 @@ void MainWindow::loadSoundFromConfig() { Log::debug << "Sound::FOLDER: " << Sound::FOLDER; } +void MainWindow::loadShortcuts() { + delete up; + delete down; + delete left; + delete right; + delete play; + delete stopGlobal; + + const Config::ShortcutConfig& sc = config.rootConfig.shortcuts; + up = loadShortcut(sc.up); + down = loadShortcut(sc.down); + left = loadShortcut(sc.left); + right = loadShortcut(sc.right); + play = loadShortcut(sc.play); + stopGlobal = loadShortcut(sc.stop); +} + void MainWindow::removeAllButtons() { //remove old buttons for (SoundButton* sb : soundbuttons) { @@ -253,4 +262,11 @@ QLayoutItem* MainWindow::findNextButton(std::function s // might the same item return item; +} + +QxtGlobalShortcut* MainWindow::loadShortcut(const std::string& key) { + QKeySequence seq(QString::fromStdString(key)); + if(seq.isEmpty()) + return nullptr; + return new QxtGlobalShortcut(seq); } \ No newline at end of file