Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickmessagedialogimpl.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
7
8#include <QtQuickTemplates2/private/qquickdialogbuttonbox_p_p.h>
9
11
12QQuickMessageDialogImplPrivate::QQuickMessageDialogImplPrivate() { }
13
14void QQuickMessageDialogImplPrivate::handleClick(QQuickAbstractButton *button)
15{
16 Q_Q(QQuickMessageDialogImpl);
17
18 if (const QQuickMessageDialogImplAttached *attached = attachedOrWarn()) {
19 QPlatformDialogHelper::StandardButton standardButton =
20 QQuickDialogButtonBoxPrivate::get(attached->buttonBox())->standardButton(button);
21 QPlatformDialogHelper::ButtonRole role = buttonRole(button);
22
23 emit q->buttonClicked(standardButton, role);
24 }
25}
26
27QQuickMessageDialogImplAttached *QQuickMessageDialogImplPrivate::attachedOrWarn()
28{
29 Q_Q(QQuickMessageDialogImpl);
30 QQuickMessageDialogImplAttached *attached = static_cast<QQuickMessageDialogImplAttached *>(
31 qmlAttachedPropertiesObject<QQuickMessageDialogImpl>(q));
32 if (!attached)
33 qmlWarning(q) << "Expected MessageDialogImpl attached object to be present on" << this;
34 return attached;
35}
36
37QQuickMessageDialogImpl::QQuickMessageDialogImpl(QObject *parent)
38 : QQuickDialog(*(new QQuickMessageDialogImplPrivate), parent)
39{
40}
41
42QSharedPointer<QMessageDialogOptions> QQuickMessageDialogImpl::options() const
43{
44 Q_D(const QQuickMessageDialogImpl);
45 return d->options;
46}
47
48void 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
66QString QQuickMessageDialogImpl::text() const
67{
68 Q_D(const QQuickMessageDialogImpl);
69 return d->options ? d->options->text() : QString();
70}
71
72QString QQuickMessageDialogImpl::informativeText() const
73{
74 Q_D(const QQuickMessageDialogImpl);
75 return d->options ? d->options->informativeText() : QString();
76}
77
78QString QQuickMessageDialogImpl::detailedText() const
79{
80 Q_D(const QQuickMessageDialogImpl);
81 return d->options ? d->options->detailedText() : QString();
82}
83
84bool QQuickMessageDialogImpl::showDetailedText() const
85{
86 Q_D(const QQuickMessageDialogImpl);
87 return d->m_showDetailedText;
88}
89
90void QQuickMessageDialogImpl::toggleShowDetailedText()
91{
92 Q_D(QQuickMessageDialogImpl);
93 d->m_showDetailedText = !d->m_showDetailedText;
94 emit showDetailedTextChanged();
95}
96
97QQuickMessageDialogImplAttached *QQuickMessageDialogImpl::qmlAttachedProperties(QObject *object)
98{
99 return new QQuickMessageDialogImplAttached(object);
100}
101
102QQuickMessageDialogImplAttached::QQuickMessageDialogImplAttached(QObject *parent)
103 : QObject(*(new QQuickMessageDialogImplAttachedPrivate), parent)
104{
105 if (!qobject_cast<QQuickMessageDialogImpl *>(parent)) {
106 qmlWarning(this) << "MessageDialogImpl attached properties should only be "
107 << "accessed through the root MessageDialogImpl instance";
108 }
109}
110
111QQuickDialogButtonBox *QQuickMessageDialogImplAttached::buttonBox() const
112{
113 Q_D(const QQuickMessageDialogImplAttached);
114 return d->buttonBox;
115}
116
117void 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 *>(parent());
126 if (messageDialogImpl) {
127 auto dialogPrivate = QQuickMessageDialogImplPrivate::get(messageDialogImpl);
128 QObjectPrivate::disconnect(d->buttonBox, &QQuickDialogButtonBox::clicked, dialogPrivate,
129 &QQuickMessageDialogImplPrivate::handleClick);
130 }
131 }
132
133 d->buttonBox = buttons;
134
135 if (d->buttonBox) {
136 QQuickMessageDialogImpl *messageDialogImpl =
137 qobject_cast<QQuickMessageDialogImpl *>(parent());
138 if (messageDialogImpl) {
139 auto dialogPrivate = QQuickMessageDialogImplPrivate::get(messageDialogImpl);
140 QObjectPrivate::connect(d->buttonBox, &QQuickDialogButtonBox::clicked, dialogPrivate,
141 &QQuickMessageDialogImplPrivate::handleClick);
142 }
143 }
144
145 emit buttonBoxChanged();
146}
147
148QQuickButton *QQuickMessageDialogImplAttached::detailedTextButton() const
149{
150 Q_D(const QQuickMessageDialogImplAttached);
151 return d->detailedTextButton;
152}
153void 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 *>(parent());
163 if (messageDialogImpl)
164 disconnect(d->detailedTextButton, &QQuickAbstractButton::clicked, messageDialogImpl,
165 &QQuickMessageDialogImpl::toggleShowDetailedText);
166 }
167
168 d->detailedTextButton = detailedTextButton;
169
170 if (d->detailedTextButton) {
171 QQuickMessageDialogImpl *messageDialogImpl =
172 qobject_cast<QQuickMessageDialogImpl *>(parent());
173 if (messageDialogImpl)
174 connect(d->detailedTextButton, &QQuickAbstractButton::clicked, messageDialogImpl,
175 &QQuickMessageDialogImpl::toggleShowDetailedText);
176 }
177
178 emit detailedTextButtonChanged();
179}
180
181QT_END_NAMESPACE
182
183#include "moc_qquickmessagedialogimpl_p.cpp"
QQuickMessageDialogImplAttached * attachedOrWarn()
void handleClick(QQuickAbstractButton *button) override