Fixup prev commit (LV2 X11 UI) -- #7837
[ardour.git] / libs / ardour / progress.cc
1 /*
2  * Copyright (C) 2010 Carl Hetherington <carl@carlh.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <cassert>
20 #include <iostream>
21 #include "ardour/progress.h"
22
23 using namespace std;
24
25 ARDOUR::Progress::Progress ()
26         : _cancelled (false)
27 {
28         descend (1);
29 }
30
31 /** Descend down one level in terms of progress reporting; e.g. if
32  *  there is a task which is split up into N subtasks, each of which
33  *  report their progress from 0 to 100%, call descend() before executing
34  *  each subtask, and ascend() afterwards to ensure that overall progress
35  *  is reported correctly.
36  *
37  *  @param p Percentage (from 0 to 1) of the current task to allocate to the subtask.
38  */
39 void
40 ARDOUR::Progress::descend (float a)
41 {
42         _stack.push_back (Level (a));
43 }
44
45 void
46 ARDOUR::Progress::ascend ()
47 {
48         assert (!_stack.empty ());
49         float const a = _stack.back().allocation;
50         _stack.pop_back ();
51         _stack.back().normalised += a;
52 }
53
54 /** Set the progress of the current task.
55  *  @param p Progress (from 0 to 1)
56  */
57 void
58 ARDOUR::Progress::set_progress (float p)
59 {
60         assert (!_stack.empty ());
61
62         _stack.back().normalised = p;
63
64         float overall = 0;
65         float factor = 1;
66         for (list<Level>::iterator i = _stack.begin(); i != _stack.end(); ++i) {
67                 factor *= i->allocation;
68                 overall += i->normalised * factor;
69         }
70
71         set_overall_progress (overall);
72 }
73
74 void
75 ARDOUR::Progress::cancel ()
76 {
77         _cancelled = true;
78 }
79
80 bool
81 ARDOUR::Progress::cancelled () const
82 {
83         return _cancelled;
84 }