vcl/inc/qt5/QtInstanceMessageDialog.hxx |    1 +
 vcl/qt5/QtInstanceMessageDialog.cxx     |   20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)

New commits:
commit e44a322fd00e017ea6494d836f66e5ddf6cfa9d5
Author:     Michael Weghorn <m.wegh...@posteo.de>
AuthorDate: Tue Jan 23 14:04:54 2024 +0100
Commit:     Michael Weghorn <m.wegh...@posteo.de>
CommitDate: Wed Jan 24 17:24:08 2024 +0100

    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  <omkarachareka...@gmail.com>
    Reviewed-by: Michael Weghorn <m.wegh...@posteo.de>

diff --git a/vcl/inc/qt5/QtInstanceMessageDialog.hxx 
b/vcl/inc/qt5/QtInstanceMessageDialog.hxx
index 0c585a9ed916..ae353268833c 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 6f252c79c816..ed48c5298a6c 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

Reply via email to