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
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qquickpopupwindow_p_p.h"
#include "qquickcombobox_p.h"
#include "qquickpopup_p.h"
#include "qquickpopup_p_p.h"
#include "qquickmenu_p_p.h"
#include "qquickmenubar_p_p.h"
#include "qquickpopupitem_p_p.h"
#include <QtGui/private/qguiapplication_p.h>
#include <QtCore/qloggingcategory.h>
#include <QtGui/private/qeventpoint_p.h>
#include <QtQuick/private/qquickitem_p.h>
#include <QtQuick/private/qquickwindowmodule_p.h>
#include <QtQuick/private/qquickwindowmodule_p_p.h>
#include <qpa/qplatformwindow_p.h>
QT_BEGIN_NAMESPACE
Q_LOGGING_CATEGORY(lcPopupWindow, "qt.quick.controls.popup.window")
class QQuickPopupWindowPrivate : public QQuickWindowQmlImplPrivate
{
Q_DECLARE_PUBLIC(QQuickPopupWindow)
public:
QPointer<QQuickItem> m_popupItem;
QPointer<QQuickPopup> m_popup;
void forwardEventToParentMenuOrMenuBar(QEvent *event);
};
QQuickPopupWindow::QQuickPopupWindow(QQuickPopup *popup, QWindow *parent)
: QQuickWindowQmlImpl(*(new QQuickPopupWindowPrivate), nullptr)
{
Q_D(QQuickPopupWindow);
d->m_popup = popup;
d->m_popupItem = popup->popupItem();
setTransientParent(parent);
connect(d->m_popup, &QQuickPopup::windowChanged, this, &QQuickPopupWindow::windowChanged);
connect(d->m_popup, &QQuickPopup::implicitWidthChanged, this, &QQuickPopupWindow::implicitWidthChanged);
connect(d->m_popup, &QQuickPopup::implicitHeightChanged, this, &QQuickPopupWindow::implicitHeightChanged);
connect(d->m_popup->window(), &QWindow::xChanged, this, &QQuickPopupWindow::parentWindowXChanged);
connect(d->m_popup->window(), &QWindow::yChanged, this, &QQuickPopupWindow::parentWindowYChanged);
setWidth(d->m_popupItem->implicitWidth());
setHeight(d->m_popupItem->implicitHeight());
const auto flags = QQuickPopupPrivate::get(popup)->popupWindowType();
// For popup windows, we'll need to draw everything, in order to have enough control over the styling.
if (flags & Qt::Popup)
setColor(QColorConstants::Transparent);
setFlags(flags);
qCDebug(lcPopupWindow) << "Created popup window with flags: " << flags;
}
QQuickPopupWindow::~QQuickPopupWindow()
{
Q_D(QQuickPopupWindow);
disconnect(d->m_popup, &QQuickPopup::windowChanged, this, &QQuickPopupWindow::windowChanged);
disconnect(d->m_popup, &QQuickPopup::implicitWidthChanged, this, &QQuickPopupWindow::implicitWidthChanged);
disconnect(d->m_popup, &QQuickPopup::implicitHeightChanged, this, &QQuickPopupWindow::implicitHeightChanged);
disconnect(d->m_popup->window(), &QWindow::xChanged, this, &QQuickPopupWindow::parentWindowXChanged);
disconnect(d->m_popup->window(), &QWindow::yChanged, this, &QQuickPopupWindow::parentWindowYChanged);
}
QQuickPopup *QQuickPopupWindow::popup() const
{
Q_D(const QQuickPopupWindow);
return d->m_popup;
}
void QQuickPopupWindow::hideEvent(QHideEvent *e)
{
Q_D(QQuickPopupWindow);
QQuickWindow::hideEvent(e);
if (QQuickPopup *popup = d->m_popup) {
QQuickPopupPrivate::get(popup)->visible = false;
emit popup->visibleChanged();
}
}
void QQuickPopupWindow::moveEvent(QMoveEvent *e)
{
handlePopupPositionChangeFromWindowSystem(e->pos());
}
void QQuickPopupWindow::resizeEvent(QResizeEvent *e)
{
Q_D(QQuickPopupWindow);
QQuickWindowQmlImpl::resizeEvent(e);
if (!d->m_popupItem)
return;
qCDebug(lcPopupWindow) << "A window system event changed the popup's size to be " << e->size();
d->m_popupItem->setWidth(e->size().width());
d->m_popupItem->setHeight(e->size().height());
}
/*! \internal
We want to handle menus specially, compared to other popups. For menus, we
want all open parent menus and sub menus that belong together to almost
act as a single popup WRT hover event delivery. This will allow the user to
hover and highlight MenuItems inside all of them, not just the leaf menu.
This function will therefore find the menu, or menu bar, under the event's
position, and forward the event to it. But note that this forwarding will
happen in parallel with normal event delivery (we don't eat the events), as
we don't want to break the delivery to e.g grabbers.
*/
void QQuickPopupWindowPrivate::forwardEventToParentMenuOrMenuBar(QEvent *event)
{
Q_Q(QQuickPopupWindow);
if (!event->isPointerEvent())
return;
auto menu = qobject_cast<QQuickMenu *>(q->popup());
if (!menu)
return;
auto *pe = static_cast<QPointerEvent *>(event);
const QPointF globalPos = pe->points().first().globalPosition();
// If there is a Menu or a MenuBar under the mouse, resolve its window.
QQuickPopupWindow *targetPopupWindow = nullptr;
QQuickWindow *targetWindow = nullptr;
QObject *menuParent = menu;
while (menuParent) {
if (auto parentMenu = qobject_cast<QQuickMenu *>(menuParent)) {
QQuickPopupWindow *popupWindow = QQuickMenuPrivate::get(parentMenu)->popupWindow;
auto popup_d = QQuickPopupPrivate::get(popupWindow->popup());
QPointF scenePos = popupWindow->contentItem()->mapFromGlobal(globalPos);
if (popup_d->contains(scenePos)) {
targetPopupWindow = popupWindow;
targetWindow = popupWindow;
break;
}
} else if (auto menuBar = qobject_cast<QQuickMenuBar *>(menuParent)) {
const QPointF menuBarPos = menuBar->mapFromGlobal(globalPos);
if (menuBar->contains(menuBarPos))
targetWindow = menuBar->window();
break;
}
menuParent = menuParent->parent();
}
if (!targetPopupWindow) {
if (pe->isBeginEvent()) {
// A QQuickPopupWindow can be bigger than the Popup itself, to make room
// for a drop-shadow. Close all popups if the user clicks either on the
// shadow or outside the window.
// QGuiApplicationPrivate::closeAllPopups(); // TODO as soon as dependency update is done
return;
}
}
if (!targetWindow)
return;
if (pe->isUpdateEvent()){
// Forward move events to the target window
const auto scenePos = pe->point(0).scenePosition();
const auto translatedScenePos = targetWindow->mapFromGlobal(globalPos);
QMutableEventPoint::setScenePosition(pe->point(0), translatedScenePos);
auto *grabber = pe->exclusiveGrabber(pe->point(0));
if (grabber) {
// Temporarily disable the grabber, to stop the delivery agent inside
// targetWindow from forwarding the event to an item outside the menu
// or menubar. This is especially important to support a press on e.g
// a MenuBarItem, followed by a drag-and-release on top of a MenuItem.
pe->setExclusiveGrabber(pe->point(0), nullptr);
}
qCDebug(lcPopupWindow) << "forwarding" << pe << "to popup menu:" << targetWindow;
QQuickWindowPrivate::get(targetWindow)->deliveryAgent->event(pe);
// Restore the event before we return
QMutableEventPoint::setScenePosition(pe->point(0), scenePos);
if (grabber)
pe->setExclusiveGrabber(pe->point(0), grabber);
} else if (pe->isEndEvent()) {
// To support opening a Menu on press (e.g on a MenuBarItem), followed by
// a drag and release on a MenuItem inside the Menu, we ask the Menu to
// perform a click on the active MenuItem, if any.
if (targetPopupWindow)
QQuickPopupPrivate::get(targetPopupWindow->popup())->handleReleaseWithoutGrab(pe->point(0));
}
}
bool QQuickPopupWindow::event(QEvent *e)
{
Q_D(QQuickPopupWindow);
d->forwardEventToParentMenuOrMenuBar(e);
return QQuickWindowQmlImpl::event(e);
}
void QQuickPopupWindow::windowChanged(QWindow *window)
{
if (window) {
connect(window, &QWindow::xChanged, this, &QQuickPopupWindow::parentWindowXChanged);
connect(window, &QWindow::yChanged, this, &QQuickPopupWindow::parentWindowYChanged);
}
}
QPoint QQuickPopupWindow::global2Local(const QPoint &pos) const
{
Q_D(const QQuickPopupWindow);
QQuickPopup *popup = d->m_popup;
Q_ASSERT(popup);
const QPoint scenePos = popup->window()->mapFromGlobal(pos);
// Popup's coordinates are relative to the nearest parent item.
return popup->parentItem() ? popup->parentItem()->mapFromScene(scenePos).toPoint() : scenePos;
}
void QQuickPopupWindow::parentWindowXChanged(int newX)
{
const auto popupLocalPos = global2Local({x(), y()});
handlePopupPositionChangeFromWindowSystem({newX + popupLocalPos.x(), y()});
}
void QQuickPopupWindow::parentWindowYChanged(int newY)
{
const auto popupLocalPos = global2Local({x(), y()});
handlePopupPositionChangeFromWindowSystem({x(), newY + popupLocalPos.y()});
}
void QQuickPopupWindow::handlePopupPositionChangeFromWindowSystem(const QPoint &pos)
{
Q_D(QQuickPopupWindow);
QQuickPopup *popup = d->m_popup;
if (!popup)
return;
QQuickPopupPrivate *popupPrivate = QQuickPopupPrivate::get(popup);
const auto localPos = global2Local(pos);
const qreal oldX = popup->x();
const qreal oldY = popup->y();
qCDebug(lcPopupWindow) << "A window system event changed the popup's position to be " << localPos;
popupPrivate->x = popupPrivate->effectiveX = localPos.x();
popupPrivate->y = popupPrivate->effectiveY = localPos.y();
if (!qFuzzyCompare(oldX, localPos.x()))
emit popup->xChanged();
if (!qFuzzyCompare(oldY, localPos.y()))
emit popup->yChanged();
}
void QQuickPopupWindow::implicitWidthChanged()
{
Q_D(const QQuickPopupWindow);
if (auto popup = d->m_popup)
setWidth(popup->implicitWidth());
}
void QQuickPopupWindow::implicitHeightChanged()
{
Q_D(const QQuickPopupWindow);
if (auto popup = d->m_popup)
setHeight(popup->implicitHeight());
}
QT_END_NAMESPACE
|