aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickdialogs/quickdialogsquickimpl/qquickmessagedialogimpl.cpp
blob: 578da6155999cb9dd3df533cc00bca75d4641f23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qquickmessagedialogimpl_p.h"
#include "qquickmessagedialogimpl_p_p.h"

#include <QtQuickTemplates2/private/qquickdialogbuttonbox_p_p.h>

QT_BEGIN_NAMESPACE

QQuickMessageDialogImplPrivate::QQuickMessageDialogImplPrivate() { }

void QQuickMessageDialogImplPrivate::handleClick(QQuickAbstractButton *button)
{
    Q_Q(QQuickMessageDialogImpl);

    if (const QQuickMessageDialogImplAttached *attached = attachedOrWarn()) {
        QPlatformDialogHelper::StandardButton standardButton =
                QQuickDialogButtonBoxPrivate::get(attached->buttonBox())->standardButton(button);
        QPlatformDialogHelper::ButtonRole role = buttonRole(button);

        emit q->buttonClicked(standardButton, role);
    }
}

QQuickMessageDialogImplAttached *QQuickMessageDialogImplPrivate::attachedOrWarn()
{
    Q_Q(QQuickMessageDialogImpl);
    QQuickMessageDialogImplAttached *attached = static_cast<QQuickMessageDialogImplAttached *>(
            qmlAttachedPropertiesObject<QQuickMessageDialogImpl>(q));
    if (!attached)
        qmlWarning(q) << "Expected MessageDialogImpl attached object to be present on" << this;
    return attached;
}

QQuickMessageDialogImpl::QQuickMessageDialogImpl(QObject *parent)
    : QQuickDialog(*(new QQuickMessageDialogImplPrivate), parent)
{
}

QSharedPointer<QMessageDialogOptions> QQuickMessageDialogImpl::options() const
{
    Q_D(const QQuickMessageDialogImpl);
    return d->options;
}

void QQuickMessageDialogImpl::setOptions(const QSharedPointer<QMessageDialogOptions> &options)
{
    Q_D(QQuickMessageDialogImpl);
    d->options = options;

    QQuickMessageDialogImplAttached *attached = d->attachedOrWarn();

    if (options && attached) {
        attached->detailedTextButton()->setVisible(!d->options->detailedText().isEmpty());
        attached->buttonBox()->setStandardButtons(d->options->standardButtons());
    }

    if (showDetailedText())
        toggleShowDetailedText();

    emit optionsChanged();
}

QString QQuickMessageDialogImpl::text() const
{
    Q_D(const QQuickMessageDialogImpl);
    return d->options ? d->options->text() : QString();
}

QString QQuickMessageDialogImpl::informativeText() const
{
    Q_D(const QQuickMessageDialogImpl);
    return d->options ? d->options->informativeText() : QString();
}

QString QQuickMessageDialogImpl::detailedText() const
{
    Q_D(const QQuickMessageDialogImpl);
    return d->options ? d->options->detailedText() : QString();
}

bool QQuickMessageDialogImpl::showDetailedText() const
{
    Q_D(const QQuickMessageDialogImpl);
    return d->m_showDetailedText;
}

void QQuickMessageDialogImpl::toggleShowDetailedText()
{
    Q_D(QQuickMessageDialogImpl);
    d->m_showDetailedText = !d->m_showDetailedText;
    emit showDetailedTextChanged();
}

QQuickMessageDialogImplAttached *QQuickMessageDialogImpl::qmlAttachedProperties(QObject *object)
{
    return new QQuickMessageDialogImplAttached(object);
}

QQuickMessageDialogImplAttached::QQuickMessageDialogImplAttached(QObject *parent)
    : QObject(*(new QQuickMessageDialogImplAttachedPrivate), parent)
{
    if (!qobject_cast<QQuickMessageDialogImpl *>(parent)) {
        qmlWarning(this) << "MessageDialogImpl attached properties should only be "
                         << "accessed through the root MessageDialogImpl instance";
    }
}

QQuickDialogButtonBox *QQuickMessageDialogImplAttached::buttonBox() const
{
    Q_D(const QQuickMessageDialogImplAttached);
    return d->buttonBox;
}

void QQuickMessageDialogImplAttached::setButtonBox(QQuickDialogButtonBox *buttons)
{
    Q_D(QQuickMessageDialogImplAttached);
    if (d->buttonBox == buttons)
        return;

    if (d->buttonBox) {
        QQuickMessageDialogImpl *messageDialogImpl =
                qobject_cast<QQuickMessageDialogImpl *>(parent());
        if (messageDialogImpl) {
            auto dialogPrivate = QQuickMessageDialogImplPrivate::get(messageDialogImpl);
            QObjectPrivate::disconnect(d->buttonBox, &QQuickDialogButtonBox::clicked, dialogPrivate,
                                       &QQuickMessageDialogImplPrivate::handleClick);
        }
    }

    d->buttonBox = buttons;

    if (d->buttonBox) {
        QQuickMessageDialogImpl *messageDialogImpl =
                qobject_cast<QQuickMessageDialogImpl *>(parent());
        if (messageDialogImpl) {
            auto dialogPrivate = QQuickMessageDialogImplPrivate::get(messageDialogImpl);
            QObjectPrivate::connect(d->buttonBox, &QQuickDialogButtonBox::clicked, dialogPrivate,
                                    &QQuickMessageDialogImplPrivate::handleClick);
        }
    }

    emit buttonBoxChanged();
}

QQuickButton *QQuickMessageDialogImplAttached::detailedTextButton() const
{
    Q_D(const QQuickMessageDialogImplAttached);
    return d->detailedTextButton;
}
void QQuickMessageDialogImplAttached::setDetailedTextButton(QQuickButton *detailedTextButton)
{
    Q_D(QQuickMessageDialogImplAttached);

    if (d->detailedTextButton == detailedTextButton)
        return;

    if (d->detailedTextButton) {
        QQuickMessageDialogImpl *messageDialogImpl =
                qobject_cast<QQuickMessageDialogImpl *>(parent());
        if (messageDialogImpl)
            disconnect(d->detailedTextButton, &QQuickAbstractButton::clicked, messageDialogImpl,
                       &QQuickMessageDialogImpl::toggleShowDetailedText);
    }

    d->detailedTextButton = detailedTextButton;

    if (d->detailedTextButton) {
        QQuickMessageDialogImpl *messageDialogImpl =
                qobject_cast<QQuickMessageDialogImpl *>(parent());
        if (messageDialogImpl)
            connect(d->detailedTextButton, &QQuickAbstractButton::clicked, messageDialogImpl,
                    &QQuickMessageDialogImpl::toggleShowDetailedText);
    }

    emit detailedTextButtonChanged();
}

QT_END_NAMESPACE

#include "moc_qquickmessagedialogimpl_p.cpp"