-
Notifications
You must be signed in to change notification settings - Fork 1
/
ef_core.hpp
195 lines (150 loc) · 4.05 KB
/
ef_core.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
This file is part of ef.qt.
Copyright (C) 2019 ClassicOldSong
Copyright (C) 2019 ReimuNotMoe
This program is free software: you can redistribute it and/or modify
it under the terms of the MIT License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef EF_CORE_HPP
#define EF_CORE_HPP
#include <QString>
#include <QObject>
#include <QLayout>
#include <QFrame>
#include <QSpacerItem>
#include <deque>
#include <memory>
#include <functional>
namespace ef::core {
class EFSpacerItem : public QSpacerItem {
private:
int _width = 1, _height = 1;
QSizePolicy::Policy hp = QSizePolicy::Minimum, vp = QSizePolicy::Minimum;
public:
EFSpacerItem(int __w = 1, int __h = 1,
QSizePolicy::Policy __hp = QSizePolicy::Minimum, QSizePolicy::Policy __vp = QSizePolicy::Minimum);
void setSizePolicy(QSizePolicy::Policy __hp, QSizePolicy::Policy __vp);;
void setSize(int w, int h);
};
class EFMountingPoint {
protected:
QWidget *parent_widget = nullptr;
QBoxLayout *parent_layout = nullptr;
QWidget *placeholder_widget = nullptr;
QWidget *mounted_widget = nullptr;
size_t placeholder_index();
public:
void __set_widget(QWidget *__pw);
QWidget *get() const noexcept {
return mounted_widget;
}
void mount(QWidget *__w);
void unmount();
EFMountingPoint &operator=(QWidget *__w);
};
class EFListMountingPoint {
protected:
QWidget *parent_widget = nullptr;
QBoxLayout *parent_layout = nullptr;
QWidget *placeholder_widget = nullptr;
std::deque<QWidget *> mounted_widget;
size_t placeholder_index();
bool widget_precheck(QWidget *__w);
public:
void __set_widget(QWidget *__pw);
void push_back(QWidget *__w);
void push_front(QWidget *__w);
void pop_back();
void pop_front();
void insert(size_t __idx, QWidget *__w);
void erase(size_t __idx);
void erase_widget(QWidget *__w);
size_t size() const noexcept {
return mounted_widget.size();
}
const std::deque<QWidget *>& get() const noexcept {
return mounted_widget;
}
// TODO: Bulk operation
// void set(const std::deque<QWidget *>& __wl) {
//
// }
// EFListMountingPoint &operator=(const std::deque<QWidget *>& __wl);
};
template <typename T>
class EFVar {
private:
T _raw;
std::deque<std::shared_ptr<std::function<void(const T&)>>> subscribers;
void __call_subscribers() {
for (auto &it : subscribers) {
(*it)(_raw);
}
}
public:
void subscribe(const std::shared_ptr<std::function<void(const T&)>>& __callback) {
subscribers.emplace_back(__callback);
(*subscribers.back())(_raw);
}
void unsubscribe(const std::shared_ptr<std::function<void(const T&)>>& __callback) {
for (size_t i=0; i<subscribers.size(); i++) {
if (subscribers[i] == __callback) {
subscribers.erase(subscribers.begin() + i);
return;
}
}
}
friend inline void swap(EFVar& __first, EFVar& __second) noexcept {
using std::swap;
swap(__first._raw, __second._raw);
}
EFVar<T>() = default;
explicit EFVar<T>(const T& __ival) {
_raw = __ival;
}
inline EFVar<T> &operator=(EFVar<T> __val) {
swap(*this, __val);
return *this;
};
template <typename T2>
inline EFVar &operator=(T2 __val) {
_raw = __val;
__call_subscribers();
return *this;
}
inline T operator+(EFVar<T> __val) {
return _raw + __val._raw;
}
inline bool operator==(const EFVar<T>& __val) {
return _raw == __val._raw;
}
inline bool operator!=(const EFVar<T>& __val) {
return _raw != __val._raw;
}
inline bool operator==(const T& __val) {
return _raw == __val;
}
inline bool operator!=(const T& __val) {
return _raw != __val;
}
inline explicit operator bool() {
if (_raw)
return true;
else
return false;
}
inline auto operator[](int __idx) {
return _raw[__idx];
}
inline T operator*() const noexcept {
return _raw;
}
explicit inline operator T() const noexcept {
return _raw;
}
};
}
#endif // EF_CORE_HPP