]> git.lyx.org Git - features.git/commitdiff
#12818 correct evaluation of message box result info
authorStephan Witt <switt@lyx.org>
Sun, 16 Jul 2023 14:48:49 +0000 (16:48 +0200)
committerStephan Witt <switt@lyx.org>
Sun, 16 Jul 2023 14:49:02 +0000 (16:49 +0200)
The help page of int QMessageBox::exec() (​https://doc.qt.io/qt-6/qmessagebox.html#exec) says:
When using a QMessageBox with standard buttons, this function returns a StandardButton value indicating the standard button that was clicked.
When using QMessageBox with custom buttons, this function returns an opaque value; use clickedButton() to determine which button was clicked.

src/frontends/alert.h
src/frontends/qt/GuiAlert.cpp

index 20da43656a52596678a131d3990c7ddcd0cac9cd..a4556ca2d4899a6f86b0e2f0e92e4733e8704570 100644 (file)
@@ -19,6 +19,8 @@ namespace lyx {
 namespace frontend {
 namespace Alert {
 
+typedef unsigned short buttonid;
+
 /**
  * Prompt for a question. Returns 0-3 for the chosen button.
  * Set default_button and cancel_button to reasonable values. b1-b3
@@ -30,8 +32,8 @@ namespace Alert {
  * "Yes" or "No", I will personally come around to your house and
  * slap you with fish, and not in an enjoyable way either.
  */
-int prompt(docstring const & title, docstring const & question,
-          int default_button, int cancel_button,
+buttonid prompt(docstring const & title, docstring const & question,
+          buttonid default_button, buttonid cancel_button,
           docstring const & b0, docstring const & b1,
           docstring const & b2 = empty_docstring(),
           docstring const & b3 = empty_docstring());
index 93b8e1d4e63171c2006ec6278cbe2578570f5764..85a5e22d6e6ae34fa93d9eb5545bf46cd6eaee58 100644 (file)
@@ -75,8 +75,8 @@ docstring toPlainText(docstring const & msg)
 }
 
 
-int doPrompt(docstring const & title, docstring const & question,
-                 int default_button, int cancel_button,
+buttonid doPrompt(docstring const & title, docstring const & question,
+                 buttonid default_button, buttonid cancel_button,
                  docstring const & b1, docstring const & b2,
                  docstring const & b3, docstring const & b4)
 {
@@ -108,7 +108,7 @@ int doPrompt(docstring const & title, docstring const & question,
 
        // FIXME replace that with guiApp->currentView()
        //LYXERR0("FOCUS: " << qApp->focusWidget());
-       QPushButton * b[4] = { 0, 0, 0, 0 };
+       QPushButton * b[4] = { nullptr, nullptr, nullptr, nullptr };
        QMessageBox msg_box(QMessageBox::Information,
                        toqstr(title), toqstr(question),
                        QMessageBox::NoButton, qApp->focusWidget());
@@ -120,23 +120,37 @@ int doPrompt(docstring const & title, docstring const & question,
                b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
        if (!b4.empty())
                b[3] = msg_box.addButton(toqstr(b4), QMessageBox::ActionRole);
-       msg_box.setDefaultButton(b[default_button]);
-       msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
-       int res = msg_box.exec();
+       if (default_button < size(b) && nullptr != b[default_button])
+               msg_box.setDefaultButton(b[default_button]);
+       if (cancel_button < size(b) && nullptr != b[cancel_button])
+               msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
+       msg_box.exec();
+       const QAbstractButton * button = msg_box.clickedButton();
 
        qApp->restoreOverrideCursor();
 
        if (long_op)
                theApp()->startLongOperation();
 
-       // Qt bug: can return -1 on cancel or WM close, despite the docs.
-       if (res == -1)
-               res = cancel_button;
+       size_t res = cancel_button;
+
+       if (button == nullptr)
+               return res;
+       else {
+               // Convert selection of the button into an integer
+               for (size_t i = 0; i < size(b); i++) {
+                       if (button == b[i]) {
+                               res = i;
+                               break;
+                       }
+               }
+       }
+
        return res;
 }
 
-int prompt(docstring const & title, docstring const & question,
-                 int default_button, int cancel_button,
+buttonid prompt(docstring const & title, docstring const & question,
+                 buttonid default_button, buttonid cancel_button,
                  docstring const & b0, docstring const & b1,
                  docstring const & b2, docstring const & b3)
 {