Kstars

simclock.cpp
1/*
2 SPDX-FileCopyrightText: 2002 Mark Hollomon <mhh@mindspring.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "simclock.h"
8
9#ifndef KSTARS_LITE
10#include "kstars.h"
11#include "simclockadaptor.h"
12#endif
13
14#include <kstars_debug.h>
15
16int SimClock::TimerInterval = 100; //msec
17
18SimClock::SimClock(QObject *parent, const KStarsDateTime &when) : QObject(parent), m_InternalTimer(this)
19{
20#ifndef KSTARS_LITE
21 new SimClockAdaptor(this);
22 QDBusConnection::sessionBus().registerObject("/KStars/SimClock", this);
23#endif
24 if (!when.isValid())
25 m_InternalTimer.stop();
26 setUTC(when);
27 m_JulianMark = m_UTC.djd();
28
29 QObject::connect(&m_InternalTimer, SIGNAL(timeout()), this, SLOT(tick()));
30}
31
33{
34 if (m_RealTime)
35 {
37 emit timeAdvanced();
38 }
39 else if (!m_ManualMode) //only tick if ManualMode is false
40 {
41 long mselapsed = m_SystemMark.elapsed();
42 if (mselapsed < m_LastElapsed)
43 {
44 // The sysmark timer has wrapped after 24 hours back to 0 ms.
45 // Reset sysmark and julianmark
46 m_JulianMark = m_UTC.djd();
47 m_SystemMark.start();
48 m_LastElapsed = 0;
49 }
50 else
51 {
52 m_LastElapsed = mselapsed;
53 }
54
55 long double scaledsec = static_cast<long double>(mselapsed) * static_cast<long double>(m_Scale) / 1000.0;
56 m_UTC.setDJD(m_JulianMark + scaledsec / (24. * 3600.));
57
58 // qDebug() << Q_FUNC_INFO << "tick() : JD = " << QLocale().toString( UTC.djd(), 7 ) <<
59 // " mselapsed = " << mselapsed << " scale = " << Scale <<
60 // " scaledsec = " << double(scaledsec);
61
62 emit timeAdvanced();
63 }
64}
65
67{
68 if (on)
69 {
70 //Turn on manual ticking.
71 m_ManualActive = m_InternalTimer.isActive();
72 m_InternalTimer.stop();
73 }
74 else
75 {
76 //Turn off manual ticking. If the Manual clock was active, start the timer.
77 if (isActive())
78 {
79 m_SystemMark.start();
80 m_JulianMark = m_UTC.djd();
81 m_LastElapsed = 0;
82 m_InternalTimer.start(TimerInterval);
83 }
84 }
85 m_ManualMode = on;
86}
87
89{
90 if(on == m_RealTime)
91 return;
92
93 m_RealTime = on;
94 emit realtimeToogled(m_RealTime);
95 if (m_RealTime)
96 {
97 m_InternalTimer.start(TimerInterval);
98 emit clockToggled(false);
99 emit scaleChanged(1.0);
100 emit timeChanged();
101 }
102 else
103 {
104 m_SystemMark.start();
105 m_JulianMark = m_UTC.djd();
106 m_LastElapsed = 0;
107 emit scaleChanged(m_Scale);
108 emit timeChanged();
109 }
110}
111
112void SimClock::manualTick(bool force, bool backward)
113{
114 if (m_RealTime)
115 return;
116
117 if (force || (m_ManualMode && m_ManualActive))
118 {
119 //The single shot timer is needed because otherwise the animation is happening so frequently
120 //that the kstars interface becomes too unresponsive.
121 //QTimer::singleShot(1, [this,backward] { setUTC(UTC.addSecs(static_cast<long double>Scale * (backward ? -1 : 1))); });
122 setUTC(m_UTC.addSecs(static_cast<long double>(m_Scale) * (backward ? -1 : 1)));
123 }
124 else if (!m_ManualMode)
125 tick();
126}
127
129{
130 if (m_ManualMode)
131 return m_ManualActive;
132 else
133 return m_InternalTimer.isActive();
134}
135
137{
138 if (m_RealTime)
139 return;
140
141 if (m_ManualMode && m_ManualActive)
142 {
143 m_ManualActive = false;
144 emit clockToggled(true);
145 }
146
147 if (!m_ManualMode && m_InternalTimer.isActive())
148 {
149 qCDebug(KSTARS) << "Stopping the timer";
150 m_InternalTimer.stop();
151 emit clockToggled(true);
152 }
153}
154
156{
157 if (m_RealTime)
158 return;
159
160 if (m_ManualMode && !m_ManualActive)
161 {
162 m_ManualActive = true;
163 m_SystemMark.start();
164 m_JulianMark = m_UTC.djd();
165 m_LastElapsed = 0;
166 emit clockToggled(false);
167 //emit timeChanged() in order to restart calls to updateTime()
168 emit timeChanged();
169 }
170 else if (!m_ManualMode && !m_InternalTimer.isActive())
171 {
172 qCDebug(KSTARS) << "Starting the timer";
173 m_SystemMark.start();
174 m_JulianMark = m_UTC.djd();
175 m_LastElapsed = 0;
176 m_InternalTimer.start(TimerInterval);
177 emit clockToggled(false);
178 }
179}
180
182{
183 //DEBUG
184 //qDebug() << Q_FUNC_INFO << newtime.toString();
185 //qDebug() << Q_FUNC_INFO << "is dateTime valid? " << newtime.isValid();
186
187 if (m_RealTime)
188 return;
189
190 if (newtime.isValid())
191 {
192 m_UTC = newtime;
193 if (m_InternalTimer.isActive())
194 {
195 m_JulianMark = m_UTC.djd();
196 m_SystemMark.start();
197 m_LastElapsed = 0;
198 }
199
200 // N.B. Too much log spam when in manual mode
201 //qCInfo(KSTARS) << QString("Setting clock: UTC: %1 JD: %2").arg(UTC.toString(), QLocale().toString((double)UTC.djd(), 'f', 2));
202 emit timeChanged();
203 }
204 else
205 {
206 qCWarning(KSTARS) << "Cannot set SimClock: Invalid Date/Time.";
207 }
208}
209
210void SimClock::setClockScale(double scale)
211{
212 if (m_Scale != scale && !m_RealTime)
213 {
214 qCInfo(KSTARS) << "New clock scale: " << scale << " sec";
215 emit scaleChanged(scale);
216 m_Scale = scale;
217 if (m_InternalTimer.isActive())
218 {
219 m_JulianMark = m_UTC.djd();
220 m_SystemMark.start();
221 m_LastElapsed = 0;
222 }
223 }
224}
Extension of QDateTime for KStars KStarsDateTime can represent the date/time as a Julian Day,...
void setDJD(long double jd)
Assign the static_cast<long double> Julian Day value, which includes the time of day encoded in the f...
KStarsDateTime addSecs(double s) const
long double djd() const
void clockToggled(bool)
This is an signal that is called on either clock start or clock stop with an appropriate boolean argu...
void timeAdvanced()
The clock has ticked (emitted by tick() )
double scale() const
Definition simclock.h:51
void timeChanged()
The time has changed (emitted by setUTC() )
void setRealTime(bool on=true)
Realtime mode will lock SimClock with system clock.
Definition simclock.cpp:88
Q_INVOKABLE bool isActive()
Whether the clock is active or not is a bit complicated by the introduction of "manual mode".
Definition simclock.cpp:128
void setManualMode(bool on=true)
Sets Manual Mode on/off according to the bool argument.
Definition simclock.cpp:66
void scaleChanged(float)
The timestep has changed.
Q_SCRIPTABLE Q_NOREPLY void setClockScale(double scale)
DBUS function to set scale of simclock.
Definition simclock.cpp:210
Q_SCRIPTABLE Q_NOREPLY void start()
DBUS function to start the SimClock.
Definition simclock.cpp:155
void tick()
Respond to the QTimer::timeout signal.
Definition simclock.cpp:32
Q_SCRIPTABLE Q_NOREPLY void setUTC(const KStarsDateTime &newtime)
DBUS function to set the time of the SimClock.
Definition simclock.cpp:181
void realtimeToogled(bool)
Emitted when realtime clock is toggled.
SimClock(QObject *parent=nullptr, const KStarsDateTime &when=KStarsDateTime::currentDateTimeUtc())
Constructor.
Definition simclock.cpp:18
Q_SCRIPTABLE Q_NOREPLY void stop()
DBUS function to stop the SimClock.
Definition simclock.cpp:136
void manualTick(bool force=false, bool backward=false)
Equivalent of tick() for manual mode.
Definition simclock.cpp:112
QDateTime currentDateTimeUtc()
bool isValid() const const
bool registerObject(const QString &path, QObject *object, RegisterOptions options)
QDBusConnection sessionBus()
qint64 elapsed() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isActive() const const
void start()
void stop()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:11:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.