summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2016-06-29 13:02:41 +0200
committerStanislaw Halik <sthalik@misaki.pl>2016-06-29 13:18:31 +0200
commit2dd8064c20d88c26a11240c6a871f670b5109d71 (patch)
treea6aa04113adc5837f02548464766b9291ecab89a
parent2eccd41897d6794a80b3ed2fcaaded85c453d46d (diff)
logic, gui: reload keybinding description on profile change
-rw-r--r--gui/options-dialog.cpp91
-rw-r--r--gui/ui.cpp14
-rw-r--r--opentrack-logic/shortcuts.cpp6
-rw-r--r--opentrack-logic/shortcuts.h6
4 files changed, 69 insertions, 48 deletions
diff --git a/gui/options-dialog.cpp b/gui/options-dialog.cpp
index 212c6078..ec304b20 100644
--- a/gui/options-dialog.cpp
+++ b/gui/options-dialog.cpp
@@ -71,27 +71,36 @@ OptionsDialog::OptionsDialog(std::function<void(bool)> pause_keybindings) :
tie_setting(main.center_method, ui.center_method);
- connect(ui.bind_center, &QPushButton::pressed, [&]() -> void { bind_key(main.key_center, ui.center_text); });
- connect(ui.bind_zero, &QPushButton::pressed, [&]() -> void { bind_key(main.key_zero, ui.zero_text); });
- connect(ui.bind_toggle, &QPushButton::pressed, [&]() -> void { bind_key(main.key_toggle, ui.toggle_text); });
- connect(ui.bind_toggle_held, &QPushButton::pressed, [&]() -> void { bind_key(main.key_toggle_press, ui.toggle_held_text); });
- connect(ui.bind_zero_held, &QPushButton::pressed, [&]() -> void { bind_key(main.key_zero_press, ui.zero_held_text); });
-
- connect(ui.bind_start, &QPushButton::pressed, [&]() -> void { bind_key(main.key_start_tracking, ui.start_tracking_text); });
- connect(ui.bind_stop, &QPushButton::pressed, [&]() -> void { bind_key(main.key_stop_tracking, ui.stop_tracking_text); });
- connect(ui.bind_toggle_tracking, &QPushButton::pressed, [&]() -> void { bind_key(main.key_toggle_tracking, ui.toggle_tracking_text); });
- connect(ui.bind_restart_tracking, &QPushButton::pressed, [&]() -> void { bind_key(main.key_restart_tracking, ui.restart_tracking_text); });
-
- ui.center_text->setText(kopts_to_string(main.key_center));
- ui.toggle_text->setText(kopts_to_string(main.key_toggle));
- ui.toggle_held_text->setText(kopts_to_string(main.key_toggle_press));
- ui.zero_text->setText(kopts_to_string(main.key_zero));
- ui.zero_held_text->setText(kopts_to_string(main.key_zero_press));
-
- ui.start_tracking_text->setText(kopts_to_string(main.key_start_tracking));
- ui.stop_tracking_text->setText(kopts_to_string(main.key_stop_tracking));
- ui.toggle_tracking_text->setText(kopts_to_string(main.key_toggle_tracking));
- ui.restart_tracking_text->setText(kopts_to_string(main.key_restart_tracking));
+ struct tmp
+ {
+ key_opts& opt;
+ QLabel* label;
+ QPushButton* button;
+ } tuples[] =
+ {
+ { main.key_center, ui.center_text, ui.bind_center },
+ { main.key_toggle, ui.toggle_text, ui.bind_toggle },
+ { main.key_toggle_press, ui.toggle_held_text, ui.bind_toggle_held },
+ { main.key_zero, ui.zero_text, ui.bind_zero },
+ { main.key_zero_press, ui.zero_held_text, ui.bind_zero_held },
+ { main.key_start_tracking, ui.start_tracking_text, ui.bind_start },
+ { main.key_stop_tracking, ui.stop_tracking_text , ui.bind_stop},
+ { main.key_toggle_tracking, ui.toggle_tracking_text, ui.bind_toggle_tracking },
+ { main.key_restart_tracking, ui.restart_tracking_text, ui.bind_restart_tracking }
+ };
+
+ for (const tmp& val_ : tuples)
+ {
+ tmp val = val_;
+ val.label->setText(kopts_to_string(val.opt));
+ connect(&val.opt.keycode,
+ static_cast<void (base_value::*)(const QString&)>(&base_value::valueChanged),
+ val.label,
+ [=](const QString&) -> void { val.label->setText(kopts_to_string(val.opt)); });
+ {
+ connect(val.button, &QPushButton::pressed, this, [=]() -> void { bind_key(val.opt, val.label); });
+ }
+ }
}
void OptionsDialog::bind_key(key_opts& kopts, QLabel* label)
@@ -99,7 +108,7 @@ void OptionsDialog::bind_key(key_opts& kopts, QLabel* label)
kopts.button = -1;
kopts.guid = "";
kopts.keycode = "";
- QDialog d(this);
+ QDialog d;
auto l = new QHBoxLayout;
l->setMargin(0);
KeyboardListener k;
@@ -108,21 +117,29 @@ void OptionsDialog::bind_key(key_opts& kopts, QLabel* label)
d.setFixedSize(QSize(500, 300));
d.setWindowFlags(Qt::Dialog);
d.setWindowModality(Qt::ApplicationModal);
- connect(&k, &KeyboardListener::key_pressed, [&] (QKeySequence s) -> void {
- kopts.keycode = s.toString(QKeySequence::PortableText);
- kopts.guid = "";
- kopts.button = -1;
- d.close();
- });
- connect(&k, &KeyboardListener::joystick_button_pressed, [&](QString guid, int idx, bool held) -> void {
- if (!held)
- {
- kopts.guid = guid;
- kopts.keycode = "";
- kopts.button = idx;
- d.close();
- }
- });
+ connect(&k,
+ &KeyboardListener::key_pressed,
+ &d,
+ [&](QKeySequence s) -> void
+ {
+ kopts.keycode = s.toString(QKeySequence::PortableText);
+ kopts.guid = "";
+ kopts.button = -1;
+ d.close();
+ });
+ connect(&k, &KeyboardListener::joystick_button_pressed,
+ &d,
+ [&](QString guid, int idx, bool held) -> void
+ {
+ if (!held)
+ {
+ kopts.guid = guid;
+ kopts.keycode = "";
+ kopts.button = idx;
+ d.close();
+ }
+ });
+ connect(main.b.get(), &options::detail::opt_bundle::reloading, &d, &QDialog::close);
pause_keybindings(true);
d.show();
d.exec();
diff --git a/gui/ui.cpp b/gui/ui.cpp
index c82d2e52..ca66104f 100644
--- a/gui/ui.cpp
+++ b/gui/ui.cpp
@@ -129,13 +129,15 @@ MainWindow::MainWindow() :
void MainWindow::register_shortcuts()
{
- using t_shortcut = std::tuple<key_opts&, Shortcuts::fun, bool>;
+ using t_key = Shortcuts::t_key;
+ using t_keys = Shortcuts::t_keys;
- std::vector<t_shortcut> keys {
- t_shortcut(s.key_start_tracking, [&](bool) -> void { emit_start_tracker(); }, true),
- t_shortcut(s.key_stop_tracking, [&](bool) -> void { emit_stop_tracker(); }, true),
- t_shortcut(s.key_toggle_tracking, [&](bool) -> void { emit_toggle_tracker(); }, true),
- t_shortcut(s.key_restart_tracking, [&](bool) -> void { emit_restart_tracker(); }, true),
+ t_keys keys
+ {
+ t_key(s.key_start_tracking, [&](bool) -> void { emit_start_tracker(); }, true),
+ t_key(s.key_stop_tracking, [&](bool) -> void { emit_stop_tracker(); }, true),
+ t_key(s.key_toggle_tracking, [&](bool) -> void { emit_toggle_tracker(); }, true),
+ t_key(s.key_restart_tracking, [&](bool) -> void { emit_restart_tracker(); }, true),
};
global_shortcuts.reload(keys);
diff --git a/opentrack-logic/shortcuts.cpp b/opentrack-logic/shortcuts.cpp
index 06c246af..8d818180 100644
--- a/opentrack-logic/shortcuts.cpp
+++ b/opentrack-logic/shortcuts.cpp
@@ -25,7 +25,7 @@ void Shortcuts::free_binding(K& key)
#endif
}
-void Shortcuts::bind_keyboard_shortcut(K &key, const key_opts& k, unused_on_unix(bool, held))
+void Shortcuts::bind_shortcut(K &key, const key_opts& k, unused_on_unix(bool, held))
{
#if !defined(_WIN32)
using sh = QxtGlobalShortcut;
@@ -98,7 +98,7 @@ void Shortcuts::receiver(const Key& k)
}
#endif
-void Shortcuts::reload(const std::vector<std::tuple<key_opts&, fun, bool>> &keys_)
+void Shortcuts::reload(const t_keys& keys_)
{
const unsigned sz = keys_.size();
keys = std::vector<tt>();
@@ -110,7 +110,7 @@ void Shortcuts::reload(const std::vector<std::tuple<key_opts&, fun, bool>> &keys
const bool held = std::get<2>(kk);
auto fun = std::get<1>(kk);
K k;
- bind_keyboard_shortcut(k, opts, held);
+ bind_shortcut(k, opts, held);
keys.push_back(tt(k, [=](unused_on_unix(bool, flag)) -> void
{
#ifdef _WIN32
diff --git a/opentrack-logic/shortcuts.h b/opentrack-logic/shortcuts.h
index 87e24d4f..d6a87672 100644
--- a/opentrack-logic/shortcuts.h
+++ b/opentrack-logic/shortcuts.h
@@ -45,6 +45,8 @@ public:
using fun = std::function<void(bool)>;
using tt = std::tuple<K, fun, bool>;
+ using t_key = std::tuple<key_opts&, fun, bool>;
+ using t_keys = std::vector<t_key>;
std::vector<tt> keys;
#ifdef _WIN32
KeybindingWorker::Token key_token;
@@ -56,10 +58,10 @@ public:
#endif
{}
- void reload(const std::vector<std::tuple<key_opts&, fun, bool>> &keys_);
+ void reload(const t_keys& keys_);
private:
void free_binding(K& key);
- void bind_keyboard_shortcut(K &key, const key_opts& k, bool held);
+ void bind_shortcut(K &key, const key_opts& k, bool held);
#ifdef _WIN32
void receiver(const Key& k);
#endif