deskflow/src/apps/deskflow-gui/widgets/KeySequenceWidget.cpp

141 lines
3 KiB
C++
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* Deskflow -- mouse and keyboard sharing utility
2016-09-07 14:24:00 +00:00
* Copyright (C) 2012-2016 Symless Ltd.
2012-06-10 16:50:54 +00:00
* Copyright (C) 2008 Volker Lanz (vl@fidra.de)
*
2012-06-10 16:50:54 +00:00
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
2012-06-10 16:50:54 +00:00
* This package 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 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 "KeySequenceWidget.h"
2014-02-14 16:21:02 +00:00
#include <QMouseEvent>
#include <iostream>
2012-06-10 16:50:54 +00:00
KeySequenceWidget::KeySequenceWidget(QWidget *parent, const KeySequence &seq)
: QPushButton(parent),
m_KeySequence(seq),
m_BackupSequence(seq),
m_Status(Stopped),
m_MousePrefix("mousebutton("),
m_MousePostfix(")"),
m_KeyPrefix("keystroke("),
m_KeyPostfix(")")
{
setFocusPolicy(Qt::NoFocus);
updateOutput();
2012-06-10 16:50:54 +00:00
}
void KeySequenceWidget::setKeySequence(const KeySequence &seq)
{
keySequence() = seq;
backupSequence() = seq;
2012-06-10 16:50:54 +00:00
setStatus(Stopped);
updateOutput();
2012-06-10 16:50:54 +00:00
}
void KeySequenceWidget::mousePressEvent(QMouseEvent *event)
{
event->accept();
2012-06-10 16:50:54 +00:00
if (status() == Stopped) {
startRecording();
return;
}
2012-06-10 16:50:54 +00:00
if (m_KeySequence.appendMouseButton(event->button()))
stopRecording();
2012-06-10 16:50:54 +00:00
updateOutput();
2012-06-10 16:50:54 +00:00
}
void KeySequenceWidget::startRecording()
{
keySequence() = KeySequence();
setDown(true);
setFocus();
grabKeyboard();
setStatus(Recording);
2012-06-10 16:50:54 +00:00
}
void KeySequenceWidget::stopRecording()
{
if (!keySequence().valid()) {
keySequence() = backupSequence();
updateOutput();
}
setDown(false);
focusNextChild();
releaseKeyboard();
setStatus(Stopped);
2024-10-21 04:08:10 +00:00
Q_EMIT keySequenceChanged();
2012-06-10 16:50:54 +00:00
}
bool KeySequenceWidget::event(QEvent *event)
{
if (status() == Recording) {
switch (event->type()) {
case QEvent::KeyPress:
keyPressEvent(static_cast<QKeyEvent *>(event));
return true;
case QEvent::MouseButtonRelease:
event->accept();
return true;
case QEvent::ShortcutOverride:
event->accept();
return true;
case QEvent::FocusOut:
stopRecording();
if (!valid()) {
keySequence() = backupSequence();
updateOutput();
}
break;
default:
break;
}
}
return QPushButton::event(event);
2012-06-10 16:50:54 +00:00
}
void KeySequenceWidget::keyPressEvent(QKeyEvent *event)
{
event->accept();
2012-06-10 16:50:54 +00:00
if (status() == Stopped)
return;
2012-06-10 16:50:54 +00:00
if (m_KeySequence.appendKey(event->key(), event->modifiers()))
stopRecording();
2012-06-10 16:50:54 +00:00
updateOutput();
2012-06-10 16:50:54 +00:00
}
void KeySequenceWidget::updateOutput()
{
QString s;
2012-06-10 16:50:54 +00:00
if (m_KeySequence.isMouseButton())
s = mousePrefix() + m_KeySequence.toString() + mousePostfix();
else
s = keyPrefix() + m_KeySequence.toString() + keyPostfix();
2012-06-10 16:50:54 +00:00
setText(s);
2012-06-10 16:50:54 +00:00
}