summaryrefslogtreecommitdiffstats
path: root/src/progressmanager.cpp
blob: 6bc02988a13c6d94bb12a6364bfeab1166ffc45c (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/***************************************************************************
    copyright            : (C) 2005-2006 by Robby Stephenson
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#include "progressmanager.h"
#include "tellico_debug.h"

#include <tqtimer.h>

using Tellico::ProgressItem;
using Tellico::ProgressManager;
ProgressManager* ProgressManager::s_self = 0;

ProgressItem::Done::~Done() {
  ProgressManager::self()->setDone(m_object);
}

ProgressItem::ProgressItem(const TQString& label_, bool canCancel_)
    : m_label(label_)
    , m_canCancel(canCancel_)
    , m_progress(0)
    , m_total(0)
    , m_cancelled(false) {
}

ProgressItem::~ProgressItem() {
//  myDebug() << "~ProgressItem() - " << m_label << endl;
}

void ProgressItem::setProgress(uint steps_) {
  m_progress = steps_;
  emit signalProgress(this);

  if(m_progress >= m_total) {
    setDone();
  }
}

void ProgressItem::setTotalSteps(uint steps_) {
  m_total = steps_;
  emit signalTotalSteps(this);
}

void ProgressItem::setDone() {
  if(!m_cancelled) {
    m_progress = m_total;
  }
  emit signalDone(this);
  // make sure the deleting doesn't interfere with anything
  TQTimer::singleShot(3000, this, TQT_SLOT(deleteLater()));
}

void ProgressItem::cancel() {
//  myDebug() << "ProgressItem::cancel()" << endl;
  if(!m_canCancel || m_cancelled) {
    return;
  }

  m_cancelled = true;
  emit signalCancelled(this);
}

ProgressManager::ProgressManager() : TQObject() {
}

void ProgressManager::setProgress(const TQObject* owner_, uint steps_) {
  if(!m_items.contains(owner_)) {
    return;
  }

  m_items[owner_] ->setProgress(steps_);
//  slotUpdateTotalProgress(); // called in ProgressItem::setProgress()
//  emit signalItemProgress(m_items[owner_]);
}

void ProgressManager::setTotalSteps(const TQObject* owner_, uint steps_) {
  if(!m_items.contains(owner_)) {
    return;
  }

  m_items[owner_]->setTotalSteps(steps_);
//  updateTotalProgress(); // called in ProgressItem::setTotalSteps()
}

void ProgressManager::setDone(const TQObject* owner_) {
  if(!m_items.contains(owner_)) {
    return;
  }
  setDone(m_items[owner_]);
}

void ProgressManager::setDone(ProgressItem* item_) {
  if(!item_) {
    myDebug() << "ProgressManager::setDone() - null ProgressItem!" << endl;
    return;
  }
  item_->setDone();
//  updateTotalProgress();
}

void ProgressManager::slotItemDone(ProgressItem* item_) {
// cancel ends up removing it from the map, so make a copy
  ProgressMap map = m_items;
  for(ProgressMap::Iterator it = map.begin(); it != map.end(); ++it) {
    if(static_cast<ProgressItem*>(it.data()) == item_) {
      m_items.remove(it.key());
      break;
    }
  }
  slotUpdateTotalProgress();
//  emit signalItemDone(item_);
}

ProgressItem& ProgressManager::newProgressItemImpl(const TQObject* owner_,
                                                   const TQString& label_,
                                                   bool canCancel_) {
//  myDebug() << "ProgressManager::newProgressItem() - " << owner_->className() << ":" << label_ << endl;
  if(m_items.contains(owner_)) {
    return *m_items[owner_];
  }

  ProgressItem* item = new ProgressItem(label_, canCancel_);
  m_items.insert(owner_, item);

  connect(item, TQT_SIGNAL(signalTotalSteps(ProgressItem*)), TQT_SLOT(slotUpdateTotalProgress()));
  connect(item, TQT_SIGNAL(signalProgress(ProgressItem*)),   TQT_SLOT(slotUpdateTotalProgress()));
  connect(item, TQT_SIGNAL(signalDone(ProgressItem*)),       TQT_SLOT(slotUpdateTotalProgress()));
  connect(item, TQT_SIGNAL(signalDone(ProgressItem*)), TQT_SLOT(slotItemDone(ProgressItem*)));

//  connect(item, TQT_SIGNAL(signalProgress(ProgressItem*)), TQT_SIGNAL(signalItemProgress(ProgressItem*)));
//  emit signalItemAdded(item);
  return *item;
}

void ProgressManager::slotUpdateTotalProgress() {
  uint progress = 0;
  uint total = 0;

  for(ProgressMap::ConstIterator it = m_items.begin(); it != m_items.end(); ++it) {
    if(it.data()) {
      progress += (*it)->progress();
      total += (*it)->totalSteps();
    }
  }

  if(total == 0) {
    emit signalTotalProgress(100);
    return;
  }

  emit signalTotalProgress(100*progress/total);
}

void ProgressManager::slotCancelAll() {
// cancel ends up removing it from the map, so make a copy
  ProgressMap map = m_items;
  for(ProgressMap::ConstIterator it = map.begin(), end = map.end(); it != end; ++it) {
    if(it.data()) {
      it.data()->cancel();
      setDone(it.data());
    }
  }
}

bool ProgressManager::anyCanBeCancelled() const {
  for(ProgressMap::ConstIterator it = m_items.begin(), end = m_items.end(); it != end; ++it) {
    if(it.data() && it.data()->canCancel()) {
      return true;
    }
  }
  return false;
}

#include "progressmanager.moc"