1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
16 #include <Draw_Interpretor.hxx>
17 #include <Draw_ProgressIndicator.hxx>
18 #include <Message.hxx>
19 #include <Message_Messenger.hxx>
20 #include <Message_ProgressScale.hxx>
21 #include <Standard_Type.hxx>
25 IMPLEMENT_STANDARD_RTTIEXT(Draw_ProgressIndicator,Message_ProgressIndicator)
27 //=======================================================================
28 //function : Draw_ProgressIndicator
30 //=======================================================================
31 Draw_ProgressIndicator::Draw_ProgressIndicator (const Draw_Interpretor &di, Standard_Real theUpdateThreshold)
32 : myTextMode ( DefaultTextMode() ),
33 myGraphMode ( DefaultGraphMode() ),
34 myDraw ( (Standard_Address)&di ),
35 myShown ( Standard_False ),
36 myBreak ( Standard_False ),
37 myUpdateThreshold ( 0.01 * theUpdateThreshold ),
38 myLastPosition ( -1. ),
43 //=======================================================================
44 //function : ~Draw_ProgressIndicator
46 //=======================================================================
48 Draw_ProgressIndicator::~Draw_ProgressIndicator()
53 //=======================================================================
56 //=======================================================================
58 void Draw_ProgressIndicator::Reset()
60 Message_ProgressIndicator::Reset();
62 ((Draw_Interpretor*)myDraw)->Eval ( "destroy .xprogress" );
63 myShown = Standard_False;
65 myBreak = Standard_False;
70 //=======================================================================
73 //=======================================================================
75 Standard_Boolean Draw_ProgressIndicator::Show(const Standard_Boolean force)
77 if ( ! myGraphMode && ! myTextMode )
78 return Standard_False;
80 // remember time of the first call to Show as process start time
85 myStartTime = (Standard_Size)aTimeT;
88 // unless show is forced, show updated state only if at least 1% progress has been reached since the last update
89 Standard_Real aPosition = GetPosition();
90 if ( ! force && aPosition < 1. && Abs (aPosition - myLastPosition) < myUpdateThreshold)
91 return Standard_False; // return if update interval has not elapsed
92 myLastPosition = aPosition;
94 // Prepare textual progress info
96 Standard_Integer n = 0;
97 n += Sprintf ( &text[n], "Progress: %.0f%%", 100. * GetPosition() );
98 for ( Standard_Integer i=GetNbScopes(); i >=1; i-- ) {
99 const Message_ProgressScale &scale = GetScope ( i );
100 if ( scale.GetName().IsNull() ) continue; // skip unnamed scopes
101 // if scope has subscopes, print end of subscope as its current position
102 Standard_Real locPos = ( i >1 ? GetScope ( i-1 ).GetLast() : GetPosition() );
103 // print progress info differently for finite and infinite scopes
104 if ( scale.GetInfinite() )
105 n += Sprintf ( &text[n], " %s: %.0f", scale.GetName()->ToCString(),
106 scale.BaseToLocal ( locPos ) );
108 n += Sprintf ( &text[n], " %s: %.0f / %.0f", scale.GetName()->ToCString(),
109 scale.BaseToLocal ( locPos ), scale.GetMax() );
112 // Show graphic progress bar
115 // In addition, write elapsed/estimated/remaining time
116 if ( GetPosition() > 0.01 ) {
119 Standard_Size aTime = (Standard_Size)aTimeT;
120 n += Sprintf ( &text[n], "\nElapsed/estimated time: %ld/%.0f sec",
121 (long)(aTime - myStartTime), ( aTime - myStartTime ) / GetPosition() );
126 Sprintf ( command, "toplevel .xprogress -height 100 -width 410;"
127 "wm title .xprogress \"Progress\";"
128 "set xprogress_stop 0;"
129 "canvas .xprogress.bar -width 402 -height 22;"
130 ".xprogress.bar create rectangle 2 2 2 21 -fill blue -tags progress;"
131 ".xprogress.bar create rectangle 2 2 2 21 -outline black -tags progress_next;"
132 "message .xprogress.text -width 400 -text \"Progress 0%%\";"
133 "button .xprogress.stop -text \"Break\" -relief groove -width 9 -command {XProgress -stop %p};"
134 "pack .xprogress.bar .xprogress.text .xprogress.stop -side top;", this );
135 ((Draw_Interpretor*)myDraw)->Eval ( command );
136 myShown = Standard_True;
139 Standard_Integer num = 0;
140 num += Sprintf ( &command[num], ".xprogress.bar coords progress 2 2 %.0f 21;",
141 1+400*GetPosition() );
142 num += Sprintf ( &command[num], ".xprogress.bar coords progress_next 2 2 %.0f 21;",
143 1+400*GetScope(1).GetLast() );
144 num += Sprintf ( &command[num], ".xprogress.text configure -text \"%s\";", text );
145 num += Sprintf ( &command[num], "update" );
146 ((Draw_Interpretor*)myDraw)->Eval ( command );
149 // Print textual progress info
151 Message::DefaultMessenger()->Send (text, Message_Info);
153 return Standard_True;
156 //=======================================================================
157 //function : UserBreak
159 //=======================================================================
161 Standard_Boolean Draw_ProgressIndicator::UserBreak()
163 if ( StopIndicator() == this ) {
164 // std::cout << "Progress Indicator - User Break: " << StopIndicator() << ", " << (void*)this << std::endl;
165 myBreak = Standard_True;
166 ((Draw_Interpretor*)myDraw)->Eval ( "XProgress -stop 0" );
171 //=======================================================================
172 //function : SetTextMode
173 //purpose : Sets text output mode (on/off)
174 //=======================================================================
176 void Draw_ProgressIndicator::SetTextMode(const Standard_Boolean theTextMode)
178 myTextMode = theTextMode;
181 //=======================================================================
182 //function : GetTextMode
183 //purpose : Returns text output mode (on/off)
184 //=======================================================================
186 Standard_Boolean Draw_ProgressIndicator::GetTextMode() const
191 //=======================================================================
192 //function : SetGraphMode
193 //purpose : Sets graphical output mode (on/off)
194 //=======================================================================
196 void Draw_ProgressIndicator::SetGraphMode(const Standard_Boolean theGraphMode)
198 myGraphMode = theGraphMode;
201 //=======================================================================
202 //function : GetGraphMode
203 //purpose : Returns graphical output mode (on/off)
204 //=======================================================================
206 Standard_Boolean Draw_ProgressIndicator::GetGraphMode() const
211 //=======================================================================
212 //function : DefaultTextMode
214 //=======================================================================
216 Standard_Boolean &Draw_ProgressIndicator::DefaultTextMode ()
218 static Standard_Boolean defTextMode = Standard_False;
222 //=======================================================================
223 //function : DefaultGraphMode
225 //=======================================================================
227 Standard_Boolean &Draw_ProgressIndicator::DefaultGraphMode ()
229 static Standard_Boolean defGraphMode = Standard_False;
233 //=======================================================================
234 //function : StopIndicator
236 //=======================================================================
238 Standard_Address &Draw_ProgressIndicator::StopIndicator ()
240 static Standard_Address stopIndicator = 0;
241 return stopIndicator;