1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
18 #include <Draw_ProgressIndicator.ixx>
19 #include <Draw_Interpretor.hxx>
21 #include <Message_ProgressScale.hxx>
22 #include <Message_Messenger.hxx>
23 #include <Message.hxx>
28 //=======================================================================
29 //function : Draw_ProgressIndicator
31 //=======================================================================
33 Draw_ProgressIndicator::Draw_ProgressIndicator(const Draw_Interpretor &di,
34 const Standard_Integer updateTime) :
35 myTextMode ( DefaultTextMode() ),
36 myGraphMode ( DefaultGraphMode() ),
37 myDraw ( (Standard_Address)&di ),
38 myShown ( Standard_False ),
39 myBreak ( Standard_False ),
40 myUpdateTime ( updateTime ),
41 myLastUpdate ( 0 ), myStartTime ( 0 )
45 //=======================================================================
48 //=======================================================================
50 void Draw_ProgressIndicator::Destroy()
55 //=======================================================================
58 //=======================================================================
60 void Draw_ProgressIndicator::Reset()
62 Message_ProgressIndicator::Reset();
64 ((Draw_Interpretor*)myDraw)->Eval ( "destroy .xprogress" );
65 myShown = Standard_False;
67 myBreak = Standard_False;
68 myLastUpdate = myStartTime = 0;
71 //=======================================================================
74 //=======================================================================
76 Standard_Boolean Draw_ProgressIndicator::Show(const Standard_Boolean force)
78 if ( ! myGraphMode && ! myTextMode ) return Standard_False;
81 Standard_Size aTime = (Standard_Size)aTimeT;
82 if ( ! myStartTime ) myStartTime = aTime;
83 if ( ! force && myUpdateTime >0 && aTime < myLastUpdate + myUpdateTime && GetPosition() < 1. )
84 return Standard_False; // return if update interval has not elapsed
87 // Prepare textual progress info
89 Standard_Integer n = 0;
90 n += Sprintf ( &text[n], "Progress: %.0f%%", 100. * GetPosition() );
91 for ( Standard_Integer i=GetNbScopes(); i >=1; i-- ) {
92 const Message_ProgressScale &scale = GetScope ( i );
93 if ( scale.GetName().IsNull() ) continue; // skip unnamed scopes
94 // if scope has subscopes, print end of subscope as its current position
95 Standard_Real locPos = ( i >1 ? GetScope ( i-1 ).GetLast() : GetPosition() );
96 // print progress info differently for finite and infinite scopes
97 if ( scale.GetInfinite() )
98 n += Sprintf ( &text[n], " %s: %.0f", scale.GetName()->ToCString(),
99 scale.BaseToLocal ( locPos ) );
101 n += Sprintf ( &text[n], " %s: %.0f / %.0f", scale.GetName()->ToCString(),
102 scale.BaseToLocal ( locPos ), scale.GetMax() );
105 // In addition, write elapsed/estimated/remaining time
106 if ( GetPosition() > 0.01 ) {
107 n += Sprintf ( &text[n], "\nElapsed/estimated time: %ld/%.0f sec",
108 (long)(aTime - myStartTime), ( aTime - myStartTime ) / GetPosition() );
111 // Show graphic progress bar
115 Sprintf ( command, "toplevel .xprogress -height 100 -width 410;"
116 "wm title .xprogress \"Progress\";"
117 "set xprogress_stop 0;"
118 "canvas .xprogress.bar -width 402 -height 22;"
119 ".xprogress.bar create rectangle 2 2 2 21 -fill blue -tags progress;"
120 ".xprogress.bar create rectangle 2 2 2 21 -outline black -tags progress_next;"
121 "message .xprogress.text -width 400 -text \"Progress 0%%\";"
122 "button .xprogress.stop -text \"Break\" -relief groove -width 9 -command {XProgress -stop %ld};"
123 "pack .xprogress.bar .xprogress.text .xprogress.stop -side top;",
125 ((Draw_Interpretor*)myDraw)->Eval ( command );
126 myShown = Standard_True;
129 Standard_Integer num = 0;
130 num += Sprintf ( &command[num], ".xprogress.bar coords progress 2 2 %.0f 21;",
131 1+400*GetPosition() );
132 num += Sprintf ( &command[num], ".xprogress.bar coords progress_next 2 2 %.0f 21;",
133 1+400*GetScope(1).GetLast() );
134 num += Sprintf ( &command[num], ".xprogress.text configure -text \"%s\";", text );
135 num += Sprintf ( &command[num], "update" );
136 ((Draw_Interpretor*)myDraw)->Eval ( command );
139 // Print textual progress info
141 Message::DefaultMessenger()->Send (text, Message_Info);
143 return Standard_True;
146 //=======================================================================
147 //function : UserBreak
149 //=======================================================================
151 Standard_Boolean Draw_ProgressIndicator::UserBreak()
153 if ( StopIndicator() == (long)(void*)this ) {
154 // cout << "Progress Indicator - User Break: " << StopIndicator() << ", " << (void*)this << endl;
155 myBreak = Standard_True;
156 ((Draw_Interpretor*)myDraw)->Eval ( "XProgress -stop 0" );
161 //=======================================================================
162 //function : SetTextMode
163 //purpose : Sets text output mode (on/off)
164 //=======================================================================
166 void Draw_ProgressIndicator::SetTextMode(const Standard_Boolean theTextMode)
168 myTextMode = theTextMode;
171 //=======================================================================
172 //function : GetTextMode
173 //purpose : Returns text output mode (on/off)
174 //=======================================================================
176 Standard_Boolean Draw_ProgressIndicator::GetTextMode() const
181 //=======================================================================
182 //function : SetGraphMode
183 //purpose : Sets graphical output mode (on/off)
184 //=======================================================================
186 void Draw_ProgressIndicator::SetGraphMode(const Standard_Boolean theGraphMode)
188 myGraphMode = theGraphMode;
191 //=======================================================================
192 //function : GetGraphMode
193 //purpose : Returns graphical output mode (on/off)
194 //=======================================================================
196 Standard_Boolean Draw_ProgressIndicator::GetGraphMode() const
201 //=======================================================================
202 //function : DefaultTextMode
204 //=======================================================================
206 Standard_Boolean &Draw_ProgressIndicator::DefaultTextMode ()
208 static Standard_Boolean defTextMode = Standard_False;
212 //=======================================================================
213 //function : DefaultGraphMode
215 //=======================================================================
217 Standard_Boolean &Draw_ProgressIndicator::DefaultGraphMode ()
219 static Standard_Boolean defGraphMode = Standard_False;
223 //=======================================================================
224 //function : StopIndicator
226 //=======================================================================
228 Standard_Integer &Draw_ProgressIndicator::StopIndicator ()
230 static Standard_Integer stopIndicator = 0;
231 return stopIndicator;