diff --git a/CMakeLists.txt b/CMakeLists.txt index ddd07b2..42735b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ qt_add_executable(kak-qt src/rpc/color.cpp src/rpc/attribute.hpp src/rpc/rpc.cpp + src/keybindings.cpp src/drawoptions.cpp src/kakounesession.cpp src/kakouneclient.cpp diff --git a/src/kakounewidget.cpp b/src/kakounewidget.cpp index 4f7cf81..20c1b05 100644 --- a/src/kakounewidget.cpp +++ b/src/kakounewidget.cpp @@ -60,3 +60,8 @@ void KakouneWidget::clientRefreshed() repaint(); emit refresh(); } + +void KakouneWidget::installEventFilter(QObject *filter) +{ + m_textedit->installEventFilter(filter); +} diff --git a/src/kakounewidget.hpp b/src/kakounewidget.hpp index db35057..84a8635 100644 --- a/src/kakounewidget.hpp +++ b/src/kakounewidget.hpp @@ -17,6 +17,8 @@ class KakouneWidget : public QWidget QWidget *parent = nullptr); ~KakouneWidget(); + void installEventFilter(QObject *filter); + QUuid getID(); KakouneClient *getClient(); diff --git a/src/keybindings.cpp b/src/keybindings.cpp new file mode 100644 index 0000000..7900f82 --- /dev/null +++ b/src/keybindings.cpp @@ -0,0 +1,29 @@ +#include "keybindings.hpp" + +QList keybindings = { + {QKeyCombination(Qt::ControlModifier | Qt::ShiftModifier, Qt::Key_H), + [](MainWindow *window) { window->focusLeft(); }}, + {QKeyCombination(Qt::ControlModifier | Qt::ShiftModifier, Qt::Key_L), + [](MainWindow *window) { window->focusRight(); }}, +}; + +KeyBindingsFilter::KeyBindingsFilter(MainWindow *main_window) : m_main_window(main_window) +{ +} + +bool KeyBindingsFilter::eventFilter(QObject *object, QEvent *event) +{ + if (event->type() == QEvent::KeyPress) + { + QKeyEvent *keyEvent = static_cast(event); + for (const KeyBinding &binding : keybindings) + { + if (binding.keyCombination == keyEvent->keyCombination()) + { + binding.action(m_main_window); + return true; + } + } + } + return false; +} diff --git a/src/keybindings.hpp b/src/keybindings.hpp new file mode 100644 index 0000000..3ab83a9 --- /dev/null +++ b/src/keybindings.hpp @@ -0,0 +1,26 @@ +#ifndef KEYBINDINGS_HPP +#define KEYBINDINGS_HPP + +#include "mainwindow.hpp" +#include + +struct KeyBinding +{ + QKeyCombination keyCombination; + std::function action; +}; + +class KeyBindingsFilter : public QObject +{ + Q_OBJECT + public: + KeyBindingsFilter(MainWindow *main_window); + + protected: + bool eventFilter(QObject *object, QEvent *event); + + private: + MainWindow *m_main_window; +}; + +#endif diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index e059506..23a15fb 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,4 +1,5 @@ #include "mainwindow.hpp" +#include "keybindings.hpp" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { @@ -9,7 +10,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) m_session = new KakouneSession(); - m_root = new QSplitter(parent); + m_root = new QSplitter(this); this->newClient(); setCentralWidget(m_root); @@ -32,6 +33,7 @@ void MainWindow::newClient() void MainWindow::newClient(const QString &arguments) { KakouneWidget *kakwidget = new KakouneWidget(m_session->getSessionId(), m_draw_options, arguments, m_root); + kakwidget->installEventFilter(new KeyBindingsFilter(this)); connect(kakwidget, &KakouneWidget::finished, m_root, [=]() { kakwidget->setParent(nullptr); m_windows.removeOne(kakwidget); @@ -57,3 +59,27 @@ void MainWindow::focusWindow(const QString &uuid) } } } + +void MainWindow::focusLeft() +{ + QWidget *focused_widget = qApp->focusWidget(); + int index = m_root->indexOf((QWidget *)focused_widget->parent()); // TODO + if (index <= 0) + { + return; + } + + m_windows[index - 1]->setFocus(); +} + +void MainWindow::focusRight() +{ + QWidget *focused_widget = qApp->focusWidget(); + int index = m_root->indexOf((QWidget *)focused_widget->parent()); // TODO + if (index >= m_windows.size() - 1) + { + return; + } + + m_windows[index + 1]->setFocus(); +} diff --git a/src/mainwindow.hpp b/src/mainwindow.hpp index 71d0b76..feebca5 100644 --- a/src/mainwindow.hpp +++ b/src/mainwindow.hpp @@ -15,6 +15,9 @@ class MainWindow : public QMainWindow MainWindow(QWidget *parent = nullptr); ~MainWindow(); + void focusLeft(); + void focusRight(); + public slots: void newClient(); void newClient(const QString &arguments);