Marble

MarbleClock.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2007-2008 David Roberts <dvdr18@gmail.com>
4// SPDX-FileCopyrightText: 2010 Harshit Jain <hjain.itbhu@gmail.com>
5//
6
7#include "MarbleClock.h"
8#include "MarbleDebug.h"
9
10#include <QDateTime>
11#include <QTimer>
12
13namespace Marble
14{
15
16class MarbleClockPrivate
17{
18public:
19 MarbleClock *q;
20 int m_speed;
21 QTimer m_timer;
22 QDateTime m_datetime; // stores the UTC time
23 QDateTime m_lasttime;
24 int m_timezoneInSec;
25 int m_updateInterval;
26
27 explicit MarbleClockPrivate(MarbleClock *parent);
28
29 void timerTimeout();
30};
31
32MarbleClockPrivate::MarbleClockPrivate(MarbleClock *parent)
33 : q(parent)
34 , m_speed(1)
35 , m_datetime(QDateTime::currentDateTimeUtc())
36 , m_lasttime(QDateTime::currentDateTimeUtc())
37 , m_timezoneInSec(0)
38 , m_updateInterval(60)
39{
40 // nothing to do
41}
42
43void MarbleClockPrivate::timerTimeout()
44{
45 // calculate real period elapsed since last call
47 int msecdelta = m_lasttime.msecsTo(curenttime);
48 m_lasttime = curenttime;
49
50 // update m_datetime at m_speed pace
51 m_datetime = m_datetime.addMSecs(msecdelta * m_speed);
52
53 // trigger round minute update (at m_speed pace)
54 Q_EMIT q->timeChanged();
55
56 // sleeptime is the time to sleep until next round minute, at m_speed pace
57 int sleeptime = (m_updateInterval * 1000 - (qreal)(m_datetime.time().msec() + m_datetime.time().second() * 1000)) / m_speed;
58 if (sleeptime < 1000) {
59 // don't trigger more often than 1s
60 sleeptime = 1000;
61 }
62 m_timer.start(sleeptime);
63
64 // mDebug() << "MarbleClock: will sleep for " << sleeptime;
65}
66
67MarbleClock::MarbleClock(QObject *parent)
68 : QObject(parent)
69 , d(new MarbleClockPrivate(this))
70
71{
72 connect(&d->m_timer, SIGNAL(timeout()), this, SLOT(timerTimeout()));
73 d->timerTimeout();
74}
75
76MarbleClock::~MarbleClock()
77{
78 delete d;
79}
80
81qreal MarbleClock::dayFraction() const
82{
83 qreal fraction = d->m_datetime.time().second();
84 fraction = fraction / 60.0 + d->m_datetime.time().minute();
85 fraction = fraction / 60.0 + d->m_datetime.time().hour();
86 fraction = fraction / 24.0;
87 return fraction;
88}
89
90void MarbleClock::setDateTime(const QDateTime &datetime)
91{
92 d->m_datetime = datetime;
93 d->timerTimeout();
94}
95
96QDateTime MarbleClock::dateTime() const
97{
98 return d->m_datetime;
99}
100
101void MarbleClock::setUpdateInterval(int seconds)
102{
103 d->m_updateInterval = seconds;
104 Q_EMIT updateIntervalChanged(seconds);
105}
106
107int MarbleClock::updateInterval() const
108{
109 return d->m_updateInterval;
110}
111
112int MarbleClock::speed() const
113{
114 return d->m_speed;
115}
116
117void MarbleClock::setSpeed(int speed)
118{
119 d->m_speed = speed;
120 d->timerTimeout();
121}
122
123int MarbleClock::timezone() const
124{
125 return d->m_timezoneInSec;
126}
127
128void MarbleClock::setTimezone(int timezoneInSec)
129{
130 d->m_timezoneInSec = timezoneInSec;
131}
132
133}
134
135#include "moc_MarbleClock.cpp"
Binds a QML item to a specific geodetic location in screen coordinates.
QDateTime currentDateTimeUtc()
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 12:02:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.