svgui  1.9
ProgressDialog.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2007-2008 QMUL.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "ProgressDialog.h"
00017 
00018 #include <QProgressDialog>
00019 #include <QApplication>
00020 #include <QTimer>
00021 
00022 ProgressDialog::ProgressDialog(QString message, bool cancellable,
00023                                int timeBeforeShow, QWidget *parent) : 
00024     m_showTimer(0),
00025     m_timerElapsed(false),
00026     m_cancelled(false)
00027 {
00028     m_dialog = new QProgressDialog(message, cancellable ? tr("Cancel") : 0,
00029                                    0, 100, parent);
00030     if (timeBeforeShow > 0) {
00031         m_dialog->hide();
00032         m_showTimer = new QTimer;
00033         connect(m_showTimer, SIGNAL(timeout()), this, SLOT(showTimerElapsed()));
00034         m_showTimer->setSingleShot(true);
00035         m_showTimer->start(timeBeforeShow);
00036     } else {
00037         m_dialog->show();
00038         m_dialog->raise();
00039         m_timerElapsed = true;
00040     }
00041 
00042     if (cancellable) {
00043         connect(m_dialog, SIGNAL(canceled()), this, SLOT(canceled()));
00044     }
00045 }
00046 
00047 ProgressDialog::~ProgressDialog()
00048 {
00049     delete m_showTimer;
00050     delete m_dialog;
00051 }
00052 
00053 bool
00054 ProgressDialog::isDefinite() const
00055 {
00056     return (m_dialog->maximum() > 0);
00057 }
00058 
00059 void
00060 ProgressDialog::setDefinite(bool definite)
00061 {
00062     if (definite) m_dialog->setMaximum(100);
00063     else m_dialog->setMaximum(0);
00064 }
00065 
00066 void
00067 ProgressDialog::setMessage(QString text)
00068 {
00069     m_dialog->setLabelText(text);
00070 }
00071 
00072 void
00073 ProgressDialog::canceled()
00074 {
00075     m_cancelled = true;
00076     emit cancelled();
00077 }
00078 
00079 bool
00080 ProgressDialog::wasCancelled() const
00081 {
00082     return m_cancelled;
00083 }
00084 
00085 void
00086 ProgressDialog::showTimerElapsed()
00087 {
00088     m_timerElapsed = true;
00089     if (m_dialog->value() > 0) {
00090         emit showing();
00091         m_dialog->show();
00092     }
00093     qApp->processEvents();
00094 }
00095 
00096 void
00097 ProgressDialog::setProgress(int percentage)
00098 {
00099     if (percentage > m_dialog->value()) {
00100 
00101         m_dialog->setValue(percentage);
00102 
00103         if (percentage >= 100 && isDefinite()) {
00104             m_dialog->hide();
00105         } else if (m_timerElapsed && !m_dialog->isVisible()) {
00106             emit showing();
00107             m_dialog->show();
00108             m_dialog->raise();
00109         }
00110 
00111         qApp->processEvents();
00112     }
00113 }
00114