1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Controls module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qquickmenupopupwindow_p.h"
41
42#include <qguiapplication.h>
43#include <qpa/qwindowsysteminterface.h>
44#include <qquickitem.h>
45#include <QtGui/QScreen>
46#include <QtQuick/QQuickRenderControl>
47#include "qquickmenu_p.h"
48#include "qquickmenubar_p.h"
49
50QT_BEGIN_NAMESPACE
51
52QQuickMenuPopupWindow1::QQuickMenuPopupWindow1(QQuickMenu1 *menu) :
53 m_itemAt(0),
54 m_logicalParentWindow(0),
55 m_menu(menu)
56{
57}
58
59void QQuickMenuPopupWindow1::setParentItem(QQuickItem *item)
60{
61 QQuickPopupWindow1::setParentItem(item);
62 if (item) {
63 QWindow *parentWindow = item->window();
64 QWindow *renderWindow = QQuickRenderControl::renderWindowFor(win: static_cast<QQuickWindow *>(parentWindow));
65 setParentWindow(effectiveParentWindow: renderWindow ? renderWindow : parentWindow, parentWindow: item->window());
66 }
67}
68
69void QQuickMenuPopupWindow1::setItemAt(QQuickItem *menuItem)
70{
71 if (m_itemAt) {
72 disconnect(sender: m_itemAt, SIGNAL(xChanged()), receiver: this, SLOT(updatePosition()));
73 disconnect(sender: m_itemAt, SIGNAL(yChanged()), receiver: this, SLOT(updatePosition()));
74 }
75
76 m_itemAt = menuItem;
77 if (menuItem) {
78 m_oldItemPos = menuItem->position().toPoint();
79 connect(sender: menuItem, SIGNAL(xChanged()), receiver: this, SLOT(updatePosition()));
80 connect(sender: menuItem, SIGNAL(yChanged()), receiver: this, SLOT(updatePosition()));
81 }
82}
83
84void QQuickMenuPopupWindow1::setParentWindow(QWindow *effectiveParentWindow, QQuickWindow *parentWindow)
85{
86 while (effectiveParentWindow && effectiveParentWindow->parent())
87 effectiveParentWindow = effectiveParentWindow->parent();
88 if (transientParent() != effectiveParentWindow)
89 setTransientParent(effectiveParentWindow);
90 m_logicalParentWindow = parentWindow;
91 if (parentWindow) {
92 if (QQuickMenuPopupWindow1 *pw = qobject_cast<QQuickMenuPopupWindow1 *>(object: parentWindow)) {
93 connect(sender: pw, SIGNAL(popupDismissed()), receiver: this, SLOT(dismissPopup()));
94 connect(sender: pw, SIGNAL(willBeDeletedLater()), receiver: this, SLOT(setToBeDeletedLater()));
95 } else {
96 connect(sender: parentWindow, SIGNAL(destroyed()), receiver: this, SLOT(deleteLater()));
97 }
98 }
99}
100
101void QQuickMenuPopupWindow1::setToBeDeletedLater()
102{
103 deleteLater();
104 emit willBeDeletedLater();
105}
106
107void QQuickMenuPopupWindow1::setGeometry(int posx, int posy, int w, int h)
108{
109 QWindow *pw = transientParent();
110 if (!pw && parentItem())
111 pw = parentItem()->window();
112 if (!pw)
113 pw = this;
114 QRect g = pw->screen()->geometry();
115
116 if (posx + w > g.right()) {
117 if (qobject_cast<QQuickMenuPopupWindow1 *>(object: transientParent())) {
118 // reposition submenu window on the parent menu's left side
119 int submenuOverlap = pw->x() + pw->width() - posx;
120 posx -= pw->width() + w - 2 * submenuOverlap;
121 } else {
122 posx = g.right() - w;
123 }
124 } else {
125 posx = qMax(a: posx, b: g.left());
126 }
127
128 posy = qBound(min: g.top(), val: posy, max: g.bottom() - h);
129
130 QQuickWindow::setGeometry(posx, posy, w, h);
131 emit geometryChanged();
132}
133
134void QQuickMenuPopupWindow1::updateSize()
135{
136 const QQuickItem *contentItem = popupContentItem();
137 if (!contentItem)
138 return;
139
140 qreal x = m_initialPos.x();
141 qreal y = m_initialPos.y();
142 if (qGuiApp->layoutDirection() == Qt::RightToLeft)
143 x -= contentItem->width();
144 setGeometry(posx: x, posy: y, w: contentItem->width(), h: contentItem->height());
145}
146
147void QQuickMenuPopupWindow1::updatePosition()
148{
149 QPointF newPos = position() + m_oldItemPos - m_itemAt->position();
150 m_initialPos += m_oldItemPos - m_itemAt->position();
151 setGeometry(posx: newPos.x(), posy: newPos.y(), w: width(), h: height());
152}
153
154void QQuickMenuPopupWindow1::focusInEvent(QFocusEvent *e)
155{
156 QQuickWindow::focusInEvent(e);
157 if (m_menu && m_menu->menuContentItem())
158 m_menu->menuContentItem()->forceActiveFocus();
159}
160
161void QQuickMenuPopupWindow1::exposeEvent(QExposeEvent *e)
162{
163 // the popup will reposition at the last moment, so its
164 // initial position must be captured for updateSize().
165 m_initialPos = position();
166 if (m_logicalParentWindow && m_logicalParentWindow->parent()) {
167 // This must be a QQuickWindow embedded via createWindowContainer.
168 m_initialPos += m_logicalParentWindow->geometry().topLeft();
169 }
170 QQuickPopupWindow1::exposeEvent(e);
171
172 if (isExposed())
173 updateSize();
174}
175
176QQuickMenu1 *QQuickMenuPopupWindow1::menu() const
177{
178 return m_menu;
179}
180
181bool QQuickMenuPopupWindow1::shouldForwardEventAfterDismiss(QMouseEvent *e) const
182{
183 // If the event falls inside this item the event should not be forwarded.
184 // For example for comboboxes or top menus of the menubar
185 QQuickMenuBar1 *mb = m_menu ? m_menu->menuBar() : nullptr;
186 QQuickItem *item = mb && !mb->isNative() ? mb->contentItem() : menu()->visualItem();
187 QWindow *window = transientParent();
188 if (item && window && item->window() == window) {
189 QPointF pos = window->mapFromGlobal(pos: mapToGlobal(pos: e->pos()));
190 pos = item->mapFromScene(point: pos);
191 if (item->contains(point: pos))
192 return false;
193 }
194
195#ifdef Q_OS_OSX
196 if (e->button() == Qt::RightButton)
197 return true;
198#endif
199
200#ifdef Q_OS_WIN
201 return true;
202#endif
203
204 return false;
205}
206
207QT_END_NAMESPACE
208

source code of qtquickcontrols/src/controls/qquickmenupopupwindow.cpp