Developer Documentation
Progress.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*
43  * Progress.hh
44  *
45  * Created on: Mar 14, 2012
46  * Author: ebke
47  */
48 
49 #ifndef PROGRESS_HH_
50 #define PROGRESS_HH_
51 
52 #include <set>
53 #include <algorithm>
54 
55 #ifdef USE_QT
56 #include <QProgressDialog>
57 #endif
58 
59 namespace ACG {
60 
90 class Progress {
91  public:
92  Progress(double maxProgress = 1.0) : parent(0), maxProgress(maxProgress), currentProgress(0.0), dialog_padding(0)
93  {};
94  virtual ~Progress() {
95  if (parent) parent->childGoesBye(this);
96  };
97 
98  void setMaxProgress(double value) {
99  maxProgress = value;
100  if (currentProgress != 0.0) progressChanged();
101  }
102 
103 #ifdef USE_QT
104  void connectToDialog(QProgressDialog *dlg) {
105  dialog = dlg;
106  dialog->setMaximum(100000);
107  updateDialog();
108  }
109 #endif
110 
114  double getNormalizedProgress() const {
115  return getProgress() / maxProgress;
116  }
117 
118  double getProgress() const {
119  double result = currentProgress;
120 
121  /*
122  * Thread safety: Lock mutex on children!
123  */
124  for (std::set<ChildRecord>::const_iterator it = children.begin(); it != children.end(); ++it) {
125  result += it->child->getNormalizedProgress() * it->contribution;
126  }
127 
128  return std::min(maxProgress, result);
129  }
130 
134  void addSubProgress(Progress *progress, double contribution) {
135  progress->parent = this;
136 
137  /*
138  * Thread safety: Lock mutex on children!
139  */
140  children.insert(ChildRecord(progress, contribution));
141  }
142 
143  void increment(double amount) {
144  currentProgress += amount;
145  progressChanged();
146  }
147 
148  protected:
149  void onChildProgress(Progress *) {
150  progressChanged();
151  }
152 
153  inline void progressChanged() {
154  if (parent) parent->onChildProgress(this);
155  updateDialog();
156  }
157 
158  inline void updateDialog() {
159 #ifdef USE_QT
160  if (dialog)
161  dialog->setValue(100000 * getNormalizedProgress());
162 #endif
163  }
164 
165  inline void childGoesBye(Progress *child) {
166  /*
167  * Thread safety: Lock mutex on children!
168  */
169  std::set<ChildRecord>::iterator it = children.find(ChildRecord(child, 0));
170  currentProgress += it->contribution;
171  children.erase(it);
172  progressChanged();
173  }
174 
175  protected:
176  Progress *parent;
177 
178  class ChildRecord {
179  public:
180  ChildRecord(Progress *child, double contribution) : child(child), contribution(contribution) {}
181 
182  bool operator==(const ChildRecord &rhs) const { return child == rhs.child; }
183  bool operator<(const ChildRecord &rhs) const { return child < rhs.child; }
184 
185  Progress *child;
186  double contribution;
187  };
188  std::set<ChildRecord> children;
189 
190  double maxProgress, currentProgress;
191 
192  union {
193 #ifdef USE_QT
194  QProgressDialog *dialog;
195 #endif
196  /*
197  * Make sure the object has the same memory layout, no matter
198  * whether USE_QT is defined or not.
199  */
200  void *dialog_padding;
201  };
202 
203  private:
204  /*
205  * Progress objects are not copyable because
206  * of the child-parent double linkage.
207  */
208  Progress(const Progress &rhs);
209  Progress &operator=(const Progress &rhs);
210 };
211 
212 } /* namespace ACG */
213 #endif /* PROGRESS_HH_ */
void addSubProgress(Progress *progress, double contribution)
Definition: Progress.hh:134
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
double getNormalizedProgress() const
Definition: Progress.hh:114