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