changeset 1267:b68f3162b5a8

Accept optional modality in constructor and fix subsequent possible side effect (re-opening dialog when just cancelled)
author Lucas Thompson <dev@lucas.im>
date Tue, 17 Apr 2018 10:47:14 +0100
parents a34a2a25907c
children 05d12869043e
files widgets/ProgressDialog.cpp widgets/ProgressDialog.h
diffstat 2 files changed, 19 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/widgets/ProgressDialog.cpp	Thu Mar 01 18:02:22 2018 +0000
+++ b/widgets/ProgressDialog.cpp	Tue Apr 17 10:47:14 2018 +0100
@@ -19,14 +19,21 @@
 #include <QApplication>
 #include <QTimer>
 
-ProgressDialog::ProgressDialog(QString message, bool cancellable,
-                               int timeBeforeShow, QWidget *parent) : 
+ProgressDialog::ProgressDialog(
+    QString message,
+    bool cancellable,
+    int timeBeforeShow,
+    QWidget *parent,
+    Qt::WindowModality modality
+) : 
     m_showTimer(0),
     m_timerElapsed(false),
     m_cancelled(false)
 {
     m_dialog = new QProgressDialog(message, cancellable ? tr("Cancel") : 0,
                                    0, 100, parent);
+    m_dialog->setWindowModality(modality);
+
     if (timeBeforeShow > 0) {
         m_dialog->hide();
         m_showTimer = new QTimer;
@@ -97,9 +104,6 @@
 ProgressDialog::setProgress(int percentage)
 {
     if (percentage > m_dialog->value()) {
-
-        m_dialog->setValue(percentage);
-
         if (percentage >= 100 && isDefinite()) {
             m_dialog->hide();
         } else if (m_timerElapsed && !m_dialog->isVisible()) {
@@ -107,8 +111,7 @@
             m_dialog->show();
             m_dialog->raise();
         }
-
-        qApp->processEvents();
+        m_dialog->setValue(percentage); // processes event loop when modal
+        if (!m_dialog->isModal()) qApp->processEvents();
     }
-}
-
+}
\ No newline at end of file
--- a/widgets/ProgressDialog.h	Thu Mar 01 18:02:22 2018 +0000
+++ b/widgets/ProgressDialog.h	Tue Apr 17 10:47:14 2018 +0100
@@ -25,8 +25,13 @@
     Q_OBJECT
     
 public:
-    ProgressDialog(QString message, bool cancellable,
-                   int timeBeforeShow = 0, QWidget *parent = 0);
+    ProgressDialog(
+        QString message,
+        bool cancellable,
+        int timeBeforeShow = 0,
+        QWidget *parent = 0,
+        Qt::WindowModality modality = Qt::NonModal
+    );
     virtual ~ProgressDialog();
 
     virtual bool isDefinite() const;