| 1 | // Copyright (C) 2021 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 "qquickmessagedialogimpl_p.h" |
| 5 | #include "qquickmessagedialogimpl_p_p.h" |
| 6 | |
| 7 | #include <QtQuickTemplates2/private/qquickdialogbuttonbox_p_p.h> |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | QQuickMessageDialogImplPrivate::QQuickMessageDialogImplPrivate() { } |
| 12 | |
| 13 | void QQuickMessageDialogImplPrivate::handleClick(QQuickAbstractButton *button) |
| 14 | { |
| 15 | Q_Q(QQuickMessageDialogImpl); |
| 16 | |
| 17 | if (const QQuickMessageDialogImplAttached *attached = attachedOrWarn()) { |
| 18 | QPlatformDialogHelper::StandardButton standardButton = |
| 19 | QQuickDialogButtonBoxPrivate::get(box: attached->buttonBox())->standardButton(button); |
| 20 | QPlatformDialogHelper::ButtonRole role = buttonRole(button); |
| 21 | |
| 22 | emit q->buttonClicked(button: standardButton, role); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | QQuickMessageDialogImplAttached *QQuickMessageDialogImplPrivate::attachedOrWarn() |
| 27 | { |
| 28 | Q_Q(QQuickMessageDialogImpl); |
| 29 | QQuickMessageDialogImplAttached *attached = static_cast<QQuickMessageDialogImplAttached *>( |
| 30 | qmlAttachedPropertiesObject<QQuickMessageDialogImpl>(obj: q)); |
| 31 | if (!attached) |
| 32 | qmlWarning(me: q) << "Expected MessageDialogImpl attached object to be present on" << this; |
| 33 | return attached; |
| 34 | } |
| 35 | |
| 36 | QQuickMessageDialogImpl::QQuickMessageDialogImpl(QObject *parent) |
| 37 | : QQuickDialog(*(new QQuickMessageDialogImplPrivate), parent) |
| 38 | { |
| 39 | setPopupType(QQuickPopup::Window); |
| 40 | } |
| 41 | |
| 42 | QSharedPointer<QMessageDialogOptions> QQuickMessageDialogImpl::options() const |
| 43 | { |
| 44 | Q_D(const QQuickMessageDialogImpl); |
| 45 | return d->options; |
| 46 | } |
| 47 | |
| 48 | void QQuickMessageDialogImpl::setOptions(const QSharedPointer<QMessageDialogOptions> &options) |
| 49 | { |
| 50 | Q_D(QQuickMessageDialogImpl); |
| 51 | d->options = options; |
| 52 | |
| 53 | QQuickMessageDialogImplAttached *attached = d->attachedOrWarn(); |
| 54 | |
| 55 | if (options && attached) { |
| 56 | attached->detailedTextButton()->setVisible(!d->options->detailedText().isEmpty()); |
| 57 | attached->buttonBox()->setStandardButtons(d->options->standardButtons()); |
| 58 | } |
| 59 | |
| 60 | if (showDetailedText()) |
| 61 | toggleShowDetailedText(); |
| 62 | |
| 63 | emit optionsChanged(); |
| 64 | } |
| 65 | |
| 66 | QString QQuickMessageDialogImpl::text() const |
| 67 | { |
| 68 | Q_D(const QQuickMessageDialogImpl); |
| 69 | return d->options ? d->options->text() : QString(); |
| 70 | } |
| 71 | |
| 72 | QString QQuickMessageDialogImpl::informativeText() const |
| 73 | { |
| 74 | Q_D(const QQuickMessageDialogImpl); |
| 75 | return d->options ? d->options->informativeText() : QString(); |
| 76 | } |
| 77 | |
| 78 | QString QQuickMessageDialogImpl::detailedText() const |
| 79 | { |
| 80 | Q_D(const QQuickMessageDialogImpl); |
| 81 | return d->options ? d->options->detailedText() : QString(); |
| 82 | } |
| 83 | |
| 84 | bool QQuickMessageDialogImpl::showDetailedText() const |
| 85 | { |
| 86 | Q_D(const QQuickMessageDialogImpl); |
| 87 | return d->m_showDetailedText; |
| 88 | } |
| 89 | |
| 90 | void QQuickMessageDialogImpl::toggleShowDetailedText() |
| 91 | { |
| 92 | Q_D(QQuickMessageDialogImpl); |
| 93 | d->m_showDetailedText = !d->m_showDetailedText; |
| 94 | emit showDetailedTextChanged(); |
| 95 | } |
| 96 | |
| 97 | QQuickMessageDialogImplAttached *QQuickMessageDialogImpl::qmlAttachedProperties(QObject *object) |
| 98 | { |
| 99 | return new QQuickMessageDialogImplAttached(object); |
| 100 | } |
| 101 | |
| 102 | QQuickMessageDialogImplAttached::QQuickMessageDialogImplAttached(QObject *parent) |
| 103 | : QObject(*(new QQuickMessageDialogImplAttachedPrivate), parent) |
| 104 | { |
| 105 | if (!qobject_cast<QQuickMessageDialogImpl *>(object: parent)) { |
| 106 | qmlWarning(me: this) << "MessageDialogImpl attached properties should only be " |
| 107 | << "accessed through the root MessageDialogImpl instance" ; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | QQuickDialogButtonBox *QQuickMessageDialogImplAttached::buttonBox() const |
| 112 | { |
| 113 | Q_D(const QQuickMessageDialogImplAttached); |
| 114 | return d->buttonBox; |
| 115 | } |
| 116 | |
| 117 | void QQuickMessageDialogImplAttached::setButtonBox(QQuickDialogButtonBox *buttons) |
| 118 | { |
| 119 | Q_D(QQuickMessageDialogImplAttached); |
| 120 | if (d->buttonBox == buttons) |
| 121 | return; |
| 122 | |
| 123 | if (d->buttonBox) { |
| 124 | QQuickMessageDialogImpl *messageDialogImpl = |
| 125 | qobject_cast<QQuickMessageDialogImpl *>(object: parent()); |
| 126 | if (messageDialogImpl) { |
| 127 | auto dialogPrivate = QQuickMessageDialogImplPrivate::get(dialog: messageDialogImpl); |
| 128 | QObjectPrivate::disconnect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::clicked, receiverPrivate: dialogPrivate, |
| 129 | slot: &QQuickMessageDialogImplPrivate::handleClick); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | d->buttonBox = buttons; |
| 134 | |
| 135 | if (d->buttonBox) { |
| 136 | QQuickMessageDialogImpl *messageDialogImpl = |
| 137 | qobject_cast<QQuickMessageDialogImpl *>(object: parent()); |
| 138 | if (messageDialogImpl) { |
| 139 | auto dialogPrivate = QQuickMessageDialogImplPrivate::get(dialog: messageDialogImpl); |
| 140 | QObjectPrivate::connect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::clicked, receiverPrivate: dialogPrivate, |
| 141 | slot: &QQuickMessageDialogImplPrivate::handleClick); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | emit buttonBoxChanged(); |
| 146 | } |
| 147 | |
| 148 | QQuickButton *QQuickMessageDialogImplAttached::detailedTextButton() const |
| 149 | { |
| 150 | Q_D(const QQuickMessageDialogImplAttached); |
| 151 | return d->detailedTextButton; |
| 152 | } |
| 153 | void QQuickMessageDialogImplAttached::setDetailedTextButton(QQuickButton *detailedTextButton) |
| 154 | { |
| 155 | Q_D(QQuickMessageDialogImplAttached); |
| 156 | |
| 157 | if (d->detailedTextButton == detailedTextButton) |
| 158 | return; |
| 159 | |
| 160 | if (d->detailedTextButton) { |
| 161 | QQuickMessageDialogImpl *messageDialogImpl = |
| 162 | qobject_cast<QQuickMessageDialogImpl *>(object: parent()); |
| 163 | if (messageDialogImpl) |
| 164 | disconnect(sender: d->detailedTextButton, signal: &QQuickAbstractButton::clicked, receiver: messageDialogImpl, |
| 165 | slot: &QQuickMessageDialogImpl::toggleShowDetailedText); |
| 166 | } |
| 167 | |
| 168 | d->detailedTextButton = detailedTextButton; |
| 169 | |
| 170 | if (d->detailedTextButton) { |
| 171 | QQuickMessageDialogImpl *messageDialogImpl = |
| 172 | qobject_cast<QQuickMessageDialogImpl *>(object: parent()); |
| 173 | if (messageDialogImpl) |
| 174 | connect(sender: d->detailedTextButton, signal: &QQuickAbstractButton::clicked, context: messageDialogImpl, |
| 175 | slot: &QQuickMessageDialogImpl::toggleShowDetailedText); |
| 176 | } |
| 177 | |
| 178 | emit detailedTextButtonChanged(); |
| 179 | } |
| 180 | |
| 181 | QT_END_NAMESPACE |
| 182 | |
| 183 | #include "moc_qquickmessagedialogimpl_p.cpp" |
| 184 | |