1// Copyright (C) 2023 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 "qcapturablewindow.h"
5#include "qcapturablewindow_p.h"
6#include "qplatformmediaintegration_p.h"
7
8QT_BEGIN_NAMESPACE
9
10QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QCapturableWindowPrivate)
11
12/*!
13 \class QCapturableWindow
14 \inmodule QtMultimedia
15 \ingroup multimedia
16 \ingroup multimedia_video
17 \since 6.6
18
19 \brief Used for getting the basic information of a capturable window.
20
21 The class contains a set of window information, except the method
22 QCapturableWindow::isValid which pulls the current state
23 whenever it's called.
24
25 \sa QWindowCapture
26*/
27/*!
28 \qmlvaluetype CapturableWindow
29 \nativetype QCapturableWindow
30 \brief The CapturableWindow type is used getting basic
31 of a window that is available for capturing via WindowCapture.
32
33 \inqmlmodule QtMultimedia
34 \ingroup multimedia_qml
35 \ingroup multimedia_video_qml
36 \since 6.6
37
38 The class contains a dump of window information, except the property
39 'isValid' which pulls the actual window state every time.
40
41 A CapturableWindow instance can be implicitly constructed from a
42 \l [QML] Window. Application developers can utilize this by passing
43 a QML Window into the 'window' property of a \l WindowCapture. See
44 the following example.
45 \qml
46 Window {
47 id: topWindow
48
49 WindowCapture {
50 window: topWindow
51 }
52 }
53 \endqml
54
55 \sa WindowCapture
56*/
57
58/*!
59 \fn QCapturableWindow::QCapturableWindow(QCapturableWindow &&other)
60
61 Constructs a QCapturableWindow by moving from \a other.
62*/
63
64/*!
65 \fn void QCapturableWindow::swap(QCapturableWindow &other) noexcept
66
67 Swaps the current window information with \a other.
68*/
69
70/*!
71 \fn QCapturableWindow &QCapturableWindow::operator=(QCapturableWindow &&other)
72
73 Moves \a other into this QCapturableWindow.
74*/
75
76/*!
77 Constructs a null capturable window information that doesn't refer to any window.
78*/
79QCapturableWindow::QCapturableWindow() = default;
80
81/*!
82 Constructs a QCapturableWindow instance that maps to the given window.
83
84 The description of the QCapturableWindow will match the title of the given QWindow.
85
86 Note, the constructor may create an invalid instance if the specified \c QWindow
87 has not been presented yet. Thus, if the Qt application is not running, an
88 invalid \c QCapturableWindow instance is expected. The validity of the instance
89 can be tracked by querying \l isValid over time.
90
91 If given a nullptr as input, this method will return an instance that will never become valid.
92
93 If given a window that is not top-level, this method will return an instance will never become
94 valid.
95
96 \since 6.10
97*/
98QCapturableWindow::QCapturableWindow(QWindow *window)
99{
100 q23::expected<QCapturableWindow, QString> capturableWindow =
101 QPlatformMediaIntegration::instance()->capturableWindowFromQWindow(window);
102 if (capturableWindow)
103 *this = std::move(capturableWindow.value());
104}
105
106/*!
107 Destroys the window information.
108 */
109QCapturableWindow::~QCapturableWindow() = default;
110
111/*!
112 Construct a new window information using \a other QCapturableWindow.
113*/
114QCapturableWindow::QCapturableWindow(const QCapturableWindow &other) = default;
115
116/*!
117 Assigns the \a other window information to this QCapturableWindow.
118*/
119QCapturableWindow& QCapturableWindow::operator=(const QCapturableWindow &other) = default;
120
121/*!
122 \fn bool QCapturableWindow::operator==(const QCapturableWindow &lhs, const QCapturableWindow &rhs)
123
124 Returns \c true if window information \a lhs and \a rhs refer to the same window,
125 otherwise returns \c false.
126*/
127
128/*!
129 \fn bool QCapturableWindow::operator!=(const QCapturableWindow &lhs, const QCapturableWindow &rhs)
130
131 Returns \c true if window information \a lhs and \a rhs refer to different windows,
132 otherwise returns \c false.
133*/
134bool operator==(const QCapturableWindow &lhs, const QCapturableWindow &rhs) noexcept
135{
136 return lhs.d == rhs.d || (lhs.d && rhs.d && lhs.d->id == rhs.d->id);
137}
138
139/*!
140 \qmlproperty string QtMultimedia::CapturableWindow::isValid
141
142 This property identifies whether a window information is valid.
143
144 An invalid window information refers to non-existing window or doesn't refer to any one.
145*/
146
147/*!
148 Identifies whether a window information is valid.
149
150 An invalid window information refers to non-existing window or doesn't refer to any one.
151
152 Returns true if the window is valid, and false if it is not.
153*/
154bool QCapturableWindow::isValid() const
155{
156 return d && QPlatformMediaIntegration::instance()->isCapturableWindowValid(*d);
157}
158
159/*!
160 \qmlproperty string QtMultimedia::CapturableWindow::description
161
162 This property holds the description of the reffered window.
163*/
164
165/*!
166 Returns a description of the window. In most cases it represents the window title.
167*/
168QString QCapturableWindow::description() const
169{
170 if (!d)
171 return {};
172
173 if (d->description.isEmpty() && d->id)
174 return QLatin1String("Window 0x") + QString::number(d->id, base: 16);
175
176 return d->description;
177}
178
179QCapturableWindow::QCapturableWindow(QCapturableWindowPrivate *capturablePrivate)
180 : d(capturablePrivate)
181{
182}
183
184#ifndef QT_NO_DEBUG_STREAM
185QDebug operator<<(QDebug dbg, const QCapturableWindow &window)
186{
187 dbg << QStringLiteral("Capturable window '%1'").arg(a: window.description());
188 return dbg;
189}
190#endif
191
192
193QT_END_NAMESPACE
194
195#include "moc_qcapturablewindow.cpp"
196

source code of qtmultimedia/src/multimedia/recording/qcapturablewindow.cpp