Commands: optional trap of keyboard input in main window to be able to trigger commands

This commit is contained in:
f4exb 2018-01-05 05:05:12 +01:00
parent 9de6aabee6
commit 5526ccd5bd
5 changed files with 188 additions and 0 deletions

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <gui/commandkeyreceiver.h>
#include <QEvent>
#include <QKeyEvent>
const std::vector<Qt::Key> 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<QKeyEvent*>(event);
if ((!keyEvent->isAutoRepeat()) && (!isComposeKey(static_cast<Qt::Key>(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<QKeyEvent*>(event);
if ((!keyEvent->isAutoRepeat()) && (!isComposeKey(static_cast<Qt::Key>(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<Qt::Key>(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();
}

View File

@ -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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef SDRGUI_GUI_COMMANDKEYRECEIVER_H_
#define SDRGUI_GUI_COMMANDKEYRECEIVER_H_
#include <QObject>
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<Qt::Key> m_composeKeys;
signals:
void capturedKey(Qt::Key key, Qt::KeyboardModifiers keyModifiers, bool release);
};
#endif /* SDRGUI_GUI_COMMANDKEYRECEIVER_H_ */

View File

@ -15,6 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <gui/commandkeyreceiver.h>
#include <QInputDialog>
#include <QMessageBox>
#include <QLabel>
@ -24,6 +25,7 @@
#include <QTextStream>
#include <QDateTime>
#include <QSysInfo>
#include <QKeyEvent>
#include <plugin/plugininstancegui.h>
#include <plugin/plugininstancegui.h>
@ -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");
}

View File

@ -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