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