-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickView.cpp
381 lines (347 loc) · 10.2 KB
/
QuickView.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include "./QuickView.h"
#include "./Settings.h"
#include "./Utils.h"
#include <QQmlContext>
#include <QQmlEngine>
#include <QQuickWindow>
#include <QRect>
#include <QScreen>
#include <QThread>
QT_UTILS_NAMESPACE_BEGIN
//! Settings version
static constexpr int s_version = 2;
//!
//! Constructor
//!
QuickView::QuickView(void)
: m_Persistence(PersistenceFlags::All)
, m_Maximized(false)
, m_FullScreen(false)
, m_Flags{}
{
// make the view control the root object size
this->setResizeMode(QQuickView::ResizeMode::SizeRootObjectToView);
// install event filtering
this->installEventFilter(this);
}
//!
//! Set the fullscreen state
//!
void QuickView::SetFullScreen(bool value)
{
if (m_FullScreen != value)
{
// note: don't remove the maxmized state: when we exit fullscreen, we want to go back to maximized.
m_FullScreen = value;
if (m_FullScreen == true)
{
// backup the state of the window
m_Flags = this->flags();
m_WindowedGeometry = GetRestoreRect();
// make it a frameless window occupying the whole screen
this->setFlags(Qt::Window | Qt::FramelessWindowHint);
this->setPosition({ 0, 0 });
this->resize(this->screen()->geometry().size());
}
else
{
// restore the window as it was previously
this->setFlags(m_Flags);
this->setPosition(m_WindowedGeometry.topLeft());
this->resize(m_WindowedGeometry.size());
this->setVisibility(m_Maximized == true ? QWindow::Maximized : QWindow::Windowed);
}
}
emit fullscreenChanged(m_FullScreen);
}
//!
//! Set the window to maximized.
//!
void QuickView::SetMaximized(bool value)
{
if (m_Maximized != value)
{
m_Maximized = value;
if (m_Maximized == true)
{
this->showMaximized();
}
else
{
this->showNormal();
}
emit maximizedChanged(m_Maximized);
}
}
//!
//! Set the minimized state of the window
//!
void QuickView::SetMinimized(bool value)
{
if (m_Minimized != value)
{
m_Minimized = value;
if (m_Minimized == true)
{
this->showMinimized();
}
else
{
if (m_Maximized == true)
{
this->showMaximized();
}
else
{
this->showNormal();
}
}
emit minimizedChanged(m_Minimized);
}
}
//!
//! Set the restore state for position
//!
void QuickView::SetPersistence(Persistence value)
{
if (m_Persistence != value)
{
m_Persistence = value;
Settings::Set("RootView.Persistence", static_cast< int >(static_cast< QFlag >(m_Persistence)));
emit persistenceChanged(m_Persistence);
}
}
//!
//! Restore the settings. This must be called before the view is ready.
//!
//! @param width
//! Default width (used on the first run)
//!
//! @param height
//! Default height (used on the first run)
//!
//! @param visibility
//! Default visibility (used on the first run)
//!
void QuickView::Restore(int width, int height, QWindow::Visibility visibility)
{
// get the settings version
int version = Settings::Get("RootView.Version", 0);
bool firstTime = version == 0;
// update the settings
switch (version)
{
case 0:
Settings::Remove("RootView.RestorePosition");
Settings::Remove("RootView.RestoreSize");
Settings::Remove("RootView.RestoreMaximized");
Settings::Remove("RootView.RestoreFullScreen");
Settings::Remove("RootView.FirstTime");
Settings::Remove("RootView.Init");
Settings::Set("RootView.Persistence", static_cast< unsigned int >(m_Persistence));
case 1:
Settings::Remove("RootView.ForceFullScreen");
Settings::Remove("RootView.FullScreen");
default:
break;
}
// read the settings
m_WindowedGeometry = {
Settings::Get("RootView.Position", this->position()),
Settings::Get("RootView.Size", this->size())
};
m_Maximized = Settings::Get("RootView.Maximized", m_Maximized);
m_Persistence = QFlag(Settings::Get("RootView.Persistence", static_cast< int >(static_cast< QFlag >(m_Persistence))));
// notify if maximized is true
if (m_Maximized == true)
{
emit maximizedChanged(m_Maximized);
}
// sometimes when we disconnect a screen or change resolution (or some bug happens) the restored geometry
// can be entirely outside the availabe screen space. Make sure that the window will be partly visible.
// note that this is not perfect, but it should handle the most common setups and avoid the application
// window being entirely out of the available screen areas.
const QRect geom = this->screen()->availableVirtualGeometry();
if (m_WindowedGeometry.right() < 0)
{
m_WindowedGeometry.moveLeft(0);
if (m_WindowedGeometry.right() > geom.width())
{
m_WindowedGeometry.setWidth(geom.width());
}
}
if (m_WindowedGeometry.x() > geom.width())
{
if (m_WindowedGeometry.width() > geom.width())
{
m_WindowedGeometry.moveLeft(0);
m_WindowedGeometry.setWidth(geom.width());
}
else
{
m_WindowedGeometry.moveLeft(geom.width() - m_WindowedGeometry.width());
}
}
if (m_WindowedGeometry.bottom() < 0)
{
m_WindowedGeometry.moveTop(0);
if (m_WindowedGeometry.bottom() > geom.height())
{
m_WindowedGeometry.setHeight(geom.height());
}
}
if (m_WindowedGeometry.y() > geom.height())
{
if (m_WindowedGeometry.height() > geom.height())
{
m_WindowedGeometry.moveTop(0);
m_WindowedGeometry.setHeight(geom.height());
}
else
{
m_WindowedGeometry.moveTop(geom.height() - m_WindowedGeometry.height());
}
}
// restore states as needed
if (firstTime == false)
{
if (m_Persistence.testFlag(PersistenceFlags::Position) == true)
{
this->setPosition(m_WindowedGeometry.topLeft());
}
if (m_Persistence.testFlag(PersistenceFlags::Size) == true)
{
this->resize(m_WindowedGeometry.size());
}
if (m_Persistence.testFlag(PersistenceFlags::Maximized) == true)
{
this->setVisibility(m_Maximized == true ? QWindow::Maximized : QWindow::Windowed);
}
}
// first time, set the options that were passed to the method
else
{
this->setVisibility(visibility);
this->resize(width, height);
m_WindowedGeometry = this->geometry();
}
}
//!
//! Local helper to change a variable and notify if the value actually changed
//!
#define SET_PROPERTY(prop, value, notify) \
if (prop != value) \
{ \
prop = value; \
notify(prop); \
}
//!
//! At some point in time, I had to implement this method to be able to catch the "about to be
//! closed" event in order to save the view's settings, all because there no fucking way to just
//! have a virtual "onClose" method, like most everything else (try Google'ing this, a ton of
//! people have the same problem, also there's a closing signals, but
//! https://bugreports.qt.io/browse/QTBUG-55722... 3 years later, still have to install a filter
//! that will be called for EACH f...ing event, just to do some stuff when I'm about to be closed)
//!
//! But then I started noticing some bugs in how the view is restored depending on its state, and
//! (more annoyingly) depending on the platform.
//!
//! And now, I'm wondering what's the fucking point of a GUI abstraction library if it fucking
//! behaves differently on each fucking platform ????? Why the hell on Windows, frameless window
//! are set to fullscreen when calling showMaximized whereas on all other platforms it correctly
//! set the state to Maximized ?
//!
//! Why the fuck do I receive move and resize events before the state is set to maximized on Windows
//! whereas on Linux I receive the maximized event before the move and resize ones ??????
//!
//! Anyways, the whole mess of this method is to try and fix the fucking inconsistencies of Qt -_-
//!
bool QuickView::eventFilter(QObject * watched, QEvent * event)
{
switch (event->type())
{
#ifndef WINDOWS
case QEvent::Move:
case QEvent::Resize:
if (m_Maximized == false && m_FullScreen == false)
{
m_WindowedGeometry = this->geometry();
}
#endif
case QEvent::WindowStateChange:
qDebug() << this->windowState();
switch (this->windowState())
{
case Qt::WindowState::WindowMinimized:
SET_PROPERTY(m_Minimized, true, minimizedChanged)
break;
case Qt::WindowState::WindowMaximized:
SET_PROPERTY(m_Minimized, false, minimizedChanged)
SET_PROPERTY(m_Maximized, true, maximizedChanged)
SET_PROPERTY(m_FullScreen, false, fullscreenChanged)
break;
case Qt::WindowState::WindowFullScreen:
// ... for fuck's sake ...
if (this->windowStates().testFlag(Qt::WindowState::WindowMaximized))
{
SET_PROPERTY(m_Minimized, false, minimizedChanged)
SET_PROPERTY(m_Maximized, true, maximizedChanged)
SET_PROPERTY(m_FullScreen, false, fullscreenChanged)
}
else
{
// ok, real fullscreen state, buggy stuff, avoid at all cost
Q_ASSERT(false && "Do not use the fullscreen visibility. Use QuickView's custom fullscreen property instead");
}
break;
case Qt::WindowState::WindowNoState:
// ok, if we come from a minimized state, we need to restore the "maximized"
if (m_Minimized == true)
{
SET_PROPERTY(m_Minimized, false, minimizedChanged);
if (m_Maximized)
{
this->showMaximized();
}
}
else if (m_FullScreen == false)
{
SET_PROPERTY(m_Maximized, false, maximizedChanged)
}
break;
default:
break;
}
break;
case QEvent::Close:
m_WindowedGeometry = GetRestoreRect();
Settings::Set("RootView.Position", m_WindowedGeometry.topLeft(), false);
Settings::Set("RootView.Size", m_WindowedGeometry.size(), false);
Settings::Set("RootView.Maximized", m_Maximized, false);
Settings::Set("RootView.Version", s_version, false);
Settings::Sync();
break;
default:
break;
}
// let the base class handle the event
return QQuickView::eventFilter(watched, event);
}
//!
//! Helper used to get the correct restore rect.
//!
QRect QuickView::GetRestoreRect(void) const
{
#ifdef WINDOWS
WINDOWPLACEMENT placement{};
GetWindowPlacement(reinterpret_cast< HWND >(this->winId()), &placement);
RECT & rect = placement.rcNormalPosition;
return {
QPoint{ rect.left, rect.top },
QSize{ rect.right - rect.left, rect.bottom - rect.top },
};
#else
return m_WindowedGeometry;
#endif
}
QT_UTILS_NAMESPACE_END