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

source code of qtdeclarative/src/quick/items/qquickwindowattached.cpp