1 /*
2  *  Copyright (c) 2012-2016, Bruno Levy
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *
8  *  * Redistributions of source code must retain the above copyright notice,
9  *  this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright notice,
11  *  this list of conditions and the following disclaimer in the documentation
12  *  and/or other materials provided with the distribution.
13  *  * Neither the name of the ALICE Project-Team nor the names of its
14  *  contributors may be used to endorse or promote products derived from this
15  *  software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  *  POSSIBILITY OF SUCH DAMAGE.
28  *
29  *  If you modify this software, you should include a notice giving the
30  *  name of the person performing the modification, the date of modification,
31  *  and the reason for such modification.
32  *
33  *  Contact: Bruno Levy
34  *
35  *     Bruno.Levy@inria.fr
36  *     http://www.loria.fr/~levy
37  *
38  *     ALICE Project
39  *     LORIA, INRIA Lorraine,
40  *     Campus Scientifique, BP 239
41  *     54506 VANDOEUVRE LES NANCY CEDEX
42  *     FRANCE
43  *
44  */
45 
46 #include <geogram_gfx/gui/status_bar.h>
47 #include <geogram_gfx/gui/application.h>
48 #include <geogram_gfx/ImGui_ext/imgui_ext.h>
49 #include <geogram_gfx/ImGui_ext/icon_font.h>
50 #include <geogram/basic/string.h>
51 
52 namespace GEO {
StatusBar()53     StatusBar::StatusBar() {
54         step_ = 0;
55         percent_ = 0;
56         progress_ = false;
57         canceled_ = false;
58         nb_active_ = 0;
59 	height_ = 0.0f;
60     }
61 
begin()62     void StatusBar::begin() {
63         progress_ = true;
64         canceled_ = false;
65         ++nb_active_;
66     }
67 
progress(GEO::index_t step,GEO::index_t percent)68     void StatusBar::progress(GEO::index_t step, GEO::index_t percent) {
69         step_ = step;
70         percent_ = percent;
71 	update();
72     }
73 
end(bool canceled)74     void StatusBar::end(bool canceled) {
75         geo_argused(canceled);
76         step_ = 0;
77         percent_ = 0;
78         progress_ = false;
79         --nb_active_;
80     }
81 
draw()82     void StatusBar::draw() {
83         ImGui::Begin(
84             "##Status", nullptr,
85             ImGuiWindowFlags_NoResize |
86             ImGuiWindowFlags_NoMove |
87             ImGuiWindowFlags_NoCollapse |
88             ImGuiWindowFlags_NoTitleBar |
89             ImGuiWindowFlags_NoScrollbar
90         );
91         if(progress_) {
92 // "Cancel" button does not work for now under Android
93 // (to be investigated...)
94 #ifndef GEO_OS_ANDROID
95             if(ImGui::SimpleButton(icon_UTF8("window-close"))) {
96                 Progress::cancel();
97             }
98             ImGui::SameLine();
99 #endif
100             ImGui::Text(
101                 "%s", Progress::current_task()->task_name().c_str()
102             );
103             ImGui::SameLine();
104             std::string overlay =
105                 String::to_string(step_) + "/" +
106                 String::to_string(
107                     Progress::current_task()->max_steps()
108                 ) + " (" +
109                 String::to_string(percent_) +
110                 "%)";
111 
112             ImGui::ProgressBar(
113                 std::max(0.001f, float(percent_)/float(100.0)),
114                 ImVec2(-1,0.0),
115                 overlay.c_str()
116             );
117         }
118 	height_ = ImGui::GetFrameHeight();
119         ImGui::End();
120     }
121 
update()122     void StatusBar::update() {
123 	if(Application::instance() != nullptr) {
124 	    Application::instance()->draw();
125 	}
126     }
127 }
128