-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.h
139 lines (105 loc) · 3.33 KB
/
common.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef COMMON_H
#define COMMON_H
#include <QObject>
#include <QWidget>
#include <QEvent>
#include <QMouseEvent>
#include <QTimer>
#include <QEventLoop>
#include <QMap>
#include <QSet>
#include <QDebug>
#include <initializer_list>
#include <utility>
template<typename QObject_T, typename... Args>
bool waitForSignal(QObject_T* object, void (QObject_T::*signal)(Args... args), int ms_timeout)
{
QTimer timer;
timer.setSingleShot(true);
QEventLoop loop;
QObject_T::connect(object, signal, &loop, &QEventLoop::quit);
QObject_T::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
timer.start(ms_timeout);
loop.exec();
return timer.isActive();
}
// Bidirectinoal map
template<typename LeftVal, typename RightVal>
class BiMap {
public:
BiMap() = default;
BiMap(const BiMap<LeftVal, RightVal>& other) = default;
BiMap(BiMap<LeftVal, RightVal>&& other) = default;
BiMap(std::initializer_list<std::pair<LeftVal, RightVal>> list) {
for (auto& pair : list) {
insert(pair.first, pair.second);
}
}
BiMap<LeftVal, RightVal>& operator=(const BiMap<LeftVal, RightVal>& other) = default;
bool insert(const LeftVal& left_val, const RightVal& right_val) {
typename QMap<LeftVal, RightVal>::iterator foundLeft =
left_to_right.find(left_val);
if (foundLeft != left_to_right.end()) {
return false;
}
typename QMap<RightVal, LeftVal>::iterator foundRight =
right_to_left.find(right_val);
if (foundRight != right_to_left.end()) {
return false;
}
left_to_right.insert(left_val, right_val);
right_to_left.insert(right_val, left_val);
return true;
}
const RightVal atL(const LeftVal& left,
const RightVal& defaultValue = RightVal()) const {
return left_to_right.value(left, defaultValue);
}
const LeftVal atR(const RightVal& right,
const LeftVal& defaultValue = LeftVal()) const {
return right_to_left.value(right, defaultValue);
}
private:
QMap<LeftVal, RightVal> left_to_right;
QMap<RightVal, LeftVal> right_to_left;
};
/*
* This class generates signals for useful events as the
* QWidget has very scarce set of signals implemented! >:(
*/
class EventSignalAdapter : public QObject {
Q_OBJECT
public:
EventSignalAdapter(QWidget* widget);
QWidget* getWidget();
virtual ~EventSignalAdapter() = default;
signals:
void rClick();
void enter();
void leave();
void focusIn();
void focusOut();
protected:
bool eventFilter(QObject* /*watched*/, QEvent* event) override;
private:
QWidget* widget;
bool right_press_no_leave = false;
};
bool ChangeCssProp(QString& css, const QString& props, const QString& value);
///*
// * Used when you need to handle events and forward them to
// * other objects. Most useful for mouse propagation.
//*/
//class MouseForwarder : public QObject
//{
// Q_OBJECT
//public:
// MouseForwarder(QWidget* to_forward, QWidget* widget);
// MouseForwarder(const QSet<QEvent::Type>& events, QWidget* to_forward, QWidget* widget);
//protected:
// bool eventFilter(QObject* /*watched*/, QEvent* event) override;
//private:
// QWidget* to_forward = nullptr;
// QSet<QEvent::Type> events;
//};
#endif // COMMON_H