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_ManualMode) //only tick if ManualMode is false
35 {
36 long mselapsed = m_SystemMark.elapsed();
37 if (mselapsed < m_LastElapsed)
38 {
39 // The sysmark timer has wrapped after 24 hours back to 0 ms.
40 // Reset sysmark and julianmark
41 m_JulianMark = m_UTC.djd();
42 m_SystemMark.start();
43 m_LastElapsed = 0;
44 }
45 else
46 {
47 m_LastElapsed = mselapsed;
48 }
49
50 long double scaledsec = static_cast<long double>(mselapsed) * static_cast<long double>(m_Scale) / 1000.0;
51 m_UTC.setDJD(m_JulianMark + scaledsec / (24. * 3600.));
52
53 // qDebug() << Q_FUNC_INFO << "tick() : JD = " << QLocale().toString( UTC.djd(), 7 ) <<
54 // " mselapsed = " << mselapsed << " scale = " << Scale <<
55 // " scaledsec = " << double(scaledsec);
56
58 }
59}
60
62{
63 if (on)
64 {
65 //Turn on manual ticking.
66 m_ManualActive = m_InternalTimer.isActive();
67 m_InternalTimer.stop();
68 }
69 else
70 {
71 //Turn off manual ticking. If the Manual clock was active, start the timer.
72 if (isActive())
73 {
74 m_SystemMark.start();
75 m_JulianMark = m_UTC.djd();
76 m_LastElapsed = 0;
77 m_InternalTimer.start(TimerInterval);
78 }
79 }
80 m_ManualMode = on;
81}
82
83void SimClock::manualTick(bool force, bool backward)
84{
85 if (force || (m_ManualMode && m_ManualActive))
86 {
87 //The single shot timer is needed because otherwise the animation is happening so frequently
88 //that the kstars interface becomes too unresponsive.
89 //QTimer::singleShot(1, [this,backward] { setUTC(UTC.addSecs(static_cast<long double>Scale * (backward ? -1 : 1))); });
90 setUTC(m_UTC.addSecs(static_cast<long double>(m_Scale) * (backward ? -1 : 1)));
91 }
92 else if (!m_ManualMode)
93 tick();
94}
95
97{
98 if (m_ManualMode)
99 return m_ManualActive;
100 else
101 return m_InternalTimer.isActive();
102}
103
105{
106 if (m_ManualMode && m_ManualActive)
107 {
108 m_ManualActive = false;
109 emit clockToggled(true);
110 }
111
112 if (!m_ManualMode && m_InternalTimer.isActive())
113 {
114 qCDebug(KSTARS) << "Stopping the timer";
115 m_InternalTimer.stop();
116 emit clockToggled(true);
117 }
118}
119
121{
122 if (m_ManualMode && !m_ManualActive)
123 {
124 m_ManualActive = true;
125 m_SystemMark.start();
126 m_JulianMark = m_UTC.djd();
127 m_LastElapsed = 0;
128 emit clockToggled(false);
129 //emit timeChanged() in order to restart calls to updateTime()
131 }
132 else if (!m_ManualMode && !m_InternalTimer.isActive())
133 {
134 qCDebug(KSTARS) << "Starting the timer";
135 m_SystemMark.start();
136 m_JulianMark = m_UTC.djd();
137 m_LastElapsed = 0;
138 m_InternalTimer.start(TimerInterval);
139 emit clockToggled(false);
140 }
141}
142
144{
145 //DEBUG
146 //qDebug() << Q_FUNC_INFO << newtime.toString();
147 //qDebug() << Q_FUNC_INFO << "is dateTime valid? " << newtime.isValid();
148
149 if (newtime.isValid())
150 {
151 m_UTC = newtime;
152 if (m_InternalTimer.isActive())
153 {
154 m_JulianMark = m_UTC.djd();
155 m_SystemMark.start();
156 m_LastElapsed = 0;
157 }
158
159 // N.B. Too much log spam when in manual mode
160 //qCInfo(KSTARS) << QString("Setting clock: UTC: %1 JD: %2").arg(UTC.toString(), QLocale().toString((double)UTC.djd(), 'f', 2));
162 }
163 else
164 {
165 qCWarning(KSTARS) << "Cannot set SimClock: Invalid Date/Time.";
166 }
167}
168
169void SimClock::setClockScale(double scale)
170{
171 if (m_Scale != scale)
172 {
173 qCInfo(KSTARS) << "New clock scale: " << scale << " sec";
175 m_Scale = scale;
176 if (m_InternalTimer.isActive())
177 {
178 m_JulianMark = m_UTC.djd();
179 m_SystemMark.start();
180 m_LastElapsed = 0;
181 }
182 }
183}
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() )
Q_INVOKABLE bool isActive()
Whether the clock is active or not is a bit complicated by the introduction of "manual mode".
Definition simclock.cpp:96
void setManualMode(bool on=true)
Sets Manual Mode on/off according to the bool argument.
Definition simclock.cpp:61
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:169
Q_SCRIPTABLE Q_NOREPLY void start()
DBUS function to start the SimClock.
Definition simclock.cpp:120
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:143
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:104
void manualTick(bool force=false, bool backward=false)
Equivalent of tick() for manual mode.
Definition simclock.cpp:83
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 Tue Mar 26 2024 11:19:04 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.