| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Labs Platform module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
| 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 http://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free |
| 28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
| 29 | ** the packaging of this file. Please review the following information to |
| 30 | ** ensure the GNU General Public License version 2.0 requirements will be |
| 31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
| 32 | ** |
| 33 | ** $QT_END_LICENSE$ |
| 34 | ** |
| 35 | ****************************************************************************/ |
| 36 | |
| 37 | #include "qquickplatformmessagedialog_p.h" |
| 38 | |
| 39 | #include <QtQml/qqmlinfo.h> |
| 40 | |
| 41 | QT_BEGIN_NAMESPACE |
| 42 | |
| 43 | /*! |
| 44 | \qmltype MessageDialog |
| 45 | \inherits Dialog |
| 46 | //! \instantiates QQuickPlatformMessageDialog |
| 47 | \inqmlmodule Qt.labs.platform |
| 48 | \since 5.8 |
| 49 | \brief A native message dialog. |
| 50 | |
| 51 | The MessageDialog type provides a QML API for native platform message dialogs. |
| 52 | |
| 53 | \image qtlabsplatform-messagedialog-android.png |
| 54 | |
| 55 | A message dialog is used to inform the user, or ask the user a question. |
| 56 | A message dialog displays a primary \l text to alert the user to a situation, |
| 57 | an \l {informativeText}{informative text} to further explain the alert or to |
| 58 | ask the user a question, and an optional \l {detailedText}{detailed text} to |
| 59 | provide even more data if the user requests it. A message box can also display |
| 60 | a configurable set of \l buttons for accepting a user response. |
| 61 | |
| 62 | To show a message dialog, construct an instance of MessageDialog, set the |
| 63 | desired properties, and call \l {Dialog::}{open()}. |
| 64 | |
| 65 | \code |
| 66 | MessageDialog { |
| 67 | buttons: MessageDialog.Ok |
| 68 | text: "The document has been modified." |
| 69 | } |
| 70 | \endcode |
| 71 | |
| 72 | The user must click the \uicontrol OK button to dismiss the message dialog. |
| 73 | A modal message dialog blocks the rest of the GUI until the message is |
| 74 | dismissed. |
| 75 | |
| 76 | A more elaborate approach than just alerting the user to an event is to |
| 77 | also ask the user what to do about it. Store the question in the |
| 78 | \l {informativeText}{informative text} property, and specify the \l buttons |
| 79 | property to the set of buttons you want as the set of user responses. The |
| 80 | buttons are specified by combining values using the bitwise OR operator. The |
| 81 | display order for the buttons is platform dependent. |
| 82 | |
| 83 | \code |
| 84 | MessageDialog { |
| 85 | text: "The document has been modified." |
| 86 | informativeText: "Do you want to save your changes?" |
| 87 | buttons: MessageDialog.Ok | MessageDialog.Cancel |
| 88 | |
| 89 | onAccepted: document.save() |
| 90 | } |
| 91 | \endcode |
| 92 | |
| 93 | \image qtlabsplatform-messagedialog-informative-android.png |
| 94 | |
| 95 | The \l clicked() signal passes the information of which button was clicked. |
| 96 | |
| 97 | A native platform message dialog is currently available on the following platforms: |
| 98 | |
| 99 | \list |
| 100 | \li iOS |
| 101 | \li Android |
| 102 | \li WinRT |
| 103 | \endlist |
| 104 | |
| 105 | \input includes/widgets.qdocinc 1 |
| 106 | |
| 107 | \labs |
| 108 | */ |
| 109 | |
| 110 | /*! |
| 111 | \qmlsignal Qt.labs.platform::MessageDialog::clicked(button) |
| 112 | |
| 113 | This signal is emitted when a dialog \a button is clicked. |
| 114 | |
| 115 | \sa buttons |
| 116 | */ |
| 117 | |
| 118 | /*! |
| 119 | \qmlsignal Qt.labs.platform::MessageDialog::okClicked() |
| 120 | |
| 121 | This signal is emitted when \uicontrol Ok is clicked. |
| 122 | */ |
| 123 | |
| 124 | /*! |
| 125 | \qmlsignal Qt.labs.platform::MessageDialog::saveClicked() |
| 126 | |
| 127 | This signal is emitted when \uicontrol Save is clicked. |
| 128 | */ |
| 129 | |
| 130 | /*! |
| 131 | \qmlsignal Qt.labs.platform::MessageDialog::saveAllClicked() |
| 132 | |
| 133 | This signal is emitted when \uicontrol {Save All} is clicked. |
| 134 | */ |
| 135 | |
| 136 | /*! |
| 137 | \qmlsignal Qt.labs.platform::MessageDialog::openClicked() |
| 138 | |
| 139 | This signal is emitted when \uicontrol Open is clicked. |
| 140 | */ |
| 141 | |
| 142 | /*! |
| 143 | \qmlsignal Qt.labs.platform::MessageDialog::yesClicked() |
| 144 | |
| 145 | This signal is emitted when \uicontrol Yes is clicked. |
| 146 | */ |
| 147 | |
| 148 | /*! |
| 149 | \qmlsignal Qt.labs.platform::MessageDialog::yesToAllClicked() |
| 150 | |
| 151 | This signal is emitted when \uicontrol {Yes To All} is clicked. |
| 152 | */ |
| 153 | |
| 154 | /*! |
| 155 | \qmlsignal Qt.labs.platform::MessageDialog::noClicked() |
| 156 | |
| 157 | This signal is emitted when \uicontrol No is clicked. |
| 158 | */ |
| 159 | |
| 160 | /*! |
| 161 | \qmlsignal Qt.labs.platform::MessageDialog::noToAllClicked() |
| 162 | |
| 163 | This signal is emitted when \uicontrol {No To All} is clicked. |
| 164 | */ |
| 165 | |
| 166 | /*! |
| 167 | \qmlsignal Qt.labs.platform::MessageDialog::abortClicked() |
| 168 | |
| 169 | This signal is emitted when \uicontrol Abort is clicked. |
| 170 | */ |
| 171 | |
| 172 | /*! |
| 173 | \qmlsignal Qt.labs.platform::MessageDialog::retryClicked() |
| 174 | |
| 175 | This signal is emitted when \uicontrol Retry is clicked. |
| 176 | */ |
| 177 | |
| 178 | /*! |
| 179 | \qmlsignal Qt.labs.platform::MessageDialog::ignoreClicked() |
| 180 | |
| 181 | This signal is emitted when \uicontrol Ignore is clicked. |
| 182 | */ |
| 183 | |
| 184 | /*! |
| 185 | \qmlsignal Qt.labs.platform::MessageDialog::closeClicked() |
| 186 | |
| 187 | This signal is emitted when \uicontrol Close is clicked. |
| 188 | */ |
| 189 | |
| 190 | /*! |
| 191 | \qmlsignal Qt.labs.platform::MessageDialog::cancelClicked() |
| 192 | |
| 193 | This signal is emitted when \uicontrol Cancel is clicked. |
| 194 | */ |
| 195 | |
| 196 | /*! |
| 197 | \qmlsignal Qt.labs.platform::MessageDialog::discardClicked() |
| 198 | |
| 199 | This signal is emitted when \uicontrol Discard is clicked. |
| 200 | */ |
| 201 | |
| 202 | /*! |
| 203 | \qmlsignal Qt.labs.platform::MessageDialog::helpClicked() |
| 204 | |
| 205 | This signal is emitted when \uicontrol Help is clicked. |
| 206 | */ |
| 207 | |
| 208 | /*! |
| 209 | \qmlsignal Qt.labs.platform::MessageDialog::applyClicked() |
| 210 | |
| 211 | This signal is emitted when \uicontrol Apply is clicked. |
| 212 | */ |
| 213 | |
| 214 | /*! |
| 215 | \qmlsignal Qt.labs.platform::MessageDialog::resetClicked() |
| 216 | |
| 217 | This signal is emitted when \uicontrol Reset is clicked. |
| 218 | */ |
| 219 | |
| 220 | /*! |
| 221 | \qmlsignal Qt.labs.platform::MessageDialog::restoreDefaultsClicked() |
| 222 | |
| 223 | This signal is emitted when \uicontrol {Restore Defaults} is clicked. |
| 224 | */ |
| 225 | |
| 226 | QQuickPlatformMessageDialog::QQuickPlatformMessageDialog(QObject *parent) |
| 227 | : QQuickPlatformDialog(QPlatformTheme::MessageDialog, parent), |
| 228 | m_options(QMessageDialogOptions::create()) |
| 229 | { |
| 230 | } |
| 231 | |
| 232 | /*! |
| 233 | \qmlproperty string Qt.labs.platform::MessageDialog::text |
| 234 | |
| 235 | This property holds the text to be displayed on the message dialog. |
| 236 | |
| 237 | \sa informativeText, detailedText |
| 238 | */ |
| 239 | QString QQuickPlatformMessageDialog::text() const |
| 240 | { |
| 241 | return m_options->text(); |
| 242 | } |
| 243 | |
| 244 | void QQuickPlatformMessageDialog::setText(const QString &text) |
| 245 | { |
| 246 | if (m_options->text() == text) |
| 247 | return; |
| 248 | |
| 249 | m_options->setText(text); |
| 250 | emit textChanged(); |
| 251 | } |
| 252 | |
| 253 | /*! |
| 254 | \qmlproperty string Qt.labs.platform::MessageDialog::informativeText |
| 255 | |
| 256 | This property holds the informative text that provides a fuller description for the message. |
| 257 | |
| 258 | Informative text can be used to expand upon the \l text to give more information to the user. |
| 259 | |
| 260 | \sa text, detailedText |
| 261 | */ |
| 262 | QString QQuickPlatformMessageDialog::informativeText() const |
| 263 | { |
| 264 | return m_options->informativeText(); |
| 265 | } |
| 266 | |
| 267 | void QQuickPlatformMessageDialog::setInformativeText(const QString &text) |
| 268 | { |
| 269 | if (m_options->informativeText() == text) |
| 270 | return; |
| 271 | |
| 272 | m_options->setInformativeText(text); |
| 273 | emit informativeTextChanged(); |
| 274 | } |
| 275 | |
| 276 | /*! |
| 277 | \qmlproperty string Qt.labs.platform::MessageDialog::detailedText |
| 278 | |
| 279 | This property holds the text to be displayed in the details area. |
| 280 | |
| 281 | \sa text, informativeText |
| 282 | */ |
| 283 | QString QQuickPlatformMessageDialog::detailedText() const |
| 284 | { |
| 285 | return m_options->detailedText(); |
| 286 | } |
| 287 | |
| 288 | void QQuickPlatformMessageDialog::setDetailedText(const QString &text) |
| 289 | { |
| 290 | if (m_options->detailedText() == text) |
| 291 | return; |
| 292 | |
| 293 | m_options->setDetailedText(text); |
| 294 | emit detailedTextChanged(); |
| 295 | } |
| 296 | |
| 297 | /*! |
| 298 | \qmlproperty flags Qt.labs.platform::MessageDialog::buttons |
| 299 | |
| 300 | This property holds a combination of buttons that are used by the message dialog. |
| 301 | The default value is \c MessageDialog.NoButton. |
| 302 | |
| 303 | Possible flags: |
| 304 | \value MessageDialog.Ok An "OK" button defined with the \c AcceptRole. |
| 305 | \value MessageDialog.Open An "Open" button defined with the \c AcceptRole. |
| 306 | \value MessageDialog.Save A "Save" button defined with the \c AcceptRole. |
| 307 | \value MessageDialog.Cancel A "Cancel" button defined with the \c RejectRole. |
| 308 | \value MessageDialog.Close A "Close" button defined with the \c RejectRole. |
| 309 | \value MessageDialog.Discard A "Discard" or "Don't Save" button, depending on the platform, defined with the \c DestructiveRole. |
| 310 | \value MessageDialog.Apply An "Apply" button defined with the \c ApplyRole. |
| 311 | \value MessageDialog.Reset A "Reset" button defined with the \c ResetRole. |
| 312 | \value MessageDialog.RestoreDefaults A "Restore Defaults" button defined with the \c ResetRole. |
| 313 | \value MessageDialog.Help A "Help" button defined with the \c HelpRole. |
| 314 | \value MessageDialog.SaveAll A "Save All" button defined with the \c AcceptRole. |
| 315 | \value MessageDialog.Yes A "Yes" button defined with the \c YesRole. |
| 316 | \value MessageDialog.YesToAll A "Yes to All" button defined with the \c YesRole. |
| 317 | \value MessageDialog.No A "No" button defined with the \c NoRole. |
| 318 | \value MessageDialog.NoToAll A "No to All" button defined with the \c NoRole. |
| 319 | \value MessageDialog.Abort An "Abort" button defined with the \c RejectRole. |
| 320 | \value MessageDialog.Retry A "Retry" button defined with the \c AcceptRole. |
| 321 | \value MessageDialog.Ignore An "Ignore" button defined with the \c AcceptRole. |
| 322 | \value MessageDialog.NoButton The dialog has no buttons. |
| 323 | |
| 324 | \sa clicked() |
| 325 | */ |
| 326 | QPlatformDialogHelper::StandardButtons QQuickPlatformMessageDialog::buttons() const |
| 327 | { |
| 328 | return m_options->standardButtons(); |
| 329 | } |
| 330 | |
| 331 | void QQuickPlatformMessageDialog::setButtons(QPlatformDialogHelper::StandardButtons buttons) |
| 332 | { |
| 333 | if (m_options->standardButtons() == buttons) |
| 334 | return; |
| 335 | |
| 336 | m_options->setStandardButtons(buttons); |
| 337 | emit buttonsChanged(); |
| 338 | } |
| 339 | |
| 340 | void QQuickPlatformMessageDialog::onCreate(QPlatformDialogHelper *dialog) |
| 341 | { |
| 342 | if (QPlatformMessageDialogHelper *messageDialog = qobject_cast<QPlatformMessageDialogHelper *>(object: dialog)) { |
| 343 | connect(sender: messageDialog, signal: &QPlatformMessageDialogHelper::clicked, receiver: this, slot: &QQuickPlatformMessageDialog::handleClick); |
| 344 | messageDialog->setOptions(m_options); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | void QQuickPlatformMessageDialog::onShow(QPlatformDialogHelper *dialog) |
| 349 | { |
| 350 | m_options->setWindowTitle(title()); |
| 351 | if (QPlatformMessageDialogHelper *messageDialog = qobject_cast<QPlatformMessageDialogHelper *>(object: dialog)) |
| 352 | messageDialog->setOptions(m_options); |
| 353 | } |
| 354 | |
| 355 | void QQuickPlatformMessageDialog::handleClick(QPlatformDialogHelper::StandardButton button) |
| 356 | { |
| 357 | done(result: button); |
| 358 | emit clicked(button); |
| 359 | |
| 360 | switch (button) { |
| 361 | case QPlatformDialogHelper::Ok: emit okClicked(); break; |
| 362 | case QPlatformDialogHelper::Save: emit saveClicked(); break; |
| 363 | case QPlatformDialogHelper::SaveAll: emit saveAllClicked(); break; |
| 364 | case QPlatformDialogHelper::Open: emit openClicked(); break; |
| 365 | case QPlatformDialogHelper::Yes: emit yesClicked(); break; |
| 366 | case QPlatformDialogHelper::YesToAll: emit yesToAllClicked(); break; |
| 367 | case QPlatformDialogHelper::No: emit noClicked(); break; |
| 368 | case QPlatformDialogHelper::NoToAll: emit noToAllClicked(); break; |
| 369 | case QPlatformDialogHelper::Abort: emit abortClicked(); break; |
| 370 | case QPlatformDialogHelper::Retry: emit retryClicked(); break; |
| 371 | case QPlatformDialogHelper::Ignore: emit ignoreClicked(); break; |
| 372 | case QPlatformDialogHelper::Close: emit closeClicked(); break; |
| 373 | case QPlatformDialogHelper::Cancel: emit cancelClicked(); break; |
| 374 | case QPlatformDialogHelper::Discard: emit discardClicked(); break; |
| 375 | case QPlatformDialogHelper::Help: emit helpClicked(); break; |
| 376 | case QPlatformDialogHelper::Apply: emit applyClicked(); break; |
| 377 | case QPlatformDialogHelper::Reset: emit resetClicked(); break; |
| 378 | case QPlatformDialogHelper::RestoreDefaults: emit restoreDefaultsClicked(); break; |
| 379 | default: qmlWarning(me: this) << "unknown button" << int(button); break; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | QT_END_NAMESPACE |
| 384 | |