1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2025-09-04 06:07:49 -04:00

Commands: use the command key event filter to capture key sequences in the edit command dialog window

This commit is contained in:
f4exb 2018-01-05 09:59:37 +01:00
parent 5526ccd5bd
commit b82e667240
3 changed files with 29 additions and 47 deletions

View File

@ -39,10 +39,10 @@ bool CommandKeyReceiver::eventFilter(QObject* obj, QEvent* event)
Qt::KeyboardModifiers keyModifiers; Qt::KeyboardModifiers keyModifiers;
keyEventHandler(keyEvent, key, keyModifiers); keyEventHandler(keyEvent, key, keyModifiers);
emit capturedKey(key, keyModifiers, false); emit capturedKey(key, keyModifiers, false);
}
if (!m_pass) { if (!m_pass) { // do not pass the event
return true; return true;
}
} }
} }
else if (m_release && (event->type()==QEvent::KeyRelease)) else if (m_release && (event->type()==QEvent::KeyRelease))
@ -56,14 +56,14 @@ bool CommandKeyReceiver::eventFilter(QObject* obj, QEvent* event)
Qt::KeyboardModifiers keyModifiers; Qt::KeyboardModifiers keyModifiers;
keyEventHandler(keyEvent, key, keyModifiers); keyEventHandler(keyEvent, key, keyModifiers);
emit capturedKey(key, keyModifiers, true); emit capturedKey(key, keyModifiers, true);
}
if (!m_pass) { if (!m_pass) { // do not pass the event
return true; return true;
}
} }
} }
return QObject::eventFilter(obj, event); return QObject::eventFilter(obj, event); // pass the event on
} }
void CommandKeyReceiver::keyEventHandler(QKeyEvent *e, Qt::Key& key, Qt::KeyboardModifiers& keyModifiers) void CommandKeyReceiver::keyEventHandler(QKeyEvent *e, Qt::Key& key, Qt::KeyboardModifiers& keyModifiers)

View File

@ -17,14 +17,12 @@
#include "editcommanddialog.h" #include "editcommanddialog.h"
#include "ui_editcommanddialog.h" #include "ui_editcommanddialog.h"
#include "commands/command.h" #include "commands/command.h"
#include "commandkeyreceiver.h"
#include <QFileInfo> #include <QFileInfo>
#include <QFileDialog> #include <QFileDialog>
#include <QKeyEvent>
#include <algorithm> #include <algorithm>
const std::vector<Qt::Key> EditCommandDialog::m_composeKeys = {Qt::Key_Shift, Qt::Key_Control, Qt::Key_Meta, Qt::Key_Alt, Qt::Key_AltGr};
EditCommandDialog::EditCommandDialog(const QStringList& groups, const QString& group, QWidget* parent) : EditCommandDialog::EditCommandDialog(const QStringList& groups, const QString& group, QWidget* parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::EditCommandDialog), ui(new Ui::EditCommandDialog),
@ -35,10 +33,14 @@ EditCommandDialog::EditCommandDialog(const QStringList& groups, const QString& g
ui->group->lineEdit()->setText(group); ui->group->lineEdit()->setText(group);
setKeyAssociate(); setKeyAssociate();
setKeyLabel(); setKeyLabel();
m_commandKeyReceiver = new CommandKeyReceiver();
this->installEventFilter(m_commandKeyReceiver);
} }
EditCommandDialog::~EditCommandDialog() EditCommandDialog::~EditCommandDialog()
{ {
m_commandKeyReceiver->deleteLater();
delete ui; delete ui;
} }
@ -153,35 +155,13 @@ void EditCommandDialog::on_keyCapture_toggled(bool checked)
{ {
ui->keyCapture->setFocus(); ui->keyCapture->setFocus();
ui->keyCapture->setFocusPolicy(Qt::StrongFocus); ui->keyCapture->setFocusPolicy(Qt::StrongFocus);
connect(m_commandKeyReceiver, SIGNAL(capturedKey(Qt::Key, Qt::KeyboardModifiers, bool)),
this, SLOT(commandKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool)));
} }
else else
{ {
ui->keyCapture->setFocusPolicy(Qt::NoFocus); disconnect(m_commandKeyReceiver, SIGNAL(capturedKey(Qt::Key, Qt::KeyboardModifiers, bool)),
ui->keyCapture->clearFocus(); this, SLOT(commandKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool)));
}
}
void EditCommandDialog::keyPressEvent(QKeyEvent *e)
{
if (ui->keyCapture->isChecked() && (!isComposeKey(static_cast<Qt::Key>(e->key()))))
{
m_key = static_cast<Qt::Key>(e->key());
if (e->modifiers())
{
//qDebug("EditCommandDialog::keyPressEvent: has modifiers: %x", QFlags<Qt::KeyboardModifier>::Int(e->modifiers()));
m_keyModifiers = e->modifiers();
}
else
{
m_keyModifiers = Qt::NoModifier;
}
setKeyAssociate();
setKeyLabel();
//qDebug("EditCommandDialog::keyPressEvent: key: %x", m_key);
ui->keyCapture->setChecked(false);
ui->keyCapture->setFocusPolicy(Qt::NoFocus); ui->keyCapture->setFocusPolicy(Qt::NoFocus);
ui->keyCapture->clearFocus(); ui->keyCapture->clearFocus();
} }
@ -213,12 +193,6 @@ void EditCommandDialog::fromCommand(const Command& command)
ui->keyRelease->setChecked(command.getRelease()); ui->keyRelease->setChecked(command.getRelease());
} }
bool EditCommandDialog::isComposeKey(Qt::Key key)
{
auto it = std::find(m_composeKeys.begin(), m_composeKeys.end(), key);
return it != m_composeKeys.end();
}
void EditCommandDialog::setKeyLabel() void EditCommandDialog::setKeyLabel()
{ {
if (m_key == 0) if (m_key == 0)
@ -249,3 +223,13 @@ void EditCommandDialog::setKeyAssociate()
ui->keyAssociate->setEnabled(true); ui->keyAssociate->setEnabled(true);
} }
} }
void EditCommandDialog::commandKeyPressed(Qt::Key key, Qt::KeyboardModifiers keyModifiers, bool release __attribute__((unused)))
{
// qDebug("EditCommandDialog::commandKeyPressed: key: %x", m_key);
// qDebug("EditCommandDialog::commandKeyPressed: has modifiers: %x", QFlags<Qt::KeyboardModifier>::Int(keyModifiers));
m_key = key;
m_keyModifiers = keyModifiers;
setKeyLabel();
ui->keyCapture->setChecked(false);
}

View File

@ -25,6 +25,7 @@ namespace Ui {
} }
class Command; class Command;
class CommandKeyReceiver;
class EditCommandDialog : public QDialog { class EditCommandDialog : public QDialog {
Q_OBJECT Q_OBJECT
@ -56,18 +57,15 @@ private:
Ui::EditCommandDialog* ui; Ui::EditCommandDialog* ui;
Qt::Key m_key; Qt::Key m_key;
Qt::KeyboardModifiers m_keyModifiers; Qt::KeyboardModifiers m_keyModifiers;
CommandKeyReceiver *m_commandKeyReceiver;
virtual void keyPressEvent(QKeyEvent *e);
bool isComposeKey(Qt::Key key);
void setKeyLabel(); void setKeyLabel();
void setKeyAssociate(); void setKeyAssociate();
static const std::vector<Qt::Key> m_composeKeys;
private slots: private slots:
void on_showFileDialog_clicked(bool checked); void on_showFileDialog_clicked(bool checked);
void on_keyCapture_toggled(bool checked); void on_keyCapture_toggled(bool checked);
void commandKeyPressed(Qt::Key key, Qt::KeyboardModifiers keyModifiers, bool release);
}; };