summaryrefslogtreecommitdiff
path: root/vcl/qt5/QtInstanceMessageDialog.cxx
blob: 76903fc5c63c1ded2dace427d9d5199e07f89149 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* -*- 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)
{
    SolarMutexGuard g;
    QtInstance* pQtInstance = GetQtInstance();
    if (!pQtInstance->IsMainThread())
    {
        pQtInstance->RunInMainThread([&] { set_primary_text(rText); });
        return;
    }

    m_pMessageDialog->setText(toQString(rText));
}

void QtInstanceMessageDialog::set_secondary_text(const rtl::OUString& rText)
{
    SolarMutexGuard g;
    QtInstance* pQtInstance = GetQtInstance();
    if (!pQtInstance->IsMainThread())
    {
        pQtInstance->RunInMainThread([&] { set_secondary_text(rText); });
        return;
    }

    m_pMessageDialog->setInformativeText(toQString(rText));
}

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

OUString QtInstanceMessageDialog::get_primary_text() const
{
    SolarMutexGuard g;
    QtInstance* pQtInstance = GetQtInstance();
    OUString sText;
    if (!pQtInstance->IsMainThread())
    {
        pQtInstance->RunInMainThread([&] { sText = get_primary_text(); });
        return sText;
    }

    assert(m_pMessageDialog);
    return toOUString(m_pMessageDialog->text());
}

OUString QtInstanceMessageDialog::get_secondary_text() const
{
    SolarMutexGuard g;
    QtInstance* pQtInstance = GetQtInstance();
    OUString sText;
    if (!pQtInstance->IsMainThread())
    {
        pQtInstance->RunInMainThread([&] { sText = get_secondary_text(); });
        return sText;
    }

    assert(m_pMessageDialog);
    return toOUString(m_pMessageDialog->informativeText());
}

void QtInstanceMessageDialog::add_button(const OUString& rText, int nResponse, const OUString&)
{
    SolarMutexGuard g;
    QtInstance* pQtInstance = GetQtInstance();
    if (!pQtInstance->IsMainThread())
    {
        pQtInstance->RunInMainThread([&] { add_button(rText, nResponse); });
        return;
    }

    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)
{
    SolarMutexGuard g;
    QtInstance* pQtInstance = GetQtInstance();
    if (!pQtInstance->IsMainThread())
    {
        pQtInstance->RunInMainThread([&] { set_default_response(nResponse); });
        return;
    }

    assert(m_pMessageDialog);

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

QtInstanceButton* QtInstanceMessageDialog::weld_widget_for_response(int nResponse)
{
    SolarMutexGuard g;
    QtInstance* pQtInstance = GetQtInstance();
    if (!pQtInstance->IsMainThread())
    {
        QtInstanceButton* pButton;
        pQtInstance->RunInMainThread([&] { pButton = weld_widget_for_response(nResponse); });
        return pButton;
    }

    if (QPushButton* pButton = buttonForResponseCode(nResponse))
        return new QtInstanceButton(pButton);

    return nullptr;
}

int QtInstanceMessageDialog::run()
{
    SolarMutexGuard g;
    QtInstance* pQtInstance = GetQtInstance();
    if (!pQtInstance->IsMainThread())
    {
        int nRet = 0;
        pQtInstance->RunInMainThread([&] { nRet = run(); });
        return nRet;
    }

    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)
{
    SolarMutexGuard g;
    QtInstance* pQtInstance = GetQtInstance();
    if (!pQtInstance->IsMainThread())
    {
        bool bRet = false;
        pQtInstance->RunInMainThread([&] { bRet = runAsync(rxOwner, func); });
        return bRet;
    }

    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)
{
    SolarMutexGuard g;
    QtInstance* pQtInstance = GetQtInstance();
    if (!pQtInstance->IsMainThread())
    {
        bool bRet;
        pQtInstance->RunInMainThread([&] { bRet = runAsync(rxSelf, func); });
        return bRet;
    }

    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)
{
    SolarMutexGuard g;
    QtInstance* pQtInstance = GetQtInstance();
    if (!pQtInstance->IsMainThread())
    {
        pQtInstance->RunInMainThread([&] { response(nResponse); });
        return;
    }

    assert(m_pMessageDialog);
    m_pMessageDialog->done(nResponse);
}

void QtInstanceMessageDialog::dialogFinished(int nResult)
{
    SolarMutexGuard g;
    QtInstance* pQtInstance = GetQtInstance();
    if (!pQtInstance->IsMainThread())
    {
        pQtInstance->RunInMainThread([&] { dialogFinished(nResult); });
        return;
    }

    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();

    aFunc(nRet);

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

QPushButton* QtInstanceMessageDialog::buttonForResponseCode(int nResponse)
{
    SolarMutexGuard g;
    QtInstance* pQtInstance = GetQtInstance();
    if (!pQtInstance->IsMainThread())
    {
        QPushButton* pButton;
        pQtInstance->RunInMainThread([&] { pButton = buttonForResponseCode(nResponse); });
        return pButton;
    }

    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: */