MorphoGraphX
Progress.hpp
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef PROGRESS_H
12 #define PROGRESS_H
13 
14 #include <Config.hpp>
15 
16 #include <QEvent>
17 #include <QList>
18 #include <QMutex>
19 #include <QObject>
20 #include <QPointer>
21 #include <QString>
22 #include <QtGui>
23 #include <string>
24 
25 class QPushButton;
26 class QTimer;
27 
28 namespace mgx {
29 // Progress dialog class
30 class mgx_EXPORT GlobalProgress : public QObject {
31  Q_OBJECT
32 
33  QWidget* parent;
34 
35  // On linux we can't recycle the same dialog, we have to re-create is all the time or it will not
36  // be located on the screen properly (Arrg!)
37  QProgressDialog* prg;
38  static GlobalProgress* _instance;
39  QList<int> value_stack;
40  QList<bool> cancel_stack;
41  QStringList stack;
42 
43  bool ismodal;
44  bool _canceled;
45 
46  QMutex access;
47 
48  QTimer* showTimer;
49 
50 public:
52  ~GlobalProgress();
53 
54  void setParent(QWidget* parent);
55  void start(const QString& msg, int steps, bool allow_cancel = true);
56  void start(const char* msg, int steps, bool allow_cancel = true)
57  {
58  start(QString::fromLocal8Bit(msg), steps, allow_cancel);
59  }
60  void start(std::string msg, int steps, bool allow_cancel = true)
61  {
62  start(QString::fromStdString(msg), steps, allow_cancel);
63  }
64  bool advance(int step);
65  bool canceled();
66  void stop();
67  void setMaximum(int steps);
68  void setMsg(const QString& msg);
69  void setMsg(const char* msg) {
70  setMsg(QString::fromLocal8Bit(msg));
71  }
72  void setMsg(std::string msg) {
73  setMsg(QString::fromStdString(msg));
74  }
75  static void clear(); // Remove any progress bars and clear the stack
76  int exec();
77 
78  static void useModal();
79  static void useModeless();
80 
81  static GlobalProgress& instance();
82  QProgressDialog* dialog() {
83  return prg;
84  }
85 
86 public slots:
87  void close();
88 
89 protected:
90  bool event(QEvent* e);
91  void createDialog(const QString& msg, int steps, int value, bool allow_cancel);
92  QPointer<QPushButton> cancel;
93 
94 private slots:
95  void wasCanceled();
96  void showDialog();
97 };
98 
99 class mgx_EXPORT Progress : public QObject {
100  Q_OBJECT
101 public:
102  Progress(const QString& msg, int steps, bool allow_cancel = true)
103  {
104  GlobalProgress::instance().start(msg, steps, allow_cancel);
105  }
106  Progress(std::string msg, int steps, bool allow_cancel = true)
107  {
108  GlobalProgress::instance().start(msg, steps, allow_cancel);
109  }
110  Progress(const char* msg, int steps, bool allow_cancel = true)
111  {
112  GlobalProgress::instance().start(msg, steps, allow_cancel);
113  }
114 
115  ~Progress() {
116  GlobalProgress::instance().stop();
117  }
118 
119  bool restart(const QString& msg, int steps)
120  {
121  GlobalProgress::instance().stop();
122  GlobalProgress::instance().start(msg, steps);
123  return true;
124  }
125  bool restart(std::string msg, int steps) {
126  return restart(QString::fromStdString(msg), steps);
127  }
128  bool restart(const char* msg, int steps) {
129  return restart(QString::fromLocal8Bit(msg), steps);
130  }
131 
132  void setMsg(const QString& msg) {
133  GlobalProgress::instance().setMsg(msg);
134  }
135  void setMsg(std::string msg) {
136  setMsg(QString::fromStdString(msg));
137  }
138  void setMsg(const char* msg) {
139  setMsg(QString::fromLocal8Bit(msg));
140  }
141 
142  bool advance(int step) {
143  return GlobalProgress::instance().advance(step);
144  }
145  bool canceled() {
146  return GlobalProgress::instance().canceled();
147  }
148 
149  void setMaximum(int step) {
150  GlobalProgress::instance().setMaximum(step);
151  }
152  int exec() {
153  return GlobalProgress::instance().exec();
154  }
155 };
156 } // namespace mgx
157 #endif
Definition: Progress.hpp:30
fromLocal8Bit(const char *str, int size=-1)
fromStdString(const std::string &str)
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
Definition: Progress.hpp:99