annotate widgets/ProgressDialog.cpp @ 1531:084c591622c5

Slightly streamline image scaling
author Chris Cannam
date Wed, 09 Oct 2019 11:17:26 +0100
parents c8a6fd3f9dff
children
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@1268 22 ProgressDialog::ProgressDialog(QString message,
dev@1268 23 bool cancellable,
dev@1268 24 int timeBeforeShow,
dev@1268 25 QWidget *parent,
dev@1268 26 Qt::WindowModality modality) :
Chris@1408 27 m_showTimer(nullptr),
Chris@410 28 m_timerElapsed(false),
Chris@410 29 m_cancelled(false)
Chris@378 30 {
Chris@1408 31 m_dialog = new QProgressDialog(message, cancellable ? tr("Cancel") : nullptr,
Chris@378 32 0, 100, parent);
dev@1267 33 m_dialog->setWindowModality(modality);
dev@1267 34
Chris@378 35 if (timeBeforeShow > 0) {
Chris@378 36 m_dialog->hide();
Chris@378 37 m_showTimer = new QTimer;
Chris@378 38 connect(m_showTimer, SIGNAL(timeout()), this, SLOT(showTimerElapsed()));
Chris@378 39 m_showTimer->setSingleShot(true);
Chris@378 40 m_showTimer->start(timeBeforeShow);
Chris@378 41 } else {
Chris@378 42 m_dialog->show();
Chris@378 43 m_dialog->raise();
Chris@378 44 m_timerElapsed = true;
Chris@378 45 }
Chris@378 46
Chris@378 47 if (cancellable) {
Chris@410 48 connect(m_dialog, SIGNAL(canceled()), this, SLOT(canceled()));
Chris@378 49 }
Chris@378 50 }
Chris@378 51
Chris@378 52 ProgressDialog::~ProgressDialog()
Chris@378 53 {
Chris@378 54 delete m_showTimer;
Chris@378 55 delete m_dialog;
Chris@378 56 }
Chris@378 57
Chris@410 58 bool
Chris@410 59 ProgressDialog::isDefinite() const
Chris@410 60 {
Chris@410 61 return (m_dialog->maximum() > 0);
Chris@410 62 }
Chris@410 63
Chris@410 64 void
Chris@410 65 ProgressDialog::setDefinite(bool definite)
Chris@410 66 {
Chris@410 67 if (definite) m_dialog->setMaximum(100);
Chris@410 68 else m_dialog->setMaximum(0);
Chris@410 69 }
Chris@410 70
Chris@378 71 void
Chris@378 72 ProgressDialog::setMessage(QString text)
Chris@378 73 {
Chris@378 74 m_dialog->setLabelText(text);
Chris@378 75 }
Chris@378 76
Chris@378 77 void
Chris@410 78 ProgressDialog::canceled()
Chris@410 79 {
Chris@410 80 m_cancelled = true;
Chris@410 81 emit cancelled();
Chris@410 82 }
Chris@410 83
Chris@410 84 bool
Chris@410 85 ProgressDialog::wasCancelled() const
Chris@410 86 {
Chris@410 87 return m_cancelled;
Chris@410 88 }
Chris@410 89
Chris@410 90 void
Chris@378 91 ProgressDialog::showTimerElapsed()
Chris@378 92 {
Chris@378 93 m_timerElapsed = true;
Chris@378 94 if (m_dialog->value() > 0) {
Chris@410 95 emit showing();
Chris@378 96 m_dialog->show();
Chris@378 97 }
Chris@378 98 qApp->processEvents();
Chris@378 99 }
Chris@378 100
Chris@378 101 void
Chris@378 102 ProgressDialog::setProgress(int percentage)
Chris@378 103 {
Chris@378 104 if (percentage > m_dialog->value()) {
Chris@410 105 if (percentage >= 100 && isDefinite()) {
Chris@378 106 m_dialog->hide();
Chris@378 107 } else if (m_timerElapsed && !m_dialog->isVisible()) {
Chris@410 108 emit showing();
Chris@378 109 m_dialog->show();
Chris@378 110 m_dialog->raise();
Chris@378 111 }
dev@1267 112 m_dialog->setValue(percentage); // processes event loop when modal
dev@1267 113 if (!m_dialog->isModal()) qApp->processEvents();
Chris@378 114 }
dev@1267 115 }