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
15class MarbleClockPrivate
16{
17public:
18 MarbleClock* q;
19 int m_speed;
20 QTimer m_timer;
21 QDateTime m_datetime; // stores the UTC time
22 QDateTime m_lasttime;
23 int m_timezoneInSec;
24 int m_updateInterval;
25
26 explicit MarbleClockPrivate( MarbleClock* parent );
27
28 void timerTimeout();
29};
30
31MarbleClockPrivate::MarbleClockPrivate( MarbleClock* parent ) :
32 q( parent ),
33 m_speed( 1 ),
34 m_datetime( QDateTime::currentDateTimeUtc() ),
35 m_lasttime( QDateTime::currentDateTimeUtc() ),
36 m_timezoneInSec( 0 ),
37 m_updateInterval( 60 )
38{
39 // nothing to do
40}
41
42void MarbleClockPrivate::timerTimeout()
43{
44 // calculate real period elapsed since last call
46 int msecdelta = m_lasttime.msecsTo( curenttime );
47 m_lasttime = curenttime;
48
49 // update m_datetime at m_speed pace
50 m_datetime = m_datetime.addMSecs( msecdelta * m_speed );
51
52 // trigger round minute update (at m_speed pace)
53 emit q->timeChanged();
54
55 // sleeptime is the time to sleep until next round minute, at m_speed pace
56 int sleeptime = ( m_updateInterval * 1000 - (qreal)(m_datetime.time().msec() + m_datetime.time().second() * 1000 ) ) / m_speed;
57 if ( sleeptime < 1000 ) {
58 // don't trigger more often than 1s
59 sleeptime = 1000;
60 }
61 m_timer.start( sleeptime );
62
63 //mDebug() << "MarbleClock: will sleep for " << sleeptime;
64}
65
66MarbleClock::MarbleClock( QObject* parent )
67 : QObject( parent ), d( new MarbleClockPrivate( this ) )
68
69{
70 connect( &d->m_timer, SIGNAL(timeout()),
71 this, SLOT(timerTimeout()) );
72 d->timerTimeout();
73}
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 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 Tue Mar 26 2024 11:18:17 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.