MessageDialog: handle clicked(button, role) signal properly
authorShawn Rutledge <shawn.rutledge@digia.com>
Thu, 7 Nov 2013 17:31:47 +0000 (18:31 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Fri, 8 Nov 2013 08:10:07 +0000 (09:10 +0100)
Depends on I7be753080794adabb784df9b95ac04aa1c29151c in qtbase.
Now the Android native dialog can provide the same functionality as
the QML and QMessageBox implementations.

Change-Id: Icc9c610669742199d48497096524f6cf8ed4d835
Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
Reviewed-by: BogDan Vatra <bogdan@kde.org>
src/imports/dialogs/qquickabstractmessagedialog.cpp
src/imports/dialogs/qquickabstractmessagedialog_p.h
src/imports/dialogs/qquickplatformmessagedialog.cpp
src/imports/widgets/qmessageboxhelper_p.h [new file with mode: 0644]
src/imports/widgets/qquickqmessagebox.cpp
src/imports/widgets/qquickqmessagebox_p.h
src/imports/widgets/widgets.pro

index cfcf056..a444649 100644 (file)
@@ -40,6 +40,7 @@
 ****************************************************************************/
 
 #include "qquickabstractmessagedialog_p.h"
+#include <QtGui/qpa/qplatformdialoghelper.h>
 
 QT_BEGIN_NAMESPACE
 
@@ -133,10 +134,46 @@ void QQuickAbstractMessageDialog::setStandardButtons(StandardButtons buttons)
     }
 }
 
-void QQuickAbstractMessageDialog::click(QQuickAbstractMessageDialog::StandardButton button)
+void QQuickAbstractMessageDialog::click(QMessageDialogOptions::StandardButton button, QMessageDialogOptions::ButtonRole role)
 {
-    m_clickedButton = button;
+    setVisible(false);
+    m_clickedButton = static_cast<StandardButton>(button);
     emit buttonClicked();
+    switch (role) {
+    case QMessageDialogOptions::AcceptRole:
+        emit accept();
+        break;
+    case QMessageDialogOptions::RejectRole:
+        emit reject();
+        break;
+    case QMessageDialogOptions::DestructiveRole:
+        emit discard();
+        break;
+    case QMessageDialogOptions::HelpRole:
+        emit help();
+        break;
+    case QMessageDialogOptions::YesRole:
+        emit yes();
+        break;
+    case QMessageDialogOptions::NoRole:
+        emit no();
+        break;
+    case QMessageDialogOptions::ApplyRole:
+        emit apply();
+        break;
+    case QMessageDialogOptions::ResetRole:
+        emit reset();
+        break;
+    default:
+        qWarning("unhandled MessageDialog button %d with role %d", button, role);
+    }
+}
+
+void QQuickAbstractMessageDialog::click(QQuickAbstractMessageDialog::StandardButton button)
+{
+    click(static_cast<QMessageDialogOptions::StandardButton>(button),
+        static_cast<QMessageDialogOptions::ButtonRole>(
+            QMessageDialogOptions::buttonRole(static_cast<QMessageDialogOptions::StandardButton>(button))));
 }
 
 QT_END_NAMESPACE
index 3f1b842..f2427bb 100644 (file)
@@ -133,7 +133,8 @@ public Q_SLOTS:
     void setDetailedText(const QString &arg);
     void setIcon(Icon icon);
     void setStandardButtons(StandardButtons buttons);
-    void click(StandardButton button);
+    void click(QMessageDialogOptions::StandardButton button, QMessageDialogOptions::ButtonRole);
+    void click(QQuickAbstractMessageDialog::StandardButton button);
 
 Q_SIGNALS:
     void textChanged();
index 6ee8f10..00c750a 100644 (file)
@@ -153,8 +153,12 @@ QPlatformMessageDialogHelper *QQuickPlatformMessageDialog::helper()
            ->createPlatformDialogHelper(QPlatformTheme::MessageDialog));
         if (!m_dlgHelper)
             return m_dlgHelper;
+        // accept() shouldn't be emitted.  reject() happens only if the dialog is
+        // dismissed by closing the window rather than by one of its button widgets.
         connect(m_dlgHelper, SIGNAL(accept()), this, SLOT(accept()));
         connect(m_dlgHelper, SIGNAL(reject()), this, SLOT(reject()));
+        connect(m_dlgHelper, SIGNAL(clicked(QMessageDialogOptions::StandardButton, QMessageDialogOptions::ButtonRole)),
+            this, SLOT(click(QMessageDialogOptions::StandardButton, QMessageDialogOptions::ButtonRole)));
     }
 
     return m_dlgHelper;
