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 "qquickpopupwindow_p.h" |
41 | |
42 | #include <qguiapplication.h> |
43 | #include <qpa/qwindowsysteminterface.h> |
44 | #include <QtQuick/qquickitem.h> |
45 | #include <QtQuick/QQuickRenderControl> |
46 | |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | QQuickPopupWindow1::() : |
50 | QQuickWindow(), m_parentItem(0), m_contentItem(0), |
51 | m_mouseMoved(false), m_needsActivatedEvent(true), |
52 | m_dismissed(false), m_pressed(false) |
53 | { |
54 | setFlags(Qt::Popup); |
55 | connect(qApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)), |
56 | receiver: this, SLOT(applicationStateChanged(Qt::ApplicationState))); |
57 | } |
58 | |
59 | void QQuickPopupWindow1::(Qt::ApplicationState state) |
60 | { |
61 | if (state != Qt::ApplicationActive) |
62 | dismissPopup(); |
63 | } |
64 | |
65 | void QQuickPopupWindow1::() |
66 | { |
67 | qreal posx = x(); |
68 | qreal posy = y(); |
69 | // transientParent may not be a QQuickWindow when embedding into widgets |
70 | if (QWindow *tp = transientParent()) { |
71 | if (m_parentItem) { |
72 | QPointF pos = m_parentItem->mapToItem(item: m_parentItem->window()->contentItem(), point: QPointF(posx, posy)); |
73 | posx = pos.x(); |
74 | posy = pos.y(); |
75 | } |
76 | QPoint tlwOffset = tp->mapToGlobal(pos: QPoint()); |
77 | posx += tlwOffset.x(); |
78 | posy += tlwOffset.y(); |
79 | } else if (m_parentItem && m_parentItem->window()) { |
80 | QPoint offset; |
81 | QQuickWindow *quickWindow = m_parentItem->window(); |
82 | QWindow *renderWindow = QQuickRenderControl::renderWindowFor(win: quickWindow, offset: &offset); |
83 | |
84 | QPointF pos = m_parentItem->mapToItem(item: quickWindow->contentItem(), point: QPointF(posx, posy)); |
85 | posx = pos.x(); |
86 | posy = pos.y(); |
87 | |
88 | QPoint parentWindowOffset = (renderWindow ? renderWindow : quickWindow)->mapToGlobal(pos: QPoint()); |
89 | posx += offset.x() + parentWindowOffset.x(); |
90 | posy += offset.y() + parentWindowOffset.y(); |
91 | } |
92 | |
93 | if (m_contentItem) { |
94 | qreal initialWidth = qMax(a: qreal(1), b: m_contentItem->width()); |
95 | qreal initialHeight = qMax(a: qreal(1), b: m_contentItem->height()); |
96 | setGeometry(posx, posy, w: initialWidth, h: initialHeight); |
97 | } else { |
98 | setPosition(posx, posy); |
99 | } |
100 | emit geometryChanged(); |
101 | |
102 | if (!qobject_cast<QQuickPopupWindow1 *>(object: transientParent())) { // No need for parent menu windows |
103 | if (QQuickWindow *w = qobject_cast<QQuickWindow *>(object: transientParent())) { |
104 | if (QQuickItem *mg = w->mouseGrabberItem()) |
105 | mg->ungrabMouse(); |
106 | } else if (m_parentItem && m_parentItem->window()) { |
107 | if (QQuickItem *mg = m_parentItem->window()->mouseGrabberItem()) |
108 | mg->ungrabMouse(); |
109 | } |
110 | } |
111 | QQuickWindow::show(); |
112 | setMouseGrabEnabled(true); // Needs to be done after calling show() |
113 | setKeyboardGrabEnabled(true); |
114 | } |
115 | |
116 | void QQuickPopupWindow1::(QQuickItem *item) |
117 | { |
118 | m_parentItem = item; |
119 | if (m_parentItem) |
120 | setTransientParent(m_parentItem->window()); |
121 | } |
122 | |
123 | void QQuickPopupWindow1::(QQuickItem *contentItem) |
124 | { |
125 | if (!contentItem) |
126 | return; |
127 | |
128 | contentItem->setParentItem(this->contentItem()); |
129 | connect(sender: contentItem, SIGNAL(widthChanged()), receiver: this, SLOT(updateSize())); |
130 | connect(sender: contentItem, SIGNAL(heightChanged()), receiver: this, SLOT(updateSize())); |
131 | m_contentItem = contentItem; |
132 | } |
133 | |
134 | void QQuickPopupWindow1::() |
135 | { |
136 | setGeometry(posx: x(), posy: y(), w: popupContentItem()->width(), h: popupContentItem()->height()); |
137 | emit geometryChanged(); |
138 | } |
139 | |
140 | void QQuickPopupWindow1::() |
141 | { |
142 | m_dismissed = true; |
143 | emit popupDismissed(); |
144 | hide(); |
145 | } |
146 | |
147 | void QQuickPopupWindow1::(QMouseEvent *e) |
148 | { |
149 | QRect rect = QRect(QPoint(), size()); |
150 | m_mouseMoved = true; |
151 | if (rect.contains(p: e->pos())) { |
152 | if (e->buttons() != Qt::NoButton) |
153 | m_pressed = true; |
154 | QQuickWindow::mouseMoveEvent(e); |
155 | } |
156 | else |
157 | forwardEventToTransientParent(e); |
158 | } |
159 | |
160 | void QQuickPopupWindow1::(QMouseEvent *e) |
161 | { |
162 | m_pressed = true; |
163 | QRect rect = QRect(QPoint(), size()); |
164 | if (rect.contains(p: e->pos())) { |
165 | QQuickWindow::mousePressEvent(e); |
166 | } |
167 | else |
168 | forwardEventToTransientParent(e); |
169 | } |
170 | |
171 | void QQuickPopupWindow1::(QMouseEvent *e) |
172 | { |
173 | QRect rect = QRect(QPoint(), size()); |
174 | if (rect.contains(p: e->pos())) { |
175 | if (m_mouseMoved) { |
176 | QMouseEvent pe = QMouseEvent(QEvent::MouseButtonPress, e->pos(), e->button(), e->buttons(), e->modifiers()); |
177 | QQuickWindow::mousePressEvent(&pe); |
178 | if (!m_dismissed) |
179 | QQuickWindow::mouseReleaseEvent(e); |
180 | } |
181 | m_mouseMoved = true; // Initial mouse release counts as move. |
182 | } else { |
183 | if (m_pressed) |
184 | forwardEventToTransientParent(e); |
185 | } |
186 | m_pressed = false; |
187 | } |
188 | |
189 | void QQuickPopupWindow1::(QMouseEvent *e) |
190 | { |
191 | bool forwardEvent = true; |
192 | |
193 | if (!qobject_cast<QQuickPopupWindow1*>(object: transientParent()) |
194 | && ((m_mouseMoved && e->type() == QEvent::MouseButtonRelease) |
195 | || e->type() == QEvent::MouseButtonPress)) { |
196 | // Clicked outside any popup |
197 | dismissPopup(); |
198 | forwardEvent = shouldForwardEventAfterDismiss(e); |
199 | } |
200 | |
201 | if (forwardEvent && transientParent()) { |
202 | QPoint parentPos = transientParent()->mapFromGlobal(pos: mapToGlobal(pos: e->pos())); |
203 | QMouseEvent pe = QMouseEvent(e->type(), parentPos, e->button(), e->buttons(), e->modifiers()); |
204 | QGuiApplication::sendEvent(receiver: transientParent(), event: &pe); |
205 | } |
206 | } |
207 | |
208 | bool QQuickPopupWindow1::(QMouseEvent*) const |
209 | { |
210 | return false; |
211 | } |
212 | |
213 | void QQuickPopupWindow1::(QExposeEvent *e) |
214 | { |
215 | if (isExposed() && m_needsActivatedEvent) { |
216 | m_needsActivatedEvent = false; |
217 | QWindowSystemInterface::handleWindowActivated(window: this); |
218 | } else if (!isExposed() && !m_needsActivatedEvent) { |
219 | m_needsActivatedEvent = true; |
220 | if (QWindow *tp = transientParent()) |
221 | QWindowSystemInterface::handleWindowActivated(window: tp); |
222 | } |
223 | QQuickWindow::exposeEvent(e); |
224 | } |
225 | |
226 | void QQuickPopupWindow1::(QHideEvent *e) |
227 | { |
228 | if (QWindow *tp = !m_needsActivatedEvent ? transientParent() : 0) { |
229 | m_needsActivatedEvent = true; |
230 | if (tp->isVisible()) |
231 | QWindowSystemInterface::handleWindowActivated(window: tp); |
232 | } |
233 | |
234 | QQuickWindow::hideEvent(e); |
235 | } |
236 | |
237 | /*! \reimp */ |
238 | bool QQuickPopupWindow1::(QEvent *event) |
239 | { |
240 | //QTBUG-45079 |
241 | //This is a workaround for popup menu not being closed when using touch input. |
242 | //Currently mouse synthesized events are not created for touch events which are |
243 | //outside the qquickwindow. |
244 | |
245 | if (event->type() == QEvent::TouchBegin && !qobject_cast<QQuickPopupWindow1*>(object: transientParent())) { |
246 | QRect rect = QRect(QPoint(), size()); |
247 | QTouchEvent *touch = static_cast<QTouchEvent*>(event); |
248 | QTouchEvent::TouchPoint point = touch->touchPoints().first(); |
249 | if ((point.state() == Qt::TouchPointPressed) && !rect.contains(p: point.pos().toPoint())) { |
250 | //first default handling |
251 | bool result = QQuickWindow::event(event); |
252 | //now specific broken case |
253 | if (!m_dismissed) |
254 | dismissPopup(); |
255 | return result; |
256 | } |
257 | } |
258 | return QQuickWindow::event(event); |
259 | } |
260 | QT_END_NAMESPACE |
261 | |