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

marble

  • sources
  • kde-4.12
  • 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 #if QT_VERSION < 0x040700
39  m_datetime( QDateTime::currentDateTime().toUTC() ),
40  m_lasttime( QDateTime::currentDateTime().toUTC() ),
41 #else
42  m_datetime( QDateTime::currentDateTimeUtc() ),
43  m_lasttime( QDateTime::currentDateTimeUtc() ),
44 #endif
45  m_timezoneInSec( 0 ),
46  m_updateInterval( 60 )
47 {
48  // nothing to do
49 }
50 
51 void MarbleClockPrivate::timerTimeout()
52 {
53  // calculate real period elapsed since last call
54 #if QT_VERSION < 0x040700
55  QDateTime curenttime( QDateTime::currentDateTime().toUTC() );
56  int msecdelta = 1000 * m_lasttime.secsTo( curenttime );
57 #else
58  QDateTime curenttime( QDateTime::currentDateTimeUtc() );
59  int msecdelta = m_lasttime.msecsTo( curenttime );
60 #endif
61  m_lasttime = curenttime;
62 
63  // update m_datetime at m_speed pace
64  m_datetime = m_datetime.addMSecs( msecdelta * m_speed );
65 
66  // trigger round minute update (at m_speed pace)
67  emit q->timeChanged();
68 
69  // sleeptime is the time to sleep until next round minute, at m_speed pace
70  int sleeptime = ( m_updateInterval * 1000 - (qreal)(m_datetime.time().msec() + m_datetime.time().second() * 1000 ) ) / m_speed;
71  if ( sleeptime < 1000 ) {
72  // don't trigger more often than 1s
73  sleeptime = 1000;
74  }
75  m_timer.start( sleeptime );
76 
77  //mDebug() << "MarbleClock: will sleep for " << sleeptime;
78 }
79 
80 MarbleClock::MarbleClock( QObject* parent )
81  : QObject( parent ), d( new MarbleClockPrivate( this ) )
82 
83 {
84  connect( &d->m_timer, SIGNAL(timeout()),
85  this, SLOT(timerTimeout()) );
86  d->timerTimeout();
87 }
88 
89 
90 MarbleClock::~MarbleClock()
91 {
92  delete d;
93 }
94 
95 qreal MarbleClock::dayFraction() const
96 {
97  qreal fraction = d->m_datetime.time().second();
98  fraction = fraction/60.0 + d->m_datetime.time().minute();
99  fraction = fraction/60.0 + d->m_datetime.time().hour();
100  fraction = fraction/24.0;
101  return fraction;
102 }
103 
104 void MarbleClock::setDateTime( const QDateTime& datetime )
105 {
106  d->m_datetime = datetime;
107  d->timerTimeout();
108 }
109 
110 QDateTime MarbleClock::dateTime() const
111 {
112  return d->m_datetime;
113 }
114 
115 void MarbleClock::setUpdateInterval( int seconds )
116 {
117  d->m_updateInterval = seconds;
118  emit updateIntervalChanged( seconds );
119 }
120 
121 int MarbleClock::updateInterval()
122 {
123  return d->m_updateInterval;
124 }
125 
126 int MarbleClock::speed() const
127 {
128  return d->m_speed;
129 }
130 
131 void MarbleClock::setSpeed( int speed )
132 {
133  d->m_speed = speed;
134  d->timerTimeout();
135 }
136 
137 int MarbleClock::timezone() const
138 {
139  return d->m_timezoneInSec;
140 }
141 
142 void MarbleClock::setTimezone( int timezoneInSec )
143 {
144  d->m_timezoneInSec = timezoneInSec;
145 }
146 
147 }
148 
149 #include "MarbleClock.moc"
Marble::MarbleClock::setDateTime
void setDateTime(const QDateTime &datetime)
Sets the internal date and time a custom one.
Definition: MarbleClock.cpp:104
Marble::MarbleClock::setUpdateInterval
void setUpdateInterval(int seconds)
Set the interval at which dateTime() is updated and timeChanged() is emitted.
Definition: MarbleClock.cpp:115
Marble::MarbleClock::timezone
int timezone() const
Returns the timezone of the clock.
Definition: MarbleClock.cpp:137
Marble::MarbleClock::updateInterval
int updateInterval()
Returns the interval at which dateTime() is updated and timeChanged() is emitted, The default is 60 s...
Definition: MarbleClock.cpp:121
QObject
MarbleDebug.h
Marble::MarbleClock::dateTime
QDateTime dateTime() const
Returns the internal date and time.
Definition: MarbleClock.cpp:110
Marble::MarbleClock::~MarbleClock
~MarbleClock()
Definition: MarbleClock.cpp:90
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:131
Marble::MarbleClock::dayFraction
qreal dayFraction() const
Determine how much of the current day has elapsed.
Definition: MarbleClock.cpp:95
Marble::MarbleClock::MarbleClock
MarbleClock(QObject *parent=0)
Definition: MarbleClock.cpp:80
Marble::MarbleClock::speed
int speed() const
Returns the speed of the timer.
Definition: MarbleClock.cpp:126
Marble::MarbleClock::setTimezone
void setTimezone(int timeInSec)
Sets the timezone of the clock.
Definition: MarbleClock.cpp:142
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:51 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
  • kstars
  • libkdeedu
  •   keduvocdocument
  • 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