comparison widgets/ProgressDialog.cpp @ 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 33b7f5e54d60
children 05d12869043e
comparison
equal deleted inserted replaced
1266:a34a2a25907c 1267:b68f3162b5a8
17 17
18 #include <QProgressDialog> 18 #include <QProgressDialog>
19 #include <QApplication> 19 #include <QApplication>
20 #include <QTimer> 20 #include <QTimer>
21 21
22 ProgressDialog::ProgressDialog(QString message, bool cancellable, 22 ProgressDialog::ProgressDialog(
23 int timeBeforeShow, QWidget *parent) : 23 QString message,
24 bool cancellable,
25 int timeBeforeShow,
26 QWidget *parent,
27 Qt::WindowModality modality
28 ) :
24 m_showTimer(0), 29 m_showTimer(0),
25 m_timerElapsed(false), 30 m_timerElapsed(false),
26 m_cancelled(false) 31 m_cancelled(false)
27 { 32 {
28 m_dialog = new QProgressDialog(message, cancellable ? tr("Cancel") : 0, 33 m_dialog = new QProgressDialog(message, cancellable ? tr("Cancel") : 0,
29 0, 100, parent); 34 0, 100, parent);
35 m_dialog->setWindowModality(modality);
36
30 if (timeBeforeShow > 0) { 37 if (timeBeforeShow > 0) {
31 m_dialog->hide(); 38 m_dialog->hide();
32 m_showTimer = new QTimer; 39 m_showTimer = new QTimer;
33 connect(m_showTimer, SIGNAL(timeout()), this, SLOT(showTimerElapsed())); 40 connect(m_showTimer, SIGNAL(timeout()), this, SLOT(showTimerElapsed()));
34 m_showTimer->setSingleShot(true); 41 m_showTimer->setSingleShot(true);
95 102
96 void 103 void
97 ProgressDialog::setProgress(int percentage) 104 ProgressDialog::setProgress(int percentage)
98 { 105 {
99 if (percentage > m_dialog->value()) { 106 if (percentage > m_dialog->value()) {
100
101 m_dialog->setValue(percentage);
102
103 if (percentage >= 100 && isDefinite()) { 107 if (percentage >= 100 && isDefinite()) {
104 m_dialog->hide(); 108 m_dialog->hide();
105 } else if (m_timerElapsed && !m_dialog->isVisible()) { 109 } else if (m_timerElapsed && !m_dialog->isVisible()) {
106 emit showing(); 110 emit showing();
107 m_dialog->show(); 111 m_dialog->show();
108 m_dialog->raise(); 112 m_dialog->raise();
109 } 113 }
110 114 m_dialog->setValue(percentage); // processes event loop when modal
111 qApp->processEvents(); 115 if (!m_dialog->isModal()) qApp->processEvents();
112 } 116 }
113 } 117 }
114