Salome HOME
Fix for Bug IPAL8945
[modules/visu.git] / src / VISUGUI / VisuGUI_Timer.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : VisuGUI_Timer.cxx
25 //  Module : SALOME
26
27 #include "VisuGUI_Timer.h"
28
29 #include "SUIT_Desktop.h"
30
31 #include "utilities.h"
32
33 #ifndef WNT
34 static struct timezone *tz=(struct timezone*) malloc(sizeof(struct timezone));
35 #else
36 //timezone *tz=_timezone;
37 #endif
38
39 #ifndef CLK_TCK
40 # define CLK_TCK      CLOCKS_PER_SEC
41 #endif
42
43 VisuGUI_Timer::VisuGUI_Timer() :
44   Utils_Timer()
45 {
46 }
47
48 VisuGUI_Timer::~VisuGUI_Timer()
49 {
50 }
51
52 void VisuGUI_Timer::Start()
53 {
54   if (Stopped) {
55     Stopped = 0;
56 #ifndef WNT
57     times(RefToInitialTMS);
58     gettimeofday(RefToInitialTimeB,tz);
59 #else
60     SYSTEMTIME st;
61     GetSystemTime(&st);
62     SystemTimeToFileTime(&st, RefToInitialTMS);
63           time(RefToCurrentTimeB);
64 #endif
65   }
66 }
67
68 void VisuGUI_Timer::Stop()
69 {
70   if (!Stopped) {
71 #ifndef WNT
72     times(RefToCurrentTMS);
73     int diffr_user = RefToCurrentTMS->tms_utime - RefToInitialTMS->tms_utime;
74     int diffr_sys  = RefToCurrentTMS->tms_stime - RefToInitialTMS->tms_stime;
75     gettimeofday(RefToCurrentTimeB,tz);
76
77     static long aCLK_TCK=sysconf(_SC_CLK_TCK);
78     Cumul_user += (double) diffr_user / aCLK_TCK ;
79     Cumul_sys  += (double) diffr_sys  / aCLK_TCK ;
80 #else
81     SYSTEMTIME st;
82     GetSystemTime(&st);
83     SystemTimeToFileTime(&st, RefToCurrentTMS);
84     Cumul_user += (int)(((ULARGE_INTEGER*)(RefToCurrentTMS))->QuadPart - ((ULARGE_INTEGER*)(RefToInitialTMS))->QuadPart) / 10000000;
85           Cumul_sys = Cumul_user;
86           time(RefToCurrentTimeB);
87 #endif
88    Stopped = 1;
89   }
90 }
91
92 void VisuGUI_Timer::Reset() {
93   Stopped     = 1;
94   Cumul_user  = Cumul_sys = 0. ;
95 }
96
97 QString VisuGUI_Timer::GetTime() {
98   bool StopSav = Stopped;
99   if (!StopSav) Stop();
100
101   return QString::number( Cumul_user );
102
103   if (!StopSav) Start();
104 }