1 | /* $Id: QIWithRestorableGeometry.h 103708 2024-03-06 16:13:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - QIWithRestorableGeometry class declaration.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2016-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef FEQT_INCLUDED_SRC_globals_QIWithRestorableGeometry_h
|
---|
29 | #define FEQT_INCLUDED_SRC_globals_QIWithRestorableGeometry_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | /* Qt includes: */
|
---|
35 | #include <QMainWindow>
|
---|
36 | #include <QRect>
|
---|
37 | #include <QResizeEvent>
|
---|
38 |
|
---|
39 | /* GUI includes: */
|
---|
40 | #include "UILibraryDefs.h"
|
---|
41 | #ifdef VBOX_WS_MAC
|
---|
42 | # include "VBoxUtils-darwin.h"
|
---|
43 | #endif
|
---|
44 | #ifdef VBOX_WS_NIX
|
---|
45 | # include "UIDesktopWidgetWatchdog.h"
|
---|
46 | #endif
|
---|
47 |
|
---|
48 |
|
---|
49 | /** Template with geometry saving/restoring capabilities. */
|
---|
50 | template <class Base>
|
---|
51 | class QIWithRestorableGeometry : public Base
|
---|
52 | {
|
---|
53 | public:
|
---|
54 |
|
---|
55 | /** Constructs main window passing @a pParent and @a enmFlags to base-class. */
|
---|
56 | QIWithRestorableGeometry(QWidget *pParent = 0, Qt::WindowFlags enmFlags = Qt::WindowFlags())
|
---|
57 | : Base(pParent, enmFlags)
|
---|
58 | {}
|
---|
59 |
|
---|
60 | protected:
|
---|
61 |
|
---|
62 | /** Handles move @a pEvent. */
|
---|
63 | virtual void moveEvent(QMoveEvent *pEvent) RT_OVERRIDE
|
---|
64 | {
|
---|
65 | /* Call to base-class: */
|
---|
66 | QMainWindow::moveEvent(pEvent);
|
---|
67 |
|
---|
68 | #ifdef VBOX_WS_NIX
|
---|
69 | /* Prevent further handling if fake screen detected: */
|
---|
70 | if (UIDesktopWidgetWatchdog::isFakeScreenDetected())
|
---|
71 | return;
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | /* Prevent handling for yet/already invisible window or if window is in minimized state: */
|
---|
75 | if (this->isVisible() && (this->windowState() & Qt::WindowMinimized) == 0)
|
---|
76 | {
|
---|
77 | #if defined(VBOX_WS_MAC) || defined(VBOX_WS_WIN)
|
---|
78 | /* Use the old approach for OSX/Win: */
|
---|
79 | m_geometry.moveTo(this->frameGeometry().x(), this->frameGeometry().y());
|
---|
80 | #else
|
---|
81 | /* Use the new approach otherwise: */
|
---|
82 | m_geometry.moveTo(this->geometry().x(), this->geometry().y());
|
---|
83 | #endif
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** Handles resize @a pEvent. */
|
---|
88 | virtual void resizeEvent(QResizeEvent *pEvent) RT_OVERRIDE
|
---|
89 | {
|
---|
90 | /* Call to base-class: */
|
---|
91 | QMainWindow::resizeEvent(pEvent);
|
---|
92 |
|
---|
93 | #ifdef VBOX_WS_NIX
|
---|
94 | /* Prevent handling if fake screen detected: */
|
---|
95 | if (UIDesktopWidgetWatchdog::isFakeScreenDetected())
|
---|
96 | return;
|
---|
97 | #endif
|
---|
98 |
|
---|
99 | /* Prevent handling for yet/already invisible window or if window is in minimized state: */
|
---|
100 | if (this->isVisible() && (this->windowState() & Qt::WindowMinimized) == 0)
|
---|
101 | {
|
---|
102 | QResizeEvent *pResizeEvent = static_cast<QResizeEvent*>(pEvent);
|
---|
103 | m_geometry.setSize(pResizeEvent->size());
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Returns whether the window should be maximized when geometry being restored. */
|
---|
108 | virtual bool shouldBeMaximized() const { return false; }
|
---|
109 |
|
---|
110 | /** Restores the window geometry to passed @a rect. */
|
---|
111 | void restoreGeometry(const QRect &rect)
|
---|
112 | {
|
---|
113 | m_geometry = rect;
|
---|
114 | #if defined(VBOX_WS_MAC) || defined(VBOX_WS_WIN)
|
---|
115 | /* Use the old approach for OSX/Win: */
|
---|
116 | this->move(m_geometry.topLeft());
|
---|
117 | this->resize(m_geometry.size());
|
---|
118 | #else
|
---|
119 | /* Use the new approach otherwise: */
|
---|
120 | UIDesktopWidgetWatchdog::setTopLevelGeometry(this, m_geometry);
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | /* Maximize (if necessary): */
|
---|
124 | if (shouldBeMaximized())
|
---|
125 | this->showMaximized();
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** Returns current window geometry. */
|
---|
129 | QRect currentGeometry() const
|
---|
130 | {
|
---|
131 | return m_geometry;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Returns whether the window is currently maximized. */
|
---|
135 | bool isCurrentlyMaximized()
|
---|
136 | {
|
---|
137 | #ifdef VBOX_WS_MAC
|
---|
138 | return ::darwinIsWindowMaximized(this);
|
---|
139 | #else
|
---|
140 | return this->isMaximized();
|
---|
141 | #endif
|
---|
142 | }
|
---|
143 |
|
---|
144 | private:
|
---|
145 |
|
---|
146 | /** Holds the cached window geometry. */
|
---|
147 | QRect m_geometry;
|
---|
148 | };
|
---|
149 |
|
---|
150 | /** Explicit QIWithRestorableGeometry instantiation for QMainWindow class.
|
---|
151 | * @note On Windows it's important that all template cases are instantiated just once across
|
---|
152 | * the linking space. In case we have particular template case instantiated from both
|
---|
153 | * library and executable sides, - we have multiple definition case and need to strictly
|
---|
154 | * ask compiler to do it just once and link such cases against library only.
|
---|
155 | * I would also note that it would be incorrect to just make whole the template exported
|
---|
156 | * to library because latter can have lack of required instantiations (current case). */
|
---|
157 | template class SHARED_LIBRARY_STUFF QIWithRestorableGeometry<QMainWindow>;
|
---|
158 |
|
---|
159 | #endif /* !FEQT_INCLUDED_SRC_globals_QIWithRestorableGeometry_h */
|
---|