1 | /* $Id: QIWithRestorableGeometry.h 100064 2023-06-04 09:10:01Z 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 "UICommon.h"
|
---|
46 | # include "UIDesktopWidgetWatchdog.h"
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | /* Other VBox includes: */
|
---|
50 | #ifdef VBOX_WS_MAC
|
---|
51 | # include "iprt/cpp/utils.h"
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | /** Template with geometry saving/restoring capabilities. */
|
---|
55 | template <class Base>
|
---|
56 | class QIWithRestorableGeometry : public Base
|
---|
57 | {
|
---|
58 | public:
|
---|
59 |
|
---|
60 | /** Constructs main window passing @a pParent and @a enmFlags to base-class. */
|
---|
61 | QIWithRestorableGeometry(QWidget *pParent = 0, Qt::WindowFlags enmFlags = Qt::WindowFlags())
|
---|
62 | : Base(pParent, enmFlags)
|
---|
63 | {}
|
---|
64 |
|
---|
65 | protected:
|
---|
66 |
|
---|
67 | /** Handles move @a pEvent. */
|
---|
68 | virtual void moveEvent(QMoveEvent *pEvent) RT_OVERRIDE
|
---|
69 | {
|
---|
70 | /* Call to base-class: */
|
---|
71 | QMainWindow::moveEvent(pEvent);
|
---|
72 |
|
---|
73 | #ifdef VBOX_WS_NIX
|
---|
74 | /* Prevent further handling if fake screen detected: */
|
---|
75 | if (UIDesktopWidgetWatchdog::isFakeScreenDetected())
|
---|
76 | return;
|
---|
77 | #endif
|
---|
78 |
|
---|
79 | /* Prevent handling for yet/already invisible window or if window is in minimized state: */
|
---|
80 | if (this->isVisible() && (this->windowState() & Qt::WindowMinimized) == 0)
|
---|
81 | {
|
---|
82 | #if defined(VBOX_WS_MAC) || defined(VBOX_WS_WIN)
|
---|
83 | /* Use the old approach for OSX/Win: */
|
---|
84 | m_geometry.moveTo(this->frameGeometry().x(), this->frameGeometry().y());
|
---|
85 | #else
|
---|
86 | /* Use the new approach otherwise: */
|
---|
87 | m_geometry.moveTo(this->geometry().x(), this->geometry().y());
|
---|
88 | #endif
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** Handles resize @a pEvent. */
|
---|
93 | virtual void resizeEvent(QResizeEvent *pEvent) RT_OVERRIDE
|
---|
94 | {
|
---|
95 | /* Call to base-class: */
|
---|
96 | QMainWindow::resizeEvent(pEvent);
|
---|
97 |
|
---|
98 | #ifdef VBOX_WS_NIX
|
---|
99 | /* Prevent handling if fake screen detected: */
|
---|
100 | if (UIDesktopWidgetWatchdog::isFakeScreenDetected())
|
---|
101 | return;
|
---|
102 | #endif
|
---|
103 |
|
---|
104 | /* Prevent handling for yet/already invisible window or if window is in minimized state: */
|
---|
105 | if (this->isVisible() && (this->windowState() & Qt::WindowMinimized) == 0)
|
---|
106 | {
|
---|
107 | QResizeEvent *pResizeEvent = static_cast<QResizeEvent*>(pEvent);
|
---|
108 | m_geometry.setSize(pResizeEvent->size());
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** Returns whether the window should be maximized when geometry being restored. */
|
---|
113 | virtual bool shouldBeMaximized() const { return false; }
|
---|
114 |
|
---|
115 | /** Restores the window geometry to passed @a rect. */
|
---|
116 | void restoreGeometry(const QRect &rect)
|
---|
117 | {
|
---|
118 | m_geometry = rect;
|
---|
119 | #if defined(VBOX_WS_MAC) || defined(VBOX_WS_WIN)
|
---|
120 | /* Use the old approach for OSX/Win: */
|
---|
121 | this->move(m_geometry.topLeft());
|
---|
122 | this->resize(m_geometry.size());
|
---|
123 | #else
|
---|
124 | /* Use the new approach otherwise: */
|
---|
125 | UIDesktopWidgetWatchdog::setTopLevelGeometry(this, m_geometry);
|
---|
126 | #endif
|
---|
127 |
|
---|
128 | /* Maximize (if necessary): */
|
---|
129 | if (shouldBeMaximized())
|
---|
130 | this->showMaximized();
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** Returns current window geometry. */
|
---|
134 | QRect currentGeometry() const
|
---|
135 | {
|
---|
136 | return m_geometry;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** Returns whether the window is currently maximized. */
|
---|
140 | bool isCurrentlyMaximized() const
|
---|
141 | {
|
---|
142 | #ifdef VBOX_WS_MAC
|
---|
143 | return ::darwinIsWindowMaximized(unconst(this));
|
---|
144 | #else
|
---|
145 | return this->isMaximized();
|
---|
146 | #endif
|
---|
147 | }
|
---|
148 |
|
---|
149 | private:
|
---|
150 |
|
---|
151 | /** Holds the cached window geometry. */
|
---|
152 | QRect m_geometry;
|
---|
153 | };
|
---|
154 |
|
---|
155 | /** Explicit QIWithRestorableGeometry instantiation for QMainWindow class.
|
---|
156 | * @note On Windows it's important that all template cases are instantiated just once across
|
---|
157 | * the linking space. In case we have particular template case instantiated from both
|
---|
158 | * library and executable sides, - we have multiple definition case and need to strictly
|
---|
159 | * ask compiler to do it just once and link such cases against library only.
|
---|
160 | * I would also note that it would be incorrect to just make whole the template exported
|
---|
161 | * to library because latter can have lack of required instantiations (current case). */
|
---|
162 | template class SHARED_LIBRARY_STUFF QIWithRestorableGeometry<QMainWindow>;
|
---|
163 |
|
---|
164 | #endif /* !FEQT_INCLUDED_SRC_globals_QIWithRestorableGeometry_h */
|
---|