Repository navigation
Expand file tree
/
Copy pathQelPopupManager.cpp
More file actions
312 lines (266 loc) · 8.93 KB
/
Copy pathQelPopupManager.cpp
File metadata and controls
312 lines (266 loc) · 8.93 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "QelPopupManager.h"
#include <QApplication>
#include <QScreen>
#include <QWidget>
namespace qel {
namespace {
bool isTop(PopupPlacement placement)
{
return placement == PopupPlacement::TopStart
|| placement == PopupPlacement::Top
|| placement == PopupPlacement::TopEnd;
}
bool isBottom(PopupPlacement placement)
{
return placement == PopupPlacement::BottomStart
|| placement == PopupPlacement::Bottom
|| placement == PopupPlacement::BottomEnd;
}
bool isLeft(PopupPlacement placement)
{
return placement == PopupPlacement::LeftStart
|| placement == PopupPlacement::Left
|| placement == PopupPlacement::LeftEnd;
}
PopupPlacement oppositePlacement(PopupPlacement placement)
{
switch (placement) {
case PopupPlacement::TopStart:
return PopupPlacement::BottomStart;
case PopupPlacement::Top:
return PopupPlacement::Bottom;
case PopupPlacement::TopEnd:
return PopupPlacement::BottomEnd;
case PopupPlacement::BottomStart:
return PopupPlacement::TopStart;
case PopupPlacement::Bottom:
return PopupPlacement::Top;
case PopupPlacement::BottomEnd:
return PopupPlacement::TopEnd;
case PopupPlacement::LeftStart:
return PopupPlacement::RightStart;
case PopupPlacement::Left:
return PopupPlacement::Right;
case PopupPlacement::LeftEnd:
return PopupPlacement::RightEnd;
case PopupPlacement::RightStart:
return PopupPlacement::LeftStart;
case PopupPlacement::Right:
return PopupPlacement::Left;
case PopupPlacement::RightEnd:
return PopupPlacement::LeftEnd;
}
return PopupPlacement::Bottom;
}
QPoint candidatePosition(const QRect &anchorRect,
const QSize &popupSize,
PopupPlacement placement,
int offset)
{
const int centeredX = anchorRect.center().x() - popupSize.width() / 2;
const int centeredY = anchorRect.center().y() - popupSize.height() / 2;
switch (placement) {
case PopupPlacement::TopStart:
return QPoint(anchorRect.left(), anchorRect.top() - popupSize.height() - offset);
case PopupPlacement::Top:
return QPoint(centeredX, anchorRect.top() - popupSize.height() - offset);
case PopupPlacement::TopEnd:
return QPoint(anchorRect.right() - popupSize.width() + 1,
anchorRect.top() - popupSize.height() - offset);
case PopupPlacement::BottomStart:
return QPoint(anchorRect.left(), anchorRect.bottom() + 1 + offset);
case PopupPlacement::Bottom:
return QPoint(centeredX, anchorRect.bottom() + 1 + offset);
case PopupPlacement::BottomEnd:
return QPoint(anchorRect.right() - popupSize.width() + 1,
anchorRect.bottom() + 1 + offset);
case PopupPlacement::LeftStart:
return QPoint(anchorRect.left() - popupSize.width() - offset, anchorRect.top());
case PopupPlacement::Left:
return QPoint(anchorRect.left() - popupSize.width() - offset, centeredY);
case PopupPlacement::LeftEnd:
return QPoint(anchorRect.left() - popupSize.width() - offset,
anchorRect.bottom() - popupSize.height() + 1);
case PopupPlacement::RightStart:
return QPoint(anchorRect.right() + 1 + offset, anchorRect.top());
case PopupPlacement::Right:
return QPoint(anchorRect.right() + 1 + offset, centeredY);
case PopupPlacement::RightEnd:
return QPoint(anchorRect.right() + 1 + offset,
anchorRect.bottom() - popupSize.height() + 1);
}
return QPoint(anchorRect.left(), anchorRect.bottom() + 1 + offset);
}
bool overflowsMainAxis(const QPoint &position,
const QSize &popupSize,
PopupPlacement placement,
const QRect &available)
{
if (isTop(placement)) {
return position.y() < available.top();
}
if (isBottom(placement)) {
return position.y() + popupSize.height() - 1 > available.bottom();
}
if (isLeft(placement)) {
return position.x() < available.left();
}
return position.x() + popupSize.width() - 1 > available.right();
}
QRect paddedAvailableGeometry(QScreen *screen, int padding)
{
QRect available = screen->availableGeometry();
const int safePadding = qMax(0, padding);
const QRect padded =
available.adjusted(safePadding, safePadding, -safePadding, -safePadding);
return padded.isValid() ? padded : available;
}
} // namespace
QList<QelPopupManager::PopupRecord> QelPopupManager::popups_;
int QelPopupManager::zIndexSeed_ = 2000;
QPoint QelPopupManager::computePopupPosition(QWidget *anchor,
QSize popupSize,
PopupPlacement placement,
int offset,
PopupPlacement *resolvedPlacement)
{
return computePopupPosition(
anchor,
popupSize,
placement,
offset,
QList<PopupPlacement>(),
0,
resolvedPlacement);
}
QPoint QelPopupManager::computePopupPosition(
QWidget *anchor,
QSize popupSize,
PopupPlacement placement,
int offset,
const QList<PopupPlacement> &fallbackPlacements,
int boundariesPadding,
PopupPlacement *resolvedPlacement)
{
if (resolvedPlacement != nullptr) {
*resolvedPlacement = placement;
}
if (anchor == nullptr) {
return QPoint(0, 0);
}
const QPoint globalTopLeft = anchor->mapToGlobal(QPoint(0, 0));
const QRect anchorRect(globalTopLeft, anchor->size());
QScreen *screen = anchor->screen();
if (screen == nullptr) {
screen = QApplication::primaryScreen();
}
QPoint result = candidatePosition(anchorRect, popupSize, placement, offset);
if (screen == nullptr) {
return result;
}
const QRect available =
paddedAvailableGeometry(screen, boundariesPadding);
QList<PopupPlacement> candidates;
candidates.append(placement);
if (fallbackPlacements.isEmpty()) {
candidates.append(oppositePlacement(placement));
} else {
for (PopupPlacement fallback : fallbackPlacements) {
if (!candidates.contains(fallback)) {
candidates.append(fallback);
}
}
}
for (PopupPlacement candidate : candidates) {
const QPoint candidatePoint =
candidatePosition(anchorRect, popupSize, candidate, offset);
if (!overflowsMainAxis(
candidatePoint,
popupSize,
candidate,
available)) {
result = candidatePoint;
if (resolvedPlacement != nullptr) {
*resolvedPlacement = candidate;
}
break;
}
}
const int maxX = qMax(
available.left(),
available.right() - popupSize.width() + 1);
const int maxY = qMax(
available.top(),
available.bottom() - popupSize.height() + 1);
result.setX(qBound(available.left(), result.x(), maxX));
result.setY(qBound(available.top(), result.y(), maxY));
return result;
}
int QelPopupManager::acquireZIndex(PopupType)
{
++zIndexSeed_;
return zIndexSeed_;
}
void QelPopupManager::registerPopup(QWidget *popup, PopupPolicy policy)
{
if (popup == nullptr) {
return;
}
for (int i = popups_.size() - 1; i >= 0; --i) {
if (popups_.at(i).popup == nullptr) {
popups_.removeAt(i);
}
}
if (policy.exclusive) {
closeAll(policy.type);
}
for (int i = 0; i < popups_.size(); ++i) {
if (popups_.at(i).popup == popup) {
popups_[i].policy = policy;
return;
}
}
popups_.append({popup, policy});
}
void QelPopupManager::closeAll(PopupType type)
{
for (int i = popups_.size() - 1; i >= 0; --i) {
PopupRecord &record = popups_[i];
if (record.popup == nullptr) {
popups_.removeAt(i);
continue;
}
if (record.policy.type == type) {
record.popup->hide();
}
}
}
void QelPopupManager::handleScreenBoundary(QWidget *popup)
{
if (popup == nullptr) {
return;
}
QScreen *screen = popup->screen();
if (screen == nullptr) {
screen = QApplication::primaryScreen();
}
if (screen == nullptr) {
return;
}
const QRect available = screen->availableGeometry();
QRect geometry = popup->geometry();
if (geometry.right() > available.right()) {
geometry.moveRight(available.right());
}
if (geometry.left() < available.left()) {
geometry.moveLeft(available.left());
}
if (geometry.bottom() > available.bottom()) {
geometry.moveBottom(available.bottom());
}
if (geometry.top() < available.top()) {
geometry.moveTop(available.top());
}
popup->setGeometry(geometry);
}
} // namespace qel