Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquicklabsplatformdialog.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 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// Qt-Security score:significant reason:default
4
6
7#include <QtCore/qloggingcategory.h>
8#include <QtGui/private/qguiapplication_p.h>
9#include <QtQuick/qquickitem.h>
10#include <QtQuick/qquickwindow.h>
11
12#include "widgets/qwidgetplatform_p.h"
13
15
16/*!
17 \qmltype Dialog
18 \inherits QtObject
19//! \nativetype QQuickLabsPlatformDialog
20 \inqmlmodule Qt.labs.platform
21 \since 5.8
22 \deprecated [6.11] Use \l [QtQuickDialogs] {Dialog}{QtQuick.Dialogs Dialog} instead.
23 \brief The base class of native dialogs.
24
25 The Dialog type provides common QML API for native platform dialogs.
26
27 To show a native dialog, construct an instance of one of the concrete
28 Dialog implementations, set the desired properties, and call \l open().
29 Dialog emits \l accepted() or \l rejected() when the user is done with
30 the dialog.
31
32 \labs
33*/
34
35/*!
36 \qmlsignal void Qt.labs.platform::Dialog::accepted()
37
38 This signal is emitted when the dialog has been accepted either
39 interactively or by calling \l accept().
40
41 \note This signal is \e not emitted when closing the dialog with \l close().
42
43 \sa rejected()
44*/
45
46/*!
47 \qmlsignal void Qt.labs.platform::Dialog::rejected()
48
49 This signal is emitted when the dialog has been rejected either
50 interactively or by calling \l reject().
51
52 \note This signal is \e not emitted when closing the dialog with \l close().
53
54 \sa accepted()
55*/
56
57Q_STATIC_LOGGING_CATEGORY(qtLabsPlatformDialogs, "qt.labs.platform.dialogs")
58
59QQuickLabsPlatformDialog::QQuickLabsPlatformDialog(QPlatformTheme::DialogType type, QObject *parent)
60 : QObject(parent),
61 m_visible(false),
62 m_complete(false),
63 m_result(0),
64 m_parentWindow(nullptr),
65 m_flags(Qt::Dialog),
66 m_modality(Qt::WindowModal),
67 m_type(type),
68 m_handle(nullptr)
69{
70}
71
76
77QPlatformDialogHelper *QQuickLabsPlatformDialog::handle() const
78{
79 return m_handle;
80}
81
82/*!
83 \qmldefault
84 \qmlproperty list<QtObject> Qt.labs.platform::Dialog::data
85
86 This default property holds the list of all objects declared as children of
87 the dialog.
88*/
90{
91 return QQmlListProperty<QObject>(this, &m_data);
92}
93
94/*!
95 \qmlproperty Window Qt.labs.platform::Dialog::parentWindow
96
97 This property holds the parent window of the dialog.
98
99 Unless explicitly set, the window is automatically resolved by iterating
100 the QML parent objects until a \l Window or an \l Item that has a window
101 is found.
102*/
104{
105 return m_parentWindow;
106}
107
109{
110 if (m_parentWindow == window)
111 return;
112
113 m_parentWindow = window;
114 emit parentWindowChanged();
115}
116
117/*!
118 \qmlproperty string Qt.labs.platform::Dialog::title
119
120 This property holds the title of the dialog.
121*/
123{
124 return m_title;
125}
126
127void QQuickLabsPlatformDialog::setTitle(const QString &title)
128{
129 if (m_title == title)
130 return;
131
132 m_title = title;
133 emit titleChanged();
134}
135
136/*!
137 \qmlproperty Qt::WindowFlags Qt.labs.platform::Dialog::flags
138
139 This property holds the window flags of the dialog. The default value is \c Qt.Dialog.
140*/
142{
143 return m_flags;
144}
145
146void QQuickLabsPlatformDialog::setFlags(Qt::WindowFlags flags)
147{
148 if (m_flags == flags)
149 return;
150
151 m_flags = flags;
152 emit flagsChanged();
153}
154
155/*!
156 \qmlproperty Qt::WindowModality Qt.labs.platform::Dialog::modality
157
158 This property holds the modality of the dialog. The default value is \c Qt.WindowModal.
159
160 Available values:
161 \value Qt.NonModal The dialog is not modal and does not block input to other windows.
162 \value Qt.WindowModal The dialog is modal to a single window hierarchy and blocks input to its parent window, all grandparent windows, and all siblings of its parent and grandparent windows.
163 \value Qt.ApplicationModal The dialog is modal to the application and blocks input to all windows.
164*/
166{
167 return m_modality;
168}
169
170void QQuickLabsPlatformDialog::setModality(Qt::WindowModality modality)
171{
172 if (m_modality == modality)
173 return;
174
175 m_modality = modality;
176 emit modalityChanged();
177}
178
179/*!
180 \qmlproperty bool Qt.labs.platform::Dialog::visible
181
182 This property holds the visibility of the dialog. The default value is \c false.
183
184 \sa open(), close()
185*/
187{
188 return m_handle && m_visible;
189}
190
192{
193 if (visible)
194 open();
195 else
196 close();
197}
198
199/*!
200 \qmlproperty int Qt.labs.platform::Dialog::result
201
202 This property holds the result code.
203
204 Standard result codes:
205 \value Dialog.Accepted
206 \value Dialog.Rejected
207
208 \note MessageDialog sets the result to the value of the clicked standard
209 button instead of using the standard result codes.
210*/
211int QQuickLabsPlatformDialog::result() const
212{
213 return m_result;
214}
215
217{
218 if (m_result == result)
219 return;
220
221 m_result = result;
222 emit resultChanged();
223}
224
225/*!
226 \qmlmethod void Qt.labs.platform::Dialog::open()
227
228 Opens the dialog.
229
230 \sa visible, close()
231*/
233{
234 if (m_visible || !create())
235 return;
236
237 onShow(m_handle);
238 m_visible = m_handle->show(m_flags, m_modality, m_parentWindow);
239 if (m_visible)
240 emit visibleChanged();
241}
242
243/*!
244 \qmlmethod void Qt.labs.platform::Dialog::close()
245
246 Closes the dialog.
247
248 \sa visible, open()
249*/
251{
252 if (!m_handle || !m_visible)
253 return;
254
255 onHide(m_handle);
256 m_handle->hide();
257 m_visible = false;
258 emit visibleChanged();
259}
260
261/*!
262 \qmlmethod void Qt.labs.platform::Dialog::accept()
263
264 Closes the dialog and emits the \l accepted() signal.
265
266 \sa reject()
267*/
272
273/*!
274 \qmlmethod void Qt.labs.platform::Dialog::reject()
275
276 Closes the dialog and emits the \l rejected() signal.
277
278 \sa accept()
279*/
284
285/*!
286 \qmlmethod void Qt.labs.platform::Dialog::done(int result)
287
288 Closes the dialog and sets the \a result.
289
290 \sa accept(), reject(), result
291*/
293{
294 close();
295 setResult(result);
296
297 if (result == Accepted)
298 emit accepted();
299 else if (result == Rejected)
300 emit rejected();
301}
302
306
308{
309 m_complete = true;
310 if (!m_parentWindow)
312}
313
314static const char *qmlTypeName(const QObject *object)
315{
316 return object->metaObject()->className() + qstrlen("QQuickLabsPlatform");
317}
318
320{
321 if (!m_handle) {
322 if (useNativeDialog())
323 m_handle = QGuiApplicationPrivate::platformTheme()->createPlatformDialogHelper(m_type);
324 if (!m_handle)
325 m_handle = QWidgetPlatform::createDialog(m_type, this);
326 qCDebug(qtLabsPlatformDialogs) << qmlTypeName(this) << "->" << m_handle;
327 if (m_handle) {
328 onCreate(m_handle);
329 connect(m_handle, &QPlatformDialogHelper::accept, this, &QQuickLabsPlatformDialog::accept);
330 connect(m_handle, &QPlatformDialogHelper::reject, this, &QQuickLabsPlatformDialog::reject);
331 }
332 }
333 return m_handle;
334}
335
337{
338 delete m_handle;
339 m_handle = nullptr;
340}
341
343{
344 return !QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
345 && QGuiApplicationPrivate::platformTheme()->usePlatformNativeDialog(m_type);
346}
347
348void QQuickLabsPlatformDialog::onCreate(QPlatformDialogHelper *dialog)
349{
350 Q_UNUSED(dialog);
351}
352
353void QQuickLabsPlatformDialog::onShow(QPlatformDialogHelper *dialog)
354{
355 Q_UNUSED(dialog);
356}
357
358void QQuickLabsPlatformDialog::onHide(QPlatformDialogHelper *dialog)
359{
360 Q_UNUSED(dialog);
361}
362
364{
365 QObject *obj = parent();
366 while (obj) {
367 QWindow *window = qobject_cast<QWindow *>(obj);
368 if (window)
369 return window;
370 QQuickItem *item = qobject_cast<QQuickItem *>(obj);
371 if (item && item->window())
372 return item->window();
373 obj = obj->parent();
374 }
375 return nullptr;
376}
377
378QT_END_NAMESPACE
379
380#include "moc_qquicklabsplatformdialog_p.cpp"
QQmlListProperty< QObject > data()
virtual void reject()
\qmlmethod void Qt.labs.platform::Dialog::reject()
void close()
\qmlmethod void Qt.labs.platform::Dialog::close()
bool isVisible() const
\qmlproperty bool Qt.labs.platform::Dialog::visible
void classBegin() override
Invoked after class creation, but before any properties have been set.
QWindow * parentWindow() const
\qmlproperty Window Qt.labs.platform::Dialog::parentWindow
virtual void onShow(QPlatformDialogHelper *dialog)
virtual void onHide(QPlatformDialogHelper *dialog)
QPlatformDialogHelper * handle() const
QString title() const
\qmlproperty string Qt.labs.platform::Dialog::title
Qt::WindowModality modality() const
\qmlproperty Qt::WindowModality Qt.labs.platform::Dialog::modality
virtual void done(int result)
\qmlmethod void Qt.labs.platform::Dialog::done(int result)
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
void setTitle(const QString &title)
virtual void onCreate(QPlatformDialogHelper *dialog)
virtual void accept()
\qmlmethod void Qt.labs.platform::Dialog::accept()
Qt::WindowFlags flags() const
\qmlproperty Qt::WindowFlags Qt.labs.platform::Dialog::flags
Combined button and popup list for selecting options.
static const char * qmlTypeName(const QObject *object)