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

source code of qtdeclarative/src/quickdialogs/quickdialogsquickimpl/qquickmessagedialogimpl.cpp