• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
MarbleClock.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2007-2008 David Roberts <dvdr18@gmail.com>
9 // Copyright 2010 Harshit Jain <hjain.itbhu@gmail.com>
10 //
11 
12 #include "MarbleClock.h"
13 #include "MarbleDebug.h"
14 
15 #include <QTimer>
16 
17 namespace Marble {
18 
19 class MarbleClockPrivate
20 {
21 public:
22  MarbleClock* q;
23  int m_speed;
24  QTimer m_timer;
25  QDateTime m_datetime; // stores the UTC time
26  QDateTime m_lasttime;
27  int m_timezoneInSec;
28  int m_updateInterval;
29 
30  explicit MarbleClockPrivate( MarbleClock* parent );
31 
32  void timerTimeout();
33 };
34 
35 MarbleClockPrivate::MarbleClockPrivate( MarbleClock* parent ) :
36  q( parent ),
37  m_speed( 1 ),
38  m_datetime( QDateTime::currentDateTimeUtc() ),
39  m_lasttime( QDateTime::currentDateTimeUtc() ),
40  m_timezoneInSec( 0 ),
41  m_updateInterval( 60 )
42 {
43  // nothing to do
44 }
45 
46 void MarbleClockPrivate::timerTimeout()
47 {
48  // calculate real period elapsed since last call
49  QDateTime curenttime( QDateTime::currentDateTimeUtc() );
50  int msecdelta = m_lasttime.msecsTo( curenttime );
51  m_lasttime = curenttime;
52 
53  // update m_datetime at m_speed pace
54  m_datetime = m_datetime.addMSecs( msecdelta * m_speed );
55 
56  // trigger round minute update (at m_speed pace)
57  emit q->timeChanged();
58 
59  // sleeptime is the time to sleep until next round minute, at m_speed pace
60  int sleeptime = ( m_updateInterval * 1000 - (qreal)(m_datetime.time().msec() + m_datetime.time().second() * 1000 ) ) / m_speed;
61  if ( sleeptime < 1000 ) {
62  // don't trigger more often than 1s
63  sleeptime = 1000;
64  }
65  m_timer.start( sleeptime );
66 
67  //mDebug() << "MarbleClock: will sleep for " << sleeptime;
68 }
69 
70 MarbleClock::MarbleClock( QObject* parent )
71  : QObject( parent ), d( new MarbleClockPrivate( this ) )
72 
73 {
74  connect( &d->m_timer, SIGNAL(timeout()),
75  this, SLOT(timerTimeout()) );
76  d->timerTimeout();
77 }
78 
79 
80 MarbleClock::~MarbleClock()
81 {
82  delete d;
83 }
84 
85 qreal MarbleClock::dayFraction() const
86 {
87  qreal fraction = d->m_datetime.time().second();
88  fraction = fraction/60.0 + d->m_datetime.time().minute();
89  fraction = fraction/60.0 + d->m_datetime.time().hour();
90  fraction = fraction/24.0;
91  return fraction;
92 }
93 
94 void MarbleClock::setDateTime( const QDateTime& datetime )
95 {
96  d->m_datetime = datetime;
97  d->timerTimeout();
98 }
99 
100 QDateTime MarbleClock::dateTime() const
101 {
102  return d->m_datetime;
103 }
104 
105 void MarbleClock::setUpdateInterval( int seconds )
106 {
107  d->m_updateInterval = seconds;
108  emit updateIntervalChanged( seconds );
109 }
110 
111 int MarbleClock::updateInterval() const
112 {
113  return d->m_updateInterval;
114 }
115 
116 int MarbleClock::speed() const
117 {
118  return d->m_speed;
119 }
120 
121 void MarbleClock::setSpeed( int speed )
122 {
123  d->m_speed = speed;
124  d->timerTimeout();
125 }
126 
127 int MarbleClock::timezone() const
128 {
129  return d->m_timezoneInSec;
130 }
131 
132 void MarbleClock::setTimezone( int timezoneInSec )
133 {
134  d->m_timezoneInSec = timezoneInSec;
135 }
136 
137 }
138 
139 #include "MarbleClock.moc"
Marble::MarbleClock::setDateTime
void setDateTime(const QDateTime &datetime)
Sets the internal date and time a custom one.
Definition: MarbleClock.cpp:94
Marble::MarbleClock::setUpdateInterval
void setUpdateInterval(int seconds)
Set the interval at which dateTime() is updated and timeChanged() is emitted.
Definition: MarbleClock.cpp:105
Marble::MarbleClock::timezone
int timezone() const
Returns the timezone of the clock.
Definition: MarbleClock.cpp:127
MarbleDebug.h
Marble::MarbleClock::dateTime
QDateTime dateTime() const
Returns the internal date and time.
Definition: MarbleClock.cpp:100
Marble::MarbleClock::~MarbleClock
~MarbleClock()
Definition: MarbleClock.cpp:80
QTimer
QObject
Marble::MarbleClock::updateIntervalChanged
void updateIntervalChanged(int seconds)
Emitted when setUpdateInterval() is called.
MarbleClock.h
Marble::MarbleClock::setSpeed
void setSpeed(int speed)
Sets the speed of the timer which is how fast the marble clock can run relative to actual speed of ti...
Definition: MarbleClock.cpp:121
Marble::MarbleClock::dayFraction
qreal dayFraction() const
Determine how much of the current day has elapsed.
Definition: MarbleClock.cpp:85
Marble::MarbleClock::MarbleClock
MarbleClock(QObject *parent=0)
Definition: MarbleClock.cpp:70
Marble::MarbleClock::speed
int speed() const
Returns the speed of the timer.
Definition: MarbleClock.cpp:116
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Marble::MarbleClock::updateInterval
int updateInterval() const
Returns the interval at which dateTime() is updated and timeChanged() is emitted, The default is 60 s...
Definition: MarbleClock.cpp:111
QDateTime::currentDateTimeUtc
QDateTime currentDateTimeUtc()
Marble::MarbleClock::setTimezone
void setTimezone(int timeInSec)
Sets the timezone of the clock.
Definition: MarbleClock.cpp:132
QDateTime
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal