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

pull/127/head
f4exb 2018-01-05 09:59:37 +01:00
rodzic 5526ccd5bd
commit b82e667240
3 zmienionych plików z 29 dodań i 47 usunięć

Wyświetl plik

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

Wyświetl plik

@ -17,14 +17,12 @@
#include "editcommanddialog.h"
#include "ui_editcommanddialog.h"
#include "commands/command.h"
#include "commandkeyreceiver.h"
#include <QFileInfo>
#include <QFileDialog>
#include <QKeyEvent>
#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) :
QDialog(parent),
ui(new Ui::EditCommandDialog),
@ -35,10 +33,14 @@ EditCommandDialog::EditCommandDialog(const QStringList& groups, const QString& g
ui->group->lineEdit()->setText(group);
setKeyAssociate();
setKeyLabel();
m_commandKeyReceiver = new CommandKeyReceiver();
this->installEventFilter(m_commandKeyReceiver);
}
EditCommandDialog::~EditCommandDialog()
{
m_commandKeyReceiver->deleteLater();
delete ui;
}
@ -153,35 +155,13 @@ void EditCommandDialog::on_keyCapture_toggled(bool checked)
{
ui->keyCapture->setFocus();
ui->keyCapture->setFocusPolicy(Qt::StrongFocus);
connect(m_commandKeyReceiver, SIGNAL(capturedKey(Qt::Key, Qt::KeyboardModifiers, bool)),
this, SLOT(commandKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool)));
}
else
{
ui->keyCapture->setFocusPolicy(Qt::NoFocus);
ui->keyCapture->clearFocus();
}
}
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);
disconnect(m_commandKeyReceiver, SIGNAL(capturedKey(Qt::Key, Qt::KeyboardModifiers, bool)),
this, SLOT(commandKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool)));
ui->keyCapture->setFocusPolicy(Qt::NoFocus);
ui->keyCapture->clearFocus();
}
@ -213,12 +193,6 @@ void EditCommandDialog::fromCommand(const Command& command)
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()
{
if (m_key == 0)
@ -249,3 +223,13 @@ void EditCommandDialog::setKeyAssociate()
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);
}

Wyświetl plik

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