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 #ifndef PROGRESS_HH_
44 #define PROGRESS_HH_
45
46 #include <set>
47 #include <algorithm>
48
49 #ifdef USE_QT
50 #include <QProgressDialog>
51 #endif
52
53 namespace ACG {
54
84 class Progress {
85 public:
86 Progress(double maxProgress = 1.0) : parent(0), maxProgress(maxProgress), currentProgress(0.0), dialog_padding(0)
87 {};
88 virtual ~Progress() {
89 if (parent) parent->childGoesBye(this);
90 };
91
92 void setMaxProgress(double value) {
93 maxProgress = value;
94 if (currentProgress != 0.0) progressChanged();
95 }
96
97 #ifdef USE_QT
98 void connectToDialog(QProgressDialog *dlg) {
99 dialog = dlg;
100 dialog->setMaximum(100000);
101 updateDialog();
102 }
103 #endif
104
108 double getNormalizedProgress() const {
109 return getProgress() / maxProgress;
110 }
111
112 double getProgress() const {
113 double result = currentProgress;
114
115 /*
116 * Thread safety: Lock mutex on children!
117 */
118 for (std::set<ChildRecord>::const_iterator it = children.begin(); it != children.end(); ++it) {
119 result += it->child->getNormalizedProgress() * it->contribution;
120 }
121
122 return std::min(maxProgress, result);
123 }
124
128 void addSubProgress(Progress *progress, double contribution) {
129 progress->parent = this;
130
131 /*
132 * Thread safety: Lock mutex on children!
133 */
134 children.insert(ChildRecord(progress, contribution));
135 }
136
137 void increment(double amount) {
138 currentProgress += amount;
139 progressChanged();
140 }
141
142 protected:
143 void onChildProgress(Progress *) {
144 progressChanged();
145 }
146
147 inline void progressChanged() {
148 if (parent) parent->onChildProgress(this);
149 updateDialog();
150 }
151
152 inline void updateDialog() {
153 #ifdef USE_QT
154 if (dialog)
155 dialog->setValue(100000 * getNormalizedProgress());
156 #endif
157 }
158
159 inline void childGoesBye(Progress *child) {
160 /*
161 * Thread safety: Lock mutex on children!
162 */
163 std::set<ChildRecord>::iterator it = children.find(ChildRecord(child, 0));
164 currentProgress += it->contribution;
165 children.erase(it);
166 progressChanged();
167 }
168
169 protected:
170 Progress *parent;
171
172 class ChildRecord {
173 public:
174 ChildRecord(Progress *child, double contribution) : child(child), contribution(contribution) {}
175
176 bool operator==(const ChildRecord &rhs) const { return child == rhs.child; }
177 bool operator<(const ChildRecord &rhs) const { return child < rhs.child; }
178
179 Progress *child;
180 double contribution;
181 };
182 std::set<ChildRecord> children;
183
184 double maxProgress, currentProgress;
185
186 union {
187 #ifdef USE_QT
188 QProgressDialog *dialog;
189 #endif
190 /*
191 * Make sure the object has the same memory layout, no matter
192 * whether USE_QT is defined or not.
193 */
194 void *dialog_padding;
195 };
196
197 private:
198 /*
199 * Progress objects are not copyable because
200 * of the child-parent double linkage.
201 */
202 Progress(const Progress &rhs);
203 Progress &operator=(const Progress &rhs);
204 };
205
206 } /* namespace ACG */
207 #endif /* PROGRESS_HH_ */
ACG::Progress::addSubProgress
void addSubProgress(Progress *progress, double contribution)
Definition: Progress.hh:128
ACG
Namespace providing different geometric functions concerning angles.
Definition: BaseObjectData.hh:68
ACG::Progress::ChildRecord
Definition: Progress.hh:172
ACG::Progress
Definition: Progress.hh:84
ACG::Progress::getNormalizedProgress
double getNormalizedProgress() const
Definition: Progress.hh:108