1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Dialogs module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qquickplatformmessagedialog_p.h"
41#include "qquickitem.h"
42
43#include <private/qguiapplication_p.h>
44#include <QWindow>
45#include <QQuickView>
46#include <QQuickWindow>
47
48QT_BEGIN_NAMESPACE
49
50/*!
51 \qmltype MessageDialog
52 \instantiates QQuickPlatformMessageDialog1
53 \inqmlmodule QtQuick.Dialogs
54 \ingroup qtquickdialogs
55 \brief Dialog component for displaying popup messages.
56 \since 5.2
57
58 The most basic use case for a MessageDialog is a popup alert. It also
59 allows the user to respond in various ways depending on which buttons are
60 enabled. The dialog is initially invisible. You need to set the properties
61 as desired first, then set \l visible to \c true or call \l open().
62
63 Here is a minimal example to show an alert and exit after the user
64 responds:
65
66 \qml
67 import QtQuick 2.2
68 import QtQuick.Dialogs 1.1
69
70 MessageDialog {
71 id: messageDialog
72 title: "May I have your attention please"
73 text: "It's so cool that you are using Qt Quick."
74 onAccepted: {
75 console.log("And of course you could only agree.")
76 Qt.quit()
77 }
78 Component.onCompleted: visible = true
79 }
80 \endqml
81
82 There are several possible handlers depending on which \l standardButtons
83 the dialog has and the \l {QMessageBox::ButtonRole} {ButtonRole} of each.
84 For example, the \l {rejected} {onRejected} handler will be called if the
85 user presses a \gui Cancel, \gui Close or \gui Abort button.
86
87 A MessageDialog window is automatically transient for its parent window. So
88 whether you declare the dialog inside an \l Item or inside a \l Window, the
89 dialog will appear centered over the window containing the item, or over
90 the Window that you declared.
91
92 The implementation of MessageDialog will be a platform message dialog if
93 possible. If that isn't possible, then it will try to instantiate a
94 \l QMessageBox. If that also isn't possible, then it will fall back to a QML
95 implementation, \c DefaultMessageDialog.qml. In that case you can customize
96 the appearance by editing this file. \c DefaultMessageDialog.qml contains a
97 \l Rectangle to hold the dialog's contents, because certain embedded systems
98 do not support multiple top-level windows. When the dialog becomes visible,
99 it will automatically be wrapped in a \l Window if possible, or simply
100 reparented on top of the main window if there can only be one window.
101*/
102
103/*!
104 \qmlsignal MessageDialog::accepted()
105
106 This signal is emitted when the user has pressed any button which has the
107 \l {QMessageBox::AcceptRole} {AcceptRole}: \gui OK, \gui Open, \gui Save,
108 \gui {Save All}, \gui Retry or \gui Ignore.
109
110 The corresponding handler is \c onAccepted.
111*/
112
113/*!
114 \qmlsignal MessageDialog::rejected()
115
116 This signal is emitted when the user has dismissed the dialog, by closing
117 the dialog window, by pressing a \gui Cancel, \gui Close or \gui Abort
118 button on the dialog, or by pressing the back button or the escape key.
119
120 The corresponding handler is \c onRejected.
121*/
122
123/*!
124 \qmlsignal MessageDialog::discard()
125
126 This signal is emitted when the user has pressed the \gui Discard button.
127
128 The corresponding handler is \c onDiscard.
129*/
130
131/*!
132 \qmlsignal MessageDialog::help()
133
134 This signal is emitted when the user has pressed the \gui Help button.
135 Depending on platform, the dialog may not be automatically dismissed
136 because the help that your application provides may need to be relevant to
137 the text shown in this dialog in order to assist the user in making a
138 decision. However on other platforms it's not possible to show a dialog and
139 a help window at the same time. If you want to be sure that the dialog will
140 close, you can set \l visible to \c false in your handler.
141
142 The corresponding handler is \c onHelp.
143*/
144
145/*!
146 \qmlsignal MessageDialog::yes()
147
148 This signal is emitted when the user has pressed any button which has
149 the \l {QMessageBox::YesRole} {YesRole}: \gui Yes or \gui {Yes to All}.
150
151 The corresponding handler is \c onYes.
152*/
153
154/*!
155 \qmlsignal MessageDialog::no()
156
157 This signal is emitted when the user has pressed any button which has
158 the \l {QMessageBox::NoRole} {NoRole}: \gui No or \gui {No to All}.
159
160 The corresponding handler is \c onNo.
161*/
162
163/*!
164 \qmlsignal MessageDialog::apply()
165
166 This signal is emitted when the user has pressed the \gui Apply button.
167
168 The corresponding handler is \c onApply.
169*/
170
171/*!
172 \qmlsignal MessageDialog::reset()
173
174 This signal is emitted when the user has pressed any button which has
175 the \l {QMessageBox::ResetRole} {ResetRole}: \gui Reset or \gui {Restore Defaults}.
176
177 The corresponding handler is \c onReset.
178*/
179
180/*! \qmlproperty StandardButton MessageDialog::clickedButton
181
182 This property holds the button pressed by the user. Its value is
183 one of the flags set for the standardButtons property.
184*/
185
186/*!
187 \class QQuickPlatformMessageDialog1
188 \inmodule QtQuick.Dialogs
189 \internal
190
191 \brief The QQuickPlatformMessageDialog1 class provides a message dialog
192
193 The dialog is implemented via the QPlatformMessageDialogHelper when possible;
194 otherwise it falls back to a QMessageBox or a QML implementation.
195
196 \since 5.2
197*/
198
199/*!
200 Constructs a file dialog with parent window \a parent.
201*/
202QQuickPlatformMessageDialog1::QQuickPlatformMessageDialog1(QObject *parent) :
203 QQuickAbstractMessageDialog(parent)
204{
205}
206
207/*!
208 Destroys the file dialog.
209*/
210QQuickPlatformMessageDialog1::~QQuickPlatformMessageDialog1()
211{
212 if (m_dlgHelper)
213 m_dlgHelper->hide();
214 delete m_dlgHelper;
215}
216
217QPlatformMessageDialogHelper *QQuickPlatformMessageDialog1::helper()
218{
219 QQuickItem *parentItem = qobject_cast<QQuickItem *>(object: parent());
220 if (parentItem)
221 m_parentWindow = parentItem->window();
222
223 if ( !m_dlgHelper && QGuiApplicationPrivate::platformTheme()->
224 usePlatformNativeDialog(type: QPlatformTheme::MessageDialog) ) {
225 m_dlgHelper = static_cast<QPlatformMessageDialogHelper *>(QGuiApplicationPrivate::platformTheme()
226 ->createPlatformDialogHelper(type: QPlatformTheme::MessageDialog));
227 if (!m_dlgHelper)
228 return m_dlgHelper;
229 // accept() shouldn't be emitted. reject() happens only if the dialog is
230 // dismissed by closing the window rather than by one of its button widgets.
231 connect(sender: m_dlgHelper, SIGNAL(accept()), receiver: this, SLOT(accept()));
232 connect(sender: m_dlgHelper, SIGNAL(reject()), receiver: this, SLOT(reject()));
233 connect(sender: m_dlgHelper, SIGNAL(clicked(QPlatformDialogHelper::StandardButton,QPlatformDialogHelper::ButtonRole)),
234 receiver: this, SLOT(click(QPlatformDialogHelper::StandardButton,QPlatformDialogHelper::ButtonRole)));
235 }
236
237 return m_dlgHelper;
238}
239
240/*!
241 \qmlproperty bool MessageDialog::visible
242
243 This property holds whether the dialog is visible. By default this is
244 \c false.
245
246 \sa modality
247*/
248
249/*!
250 \qmlproperty Qt::WindowModality MessageDialog::modality
251
252 Whether the dialog should be shown modal with respect to the window
253 containing the dialog's parent Item, modal with respect to the whole
254 application, or non-modal.
255
256 By default it is \c Qt.WindowModal.
257
258 Modality does not mean that there are any blocking calls to wait for the
259 dialog to be accepted or rejected; it's only that the user will be
260 prevented from interacting with the parent window and/or the application
261 windows until the dialog is dismissed.
262*/
263
264/*!
265 \qmlmethod void MessageDialog::open()
266
267 Shows the dialog to the user. It is equivalent to setting \l visible to
268 \c true.
269*/
270
271/*!
272 \qmlmethod void MessageDialog::close()
273
274 Closes the dialog.
275*/
276
277/*!
278 \qmlproperty string MessageDialog::title
279
280 The title of the dialog window.
281*/
282
283/*!
284 \qmlproperty string MessageDialog::text
285
286 The primary text to be displayed.
287*/
288
289/*!
290 \qmlproperty string MessageDialog::informativeText
291
292 The informative text that provides a fuller description for the message.
293
294 Informative text can be used to supplement the \c text to give more
295 information to the user. Depending on the platform, it may appear in a
296 smaller font below the text, or simply appended to the text.
297
298 \sa {MessageDialog::text}{text}
299*/
300
301/*!
302 \qmlproperty string MessageDialog::detailedText
303
304 The text to be displayed in the details area, which is hidden by default.
305 The user will then be able to press the \gui {Show Details...} button to
306 make it visible.
307
308 \sa {MessageDialog::text}{text}
309*/
310
311/*!
312 \qmlproperty QQuickStandardIcon::Icon MessageDialog::icon
313
314 The icon of the message box can be specified with one of these values:
315
316 \table
317 \row
318 \li no icon
319 \li \c StandardIcon.NoIcon
320 \li For an unadorned text alert.
321 \row
322 \li \inlineimage ../images/question.png "Question icon"
323 \li \c StandardIcon.Question
324 \li For asking a question during normal operations.
325 \row
326 \li \image information.png
327 \li \c StandardIcon.Information
328 \li For reporting information about normal operations.
329 \row
330 \li \image warning.png
331 \li \c StandardIcon.Warning
332 \li For reporting non-critical errors.
333 \row
334 \li \image critical.png
335 \li \c StandardIcon.Critical
336 \li For reporting critical errors.
337 \endtable
338
339 The default is \c StandardIcon.NoIcon.
340
341 The enum values are the same as in \l QMessageBox::Icon.
342*/
343
344/*!
345 \qmlproperty StandardButtons MessageDialog::standardButtons
346
347 The MessageDialog has a row of buttons along the bottom, each of which has
348 a \l {QMessageBox::ButtonRole} {ButtonRole} that determines which signal
349 will be emitted when the button is pressed. You can also find out which
350 specific button was pressed after the fact via the \l clickedButton
351 property. You can control which buttons are available by setting
352 standardButtons to a bitwise-or combination of the following flags:
353
354 \value StandardButton.Ok
355 An \gui OK button defined with the
356 \l {QMessageBox::}{AcceptRole}.
357 \value StandardButton.Open
358 An \gui Open button defined with the
359 \l {QMessageBox::}{AcceptRole}.
360 \value StandardButton.Save
361 A \gui Save button defined with the
362 \l {QMessageBox::}{AcceptRole}.
363 \value StandardButton.Cancel
364 A \gui Cancel button defined with the
365 \l {QMessageBox::}{RejectRole}.
366 \value StandardButton.Close
367 A \gui Close button defined with the
368 \l {QMessageBox::}{RejectRole}.
369 \value StandardButton.Discard
370 A \gui Discard or \gui {Don't Save} button,
371 depending on the platform, defined with the
372 \l {QMessageBox::}{DestructiveRole}.
373 \value StandardButton.Apply
374 An \gui Apply button defined with the
375 \l {QMessageBox::}{ApplyRole}.
376 \value StandardButton.Reset
377 A \gui Reset button defined with the
378 \l {QMessageBox::}{ResetRole}.
379 \value StandardButton.RestoreDefaults
380 A \gui {Restore Defaults} button defined with the
381 \l {QMessageBox::}{ResetRole}.
382 \value StandardButton.Help
383 A \gui Help button defined with the
384 \l {QMessageBox::}{HelpRole}.
385 \value StandardButton.SaveAll
386 A \gui {Save All} button defined with the
387 \l {QMessageBox::}{AcceptRole}.
388 \value StandardButton.Yes
389 A \gui Yes button defined with the
390 \l {QMessageBox::}{YesRole}.
391 \value StandardButton.YesToAll
392 A \gui {Yes to All} button defined with the
393 \l {QMessageBox::}{YesRole}.
394 \value StandardButton.No
395 A \gui No button defined with the
396 \l {QMessageBox::}{NoRole}.
397 \value StandardButton.NoToAll
398 A \gui {No to All} button defined with the
399 \l {QMessageBox::}{NoRole}.
400 \value StandardButton.Abort
401 An \gui Abort button defined with the
402 \l {QMessageBox::}{RejectRole}.
403 \value StandardButton.Retry
404 A \gui Retry button defined with the
405 \l {QMessageBox::}{AcceptRole}.
406 \value StandardButton.Ignore
407 An \gui Ignore button defined with the
408 \l {QMessageBox::}{AcceptRole}.
409
410 For example the following dialog will ask a question with 5 possible answers:
411
412 \qml
413 import QtQuick 2.2
414 import QtQuick.Dialogs 1.1
415
416 MessageDialog {
417 title: "Overwrite?"
418 icon: StandardIcon.Question
419 text: "file.txt already exists. Replace?"
420 detailedText: "To replace a file means that its existing contents will be lost. " +
421 "The file that you are copying now will be copied over it instead."
422 standardButtons: StandardButton.Yes | StandardButton.YesToAll |
423 StandardButton.No | StandardButton.NoToAll | StandardButton.Abort
424 Component.onCompleted: visible = true
425 onYes: console.log("copied")
426 onNo: console.log("didn't copy")
427 onRejected: console.log("aborted")
428 }
429 \endqml
430
431 \image replacefile.png
432
433 The default is \c StandardButton.Ok.
434
435 The enum values are the same as in \l QMessageBox::StandardButtons.
436*/
437
438QT_END_NAMESPACE
439

source code of qtquickcontrols/src/dialogs/qquickplatformmessagedialog.cpp