Kstars

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

KDE's Doxygen guidelines are available online.