Kstars

simclock.h
1/*
2 SPDX-FileCopyrightText: 2002 Mark Hollomon <mhh@mindspring.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "kstarsdatetime.h"
10
11#include <QTime>
12#include <QElapsedTimer>
13#include <QTimer>
14#include <qtdbusglobal.h>
15
16/** @class SimClock
17 *@short kstars simulation clock
18 *@author Mark Hollomon
19 *@version 1.0
20 */
21
22class SimClock : public QObject
23{
25 Q_CLASSINFO("D-Bus Interface", "org.kde.kstars.SimClock")
26 public:
27 /**
28 * Constructor
29 * @param parent parent object for the clock
30 * @param when the date/time to which the SimClock should be initialized in UTC
31 */
32 explicit SimClock(QObject *parent = nullptr, const KStarsDateTime &when = KStarsDateTime::currentDateTimeUtc());
33
34 /** @return const reference to the current simulation Universal Time. */
35 const KStarsDateTime &utc() const
36 {
37 return m_UTC;
38 }
39
40 /** Whether the clock is active or not is a bit complicated by the
41 *introduction of "manual mode". In manual mode, SimClock's internal timer
42 *is stopped, because the clock is ticked manually when the current update
43 *has finished. So, if ManualMode is true, then isActive() checks
44 *whether ManualActive is true. Otherwise, it checks whether the timer is
45 *running.
46 *@returns true if the Simulation clock is actively running.
47 */
48 Q_INVOKABLE bool isActive();
49
50 /** @returns the current timestep setting */
51 double scale() const
52 {
53 if (m_RealTime)
54 return 1.0;
55 else
56 return m_Scale;
57 }
58
59 /** Manual Mode is a new (04/2002) addition to the SimClock. It is
60 *intended to be activated for large timesteps, when we want each frame
61 *drawn to the screen to be precisely Scale seconds later than the
62 *previous frame. (i.e., if the timescale is 1 year, then each successive
63 *frame should be 1 year later than the previous frame). ManualMode
64 *accomplishes this by stopping the internal timer and allowing the clock
65 *to be advanced manually (the manualTick() slot is called at the end of each
66 *KStars::updateTime()).
67 *@returns whether Manual Mode is active.
68 */
69 bool isManualMode() const
70 {
71 return m_ManualMode;
72 }
73
74 /**Sets Manual Mode on/off according to the bool argument. */
75 void setManualMode(bool on = true);
76
77 bool isRealTime() const
78 {
79 return m_RealTime;
80 }
81
82 public Q_SLOTS:
83#ifndef KSTARS_LITE
84 /** DBUS function to stop the SimClock. */
85 Q_SCRIPTABLE Q_NOREPLY void stop();
86
87 /** DBUS function to start the SimClock. */
88 Q_SCRIPTABLE Q_NOREPLY void start();
89
90 /** DBUS function to set the time of the SimClock. */
91 Q_SCRIPTABLE Q_NOREPLY void setUTC(const KStarsDateTime &newtime);
92
93 /** DBUS function to set the current time of the SimClock. */
94 Q_SCRIPTABLE Q_NOREPLY void setNow()
95 {
97 }
98
99 /** DBUS function to set scale of simclock. */
100 /**
101 * @brief setClockScale Set simulation clock scale per second.
102 * @param scale Scale per second. 1 means normal scale, each 1 simulation
103 * second equals 1 real second. Value less than one *slows* the simulation clock,
104 * while values over 1 speeds it up. A scale of 0.5 makes the simulation clock ticks
105 * once every 2 actual seconds (or half-ticks per one actual second),
106 * while a value of 60 make the simulation clock ticks 60 simulation seconds (1 simulation minute)
107 * for every 1 actual second.
108 */
109 Q_SCRIPTABLE Q_NOREPLY void setClockScale(double scale);
110#else
111 // Define non-DBUS versions of functions above for use within KStarsLite
112 /** Function to stop the SimClock. */
113 void stop();
114
115 /** Function to start the SimClock. */
116 void start();
117
118 /** Function to set the time of the SimClock. */
119 void setUTC(const KStarsDateTime &newtime);
120
121 /** Function to set scale of simclock. */
122 void setClockScale(float s);
123#endif
124
125 /** Respond to the QTimer::timeout signal */
126 void tick();
127
128 /** Equivalent of tick() for manual mode.
129 * If ManualActive is true, add Scale seconds to the SimClock time.
130 * (we may want to modify this slightly...e.g., the number of seconds in a
131 * year is not constant (leap years), so it is better to increment the
132 * year, instead of adding 31 million seconds.
133 * set backward to true to reverse sign of Scale value
134 */
135
136 void manualTick(bool force = false, bool backward = false);
137
138 /**
139 * Realtime mode will lock SimClock with system clock. This should prevent
140 * issues that occur when system goes to sleep and clock stops ticking.
141 */
142 void setRealTime(bool on = true);
143
144 signals:
145 /** The time has changed (emitted by setUTC() ) */
147
148 /** The clock has ticked (emitted by tick() )*/
150
151 /** The timestep has changed*/
152 void scaleChanged(float);
153
154 /** This is an signal that is called on either clock start or
155 clock stop with an appropriate boolean argument. Required so
156 that we can bind it to KToggleAction::slotToggled(bool) */
157 void clockToggled(bool);
158
159 /** Emitted when realtime clock is toggled */
160 void realtimeToogled(bool);
161
162 private:
163 long double m_JulianMark { 0 };
164 KStarsDateTime m_UTC;
165 QTimer m_InternalTimer;
166 double m_Scale { 1 };
167 QElapsedTimer m_SystemMark;
168 int m_LastElapsed { 0 };
169 bool m_ManualMode { false };
170 bool m_ManualActive { false };
171 bool m_RealTime { false };
172
173 // used to generate names for dcop interfaces
174 //static int idgen;
175 // how often to update
176 static int TimerInterval;
177
178 // Disallow copying
179 SimClock(const SimClock &);
180 SimClock &operator=(const SimClock &);
181};
Extension of QDateTime for KStars KStarsDateTime can represent the date/time as a Julian Day,...
static KStarsDateTime currentDateTimeUtc()
kstars simulation clock
Definition simclock.h:23
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 setNow()
DBUS function to set the current time of the SimClock.
Definition simclock.h:94
Q_SCRIPTABLE Q_NOREPLY void setClockScale(double scale)
DBUS function to set scale of simclock.
Definition simclock.cpp:210
const KStarsDateTime & utc() const
Definition simclock.h:35
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
bool isManualMode() const
Manual Mode is a new (04/2002) addition to the SimClock.
Definition simclock.h:69
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
Q_CLASSINFO(Name, Value)
Q_INVOKABLEQ_INVOKABLE
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:59:52 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.