From 5526ccd5bd14ad690b63b5d4ac9ec338ce768600 Mon Sep 17 00:00:00 2001 From: f4exb Date: Fri, 5 Jan 2018 05:05:12 +0100 Subject: [PATCH] Commands: optional trap of keyboard input in main window to be able to trigger commands --- sdrgui/CMakeLists.txt | 2 + sdrgui/gui/commandkeyreceiver.cpp | 87 +++++++++++++++++++++++++++++++ sdrgui/gui/commandkeyreceiver.h | 50 ++++++++++++++++++ sdrgui/mainwindow.cpp | 43 +++++++++++++++ sdrgui/mainwindow.h | 6 +++ 5 files changed, 188 insertions(+) create mode 100644 sdrgui/gui/commandkeyreceiver.cpp create mode 100644 sdrgui/gui/commandkeyreceiver.h diff --git a/sdrgui/CMakeLists.txt b/sdrgui/CMakeLists.txt index ab0d005ca..42f9132e0 100644 --- a/sdrgui/CMakeLists.txt +++ b/sdrgui/CMakeLists.txt @@ -10,6 +10,7 @@ set(sdrgui_SOURCES gui/clickablelabel.cpp gui/colormapper.cpp gui/commanditem.cpp + gui/commandkeyreceiver.cpp gui/commandoutputdialog.cpp gui/cwkeyergui.cpp gui/editcommanddialog.cpp @@ -64,6 +65,7 @@ set(sdrgui_HEADERS gui/channelwindow.h gui/colormapper.h gui/commanditem.h + gui/commandkeyreceiver.h gui/commandoutputdialog.h gui/cwkeyergui.h gui/editcommanddialog.h diff --git a/sdrgui/gui/commandkeyreceiver.cpp b/sdrgui/gui/commandkeyreceiver.cpp new file mode 100644 index 000000000..c586c89c7 --- /dev/null +++ b/sdrgui/gui/commandkeyreceiver.cpp @@ -0,0 +1,87 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Edouard Griffiths, F4EXB // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include + +const std::vector CommandKeyReceiver::m_composeKeys = {Qt::Key_Shift, Qt::Key_Control, Qt::Key_Meta, Qt::Key_Alt, Qt::Key_AltGr}; + +CommandKeyReceiver::CommandKeyReceiver() : + m_release(false), + m_pass(true) +{ +} + +bool CommandKeyReceiver::eventFilter(QObject* obj, QEvent* event) +{ + if (event->type() == QEvent::KeyPress) + { + QKeyEvent* keyEvent = static_cast(event); + + if ((!keyEvent->isAutoRepeat()) && (!isComposeKey(static_cast(keyEvent->key())))) + { +// qDebug("KeyReceiver::eventFilter: KeyPress"); + Qt::Key key; + Qt::KeyboardModifiers keyModifiers; + keyEventHandler(keyEvent, key, keyModifiers); + emit capturedKey(key, keyModifiers, false); + } + + if (!m_pass) { + return true; + } + } + else if (m_release && (event->type()==QEvent::KeyRelease)) + { + QKeyEvent* keyEvent = static_cast(event); + + if ((!keyEvent->isAutoRepeat()) && (!isComposeKey(static_cast(keyEvent->key())))) + { +// qDebug("KeyReceiver::eventFilter: KeyRelease"); + Qt::Key key; + Qt::KeyboardModifiers keyModifiers; + keyEventHandler(keyEvent, key, keyModifiers); + emit capturedKey(key, keyModifiers, true); + } + + if (!m_pass) { + return true; + } + } + + return QObject::eventFilter(obj, event); +} + +void CommandKeyReceiver::keyEventHandler(QKeyEvent *e, Qt::Key& key, Qt::KeyboardModifiers& keyModifiers) +{ + key = static_cast(e->key()); + + if (e->modifiers()) + { + keyModifiers = e->modifiers(); + } + else + { + keyModifiers = Qt::NoModifier; + } +} + +bool CommandKeyReceiver::isComposeKey(Qt::Key key) +{ + auto it = std::find(m_composeKeys.begin(), m_composeKeys.end(), key); + return it != m_composeKeys.end(); +} diff --git a/sdrgui/gui/commandkeyreceiver.h b/sdrgui/gui/commandkeyreceiver.h new file mode 100644 index 000000000..5dce1d45f --- /dev/null +++ b/sdrgui/gui/commandkeyreceiver.h @@ -0,0 +1,50 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Edouard Griffiths, F4EXB // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef SDRGUI_GUI_COMMANDKEYRECEIVER_H_ +#define SDRGUI_GUI_COMMANDKEYRECEIVER_H_ + +#include + +class QKeyEvent; + +class CommandKeyReceiver : public QObject +{ + Q_OBJECT +public: + CommandKeyReceiver(); + + void setRelease(bool release) { m_release = release; } + void setPass(bool release) { m_release = release; } + +protected: + bool eventFilter(QObject* obj, QEvent* event); + +private: + bool m_release; //!< check release events + bool m_pass; //!< do not block events just tap them + + void keyEventHandler(QKeyEvent *e, Qt::Key& key, Qt::KeyboardModifiers& keyModifiers); + bool isComposeKey(Qt::Key key); + + static const std::vector m_composeKeys; + +signals: + void capturedKey(Qt::Key key, Qt::KeyboardModifiers keyModifiers, bool release); +}; + + +#endif /* SDRGUI_GUI_COMMANDKEYRECEIVER_H_ */ diff --git a/sdrgui/mainwindow.cpp b/sdrgui/mainwindow.cpp index d79b316f2..939aa996c 100644 --- a/sdrgui/mainwindow.cpp +++ b/sdrgui/mainwindow.cpp @@ -15,6 +15,7 @@ // along with this program. If not, see . // /////////////////////////////////////////////////////////////////////////////////// +#include #include #include #include @@ -24,6 +25,7 @@ #include #include #include +#include #include #include @@ -183,11 +185,18 @@ MainWindow::MainWindow(qtwebapp::LoggerWithFile *logger, const MainParser& parse m_apiServer = new WebAPIServer(parser.getServerAddress(), parser.getServerPort(), m_requestMapper); m_apiServer->start(); + connect(qApp, SIGNAL(focusChanged(QWidget *, QWidget *)), this, SLOT(focusHasChanged(QWidget *, QWidget *))); + m_commandKeyReceiver = new CommandKeyReceiver(); + m_commandKeyReceiver->setRelease(true); + this->installEventFilter(m_commandKeyReceiver); + qDebug() << "MainWindow::MainWindow: end"; } MainWindow::~MainWindow() { + m_commandKeyReceiver->deleteLater(); + m_apiServer->stop(); delete m_apiServer; delete m_requestMapper; @@ -963,6 +972,23 @@ void MainWindow::on_commandOutput_clicked() } } +void MainWindow::on_commandKeyboardConnect_toggled(bool checked) +{ + qDebug("on_commandKeyboardConnect_toggled: %s", checked ? "true" : "false"); + + if (checked) + { + setFocus(); + connect(m_commandKeyReceiver, SIGNAL(capturedKey(Qt::Key, Qt::KeyboardModifiers, bool)), + this, SLOT(commandKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool))); + } + else + { + disconnect(m_commandKeyReceiver, SIGNAL(capturedKey(Qt::Key, Qt::KeyboardModifiers, bool)), + this, SLOT(commandKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool))); + } +} + void MainWindow::on_presetSave_clicked() { QStringList groups; @@ -1501,3 +1527,20 @@ void MainWindow::setLoggingOptions() m_logger->setUseFileLogger(m_settings.getUseLogFile()); } + +void MainWindow::focusHasChanged(QWidget *oldWidget __attribute__((unused)), QWidget *newWidget) +{ + // this is the hard way: +// if (ui->commandKeyboardConnect->isChecked() && (newWidget != this)) { +// setFocus(); +// } + // this is the soft way: + if (newWidget != this) { + ui->commandKeyboardConnect->setChecked(false); + } +} + +void MainWindow::commandKeyPressed(Qt::Key key, Qt::KeyboardModifiers keyModifiers, bool release) +{ + qDebug("MainWindow::commandKeyPressed: key: %x mod: %x %s", (int) key, (int) keyModifiers, release ? "release" : "press"); +} diff --git a/sdrgui/mainwindow.h b/sdrgui/mainwindow.h index b15ff8835..b40b6ad39 100644 --- a/sdrgui/mainwindow.h +++ b/sdrgui/mainwindow.h @@ -56,6 +56,7 @@ class WebAPIServer; class WebAPIAdapterGUI; class Preset; class Command; +class CommandKeyReceiver; namespace qtwebapp { class LoggerWithFile; @@ -311,6 +312,8 @@ private: WebAPIServer *m_apiServer; WebAPIAdapterGUI *m_apiAdapter; + CommandKeyReceiver *m_commandKeyReceiver; + void loadSettings(); void loadPresetSettings(const Preset* preset, int tabIndex); void savePresetSettings(Preset* preset, int tabIndex); @@ -350,6 +353,7 @@ private slots: void on_commandDelete_clicked(); void on_commandRun_clicked(); void on_commandOutput_clicked(); + void on_commandKeyboardConnect_toggled(bool checked); void on_action_Audio_triggered(); void on_action_Logging_triggered(); void on_action_DV_Serial_triggered(bool checked); @@ -364,6 +368,8 @@ private slots: void on_action_removeLastDevice_triggered(); void on_action_Exit_triggered(); void tabInputViewIndexChanged(); + void focusHasChanged(QWidget *oldWidget, QWidget *newWidget); + void commandKeyPressed(Qt::Key key, Qt::KeyboardModifiers keyModifiers, bool release); }; #endif // INCLUDE_MAINWINDOW_H