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