diff --git a/src/imports/widgets/qmessageboxhelper_p.h b/src/imports/widgets/qmessageboxhelper_p.h
new file mode 100644 (file)
index 0000000..4f1070f
--- /dev/null
@@ -0,0 +1,107 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtQml module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QMESSAGEBOXHELPER_P_H
+#define QMESSAGEBOXHELPER_P_H
+
+//
+//  W A R N I N G
+//  -------------
+//
+// This file is not part of the Qt API.  It exists purely as an
+// implementation detail.  This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <QMessageBox>
+#include "../dialogs/qquickabstractmessagedialog_p.h"
+
+QT_BEGIN_NAMESPACE
+
+class QMessageBoxHelper : public QPlatformMessageDialogHelper
+{
+    Q_OBJECT
+public:
+    QMessageBoxHelper() {
+        connect(&m_dialog, SIGNAL(accepted()), this, SIGNAL(accept()));
+        connect(&m_dialog, SIGNAL(rejected()), this, SIGNAL(reject()));
+        connect(&m_dialog, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(buttonClicked(QAbstractButton*)));
+    }
+
+    virtual void exec() { m_dialog.exec(); }
+
+    virtual bool show(Qt::WindowFlags f, Qt::WindowModality m, QWindow *parent) {
+        m_dialog.winId();
+        QWindow *window = m_dialog.windowHandle();
+        Q_ASSERT(window);
+        window->setTransientParent(parent);
+        window->setFlags(f);
+        m_dialog.setWindowModality(m);
+        m_dialog.setWindowTitle(QPlatformMessageDialogHelper::options()->windowTitle());
+        m_dialog.setIcon(static_cast<QMessageBox::Icon>(QPlatformMessageDialogHelper::options()->icon()));
+        if (!QPlatformMessageDialogHelper::options()->text().isNull())
+            m_dialog.setText(QPlatformMessageDialogHelper::options()->text());
+        if (!QPlatformMessageDialogHelper::options()->informativeText().isNull())
+            m_dialog.setInformativeText(QPlatformMessageDialogHelper::options()->informativeText());
+        if (!QPlatformMessageDialogHelper::options()->detailedText().isNull())
+            m_dialog.setDetailedText(QPlatformMessageDialogHelper::options()->detailedText());
+        m_dialog.setStandardButtons(static_cast<QMessageBox::StandardButtons>(static_cast<int>(
+            QPlatformMessageDialogHelper::options()->standardButtons())));
+        m_dialog.show();
+        return m_dialog.isVisible();
+    }
+
+    virtual void hide() { m_dialog.hide(); }
+
+    QMessageBox m_dialog;
+
+public Q_SLOTS:
+    void buttonClicked(QAbstractButton* button) {
+        emit clicked(static_cast<QMessageDialogOptions::StandardButton>(m_dialog.standardButton(button)),
+            static_cast<QMessageDialogOptions::ButtonRole>(m_dialog.buttonRole(button)));
+    }
+};
+
+QT_END_NAMESPACE
+
+#endif // QMESSAGEBOXHELPER_P_H
index 2f7c748..1b92efc 100644 (file)
@@ -40,6 +40,7 @@
 ****************************************************************************/
 
 #include "qquickqmessagebox_p.h"
+#include "qmessageboxhelper_p.h"
 #include "qquickitem.h"
 
 #include <private/qguiapplication_p.h>
 
 QT_BEGIN_NAMESPACE
 
