tdf#154381 qt weld: Allow setting message box default button

Implement `QtInstanceMessageDialog::set_default_response`
by identifying the `QPushButton` in the `QMessageBox` that
has the `QMessageBox::ButtonRole` corresponding to the
given VCL return type and setting that as the default
button in the `QMessageBox`.

With this in place, the qt6 welded message dialog that
shows up when opening a file that's already open in another
instance of LibreOffice (s. `AlreadyOpenQueryBox::AlreadyOpenQueryBox`)
has initial focus on the "Open Read-Only" button when it opens,
just as is the case for the non-welded one
(whose use can be forced by setting env var
`SAL_VCL_QT_NO_WELDED_WIDGETS=1`).

Change-Id: I6d543b43cb6adec2dde9646e1452aa03bdcbf331
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/162441
Tested-by: Jenkins
Reviewed-by: Omkar Acharekar  <omkaracharekar12@gmail.com>
Reviewed-by: Michael Weghorn <m.weghorn@posteo.de>
diff --git a/vcl/inc/qt5/QtInstanceMessageDialog.hxx b/vcl/inc/qt5/QtInstanceMessageDialog.hxx
index 0c585a9e..ae35326 100644
--- a/vcl/inc/qt5/QtInstanceMessageDialog.hxx
+++ b/vcl/inc/qt5/QtInstanceMessageDialog.hxx
@@ -33,6 +33,7 @@ public:
    // weld::Dialog overrides
    virtual void add_button(const OUString& rText, int nResponse,
                            const OUString& rHelpId = {}) override;
    virtual void set_default_response(int nResponse) override;
    virtual int run() override;
};

diff --git a/vcl/qt5/QtInstanceMessageDialog.cxx b/vcl/qt5/QtInstanceMessageDialog.cxx
index 6f252c7..ed48c52 100644
--- a/vcl/qt5/QtInstanceMessageDialog.cxx
+++ b/vcl/qt5/QtInstanceMessageDialog.cxx
@@ -9,6 +9,8 @@

#include <QtInstanceMessageDialog.hxx>

#include <QtWidgets/QPushButton>

namespace
{
QMessageBox::ButtonRole lcl_vclResponseTypeToQtMessageBoxButtonRole(int nResponseType)
@@ -109,6 +111,24 @@ void QtInstanceMessageDialog::add_button(const OUString& rText, int nResponse, c
                                lcl_vclResponseTypeToQtMessageBoxButtonRole(nResponse));
}

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

    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)
        {
            m_pMessageDialog->setDefaultButton(pButton);
            return;
        }
    }
}

int QtInstanceMessageDialog::run()
{
    // cannot use the QMessageBox::exec() return value right away, because it returns the