Gaia
textprogress.h
1 /*
2  * Copyright (C) 2006-2013 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Gaia
5  *
6  * Gaia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef GAIA_TEXTPROGRESS_H
21 #define GAIA_TEXTPROGRESS_H
22 
23 #include <iostream>
24 
25 namespace gaia2 {
26 
27 enum UpdateType {
28  UpdateOnUnit,
29  UpdateOnPercent
30 };
31 
39 class TextProgress {
40 
41  protected:
42  QString _display;
43  int _total, _current, _previous;
44  int _fieldWidth;
45  UpdateType _updateType;
46  bool _silent;
47 
48  public:
49  TextProgress(int total, QString format = "[%1/%2] (%3% done)...", UpdateType t = UpdateOnPercent) :
50  _total(total), _current(0), _previous(-total), _updateType(t), _silent(false) {
51  _fieldWidth = QString::number(total).size();
52  _display = QChar('\r') + format;
53  }
54 
55  int total() const { return _total; }
56  void setTotal(int total) {
57  _total = total;
58  _fieldWidth = QString::number(total).size();
59  }
60 
61 
65  inline void update() {
66  if (_silent) return;
67  switch (_updateType) {
68  case UpdateOnPercent:
69  if (_current*100/_total == _previous*100/_total) return;
70  break;
71  case UpdateOnUnit:
72  if (_current == _previous) return;
73  break;
74  }
75 
76  _previous = _current;
77  int percent = _current*100/_total;
78  std::cout << _display.arg(_current, _fieldWidth).arg(_total).arg(percent) << std::flush;
79  if (percent == 100) std::cout << std::endl;
80  }
81 
87  inline void update(int value) {
88  _current = value;
89  update();
90  }
91 
92  inline void setSilent(bool silent) {
93  _silent = silent;
94  }
95 
96  inline bool isSilent() {
97  return _silent;
98  }
99 };
100 
101 } // namespace gaia2
102 
103 #endif // GAIA_TEXTPROGRESS_H
This class is a very simple class which allows you to print a progress percentage on the standard out...
Definition: textprogress.h:39
void update()
Updates the counter, ie: displays its state on the standard output.
Definition: textprogress.h:65
void update(int value)
Updates the counter to given value, then display its state on the standard output.
Definition: textprogress.h:87
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22