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 //=======================================================================
26 //function : Draw_ProgressIndicator
28 //=======================================================================
29 Draw_ProgressIndicator::Draw_ProgressIndicator(const Draw_Interpretor &di,
30 const Standard_Integer updateTime) :
31 myTextMode ( DefaultTextMode() ),
32 myGraphMode ( DefaultGraphMode() ),
33 myDraw ( (Standard_Address)&di ),
34 myShown ( Standard_False ),
35 myBreak ( Standard_False ),
36 myUpdateTime ( updateTime ),
37 myLastUpdate ( 0 ), myStartTime ( 0 )
41 //=======================================================================
44 //=======================================================================
46 void Draw_ProgressIndicator::Destroy()
51 //=======================================================================
54 //=======================================================================
56 void Draw_ProgressIndicator::Reset()
58 Message_ProgressIndicator::Reset();
60 ((Draw_Interpretor*)myDraw)->Eval ( "destroy .xprogress" );
61 myShown = Standard_False;
63 myBreak = Standard_False;
64 myLastUpdate = myStartTime = 0;
67 //=======================================================================
70 //=======================================================================
72 Standard_Boolean Draw_ProgressIndicator::Show(const Standard_Boolean force)
74 if ( ! myGraphMode && ! myTextMode ) return Standard_False;
77 Standard_Size aTime = (Standard_Size)aTimeT;
78 if ( ! myStartTime ) myStartTime = aTime;
79 if ( ! force && myUpdateTime >0 && aTime < myLastUpdate + myUpdateTime && GetPosition() < 1. )
80 return Standard_False; // return if update interval has not elapsed
83 // Prepare textual progress info
85 Standard_Integer n = 0;
86 n += Sprintf ( &text[n], "Progress: %.0f%%", 100. * GetPosition() );
87 for ( Standard_Integer i=GetNbScopes(); i >=1; i-- ) {
88 const Message_ProgressScale &scale = GetScope ( i );
89 if ( scale.GetName().IsNull() ) continue; // skip unnamed scopes
90 // if scope has subscopes, print end of subscope as its current position
91 Standard_Real locPos = ( i >1 ? GetScope ( i-1 ).GetLast() : GetPosition() );
92 // print progress info differently for finite and infinite scopes
93 if ( scale.GetInfinite() )
94 n += Sprintf ( &text[n], " %s: %.0f", scale.GetName()->ToCString(),
95 scale.BaseToLocal ( locPos ) );
97 n += Sprintf ( &text[n], " %s: %.0f / %.0f", scale.GetName()->ToCString(),
98 scale.BaseToLocal ( locPos ), scale.GetMax() );
101 // In addition, write elapsed/estimated/remaining time
102 if ( GetPosition() > 0.01 ) {
103 n += Sprintf ( &text[n], "\nElapsed/estimated time: %ld/%.0f sec",
104 (long)(aTime - myStartTime), ( aTime - myStartTime ) / GetPosition() );
107 // Show graphic progress bar
111 Sprintf ( command, "toplevel .xprogress -height 100 -width 410;"
112 "wm title .xprogress \"Progress\";"
113 "set xprogress_stop 0;"
114 "canvas .xprogress.bar -width 402 -height 22;"
115 ".xprogress.bar create rectangle 2 2 2 21 -fill blue -tags progress;"
116 ".xprogress.bar create rectangle 2 2 2 21 -outline black -tags progress_next;"
117 "message .xprogress.text -width 400 -text \"Progress 0%%\";"
118 "button .xprogress.stop -text \"Break\" -relief groove -width 9 -command {XProgress -stop %p};"
119 "pack .xprogress.bar .xprogress.text .xprogress.stop -side top;", this );
120 ((Draw_Interpretor*)myDraw)->Eval ( command );
121 myShown = Standard_True;
124 Standard_Integer num = 0;
125 num += Sprintf ( &command[num], ".xprogress.bar coords progress 2 2 %.0f 21;",
126 1+400*GetPosition() );
127 num += Sprintf ( &command[num], ".xprogress.bar coords progress_next 2 2 %.0f 21;",
128 1+400*GetScope(1).GetLast() );
129 num += Sprintf ( &command[num], ".xprogress.text configure -text \"%s\";", text );
130 num += Sprintf ( &command[num], "update" );
131 ((Draw_Interpretor*)myDraw)->Eval ( command );
134 // Print textual progress info
136 Message::DefaultMessenger()->Send (text, Message_Info);
138 return Standard_True;
141 //=======================================================================
142 //function : UserBreak
144 //=======================================================================
146 Standard_Boolean Draw_ProgressIndicator::UserBreak()
148 if ( StopIndicator() == this ) {
149 // cout << "Progress Indicator - User Break: " << StopIndicator() << ", " << (void*)this << endl;
150 myBreak = Standard_True;
151 ((Draw_Interpretor*)myDraw)->Eval ( "XProgress -stop 0" );
156 //=======================================================================
157 //function : SetTextMode
158 //purpose : Sets text output mode (on/off)
159 //=======================================================================
161 void Draw_ProgressIndicator::SetTextMode(const Standard_Boolean theTextMode)
163 myTextMode = theTextMode;
166 //=======================================================================
167 //function : GetTextMode
168 //purpose : Returns text output mode (on/off)
169 //=======================================================================
171 Standard_Boolean Draw_ProgressIndicator::GetTextMode() const
176 //=======================================================================
177 //function : SetGraphMode
178 //purpose : Sets graphical output mode (on/off)
179 //=======================================================================
181 void Draw_ProgressIndicator::SetGraphMode(const Standard_Boolean theGraphMode)
183 myGraphMode = theGraphMode;
186 //=======================================================================
187 //function : GetGraphMode
188 //purpose : Returns graphical output mode (on/off)
189 //=======================================================================
191 Standard_Boolean Draw_ProgressIndicator::GetGraphMode() const
196 //=======================================================================
197 //function : DefaultTextMode
199 //=======================================================================
201 Standard_Boolean &Draw_ProgressIndicator::DefaultTextMode ()
203 static Standard_Boolean defTextMode = Standard_False;
207 //=======================================================================
208 //function : DefaultGraphMode
210 //=======================================================================
212 Standard_Boolean &Draw_ProgressIndicator::DefaultGraphMode ()
214 static Standard_Boolean defGraphMode = Standard_False;
218 //=======================================================================
219 //function : StopIndicator
221 //=======================================================================
223 Standard_Address &Draw_ProgressIndicator::StopIndicator ()
225 static Standard_Address stopIndicator = 0;
226 return stopIndicator;