tdf#159323 qt weld: Remember VCL response code, don't try to map

Set the VCL return code of a button as a property
"response-code" for the `QPushButton` and use that instead of
trying to map the VCL response code for a
button to a `QMessageBox::ButtonRole` and then
mapping that back to the original VCL return code.

While the previous approach worked fine for return codes
from the `VclResponseType` enum, it turns out that
any arbitrary int can be used, and supporting that
needs a different approach.

One example (s. tdf#159323 comment 7) is `SbRtl_MsgBox`
(in basic/source/runtime/methods.cxx) which uses
values from the `BasicResponse` enum defined in there
that has a `BasicResponse::Yes = 6`, but the
`VclResponseType` enum has nothing for the value 6.

Just use `QMessageBox::ButtonRole::ActionRole` for
all buttons, since there's no clear way to map from
the VCL response code to the `QMessageBox::ButtonRole::ActionRole`
in general.

Change-Id: I1782512d4eb47b2dcf71214d16e64d56127e9e3f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/162560
Tested-by: Jenkins
Reviewed-by: Michael Weghorn <m.weghorn@posteo.de>
diff --git a/vcl/qt5/QtInstanceMessageDialog.cxx b/vcl/qt5/QtInstanceMessageDialog.cxx
index ed48c52..7e505af 100644
--- a/vcl/qt5/QtInstanceMessageDialog.cxx
+++ b/vcl/qt5/QtInstanceMessageDialog.cxx
@@ -11,68 +11,11 @@

#include <QtWidgets/QPushButton>

namespace
{
QMessageBox::ButtonRole lcl_vclResponseTypeToQtMessageBoxButtonRole(int nResponseType)
{
    // RET_CANCEL, RET_HELP, RET_YES, RET_NO and RET_OK have a matching equivalent
    // in Qt, the others are a bit more arbitrary; what really matters about these
    // is only that the mapping here and the other way around
    // (in lcl_qtMessageBoxButtonRoleToVclResponseType) is consistent
    switch (nResponseType)
    {
        case RET_CANCEL:
            return QMessageBox::ButtonRole::RejectRole;
        case RET_HELP:
            return QMessageBox::ButtonRole::HelpRole;
        case RET_YES:
            return QMessageBox::ButtonRole::YesRole;
        case RET_NO:
            return QMessageBox::ButtonRole::NoRole;
        case RET_OK:
            return QMessageBox::ButtonRole::AcceptRole;
        case RET_RETRY:
            return QMessageBox::ButtonRole::ResetRole;
        case RET_IGNORE:
            return QMessageBox::ButtonRole::ActionRole;
        case RET_CLOSE:
            return QMessageBox::ButtonRole::DestructiveRole;
        default:
            assert(false && "Unhandled vcl response type");
            return QMessageBox::InvalidRole;
    }
}

VclResponseType lcl_qtMessageBoxButtonRoleToVclResponseType(int nRet)
{
    // AcceptRole, HelpRole, NoRole, RejectRole and YesRole have a matching equivalent
    // in VCL, the others are a bit more arbitrary; what really matters about these
    // is only that the mapping here and the other way around
    // (in lcl_vclResponseTypeToQtMessageBoxButtonRole) is consistent
    switch (nRet)
    {
        case QMessageBox::ButtonRole::AcceptRole:
            return RET_OK;
        case QMessageBox::ButtonRole::HelpRole:
            return RET_HELP;
        case QMessageBox::ButtonRole::NoRole:
            return RET_NO;
        case QMessageBox::ButtonRole::RejectRole:
            return RET_CANCEL;
        case QMessageBox::ButtonRole::YesRole:
            return RET_YES;
        case QMessageBox::ButtonRole::ResetRole:
            return RET_RETRY;
        case QMessageBox::ButtonRole::ActionRole:
            return RET_IGNORE;
        case QMessageBox::ButtonRole::DestructiveRole:
            return RET_CLOSE;
        default:
            assert(false && "Unhandled QMessageBox::ButtonRole");
            return RET_CANCEL;
    }
}
}
/**
 * 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)
@@ -107,8 +50,9 @@ OUString QtInstanceMessageDialog::get_secondary_text() const
void QtInstanceMessageDialog::add_button(const OUString& rText, int nResponse, const OUString&)
{
    assert(m_pMessageDialog);
    m_pMessageDialog->addButton(vclToQtStringWithAccelerator(rText),
                                lcl_vclResponseTypeToQtMessageBoxButtonRole(nResponse));
    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)
@@ -118,11 +62,10 @@ void QtInstanceMessageDialog::set_default_response(int nResponse)
    const QList<QAbstractButton*> aButtons = m_pMessageDialog->buttons();
    for (QAbstractButton* pAbstractButton : aButtons)
    {
        QPushButton* pButton = dynamic_cast<QPushButton*>(pAbstractButton);
        assert(pButton);
        const QMessageBox::ButtonRole eRole = m_pMessageDialog->buttonRole(pButton);
        if (lcl_qtMessageBoxButtonRoleToVclResponseType(eRole) == nResponse)
        if (pAbstractButton->property(PROPERTY_VCL_RESPONSE_CODE).toInt() == nResponse)
        {
            QPushButton* pButton = dynamic_cast<QPushButton*>(pAbstractButton);
            assert(pButton);
            m_pMessageDialog->setDefaultButton(pButton);
            return;
        }
@@ -131,15 +74,11 @@ void QtInstanceMessageDialog::set_default_response(int nResponse)

int QtInstanceMessageDialog::run()
{
    // cannot use the QMessageBox::exec() return value right away, because it returns the
    // QMessageBox::StandardButton value or an opaque value, so use the clicked
    // button to retrieve its role instead and map that to the VCL response
    m_pMessageDialog->exec();
    QAbstractButton* pClickedButton = m_pMessageDialog->clickedButton();
    if (!pClickedButton)
        return RET_CLOSE;
    return lcl_qtMessageBoxButtonRoleToVclResponseType(
        m_pMessageDialog->buttonRole(pClickedButton));
    return pClickedButton->property(PROPERTY_VCL_RESPONSE_CODE).toInt();
}

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