forked from stijnherfst/HiveWE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowHandler.h
32 lines (28 loc) · 1 KB
/
WindowHandler.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
#pragma once
class WindowHandler : public QObject {
Q_OBJECT
public:
std::vector<std::pair<std::string, QWidget*>> windows;
/// Creates a window of type T if one doesn't exist yet. Otherwise it raises to the foreground and activates the window.
template<typename T>
T* create_or_raise(QWidget* parent, bool& created) {
auto found = std::find_if(windows.begin(), windows.end(), [&](const auto& item) { return item.first == typeid(T).name(); });
if (found != windows.end()) {
T* window = dynamic_cast<T*>((*found).second);
window->raise();
window->activateWindow();
created = false;
return window;
} else {
T* window = new T(parent);
windows.push_back(std::make_pair(typeid(T).name(), dynamic_cast<QWidget*>(window)));
connect(window, &T::destroyed, [this, window]() {
auto found = std::find_if(windows.begin(), windows.end(), [&](const auto& item) { return item.second == window; });
windows.erase(found);
});
created = true;
return window;
}
return nullptr;
}
};