summaryrefslogtreecommitdiffstats
path: root/src/common/global/progress_monitor.cpp
blob: 2902da969f6ba3894ac49c1c529c40d7b8be63da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/***************************************************************************
 *   Copyright (C) 2007 Nicolas Hadacek <hadacek@kde.org>                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "progress_monitor.h"

ProgressMonitor::ProgressMonitor(TQObject* parent)
  : TQObject(parent, "progress_monitor")
{
  _current = _tasks.end();
}

void ProgressMonitor::clear()
{
  _tasks.clear();
  _current = _tasks.end();
  emit showProgress(false);
}

void ProgressMonitor::appendTask(const TQString &label, uint nbSteps)
{
  Task task;
  task.label = label;
  task.nbSteps = nbSteps;
  task.nbDoneSteps = 0;
  _tasks.append(task);
}

void ProgressMonitor::insertTask(const TQString &label, uint nbSteps)
{
  Task task;
  task.label = label;
  task.nbSteps = nbSteps;
  task.nbDoneSteps = 0;
  if ( _current==_tasks.end() ) _current = _tasks.prepend(task);
  else _current = _tasks.insert(_current, task);
  update();
}

uint ProgressMonitor::nbSteps() const
{
  uint nb = 0;
  for (uint i=0; i<uint(_tasks.count()); i++) nb += _tasks[i].nbSteps;
  return nb;
}

uint ProgressMonitor::nbDoneSteps() const
{
  uint nb = 0;
  for (uint i=0; i<uint(_tasks.count()); i++) nb += _tasks[i].nbDoneSteps;
  return nb;
}

void ProgressMonitor::update()
{
  TQString label = (_current==_tasks.end() ? TQString() : (*_current).label);
  emit setLabel(label);
  emit setTotalProgress(nbSteps());
  emit setProgress(nbDoneSteps());
  emit showProgress(true);
}

void ProgressMonitor::startNextTask()
{
  if ( _current==_tasks.end() ) _current = _tasks.begin();
  else ++_current;
  Q_ASSERT( _current!=_tasks.end() ) ;
  update();
}

void ProgressMonitor::addTaskProgress(uint nbSteps)
{
  Q_ASSERT( _current!=_tasks.end() ) ;
  if ( _current==_tasks.end() ) return;
  uint nb = (*_current).nbDoneSteps + nbSteps;
  Q_ASSERT( nb<=(*_current).nbSteps );
  if ( nb>(*_current).nbSteps ) tqDebug("%s %i+%i > %i", (*_current).label.latin1(), (*_current).nbDoneSteps, nbSteps, (*_current).nbSteps);
  (*_current).nbDoneSteps = TQMIN(nb, (*_current).nbSteps);
  update();
}

#include "progress_monitor.moc"