summaryrefslogtreecommitdiff
path: root/vcl/qt5/QtInstanceMessageDialog.cxx
blob: b9e458c502ac4be978ec342219b17fb50bb94a73 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <QtInstanceMessageDialog.hxx>
#include <QtInstanceMessageDialog.moc>

#include <QtWidgets/QPushButton>

/**
 * Name of the property to set on a QPushButton that holds the
 * VCL response code of that button.
 */
const char* const PROPERTY_VCL_RESPONSE_CODE = "response-code";

QtInstanceMessageDialog::QtInstanceMessageDialog(QMessageBox* pMessageDialog)
    : QtInstanceDialog(pMessageDialog)
    , m_pMessageDialog(pMessageDialog)
    , m_aRunAsyncFunc(nullptr)
{
    assert(m_pMessageDialog);
}

void QtInstanceMessageDialog::set_primary_text(const rtl::OUString& rText)
{
    m_pMessageDialog->setText(toQString(rText));
}

void QtInstanceMessageDialog::set_secondary_text(const rtl::OUString& rText)
{
    m_pMessageDialog->setInformativeText(toQString(rText));
}

weld::Container* QtInstanceMessageDialog::weld_message_area() { return nullptr; }

OUString QtInstanceMessageDialog::get_primary_text() const
{
    assert(m_pMessageDialog);
    return toOUString(m_pMessageDialog->text());
}

OUString QtInstanceMessageDialog::get_secondary_text() const
{
    assert(m_pMessageDialog);
    return toOUString(m_pMessageDialog->informativeText());
}

void QtInstanceMessageDialog::add_button(const OUString& rText, int nResponse, const OUString&)
{
    assert(m_pMessageDialog);
    QPushButton* pButton = m_pMessageDialog->addButton(vclToQtStringWithAccelerator(rText),
                                                       QMessageBox::ButtonRole::ActionRole);
    pButton->setProperty(PROPERTY_VCL_RESPONSE_CODE, QVariant::fromValue(nResponse));
}

void QtInstanceMessageDialog::set_default_response(int nResponse)
{
    assert(m_pMessageDialog);

    QPushButton* pButton = buttonForResponseCode(nResponse);
    if (pButton)
        m_pMessageDialog->setDefaultButton(pButton);
}

QtInstanceButton* QtInstanceMessageDialog::weld_widget_for_response(int nResponse)
{
    if (QPushButton* pButton = buttonForResponseCode(nResponse))
        return new QtInstanceButton(pButton);

    return nullptr;
}

int QtInstanceMessageDialog::run()
{
    m_pMessageDialog->exec();
    QAbstractButton* pClickedButton = m_pMessageDialog->clickedButton();
    if (!pClickedButton)
        return RET_CLOSE;
    return pClickedButton->property(PROPERTY_VCL_RESPONSE_CODE).toInt();
}

bool QtInstanceMessageDialog::runAsync(const std::shared_ptr<weld::DialogController>& rxOwner,
                                       const std::function<void(sal_Int32)>& func)
{
    assert(m_pMessageDialog);

    m_xRunAsyncDialogController = rxOwner;
    m_aRunAsyncFunc = func;
    connect(m_pMessageDialog, &QDialog::finished, this, &QtInstanceMessageDialog::dialogFinished);
    m_pMessageDialog->open();

    return true;
}

bool QtInstanceMessageDialog::runAsync(std::shared_ptr<Dialog> const& rxSelf,
                                       const std::function<void(sal_Int32)>& func)
{
    assert(m_pMessageDialog);
    assert(rxSelf.get() == this);

    m_xRunAsyncDialog = rxSelf;
    m_aRunAsyncFunc = func;
    connect(m_pMessageDialog, &QDialog::finished, this, &QtInstanceMessageDialog::dialogFinished);
    m_pMessageDialog->open();

    return true;
}

void QtInstanceMessageDialog::response(int nResponse)
{
    assert(m_pMessageDialog);
    m_pMessageDialog->done(nResponse);
}

void QtInstanceMessageDialog::dialogFinished(int nResult)
{
    assert(m_aRunAsyncFunc);

    disconnect(m_pMessageDialog, &QDialog::finished, this,
               &QtInstanceMessageDialog::dialogFinished);

    // use local variables for these, as members might have got de-allocated by the time they're reset
    std::shared_ptr<weld::Dialog> xRunAsyncDialog = m_xRunAsyncDialog;
    std::shared_ptr<weld::DialogController> xRunAsyncDialogController = m_xRunAsyncDialogController;
    std::function<void(sal_Int32)> aFunc = m_aRunAsyncFunc;
    m_aRunAsyncFunc = nullptr;
    m_xRunAsyncDialogController.reset();
    m_xRunAsyncDialog.reset();

    // if a button was clicked, use its response code, otherwise the passed one
    int nRet = nResult;
    if (QAbstractButton* pClickedButton = m_pMessageDialog->clickedButton())
        nRet = pClickedButton->property(PROPERTY_VCL_RESPONSE_CODE).toInt();

    SolarMutexGuard g;
    aFunc(nRet);

    xRunAsyncDialogController.reset();
    xRunAsyncDialog.reset();
}

QPushButton* QtInstanceMessageDialog::buttonForResponseCode(int nResponse)
{
    assert(m_pMessageDialog);

    const QList<QAbstractButton*> aButtons = m_pMessageDialog->buttons();
    for (QAbstractButton* pAbstractButton : aButtons)
    {
        if (pAbstractButton->property(PROPERTY_VCL_RESPONSE_CODE).toInt() == nResponse)
        {
            QPushButton* pButton = dynamic_cast<QPushButton*>(pAbstractButton);
            assert(pButton);
            return pButton;
        }
    }
    return nullptr;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */