forked from easymodo/qimgv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortcutbuilder.cpp
95 lines (93 loc) · 3.27 KB
/
shortcutbuilder.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "shortcutbuilder.h"
QString ShortcutBuilder::fromEvent(QEvent *event) {
QKeyEvent *keyEvent = dynamic_cast<QKeyEvent *>(event);
QWheelEvent *wheelEvent = dynamic_cast<QWheelEvent *>(event);
QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent *>(event);
if(keyEvent) {
return processKeyEvent(keyEvent);
} else if(wheelEvent) {
return processWheelEvent(wheelEvent);
} else if(mouseEvent) {
return processMouseEvent(mouseEvent);
}
return "";
}
//------------------------------------------------------------------------------
QString ShortcutBuilder::processWheelEvent(QWheelEvent *event) {
QString sequence;
if(event->angleDelta().ry() < 0) {
sequence = "WheelUp";
} else if(event->angleDelta().ry() > 0) {
sequence = "WheelDown";
}
sequence.prepend(modifierKeys(event));
return sequence;
}
//------------------------------------------------------------------------------
// Detects mouse button clicks only
// DoubleClick works only for LMB
// Otherwise treated as a regular click
QString ShortcutBuilder::processMouseEvent(QMouseEvent *event) {
QString sequence;
if(event->button() == Qt::XButton1) {
sequence = "XButton1";
}
if(event->button() == Qt::XButton2) {
sequence = "XButton2";
}
if(event->button() == Qt::LeftButton) {
sequence = "LMB";
}
if(event->button() == Qt::RightButton) {
sequence = "RMB";
}
if(event->button() == Qt::MiddleButton) {
sequence = "MiddleButton";
}
sequence.prepend(modifierKeys(event));
// ignore everything except MouseButtonPress and double clicks
if(event->type() == QEvent::MouseButtonDblClick && event->button() == Qt::LeftButton) {
sequence.append("_DoubleClick");
return sequence;
} else if(event->type() == QEvent::MouseButtonPress) {
return sequence;
}
return "";
}
//------------------------------------------------------------------------------
QString ShortcutBuilder::processKeyEvent(QKeyEvent *event) {
QString sequence;
if(event->type() == QEvent::KeyPress) {
sequence = inputMap->keys()[event->nativeScanCode()];
if(!sequence.isEmpty()) {
sequence.prepend(modifierKeys(event));
}
}
return sequence;
}
//------------------------------------------------------------------------------
QString ShortcutBuilder::modifierKeys(QEvent *event){
QKeyEvent *keyEvent = dynamic_cast<QKeyEvent *>(event);
QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent *>(event);
QWheelEvent *wheelEvent = dynamic_cast<QWheelEvent *>(event);
QString mods;
QMapIterator<QString, Qt::KeyboardModifier> i(inputMap->modifiers());
while(i.hasNext()) {
i.next();
if(keyEvent) {
if(keyEvent->modifiers().testFlag(i.value())) {
mods.append(i.key());
}
} else if(wheelEvent){
if(wheelEvent->modifiers().testFlag(i.value())) {
mods.append(i.key());
}
} else if(mouseEvent) {
if(mouseEvent->modifiers().testFlag(i.value())) {
mods.append(i.key());
}
}
}
return mods;
}
//------------------------------------------------------------------------------