• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
simclock.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  simclock.cpp - description
3  -------------------
4  begin : Mon Feb 18 2002
5  copyright : (C) 2002 by Mark Hollomon
6  email : mhh@mindspring.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
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. *
15  * *
16  ***************************************************************************/
17 
18 #include "simclock.h"
19 
20 #include <kdebug.h>
21 #include <kglobal.h>
22 #include <klocale.h>
23 
24 #include "kstars.h"
25 #include "simclockadaptor.h"
26 
27 
28 int SimClock::TimerInterval = 100; //msec
29 
30 SimClock::SimClock(QObject *parent, const KStarsDateTime &when) :
31  QObject(parent),
32  tmr(this)
33 {
34  new SimClockAdaptor(this);
35  QDBusConnection::sessionBus().registerObject("/KStars/SimClock", this);
36 
37  if (! when.isValid() )
38  tmr.stop();
39  setUTC(when);
40  julianmark = UTC.djd();
41 
42  Scale = 1.0;
43  ManualMode = false;
44  ManualActive = false;
45 
46  QObject::connect(&tmr, SIGNAL(timeout()), this, SLOT(tick()));
47 }
48 
49 void SimClock::tick() {
50  if ( ! ManualMode ) { //only tick if ManualMode is false
51  long mselapsed = sysmark.elapsed();
52  if (mselapsed < lastelapsed) {
53  // The sysmark timer has wrapped after 24 hours back to 0 ms.
54  // Reset sysmark and julianmark
55  julianmark = UTC.djd();
56  sysmark.start();
57  lastelapsed = 0;
58  } else {
59  lastelapsed = mselapsed;
60  }
61 
62  long double scaledsec = (long double)mselapsed * (long double)Scale / 1000.0;
63  UTC.setDJD( julianmark + scaledsec / (24. * 3600.) );
64 
65  // kDebug() << "tick() : JD = " << KGlobal::locale()->formatNumber( UTC.djd(), 7 ) <<
66  // " mselapsed = " << mselapsed << " scale = " << Scale <<
67  // " scaledsec = " << double(scaledsec) << endl;
68 
69  emit timeAdvanced();
70  }
71 }
72 
73 void SimClock::setManualMode( bool on ) {
74  if ( on ) {
75  //Turn on manual ticking.
76  ManualActive = tmr.isActive();
77  tmr.stop();
78  } else {
79  //Turn off manual ticking. If the Manual clock was active, start the timer.
80  if ( isActive() ) {
81  sysmark.start();
82  julianmark = UTC.djd();
83  lastelapsed = 0;
84  tmr.start(TimerInterval);
85  }
86  }
87  ManualMode = on;
88 }
89 
90 void SimClock::manualTick( bool force ) {
91  if ( force || (ManualMode && ManualActive) ) {
92  setUTC( UTC.addSecs( (long double)Scale ) );
93  } else if ( ! ManualMode )
94  tick();
95 }
96 
97 bool SimClock::isActive() {
98  if ( ManualMode )
99  return ManualActive;
100  else
101  return tmr.isActive();
102 }
103 
104 void SimClock::stop() {
105  if ( ManualMode && ManualActive ) {
106  ManualActive = false;
107  emit clockToggled(true);
108  }
109 
110  if (!ManualMode && tmr.isActive()) {
111  kDebug() << i18n( "Stopping the timer" );
112  tmr.stop();
113  emit clockToggled(true);
114  }
115 }
116 
117 void SimClock::start() {
118  if ( ManualMode && !ManualActive ) {
119  ManualActive = true;
120  sysmark.start();
121  julianmark = UTC.djd();
122  lastelapsed = 0;
123  emit clockToggled( false );
124  //emit timeChanged() in order to restart calls to updateTime()
125  emit timeChanged();
126  } else if ( !ManualMode && !tmr.isActive()) {
127  kDebug() << i18n( "Starting the timer" );
128  sysmark.start();
129  julianmark = UTC.djd();
130  lastelapsed = 0;
131  tmr.start(TimerInterval);
132  emit clockToggled( false );
133  }
134 }
135 
136 void SimClock::setUTC(const KStarsDateTime &newtime) {
137  //DEBUG
138  kDebug() << newtime.dateTime().toString();
139  kDebug() << "is dateTime valid? " << newtime.dateTime().isValid();
140 
141  if ( newtime.isValid() ) {
142  UTC = newtime;
143  if (tmr.isActive()) {
144  julianmark = UTC.djd();
145  sysmark.start();
146  lastelapsed = 0;
147  }
148 
149  kDebug() << i18n( "Setting clock: UTC: %1 JD: %2" ,
150  UTC.toString(), KGlobal::locale()->formatNumber( UTC.djd() ) ) << endl;
151  emit timeChanged();
152  } else {
153  kDebug() << i18n( "Cannot set SimClock: Invalid Date/Time." );
154  }
155 }
156 
157 void SimClock::setClockScale(float s) {
158  if (Scale != s ) {
159  kDebug() << i18n( "New clock scale: %1 sec", s );
160  Scale = s;
161  if (tmr.isActive()) {
162  julianmark = UTC.djd();
163  sysmark.start();
164  lastelapsed = 0;
165  }
166  emit scaleChanged(s);
167  }
168 }
169 
170 #include "simclock.moc"
SimClock::setManualMode
void setManualMode(bool on=true)
Sets Manual Mode on/off according to the bool argument.
Definition: simclock.cpp:73
SimClock::scaleChanged
void scaleChanged(float)
The timestep has changed.
SimClock::tick
void tick()
Respond to the QTimer::timeout signal.
Definition: simclock.cpp:49
QObject
SimClock::isActive
bool isActive()
Whether the clock is active or not is a bit complicated by the introduction of "manual mode"...
Definition: simclock.cpp:97
SimClock::SimClock
SimClock(QObject *parent=0, const KStarsDateTime &when=KStarsDateTime::currentDateTime())
Constructor.
Definition: simclock.cpp:30
KStarsDateTime::djd
long double djd() const
Definition: kstarsdatetime.h:145
KStarsDateTime::addSecs
KStarsDateTime addSecs(double s) const
Definition: kstarsdatetime.cpp:127
SimClock::stop
Q_SCRIPTABLE Q_NOREPLY void stop()
DBUS function to stop the SimClock.
Definition: simclock.cpp:104
KStarsDateTime
Extension of KDateTime for KStars KStarsDateTime can represent the date/time as a Julian Day...
Definition: kstarsdatetime.h:45
simclock.h
SimClock::setClockScale
Q_SCRIPTABLE Q_NOREPLY void setClockScale(float s)
DBUS function to set scale of simclock.
Definition: simclock.cpp:157
SimClock::clockToggled
void clockToggled(bool)
This is an signal that is called on either clock start or clock stop with an appropriate boolean argu...
KStarsDateTime::setDJD
void setDJD(long double jd)
Assign the (long double) Julian Day value, which includes the time of day encoded in the fractional p...
Definition: kstarsdatetime.cpp:99
SimClock::timeChanged
void timeChanged()
The time has changed (emitted by setUTC() )
SimClock::manualTick
void manualTick(bool force=false)
Equivalent of tick() for manual mode.
Definition: simclock.cpp:90
SimClock::start
Q_SCRIPTABLE Q_NOREPLY void start()
DBUS function to start the SimClock.
Definition: simclock.cpp:117
SimClock::timeAdvanced
void timeAdvanced()
The clock has ticked (emitted by tick() )
kstars.h
SimClock::setUTC
Q_SCRIPTABLE Q_NOREPLY void setUTC(const KStarsDateTime &newtime)
DBUS function to set the time of the SimClock.
Definition: simclock.cpp:136
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal