-
Notifications
You must be signed in to change notification settings - Fork 1
/
ef_core.cpp
193 lines (156 loc) · 4.56 KB
/
ef_core.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
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
/*
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.
*/
#include "ef_core.hpp"
using namespace ef::core;
EFSpacerItem::EFSpacerItem(int __w, int __h, QSizePolicy::Policy __hp, QSizePolicy::Policy __vp) :
QSpacerItem(__w, __h, __hp, __vp) {
}
void EFSpacerItem::setSizePolicy(QSizePolicy::Policy __hp, QSizePolicy::Policy __vp) {
hp = __hp;
vp = __vp;
changeSize(_width, _height, hp, vp);
}
void EFSpacerItem::setSize(int w, int h) {
_width = w;
_height = h;
changeSize(_width, _height, hp, vp);
}
size_t EFMountingPoint::placeholder_index() {
for (size_t i=0; i<parent_layout->count(); i++) {
if (parent_layout->itemAt(i)->widget() == placeholder_widget) {
return i;
}
}
throw std::logic_error("placeholder not found in parent layout");
}
void EFMountingPoint::__set_widget(QWidget *__pw) {
parent_layout = (QBoxLayout *)__pw->layout();
parent_widget = __pw;
placeholder_widget = new QFrame(parent_widget);
if (parent_layout)
parent_layout->addWidget(placeholder_widget);
placeholder_widget->hide();
}
void EFMountingPoint::mount(QWidget *__w) {
if (parent_widget == __w) {
throw std::logic_error("self can't be its child");
}
if (mounted_widget == __w) {
qDebug("efqt warning: mounting same widget");
return;
}
unmount();
__w->setParent(parent_widget);
parent_layout->insertWidget(placeholder_index(), __w);
mounted_widget = __w;
}
void EFMountingPoint::unmount() {
if (mounted_widget){
parent_layout->removeWidget(mounted_widget);
mounted_widget->setParent(nullptr);
mounted_widget = nullptr;
}
}
EFMountingPoint &EFMountingPoint::operator=(QWidget *__w) {
if (__w)
mount(__w);
else
unmount();
return *this;
}
size_t EFListMountingPoint::placeholder_index() {
for (size_t i=0; i<parent_layout->count(); i++) {
if (parent_layout->itemAt(i)->widget() == placeholder_widget) {
return i;
}
}
throw std::logic_error("placeholder not found in parent layout");
}
bool EFListMountingPoint::widget_precheck(QWidget *__w) {
if (parent_widget == __w) {
throw std::logic_error("self can't be its child");
}
for (auto &it : mounted_widget) {
if (it == __w) {
qDebug("efqt warning: mounting same widget");
return false;
}
}
return true;
}
void EFListMountingPoint::__set_widget(QWidget *__pw) {
parent_layout = (QBoxLayout *)__pw->layout();
parent_widget = __pw;
placeholder_widget = new QFrame(parent_widget);
if (parent_layout)
parent_layout->addWidget(placeholder_widget);
placeholder_widget->hide();
}
void EFListMountingPoint::push_back(QWidget *__w) {
if (!widget_precheck(__w))
return;
__w->setParent(parent_widget);
parent_layout->insertWidget(placeholder_index(), __w);
mounted_widget.emplace_back(__w);
}
void EFListMountingPoint::push_front(QWidget *__w) {
if (!widget_precheck(__w))
return;
__w->setParent(parent_widget);
parent_layout->insertWidget(placeholder_index() - mounted_widget.size(), __w);
mounted_widget.emplace_front(__w);
}
void EFListMountingPoint::pop_back() {
if (!mounted_widget.empty()) {
auto &it = mounted_widget.back();
if (parent_layout)
parent_layout->removeWidget(it);
it->setParent(nullptr);
mounted_widget.pop_back();
}
}
void EFListMountingPoint::pop_front() {
if (!mounted_widget.empty()) {
auto &it = mounted_widget.front();
if (parent_layout)
parent_layout->removeWidget(it);
it->setParent(nullptr);
mounted_widget.pop_front();
}
}
void EFListMountingPoint::insert(size_t __idx, QWidget *__w) {
if (__idx > mounted_widget.size()) {
throw std::logic_error("");
}
if (!widget_precheck(__w))
return;
__w->setParent(parent_widget);
parent_layout->insertWidget(placeholder_index() - mounted_widget.size() + __idx, __w);
mounted_widget.insert(mounted_widget.begin() + __idx, __w);
}
void EFListMountingPoint::erase(size_t __idx) {
if (__idx < mounted_widget.size()) {
auto &it = mounted_widget[__idx];
if (parent_layout)
parent_layout->removeWidget(it);
it->setParent(nullptr);
mounted_widget.erase(mounted_widget.begin() + __idx);
}
}
void EFListMountingPoint::erase_widget(QWidget *__w) {
for (size_t i=0; i<mounted_widget.size(); i++) {
if (mounted_widget[i] == __w) {
mounted_widget.erase(mounted_widget.begin() + i);
}
}
parent_layout->removeWidget(__w);
__w->setParent(nullptr);
}