Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qoffscreenwindow.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qoffscreenwindow.h"
5#include "qoffscreencommon.h"
6
7#include <qpa/qplatformscreen.h>
8#include <qpa/qwindowsysteminterface.h>
9
10#include <private/qwindow_p.h>
11#include <private/qguiapplication_p.h>
12
14
17 , m_positionIncludesFrame(false)
18 , m_visible(false)
19 , m_pendingGeometryChangeOnShow(true)
20 , m_frameMarginsRequested(frameMarginsEnabled)
21{
22 if (window->windowState() == Qt::WindowNoState) {
24 } else {
25 setWindowState(window->windowStates());
26 }
27
28 static WId counter = 0;
29 m_winId = ++counter;
30
31 m_windowForWinIdHash[m_winId] = this;
32}
33
35{
38 m_windowForWinIdHash.remove(m_winId);
39}
40
42{
43 if (window()->windowState() != Qt::WindowNoState)
44 return;
45
46 m_positionIncludesFrame = qt_window_private(window())->positionPolicy == QWindowPrivate::WindowFrameInclusive;
47
48 setFrameMarginsEnabled(m_frameMarginsRequested);
49 setGeometryImpl(rect);
50
51 m_normalGeometry = geometry();
52}
53
54void QOffscreenWindow::setGeometryImpl(const QRect &rect)
55{
56 QRect adjusted = rect;
57 if (adjusted.width() <= 0)
58 adjusted.setWidth(1);
59 if (adjusted.height() <= 0)
60 adjusted.setHeight(1);
61
62 if (m_positionIncludesFrame) {
63 adjusted.translate(m_margins.left(), m_margins.top());
64 } else {
65 // make sure we're not placed off-screen
66 if (adjusted.left() < m_margins.left())
67 adjusted.translate(m_margins.left(), 0);
68 if (adjusted.top() < m_margins.top())
69 adjusted.translate(0, m_margins.top());
70 }
71
73
74 if (m_visible) {
77 } else {
78 m_pendingGeometryChangeOnShow = true;
79 }
80}
81
83{
84 if (visible == m_visible)
85 return;
86
87 if (visible) {
88 if (window()->type() != Qt::ToolTip)
90
91 if (m_pendingGeometryChangeOnShow) {
92 m_pendingGeometryChangeOnShow = false;
94 }
95 }
96
97 const QPoint cursorPos = QCursor::pos();
98 if (visible) {
102 QWindowSystemInterface::handleLeaveEvent<QWindowSystemInterface::SynchronousDelivery>
104 }
105 if (geometry().contains(cursorPos))
107 window()->mapFromGlobal(cursorPos), cursorPos);
108 } else {
110 if (window()->type() & Qt::Window) {
111 if (QWindow *windowUnderMouse = QGuiApplication::topLevelAt(cursorPos)) {
113 windowUnderMouse->mapFromGlobal(cursorPos),
114 cursorPos);
115 }
116 }
117 }
118
119 m_visible = visible;
120}
121
127
129{
130 return m_winId;
131}
132
134{
135 return m_margins;
136}
137
138void QOffscreenWindow::setFrameMarginsEnabled(bool enabled)
139{
140 if (enabled
142 && (parent() == nullptr)) {
143 m_margins = QMargins(2, 2, 2, 2);
144 } else {
145 m_margins = QMargins(0, 0, 0, 0);
146 }
147}
148
150{
151 setFrameMarginsEnabled(m_frameMarginsRequested && !(state & Qt::WindowFullScreen));
152 m_positionIncludesFrame = false;
153
155 ; // nothing to do
156 else if (state & Qt::WindowFullScreen)
157 setGeometryImpl(screen()->geometry());
158 else if (state & Qt::WindowMaximized)
159 setGeometryImpl(screen()->availableGeometry().adjusted(m_margins.left(), m_margins.top(), -m_margins.right(), -m_margins.bottom()));
160 else
161 setGeometryImpl(m_normalGeometry);
162
164}
165
167{
168 return m_windowForWinIdHash.value(id, nullptr);
169}
170
171Q_CONSTINIT QHash<WId, QOffscreenWindow *> QOffscreenWindow::m_windowForWinIdHash;
172
static QPoint pos()
Returns the position of the cursor (hot spot) of the primary screen in global screen coordinates.
Definition qcursor.cpp:188
static QWindow * currentMouseWindow
static QWindow * topLevelAt(const QPoint &pos)
Returns the top level window at the given position pos, if any.
\inmodule QtCore
Definition qmargins.h:24
constexpr int bottom() const noexcept
Returns the bottom margin.
Definition qmargins.h:115
constexpr int left() const noexcept
Returns the left margin.
Definition qmargins.h:106
constexpr int right() const noexcept
Returns the right margin.
Definition qmargins.h:112
constexpr int top() const noexcept
Returns the top margin.
Definition qmargins.h:109
static QPlatformWindow * windowContainingCursor
QOffscreenWindow(QWindow *window, bool frameMarginsEnabled)
void setWindowState(Qt::WindowStates states) override
Requests setting the window state of this surface to type.
static QOffscreenWindow * windowForWinId(WId id)
WId winId() const override
Reimplement in subclasses to return a handle to the native window.
void requestActivateWindow() override
Reimplement to let Qt be able to request activation/focus for a window.
void setGeometry(const QRect &rect) override
This function is called by Qt whenever a window is moved or resized using the QWindow API.
QMargins frameMargins() const override
void setVisible(bool visible) override
Reimplemented in subclasses to show the surface if visible is true, and hide it if visible is false.
The QPlatformWindow class provides an abstraction for top-level windows.
virtual QPoint mapFromGlobal(const QPoint &pos) const
Translates the global screen coordinate pos to window coordinates using native methods.
QWindow * window() const
Returns the window which belongs to the QPlatformWindow.
QPlatformScreen * screen() const override
Returns the platform screen handle corresponding to this platform window, or null if the window is no...
QPlatformWindow * parent() const
Returns the parent platform window (or \nullptr if orphan).
virtual void setGeometry(const QRect &rect)
This function is called by Qt whenever a window is moved or resized using the QWindow API.
virtual QRect geometry() const
Returns the current geometry of a window.
QRect windowGeometry() const
Returns the QWindow geometry.
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr int height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:239
constexpr int top() const noexcept
Returns the y-coordinate of the rectangle's top edge.
Definition qrect.h:176
constexpr int left() const noexcept
Returns the x-coordinate of the rectangle's left edge.
Definition qrect.h:173
constexpr void setWidth(int w) noexcept
Sets the width of the rectangle to the given width.
Definition qrect.h:381
constexpr QSize size() const noexcept
Returns the size of the rectangle.
Definition qrect.h:242
constexpr void translate(int dx, int dy) noexcept
Moves the rectangle dx along the x axis and dy along the y axis, relative to the current position.
Definition qrect.h:245
constexpr int width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:236
constexpr void setHeight(int h) noexcept
Sets the height of the rectangle to the given height.
Definition qrect.h:384
The QRegion class specifies a clip region for a painter.
Definition qregion.h:27
static QWindowPrivate * get(QWindow *window)
Definition qwindow_p.h:106
static void handleFocusWindowChanged(QWindow *window, Qt::FocusReason r=Qt::OtherFocusReason)
static void handleGeometryChange(QWindow *window, const QRect &newRect)
static bool handleExposeEvent(QWindow *window, const QRegion &region)
static void handleEnterEvent(QWindow *window, const QPointF &local=QPointF(), const QPointF &global=QPointF())
static void handleWindowStateChanged(QWindow *window, Qt::WindowStates newState, int oldState=-1)
\inmodule QtGui
Definition qwindow.h:63
rect
[4]
else opt state
[0]
Combined button and popup list for selecting options.
@ WindowFullScreen
Definition qnamespace.h:255
@ WindowNoState
Definition qnamespace.h:252
@ WindowMinimized
Definition qnamespace.h:253
@ WindowMaximized
Definition qnamespace.h:254
@ FramelessWindowHint
Definition qnamespace.h:225
@ ToolTip
Definition qnamespace.h:213
@ Window
Definition qnamespace.h:207
@ ActiveWindowFocusReason
static bool contains(const QJsonArray &haystack, unsigned needle)
Definition qopengl.cpp:116
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLenum type
GLbitfield flags
GLuint counter
Q_GUI_EXPORT QWindowPrivate * qt_window_private(QWindow *window)
Definition qwindow.cpp:2950
aWidget window() -> setWindowTitle("New Window Title")
[2]