-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqdmswidget.hpp
111 lines (87 loc) · 1.84 KB
/
qdmswidget.hpp
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
#ifndef QDMSWIDGET_H
#define QDMSWIDGET_H
#include <QWidget>
#include <QObject>
#include <QMenuBar>
#ifdef _WIN32
#include <windows.h>
class Mutex {
public:
Mutex() { InitializeCriticalSection(&cs); }
~Mutex() { DeleteCriticalSection(&cs); }
void lock() { EnterCriticalSection(&cs); }
void unlock() { LeaveCriticalSection(&cs); }
private:
CRITICAL_SECTION cs;
Mutex(const Mutex &) = delete;
Mutex &operator=(const Mutex &) = delete;
};
template <typename M>
class LockGuard {
public:
explicit LockGuard(M &mutex) : m_mutex(mutex) {
m_mutex.lock();
}
~LockGuard() {
m_mutex.unlock();
}
private:
M &m_mutex;
LockGuard(const LockGuard&) = delete;
LockGuard &operator=(const LockGuard&) = delete;
};
#endif
enum class WidgetType {
WINDOW,
TAB,
FLOATING_DOCK,
};
class QDMSWidget : public QWidget
{
Q_OBJECT
public:
QDMSWidget(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags())
:QWidget(parent, f) {}
bool isCloseBlocked()
{
#ifdef _WIN32
LockGuard<Mutex> lock(mutex);
#else
std::lock_guard<std::mutex> lock(mutex);
#endif
return m_blockClose;
}
void blockClose()
{
#ifdef _WIN32
LockGuard<Mutex> lock(mutex);
#else
std::lock_guard<std::mutex> lock(mutex);
#endif
m_blockClose = true;
}
void unblockClose()
{
#ifdef _WIN32
LockGuard<Mutex> lock(mutex);
#else
std::lock_guard<std::mutex> lock(mutex);
#endif
m_blockClose = false;
}
WidgetType type() const { return m_type; }
void setType(const WidgetType &t)
{
m_type = t;
}
private:
std::atomic<bool> m_blockClose;
#ifdef _WIN32
Mutex mutex;
#else
std::mutex mutex;
#endif
WidgetType m_type;
QMenuBar *menuBar{nullptr};
};
#endif // QDMSWIDGET_H