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 \brief The base class of native dialogs.
23
24 The Dialog type provides common QML API for native platform dialogs.
25
26 To show a native dialog, construct an instance of one of the concrete
27 Dialog implementations, set the desired properties, and call \l open().
28 Dialog emits \l accepted() or \l rejected() when the user is done with
29 the dialog.
30
31 \labs
32*/
33
34/*!
35 \qmlsignal void Qt.labs.platform::Dialog::accepted()
36
37 This signal is emitted when the dialog has been accepted either
38 interactively or by calling \l accept().
39
40 \note This signal is \e not emitted when closing the dialog with \l close().
41
42 \sa rejected()
43*/
44
45/*!
46 \qmlsignal void Qt.labs.platform::Dialog::rejected()
47
48 This signal is emitted when the dialog has been rejected either
49 interactively or by calling \l reject().
50
51 \note This signal is \e not emitted when closing the dialog with \l close().
52
53 \sa accepted()
54*/
55
56Q_STATIC_LOGGING_CATEGORY(qtLabsPlatformDialogs, "qt.labs.platform.dialogs")
57
58QQuickLabsPlatformDialog::QQuickLabsPlatformDialog(QPlatformTheme::DialogType type, QObject *parent)
59 : QObject(parent),
60 m_visible(false),
61 m_complete(false),
62 m_result(0),
63 m_parentWindow(nullptr),
64 m_flags(Qt::Dialog),
65 m_modality(Qt::WindowModal),
66 m_type(type),
67 m_handle(nullptr)
68{
69}
70
75
76QPlatformDialogHelper *QQuickLabsPlatformDialog::handle() const
77{
78 return m_handle;
79}
80
81/*!
82 \qmldefault
83 \qmlproperty list<QtObject> Qt.labs.platform::Dialog::data
84
85 This default property holds the list of all objects declared as children of
86 the dialog.
87*/
89{
90 return QQmlListProperty<QObject>(this, &m_data);
91}
92
93/*!
94 \qmlproperty Window Qt.labs.platform::Dialog::parentWindow
95
96 This property holds the parent window of the dialog.
97
98 Unless explicitly set, the window is automatically resolved by iterating
99 the QML parent objects until a \l Window or an \l Item that has a window
100 is found.
101*/
103{
104 return m_parentWindow;
105}
106
108{
109 if (m_parentWindow == window)
110 return;
111
112 m_parentWindow = window;
113 emit parentWindowChanged();
114}
115
116/*!
117 \qmlproperty string Qt.labs.platform::Dialog::title
118
119 This property holds the title of the dialog.
120*/
122{
123 return m_title;
124}
125
126void QQuickLabsPlatformDialog::setTitle(const QString &title)
127{
128 if (m_title == title)
129 return;
130
131 m_title = title;
132 emit titleChanged();
133}
134
135/*!
136 \qmlproperty Qt::WindowFlags Qt.labs.platform::Dialog::flags
137
138 This property holds the window flags of the dialog. The default value is \c Qt.Dialog.
139*/
141{
142 return m_flags;
143}
144
145void QQuickLabsPlatformDialog::setFlags(Qt::WindowFlags flags)
146{
147 if (m_flags == flags)
148 return;
149
150 m_flags = flags;
151 emit flagsChanged();
152}
153
154/*!
155 \qmlproperty Qt::WindowModality Qt.labs.platform::Dialog::modality
156
157 This property holds the modality of the dialog. The default value is \c Qt.WindowModal.
158
159 Available values:
160 \value Qt.NonModal The dialog is not modal and does not block input to other windows.
161 \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.
162 \value Qt.ApplicationModal The dialog is modal to the application and blocks input to all windows.
163*/
165{
166 return m_modality;
167}
168
169void QQuickLabsPlatformDialog::setModality(Qt::WindowModality modality)
170{
171 if (m_modality == modality)
172 return;
173
174 m_modality = modality;
175 emit modalityChanged();
176}
177
178/*!
179 \qmlproperty bool Qt.labs.platform::Dialog::visible
180
181 This property holds the visibility of the dialog. The default value is \c false.
182
183 \sa open(), close()
184*/
186{
187 return m_handle && m_visible;
188}
189
191{
192 if (visible)
193 open();
194 else
195 close();
196}
197
198/*!
199 \qmlproperty int Qt.labs.platform::Dialog::result
200
201 This property holds the result code.
202
203 Standard result codes:
204 \value Dialog.Accepted
205 \value Dialog.Rejected
206
207 \note MessageDialog sets the result to the value of the clicked standard
208 button instead of using the standard result codes.
209*/
210int QQuickLabsPlatformDialog::result() const
211{
212 return m_result;
213}
214
216{
217 if (m_result == result)
218 return;
219
220 m_result = result;
221 emit resultChanged();
222}
223
224/*!
225 \qmlmethod void Qt.labs.platform::Dialog::open()
226
227 Opens the dialog.
228
229 \sa visible, close()
230*/
232{
233 if (m_visible || !create())
234 return;
235
236 onShow(m_handle);
237 m_visible = m_handle->show(m_flags, m_modality, m_parentWindow);
238 if (m_visible)
239 emit visibleChanged();
240}
241
242/*!
243 \qmlmethod void Qt.labs.platform::Dialog::close()
244
245 Closes the dialog.
246
247 \sa visible, open()
248*/
250{
251 if (!m_handle || !m_visible)
252 return;
253
254 onHide(m_handle);
255 m_handle->hide();
256 m_visible = false;
257 emit visibleChanged();
258}
259
260/*!
261 \qmlmethod void Qt.labs.platform::Dialog::accept()
262
263 Closes the dialog and emits the \l accepted() signal.
264
265 \sa reject()
266*/
271
272/*!
273 \qmlmethod void Qt.labs.platform::Dialog::reject()
274
275 Closes the dialog and emits the \l rejected() signal.
276
277 \sa accept()
278*/
283
284/*!
285 \qmlmethod void Qt.labs.platform::Dialog::done(int result)
286
287 Closes the dialog and sets the \a result.
288
289 \sa accept(), reject(), result
290*/
292{
293 close();
294 setResult(result);
295
296 if (result == Accepted)
297 emit accepted();
298 else if (result == Rejected)
299 emit rejected();
300}
301
305
307{
308 m_complete = true;
309 if (!m_parentWindow)
311}
312
313static const char *qmlTypeName(const QObject *object)
314{
315 return object->metaObject()->className() + qstrlen("QQuickLabsPlatform");
316}
317
319{
320 if (!m_handle) {
321 if (useNativeDialog())
322 m_handle = QGuiApplicationPrivate::platformTheme()->createPlatformDialogHelper(m_type);
323 if (!m_handle)
324 m_handle = QWidgetPlatform::createDialog(m_type, this);
325 qCDebug(qtLabsPlatformDialogs) << qmlTypeName(this) << "->" << m_handle;
326 if (m_handle) {
327 onCreate(m_handle);
328 connect(m_handle, &QPlatformDialogHelper::accept, this, &QQuickLabsPlatformDialog::accept);
329 connect(m_handle, &QPlatformDialogHelper::reject, this, &QQuickLabsPlatformDialog::reject);
330 }
331 }
332 return m_handle;
333}
334
336{
337 delete m_handle;
338 m_handle = nullptr;
339}
340
342{
343 return !QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
344 && QGuiApplicationPrivate::platformTheme()->usePlatformNativeDialog(m_type);
345}
346
347void QQuickLabsPlatformDialog::onCreate(QPlatformDialogHelper *dialog)
348{
349 Q_UNUSED(dialog);
350}
351
352void QQuickLabsPlatformDialog::onShow(QPlatformDialogHelper *dialog)
353{
354 Q_UNUSED(dialog);
355}
356
357void QQuickLabsPlatformDialog::onHide(QPlatformDialogHelper *dialog)
358{
359 Q_UNUSED(dialog);
360}
361
363{
364 QObject *obj = parent();
365 while (obj) {
366 QWindow *window = qobject_cast<QWindow *>(obj);
367 if (window)
368 return window;
369 QQuickItem *item = qobject_cast<QQuickItem *>(obj);
370 if (item && item->window())
371 return item->window();
372 obj = obj->parent();
373 }
374 return nullptr;
375}
376
377QT_END_NAMESPACE
378
379#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
static const char * qmlTypeName(const QObject *object)