summaryrefslogtreecommitdiffstats
path: root/src/progressmanager.h
blob: 5eaac3b61c3108c498e33500297fff3bd45da821 (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
/***************************************************************************
    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;                            *
 *                                                                         *
 ***************************************************************************/

// much of this code is adapted from libkdepim
// which is GPL licensed, Copyright (c) 2004 Till Adam

#ifndef TELLICO_PROGRESSMANAGER_H
#define TELLICO_PROGRESSMANAGER_H

#include <qobject.h>
#include <qmap.h>
#include <qguardedptr.h>

namespace Tellico {

class ProgressManager;

/**
 * @author Robby Stephenson
 */
class ProgressItem : public QObject {
Q_OBJECT

friend class ProgressManager;

public:
  class Done {
  public:
    Done(const QObject* obj) : m_object(obj) {}
    ~Done();
  private:
    const QObject* m_object;
  };

  bool canCancel() const { return m_canCancel; }
  const QString& label() const { return m_label; }
  void setLabel(const QString& label);

//  uint progress() const { return m_total ? (100*m_completed/m_total) : 0; }
  uint progress() const { return m_progress; }
  void setProgress(uint steps);
  uint totalSteps() const { return m_total; }
  void setTotalSteps(uint steps);
  void setDone();

  void cancel();

signals:
  void signalProgress(ProgressItem* item);
  void signalDone(ProgressItem* item);
  void signalCancelled(ProgressItem* item);
  void signalTotalSteps(ProgressItem* item);

protected:
  /* Only to be used by the ProgressManager */
  ProgressItem(const QString& label, bool canCancel);
  virtual ~ProgressItem();

private:
  QString m_label;
  bool m_canCancel;
  uint m_progress;
  uint m_total;
  bool m_cancelled;
};

/**
 * @author Robby Stephenson
 */
class ProgressManager : public QObject {
Q_OBJECT

public:
  virtual ~ProgressManager() {}

  static ProgressManager* self() { if(!s_self) s_self = new ProgressManager(); return s_self; }

  ProgressItem& newProgressItem(const QObject* owner, const QString& label, bool canCancel = false) {
    return newProgressItemImpl(owner, label, canCancel);
  }

  void setProgress(const QObject* owner, uint steps);
  void setTotalSteps(const QObject* owner, uint steps);
  void setDone(const QObject* owner);

  bool anyCanBeCancelled() const;

signals:
//  void signalItemAdded(ProgressItem* item);
//  void signalItemProgress(ProgressItem* item);
//  void signalItemDone(ProgressItem* item);
//  void signalItemCancelled(ProgressItem* item);
  void signalTotalProgress(uint progress);

public slots:
  void slotCancelAll();

private slots:
  void slotItemDone(ProgressItem* item);
  void slotUpdateTotalProgress();

private:
  ProgressManager();
  ProgressManager(const ProgressManager&); // no copies

  ProgressItem& newProgressItemImpl(const QObject* owner, const QString& label, bool canCancel);
  void setDone(ProgressItem* item);

  typedef QMap<QGuardedPtr<const QObject>, QGuardedPtr<ProgressItem> > ProgressMap;
  ProgressMap m_items;

  static ProgressManager* s_self;
};

} // end namespace

#endif