0024275: Cppcheck warnings on uninitialized class members
[occt.git] / src / Draw / Draw_ProgressIndicator.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
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.
7 //
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.
10 //
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.
17
18 #include <Draw_ProgressIndicator.ixx>
19 #include <Draw_Interpretor.hxx>
20 #include <Draw.hxx>
21 #include <Message_ProgressScale.hxx>
22 #include <Message_Messenger.hxx>
23 #include <Message.hxx>
24
25 #include <time.h>
26 #include <stdio.h>
27
28 //=======================================================================
29 //function : Draw_ProgressIndicator
30 //purpose  : 
31 //=======================================================================
32
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 )
42 {
43 }
44
45 //=======================================================================
46 //function : Destroy
47 //purpose  : 
48 //=======================================================================
49
50 void Draw_ProgressIndicator::Destroy()
51 {
52   Reset();
53 }
54
55 //=======================================================================
56 //function : Reset
57 //purpose  : 
58 //=======================================================================
59
60 void Draw_ProgressIndicator::Reset()
61 {
62   Message_ProgressIndicator::Reset();
63   if ( myShown ) {
64     ((Draw_Interpretor*)myDraw)->Eval ( "destroy .xprogress" );
65     myShown = Standard_False;
66   }
67   myBreak = Standard_False;
68   myLastUpdate = myStartTime = 0;
69 }
70
71 //=======================================================================
72 //function : Show
73 //purpose  : 
74 //=======================================================================
75
76 Standard_Boolean Draw_ProgressIndicator::Show(const Standard_Boolean force)
77 {
78   if ( ! myGraphMode && ! myTextMode ) return Standard_False;
79   time_t aTimeT;
80   time ( &aTimeT );
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
85   myLastUpdate = aTime;
86   
87   // Prepare textual progress info
88   char text[2048];
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 ) );
100     else 
101       n += Sprintf ( &text[n], " %s: %.0f / %.0f", scale.GetName()->ToCString(), 
102                      scale.BaseToLocal ( locPos ), scale.GetMax() );
103   }
104
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() );
109   }
110   
111   // Show graphic progress bar
112   if ( myGraphMode ) {
113     if ( ! myShown ) {
114       char command[1024];
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;",
124                (long)(void*)this );
125       ((Draw_Interpretor*)myDraw)->Eval ( command );
126       myShown = Standard_True;
127     }
128     char command[1024];
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 );
137   }
138
139   // Print textual progress info
140   if ( myTextMode )
141     Message::DefaultMessenger()->Send (text, Message_Info);
142   
143   return Standard_True;
144 }
145        
146 //=======================================================================
147 //function : UserBreak
148 //purpose  : 
149 //=======================================================================
150
151 Standard_Boolean Draw_ProgressIndicator::UserBreak()
152 {
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" );
157   }
158   return myBreak;
159 }
160        
161 //=======================================================================
162 //function : SetTextMode
163 //purpose  : Sets text output mode (on/off)
164 //=======================================================================
165
166 void Draw_ProgressIndicator::SetTextMode(const Standard_Boolean theTextMode)
167 {
168   myTextMode = theTextMode;
169 }
170
171 //=======================================================================
172 //function : GetTextMode
173 //purpose  : Returns text output mode (on/off)
174 //=======================================================================
175
176 Standard_Boolean Draw_ProgressIndicator::GetTextMode() const
177 {
178   return myTextMode;
179 }
180
181 //=======================================================================
182 //function : SetGraphMode
183 //purpose  : Sets graphical output mode (on/off)
184 //=======================================================================
185
186 void Draw_ProgressIndicator::SetGraphMode(const Standard_Boolean theGraphMode)
187 {
188   myGraphMode = theGraphMode;
189 }
190
191 //=======================================================================
192 //function : GetGraphMode
193 //purpose  : Returns graphical output mode (on/off)
194 //=======================================================================
195
196 Standard_Boolean Draw_ProgressIndicator::GetGraphMode() const
197 {
198   return myGraphMode;
199 }
200
201 //=======================================================================
202 //function : DefaultTextMode
203 //purpose  : 
204 //=======================================================================
205
206 Standard_Boolean &Draw_ProgressIndicator::DefaultTextMode () 
207 {
208   static Standard_Boolean defTextMode = Standard_False;
209   return defTextMode;
210 }
211     
212 //=======================================================================
213 //function : DefaultGraphMode
214 //purpose  : 
215 //=======================================================================
216
217 Standard_Boolean &Draw_ProgressIndicator::DefaultGraphMode () 
218 {
219   static Standard_Boolean defGraphMode = Standard_False;
220   return defGraphMode;
221 }
222     
223 //=======================================================================
224 //function : StopIndicator
225 //purpose  : 
226 //=======================================================================
227
228 Standard_Integer &Draw_ProgressIndicator::StopIndicator () 
229 {
230   static Standard_Integer stopIndicator = 0;
231   return stopIndicator;
232 }
233
234