1 /***************************************************************************
2 simclock.cpp - description
4 begin : Mon Feb 18 2002
5 copyright : (C) 2002 by Mark Hollomon
6 email : mhh@mindspring.com
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
24 int SimClock::idgen
= 1;
26 int SimClock::TimerInterval
= 100; //msec
28 SimClock::SimClock(QObject
*parent
, const KStarsDateTime
&when
) :
29 DCOPObject("clock#" + QCString().setNum(idgen
++)),
33 if (! when
.isValid() ) tmr
.stop();
35 julianmark
= UTC
.djd();
41 QObject::connect(&tmr
, SIGNAL(timeout()), this, SLOT(tick()));
44 SimClock::SimClock (const SimClock
&old
) :
45 DCOPObject("clock#" + QCString().setNum(idgen
++)),
46 QObject(old
.parent()),
51 julianmark
= old
.julianmark
;
54 ManualMode
= old
.ManualMode
;
55 ManualActive
= old
.ManualActive
;
57 QObject::connect(&tmr
, SIGNAL(timeout()), this, SLOT(tick()));
60 void SimClock::tick() {
61 if ( ! ManualMode
) { //only tick if ManualMode is false
62 long mselapsed
= sysmark
.elapsed();
63 if (mselapsed
< lastelapsed
) {
64 // The sysmark timer has wrapped after 24 hours back to 0 ms.
65 // Reset sysmark and julianmark
66 julianmark
= UTC
.djd();
70 lastelapsed
= mselapsed
;
73 long double scaledsec
= (long double)mselapsed
* (long double)Scale
/ 1000.0;
74 UTC
.setDJD( julianmark
+ scaledsec
/ (24. * 3600.) );
76 // kdDebug() << "tick() : JD = " << KGlobal::locale()->formatNumber( UTC.djd(), 7 ) <<
77 // " mselapsed = " << mselapsed << " scale = " << Scale <<
78 // " scaledsec = " << double(scaledsec) << endl;
84 void SimClock::setManualMode( bool on
) {
86 //Turn on manual ticking.
87 //If the timer was active, stop the timer and set ManualActive=true.
88 //Otherwise, set ManualActive=false.
89 //Finally, set ManualMode=true.
90 if ( tmr
.isActive() ) {
98 //Turn off manual ticking. If the Manual clock was active, start the timer.
99 //Then set ManualMode=false.
102 julianmark
= UTC
.djd();
104 tmr
.start(TimerInterval
);
110 void SimClock::manualTick( bool force
) {
111 if ( force
|| (ManualMode
&& ManualActive
) ) {
112 setUTC( UTC
.addSecs( (long double)Scale
) );
113 } else if ( ! ManualMode
) tick();
116 bool SimClock::isActive() {
117 if ( ManualMode
) return ManualActive
;
118 else return tmr
.isActive();
121 // The SimClockInterface
122 void SimClock::stop() {
123 if ( ManualMode
&& ManualActive
) {
124 ManualActive
= false;
128 if (!ManualMode
&& tmr
.isActive()) {
129 kdDebug() << i18n( "Stopping the timer" ) << endl
;
135 void SimClock::start() {
136 if ( ManualMode
&& !ManualActive
) {
139 julianmark
= UTC
.djd();
142 //emit timeChanged() in order to restart calls to updateTime()
146 if (! ManualMode
&& ! tmr
.isActive()) {
147 kdDebug() << i18n( "Starting the timer" ) << endl
;
149 julianmark
= UTC
.djd();
151 tmr
.start(TimerInterval
);
156 void SimClock::setUTC(const KStarsDateTime
&newtime
) {
157 if ( newtime
.isValid() ) {
159 if (tmr
.isActive()) {
160 julianmark
= UTC
.djd();
165 kdDebug() << i18n( "Setting clock: UTC: %1 JD: %2" )
166 .arg( UTC
.toString() ).arg( KGlobal::locale()->formatNumber( UTC
.djd() ) ) << endl
;
169 kdDebug() << i18n( "Cannot set SimClock: Invalid Date/Time." ) << endl
;
173 void SimClock::setScale(float s
) {
175 kdDebug() << i18n( "New clock scale: %1 sec" ).arg( s
) << endl
;
177 if (tmr
.isActive()) {
178 julianmark
= UTC
.djd();
182 emit
scaleChanged(s
);
186 //DCOP function to set clock scale
187 void SimClock::setClockScale(float s
) {
191 #include "simclock.moc"