CEDRIC  Revision_backup-2009-02
ProgressIndicator.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Copyright (C) 2008 Max Hermann
4  *
5  * This file is part of the "CEDRIC Event Display" application.
6  *
7  *****************************************************************************/
8 
9 #ifndef PROGRESSINDICATOR_H
10 #define PROGRESSINDICATOR_H
11 
12 #include <string>
13 
38 {
39 public:
40  ProgressIndicator( int min=0, int max=100 )
41  : m_progress( min ),
42  m_canceled( false ),
43  m_minValue( min ),
44  m_maxValue( max )
45  {
46  }
47 
48  virtual ~ProgressIndicator()
49  {}
50 
51 protected:
53  virtual bool onPoll()=0;
54  virtual void onStart()=0;
55  virtual void onFinish()=0;
56  virtual void onRangeChange( int /*min*/, int /*max*/ ) {};
57 
58 public:
59  bool poll( int progress )
60  {
62  m_canceled = !onPoll();
63  return !m_canceled;
64  }
65 
66  void start()
67  {
68  m_canceled = false;
70  onStart();
71  }
72 
73  void finish()
74  {
76  poll( m_progress );
77  onFinish();
78  }
79 
80  void step()
81  {
82  poll( m_progress+1 );
83  }
84 
85  void setRange( int min, int max )
86  {
87  m_minValue = min;
88  m_maxValue = max;
89  onRangeChange( min, max );
90  }
91 
92  virtual void setTitle( const std::string& title )
93  {
94  m_title = title;
95  }
96 
97  virtual void setLabel( const std::string& label )
98  {
99  m_label = label;
100  }
101 
102  float percentage() const
103  {
104  return (m_progress-m_minValue) / (float)m_maxValue;
105  }
106 
107  //## refactor wasCanceled() to canceled() ?
108  bool wasCanceled() const { return m_canceled; }
109  int progress () const { return m_progress; }
110  int minValue () const { return m_minValue; }
111  int maxValue () const { return m_maxValue; }
112 
113  std::string title() const { return m_title; }
114  std::string& title() { return m_title; }
115  std::string label() const { return m_label; }
116  std::string& label() { return m_label; }
117 
118 protected:
123 
124  std::string m_title;
125  std::string m_label;
126 };
127 
128 
129 #include <iostream>
130 #include <iomanip>
131 
133 {
134 public:
136  : ProgressIndicator(),
137  m_cr('\r')
138  {}
139 
140 protected:
141  void onStart () {}
142  void onFinish() { std::cout << std::endl; }
143 
144  bool onPoll()
145  {
146  std::cout << title() << ": "
147  << std::setprecision(3) << std::setw(4)
148  << percentage()*100 << "% ";
149 
150  if( !label().empty() )
151  std::cout << "(" << label() << ") ";
152 
153  std::cout << m_cr;
154 
155  // cancel not possible yet
156  return true;
157  }
158 
159 private:
160  char m_cr;
161 };
162 
163 #endif // PROGRESSINDICATOR_H
int m_maxValue
Definition: ProgressIndicator.h:122
bool poll(int progress)
Definition: ProgressIndicator.h:59
Definition: ProgressIndicator.h:132
void step()
Definition: ProgressIndicator.h:80
bool m_canceled
Definition: ProgressIndicator.h:120
std::string title() const
Definition: ProgressIndicator.h:113
void onStart()
Definition: ProgressIndicator.h:141
int progress() const
Definition: ProgressIndicator.h:109
int minValue() const
Definition: ProgressIndicator.h:110
virtual ~ProgressIndicator()
Definition: ProgressIndicator.h:48
void setRange(int min, int max)
Definition: ProgressIndicator.h:85
virtual void onRangeChange(int, int)
Definition: ProgressIndicator.h:56
virtual void setLabel(const std::string &label)
Definition: ProgressIndicator.h:97
int maxValue() const
Definition: ProgressIndicator.h:111
std::string & title()
Definition: ProgressIndicator.h:114
bool onPoll()
Definition: ProgressIndicator.h:144
virtual void onFinish()=0
virtual bool onPoll()=0
ConsoleProgressIndicator()
Definition: ProgressIndicator.h:135
virtual void setTitle(const std::string &title)
Definition: ProgressIndicator.h:92
virtual void onStart()=0
void start()
Definition: ProgressIndicator.h:66
std::string label() const
Definition: ProgressIndicator.h:115
ProgressIndicator(int min=0, int max=100)
Definition: ProgressIndicator.h:40
std::string & label()
Definition: ProgressIndicator.h:116
int m_minValue
Definition: ProgressIndicator.h:121
std::string m_label
Definition: ProgressIndicator.h:125
bool wasCanceled() const
Definition: ProgressIndicator.h:108
int m_progress
Definition: ProgressIndicator.h:119
void onFinish()
Definition: ProgressIndicator.h:142
void finish()
Definition: ProgressIndicator.h:73
std::string m_title
Definition: ProgressIndicator.h:124
Interface for a polling-based progress indicator (e.g. progress bar)
Definition: ProgressIndicator.h:37
float percentage() const
Definition: ProgressIndicator.h:102