-class QMessageBoxHelper : public QPlatformMessageDialogHelper
-{
-public:
-    QMessageBoxHelper() {
-        connect(&m_dialog, SIGNAL(accepted()), this, SIGNAL(accept()));
-        connect(&m_dialog, SIGNAL(rejected()), this, SIGNAL(reject()));
-    }
-
-    virtual void exec() { m_dialog.exec(); }
-
-    virtual bool show(Qt::WindowFlags f, Qt::WindowModality m, QWindow *parent) {
-        m_dialog.winId();
-        QWindow *window = m_dialog.windowHandle();
-        Q_ASSERT(window);
-        window->setTransientParent(parent);
-        window->setFlags(f);
-        m_dialog.setWindowModality(m);
-        m_dialog.setWindowTitle(QPlatformMessageDialogHelper::options()->windowTitle());
-        m_dialog.setIcon(static_cast<QMessageBox::Icon>(QPlatformMessageDialogHelper::options()->icon()));
-        if (!QPlatformMessageDialogHelper::options()->text().isNull())
-            m_dialog.setText(QPlatformMessageDialogHelper::options()->text());
-        if (!QPlatformMessageDialogHelper::options()->informativeText().isNull())
-            m_dialog.setInformativeText(QPlatformMessageDialogHelper::options()->informativeText());
-        if (!QPlatformMessageDialogHelper::options()->detailedText().isNull())
-            m_dialog.setDetailedText(QPlatformMessageDialogHelper::options()->detailedText());
-        m_dialog.setStandardButtons(static_cast<QMessageBox::StandardButtons>(static_cast<int>(
-            QPlatformMessageDialogHelper::options()->standardButtons())));
-        m_dialog.show();
-        return m_dialog.isVisible();
-    }
-
-    virtual void hide() { m_dialog.hide(); }
-
-    QMessageBox m_dialog;
-};
-
 /*!
     \qmltype QtMessageDialog
     \instantiates QQuickQMessageBox
@@ -156,44 +121,6 @@ QQuickQMessageBox::~QQuickQMessageBox()
     delete m_dlgHelper;
 }
 
-void QQuickQMessageBox::finished(int button) {
-    click(static_cast<StandardButton>(button));
-}
-
-void QQuickQMessageBox::clicked(QAbstractButton* button) {
-    QMessageBox &mb = static_cast<QMessageBoxHelper*>(QQuickAbstractMessageDialog::m_dlgHelper)->m_dialog;
-    switch (mb.buttonRole(button)) {
-    case QMessageBox::AcceptRole:
-        emit accepted();
-        break;
-    case QMessageBox::RejectRole:
-        emit rejected();
-        break;
-    case QMessageBox::DestructiveRole:
-        emit discard();
-        break;
-    case QMessageBox::HelpRole:
-        emit help();
-        break;
-    case QMessageBox::YesRole:
-        emit yes();
-        break;
-    case QMessageBox::NoRole:
-        emit no();
-        break;
-    case QMessageBox::ApplyRole:
-        emit apply();
-        break;
-    case QMessageBox::ResetRole:
-        emit reset();
-        break;
-    default:
-        qWarning("unhandled QMessageBox button role %d", mb.buttonRole(button));
-    }
-    if (!mb.isVisible())
-        setVisible(false);
-}
-
 QPlatformDialogHelper *QQuickQMessageBox::helper()
 {
     QQuickItem *parentItem = qobject_cast<QQuickItem *>(parent());
@@ -203,10 +130,12 @@ QPlatformDialogHelper *QQuickQMessageBox::helper()
     if (!QQuickAbstractMessageDialog::m_dlgHelper) {
         QMessageBoxHelper* helper = new QMessageBoxHelper();
         QQuickAbstractMessageDialog::m_dlgHelper = helper;
+        // accept() shouldn't be emitted.  reject() happens only if the dialog is
+        // dismissed by closing the window rather than by one of its button widgets.
         connect(helper, SIGNAL(accept()), this, SLOT(accept()));
         connect(helper, SIGNAL(reject()), this, SLOT(reject()));
-        connect(&helper->m_dialog, SIGNAL(finished(int)), this, SLOT(finished(int)));
-        connect(&helper->m_dialog, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(clicked(QAbstractButton*)));
+        connect(helper, SIGNAL(clicked(QMessageDialogOptions::StandardButton, QMessageDialogOptions::ButtonRole)),
+            this, SLOT(click(QMessageDialogOptions::StandardButton, QMessageDialogOptions::ButtonRole)));
     }
 
     return QQuickAbstractMessageDialog::m_dlgHelper;
index 9c3c1f2..be91f1d 100644 (file)
 // We mean it.
 //
 
-#include <QMessageBox>
 #include "../dialogs/qquickabstractmessagedialog_p.h"
 
 QT_BEGIN_NAMESPACE
 
-class QAbstractButton;
-
 class QQuickQMessageBox : public QQuickAbstractMessageDialog
 {
-    Q_OBJECT
-
 public:
     QQuickQMessageBox(QObject *parent = 0);
     virtual ~QQuickQMessageBox();
 
-protected slots:
-    void clicked(QAbstractButton* button);
-    void finished(int button);
-
 protected:
     virtual QPlatformDialogHelper *helper();
 
index 8c07594..5320838 100644 (file)
@@ -17,6 +17,7 @@ SOURCES += \
 
 HEADERS += \
     qquickqmessagebox_p.h \
+    qmessageboxhelper_p.h \
     ../dialogs/qquickabstractmessagedialog_p.h \
     qquickqfiledialog_p.h \
     ../dialogs/qquickabstractfiledialog_p.h \