MorphoGraphX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Progress.hpp
1 #ifndef PROGRESS_H
2 #define PROGRESS_H
3 
4 #include <Config.hpp>
5 
6 #include <QEvent>
7 #include <QList>
8 #include <QMutex>
9 #include <QObject>
10 #include <QPointer>
11 #include <QString>
12 #include <QtGui>
13 #include <string>
14 
15 class QPushButton;
16 class QTimer;
17 
18 namespace mgx {
19 // Progress dialog class
20 class mgx_EXPORT GlobalProgress : public QObject {
21  Q_OBJECT
22 
23  QWidget* parent;
24 
25  // On linux we can't recycle the same dialog, we have to re-create is all the time or it will not
26  // be located on the screen properly (Arrg!)
27  QProgressDialog* prg;
28  static GlobalProgress* _instance;
29  QList<int> value_stack;
30  QList<bool> cancel_stack;
31  QStringList stack;
32 
33  bool ismodal;
34  bool _canceled;
35 
36  QMutex access;
37 
38  QTimer* showTimer;
39 
40 public:
42  ~GlobalProgress();
43 
44  void setParent(QWidget* parent);
45  void start(const QString& msg, int steps, bool allow_cancel = true);
46  void start(const char* msg, int steps, bool allow_cancel = true)
47  {
48  start(QString::fromLocal8Bit(msg), steps, allow_cancel);
49  }
50  void start(std::string msg, int steps, bool allow_cancel = true)
51  {
52  start(QString::fromStdString(msg), steps, allow_cancel);
53  }
54  bool advance(int step);
55  bool canceled();
56  void stop();
57  void setMaximum(int steps);
58  void setMsg(const QString& msg);
59  void setMsg(const char* msg) {
60  setMsg(QString::fromLocal8Bit(msg));
61  }
62  void setMsg(std::string msg) {
63  setMsg(QString::fromStdString(msg));
64  }
65  static void clear(); // Remove any progress bars and clear the stack
66  int exec();
67 
68  static void useModal();
69  static void useModeless();
70 
71  static GlobalProgress& instance();
72  QProgressDialog* dialog() {
73  return prg;
74  }
75 
76 public slots:
77  void close();
78 
79 protected:
80  bool event(QEvent* e);
81  void createDialog(const QString& msg, int steps, int value, bool allow_cancel);
82  QPointer<QPushButton> cancel;
83 
84 private slots:
85  void wasCanceled();
86  void showDialog();
87 };
88 
89 class mgx_EXPORT Progress : public QObject {
90  Q_OBJECT
91 public:
92  Progress(const QString& msg, int steps, bool allow_cancel = true)
93  {
94  GlobalProgress::instance().start(msg, steps, allow_cancel);
95  }
96  Progress(std::string msg, int steps, bool allow_cancel = true)
97  {
98  GlobalProgress::instance().start(msg, steps, allow_cancel);
99  }
100  Progress(const char* msg, int steps, bool allow_cancel = true)
101  {
102  GlobalProgress::instance().start(msg, steps, allow_cancel);
103  }
104 
105  ~Progress() {
106  GlobalProgress::instance().stop();
107  }
108 
109  bool restart(const QString& msg, int steps)
110  {
111  GlobalProgress::instance().stop();
112  GlobalProgress::instance().start(msg, steps);
113  return true;
114  }
115  bool restart(std::string msg, int steps) {
116  return restart(QString::fromStdString(msg), steps);
117  }
118  bool restart(const char* msg, int steps) {
119  return restart(QString::fromLocal8Bit(msg), steps);
120  }
121 
122  void setMsg(const QString& msg) {
123  GlobalProgress::instance().setMsg(msg);
124  }
125  void setMsg(std::string msg) {
126  setMsg(QString::fromStdString(msg));
127  }
128  void setMsg(const char* msg) {
129  setMsg(QString::fromLocal8Bit(msg));
130  }
131 
132  bool advance(int step) {
133  return GlobalProgress::instance().advance(step);
134  }
135  bool canceled() {
136  return GlobalProgress::instance().canceled();
137  }
138 
139  void setMaximum(int step) {
140  GlobalProgress::instance().setMaximum(step);
141  }
142  int exec() {
143  return GlobalProgress::instance().exec();
144  }
145 };
146 } // namespace mgx
147 #endif
Definition: Progress.hpp:20
fromLocal8Bit(const char *str, int size=-1)
fromStdString(const std::string &str)
Definition: Progress.hpp:89