| 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 "qquickwindow.h" |
| 5 | #include "qquickitem.h" |
| 6 | #include "qquickwindowattached_p.h" |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | // QDoc comments must go in qquickwindow.cpp to avoid overwriting the Window docs |
| 11 | |
| 12 | QQuickWindowAttached::QQuickWindowAttached(QObject* attachee) |
| 13 | : QObject(attachee) |
| 14 | , m_window(nullptr) |
| 15 | { |
| 16 | m_attachee = qobject_cast<QQuickItem*>(o: attachee); |
| 17 | if (!m_attachee) { |
| 18 | qmlWarning(me: attachee) |
| 19 | << "Window.window does only support types deriving from Item" ; |
| 20 | return; |
| 21 | } |
| 22 | if (m_attachee->window()) // It might not be in a window yet |
| 23 | windowChange(m_attachee->window()); |
| 24 | connect(sender: m_attachee, signal: &QQuickItem::windowChanged, context: this, slot: &QQuickWindowAttached::windowChange); |
| 25 | } |
| 26 | |
| 27 | QWindow::Visibility QQuickWindowAttached::visibility() const |
| 28 | { |
| 29 | return (m_window ? m_window->visibility() : QWindow::Hidden); |
| 30 | } |
| 31 | |
| 32 | bool QQuickWindowAttached::isActive() const |
| 33 | { |
| 34 | return (m_window ? m_window->isActive() : false); |
| 35 | } |
| 36 | |
| 37 | QQuickItem *QQuickWindowAttached::activeFocusItem() const |
| 38 | { |
| 39 | return (m_window ? m_window->activeFocusItem() : nullptr); |
| 40 | } |
| 41 | |
| 42 | QQuickItem *QQuickWindowAttached::contentItem() const |
| 43 | { |
| 44 | return (m_window ? m_window->contentItem() : nullptr); |
| 45 | } |
| 46 | |
| 47 | int QQuickWindowAttached::width() const |
| 48 | { |
| 49 | return (m_window ? m_window->width() : 0); |
| 50 | } |
| 51 | |
| 52 | int QQuickWindowAttached::height() const |
| 53 | { |
| 54 | return (m_window ? m_window->height() : 0); |
| 55 | } |
| 56 | |
| 57 | QQuickWindow *QQuickWindowAttached::window() const |
| 58 | { |
| 59 | return m_window; |
| 60 | } |
| 61 | |
| 62 | void QQuickWindowAttached::windowChange(QQuickWindow *window) |
| 63 | { |
| 64 | if (window != m_window) { |
| 65 | QQuickWindow* oldWindow = m_window; |
| 66 | m_window = window; |
| 67 | |
| 68 | if (oldWindow) |
| 69 | oldWindow->disconnect(receiver: this); |
| 70 | |
| 71 | emit windowChanged(); |
| 72 | |
| 73 | if (!oldWindow || !window || window->visibility() != oldWindow->visibility()) |
| 74 | emit visibilityChanged(); |
| 75 | if (!oldWindow || !window || window->isActive() != oldWindow->isActive()) |
| 76 | emit activeChanged(); |
| 77 | if (!oldWindow || !window || window->activeFocusItem() != oldWindow->activeFocusItem()) |
| 78 | emit activeFocusItemChanged(); |
| 79 | emit contentItemChanged(); |
| 80 | if (!oldWindow || !window || window->width() != oldWindow->width()) |
| 81 | emit widthChanged(); |
| 82 | if (!oldWindow || !window || window->height() != oldWindow->height()) |
| 83 | emit heightChanged(); |
| 84 | |
| 85 | if (!window) |
| 86 | return; |
| 87 | |
| 88 | // QQuickWindowQmlImpl::visibilityChanged also exists, and window might even |
| 89 | // be QQuickWindowQmlImpl, but that's not what we are connecting to. |
| 90 | // So this is actual window state rather than a buffered or as-requested one. |
| 91 | // If we used the metaobject connect syntax there would be a warning: |
| 92 | // QMetaObjectPrivate::indexOfSignalRelative - QMetaObject::indexOfSignal: |
| 93 | // signal visibilityChanged(QWindow::Visibility) from QQuickWindow redefined in QQuickWindowQmlImpl |
| 94 | connect(sender: window, signal: &QQuickWindow::visibilityChanged, |
| 95 | context: this, slot: &QQuickWindowAttached::visibilityChanged); |
| 96 | connect(sender: window, signal: &QQuickWindow::activeChanged, |
| 97 | context: this, slot: &QQuickWindowAttached::activeChanged); |
| 98 | connect(sender: window, signal: &QQuickWindow::activeFocusItemChanged, |
| 99 | context: this, slot: &QQuickWindowAttached::activeFocusItemChanged); |
| 100 | connect(sender: window, signal: &QQuickWindow::widthChanged, |
| 101 | context: this, slot: &QQuickWindowAttached::widthChanged); |
| 102 | connect(sender: window, signal: &QQuickWindow::heightChanged, |
| 103 | context: this, slot: &QQuickWindowAttached::heightChanged); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | QT_END_NAMESPACE |
| 108 | |
| 109 | #include "moc_qquickwindowattached_p.cpp" |
| 110 | |