annotate 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
rev   line source
Chris@378 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@378 2
Chris@378 3 /*
Chris@378 4 Sonic Visualiser
Chris@378 5 An audio file viewer and annotation editor.
Chris@378 6 Centre for Digital Music, Queen Mary, University of London.
Chris@378 7 This file copyright 2007-2008 QMUL.
Chris@378 8
Chris@378 9 This program is free software; you can redistribute it and/or
Chris@378 10 modify it under the terms of the GNU General Public License as
Chris@378 11 published by the Free Software Foundation; either version 2 of the
Chris@378 12 License, or (at your option) any later version. See the file
Chris@378 13 COPYING included with this distribution for more information.
Chris@378 14 */
Chris@378 15
Chris@378 16 #include "ProgressDialog.h"
Chris@378 17
Chris@378 18 #include <QProgressDialog>
Chris@378 19 #include <QApplication>
Chris@378 20 #include <QTimer>
Chris@378 21
dev@1267 22 ProgressDialog::ProgressDialog(
dev@1267 23 QString message,
dev@1267 24 bool cancellable,
dev@1267 25 int timeBeforeShow,
dev@1267 26 QWidget *parent,
dev@1267 27 Qt::WindowModality modality
dev@1267 28 ) :
Chris@378 29 m_showTimer(0),
Chris@410 30 m_timerElapsed(false),
Chris@410 31 m_cancelled(false)
Chris@378 32 {
Chris@378 33 m_dialog = new QProgressDialog(message, cancellable ? tr("Cancel") : 0,
Chris@378 34 0, 100, parent);
dev@1267 35 m_dialog->setWindowModality(modality);
dev@1267 36
Chris@378 37 if (timeBeforeShow > 0) {
Chris@378 38 m_dialog->hide();
Chris@378 39 m_showTimer = new QTimer;
Chris@378 40 connect(m_showTimer, SIGNAL(timeout()), this, SLOT(showTimerElapsed()));
Chris@378 41 m_showTimer->setSingleShot(true);
Chris@378 42 m_showTimer->start(timeBeforeShow);
Chris@378 43 } else {
Chris@378 44 m_dialog->show();
Chris@378 45 m_dialog->raise();
Chris@378 46 m_timerElapsed = true;
Chris@378 47 }
Chris@378 48
Chris@378 49 if (cancellable) {
Chris@410 50 connect(m_dialog, SIGNAL(canceled()), this, SLOT(canceled()));
Chris@378 51 }
Chris@378 52 }
Chris@378 53
Chris@378 54 ProgressDialog::~ProgressDialog()
Chris@378 55 {
Chris@378 56 delete m_showTimer;
Chris@378 57 delete m_dialog;
Chris@378 58 }
Chris@378 59
Chris@410 60 bool
Chris@410 61 ProgressDialog::isDefinite() const
Chris@410 62 {
Chris@410 63 return (m_dialog->maximum() > 0);
Chris@410 64 }
Chris@410 65
Chris@410 66 void
Chris@410 67 ProgressDialog::setDefinite(bool definite)
Chris@410 68 {
Chris@410 69 if (definite) m_dialog->setMaximum(100);
Chris@410 70 else m_dialog->setMaximum(0);
Chris@410 71 }
Chris@410 72
Chris@378 73 void
Chris@378 74 ProgressDialog::setMessage(QString text)
Chris@378 75 {
Chris@378 76 m_dialog->setLabelText(text);
Chris@378 77 }
Chris@378 78
Chris@378 79 void
Chris@410 80 ProgressDialog::canceled()
Chris@410 81 {
Chris@410 82 m_cancelled = true;
Chris@410 83 emit cancelled();
Chris@410 84 }
Chris@410 85
Chris@410 86 bool
Chris@410 87 ProgressDialog::wasCancelled() const
Chris@410 88 {
Chris@410 89 return m_cancelled;
Chris@410 90 }
Chris@410 91
Chris@410 92 void
Chris@378 93 ProgressDialog::showTimerElapsed()
Chris@378 94 {
Chris@378 95 m_timerElapsed = true;
Chris@378 96 if (m_dialog->value() > 0) {
Chris@410 97 emit showing();
Chris@378 98 m_dialog->show();
Chris@378 99 }
Chris@378 100 qApp->processEvents();
Chris@378 101 }
Chris@378 102
Chris@378 103 void
Chris@378 104 ProgressDialog::setProgress(int percentage)
Chris@378 105 {
Chris@378 106 if (percentage > m_dialog->value()) {
Chris@410 107 if (percentage >= 100 && isDefinite()) {
Chris@378 108 m_dialog->hide();
Chris@378 109 } else if (m_timerElapsed && !m_dialog->isVisible()) {
Chris@410 110 emit showing();
Chris@378 111 m_dialog->show();
Chris@378 112 m_dialog->raise();
Chris@378 113 }
dev@1267 114 m_dialog->setValue(percentage); // processes event loop when modal
dev@1267 115 if (!m_dialog->isModal()) qApp->processEvents();
Chris@378 116 }
dev@1267 117 }