-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_input_event_catcher.h
71 lines (61 loc) · 1.74 KB
/
widget_input_event_catcher.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef WIDGET_INPUT_EVENT_CATCHER_H
#define WIDGET_INPUT_EVENT_CATCHER_H
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++" // TODO: Refactor
#include <QWidget>
#include <QObject>
#include <QtGlobal>
#include <QMouseEvent>
#pragma GCC diagnostic pop
class QWidgetInputEventCatcher : public QWidget
{
Q_OBJECT
public:
QWidgetInputEventCatcher(QWidget *parent = nullptr) : QWidget(parent)
{
connect(this, &QWidgetInputEventCatcher::mousePress, [this](QMouseEvent *event){
if(event->button() == Qt::LeftButton)
emit mouseClicked();
});
connect(this, &QWidgetInputEventCatcher::keyPress, [this](QKeyEvent *event){
emit virtualKeyPress(event->nativeVirtualKey());
});
connect(this, &QWidgetInputEventCatcher::keyRelease, [this](QKeyEvent *event){
emit virtualKeyRelease(event->nativeVirtualKey());
});
setFocusPolicy(Qt::StrongFocus);
}
virtual ~QWidgetInputEventCatcher() { }
protected:
virtual void resizeEvent(QResizeEvent*)
{
QWidget *parentPointer = dynamic_cast<QWidget*>(this->parent());
if(parentPointer!=nullptr)
this->setFixedSize(parentPointer->size());
}
virtual void mousePressEvent(QMouseEvent *event)
{
emit mousePress(event);
}
virtual void mouseReleaseEvent(QMouseEvent *event)
{
emit mouseRelease(event);
}
virtual void keyPressEvent(QKeyEvent *event)
{
emit keyPress(event);
}
virtual void keyReleaseEvent(QKeyEvent *event)
{
emit keyRelease(event);
}
signals:
void mousePress(QMouseEvent *event);
void mouseRelease(QMouseEvent *event);
void mouseClicked();
void keyPress(QKeyEvent *event);
void keyRelease(QKeyEvent *event);
void virtualKeyPress(unsigned int virtualKey);
void virtualKeyRelease(unsigned int virtualKey);
};
#endif // WIDGET_INPUT_EVENT_CATCHER_H