KDEGames

kgameclock.cpp
1 /*
2  This file is part of the KDE games library
3  SPDX-FileCopyrightText: 2007 Mauricio Piacentini <[email protected]>
4  Portions reused from KGameLCDClock
5  SPDX-FileCopyrightText: 2001, 2002, 2003 Nicolas Hadacek <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-only
8 */
9 
10 #include "kgameclock.h"
11 
12 // Qt
13 #include <QTimer>
14 
15 class KGameClockPrivate
16 {
17 public:
18  KGameClockPrivate()
19  : sec(0), min(0), hour(0)
20  {
21  }
22 
23  QTimer *timerClock;
24  uint sec, min, hour;
25  KGameClock::ClockType clocktype;
26 };
27 
28 KGameClock::KGameClock(QObject *parent, KGameClock::ClockType clocktype)
29 : QObject(parent) , d(new KGameClockPrivate)
30 {
31  d->clocktype = clocktype;
32  d->timerClock = new QTimer(this);
33  connect(d->timerClock, &QTimer::timeout, this, &KGameClock::timeoutClock);
34 }
35 
36 KGameClock::~KGameClock() = default;
37 
38 void KGameClock::timeoutClock()
39 {
40  if ( d->hour==23 && d->min==59 && d->sec==59 ) return;
41  d->sec++;
42  if (d->sec==60) {
43  d->min++;
44  d->sec = 0;
45  }
46  if (d->min==60) {
47  d->hour++;
48  d->min = 0;
49  }
50  showTime();
51 }
52 
54 {
55  QString sec = QString::number(d->sec).rightJustified(2, QLatin1Char( '0' ), true);
56  QString min = QString::number(d->min).rightJustified(2, QLatin1Char( '0' ), true);
57  if (d->clocktype==MinSecOnly) return min + QLatin1Char( ':' ) + sec;
58  //else return hour as well
59  QString hour = QString::number(d->hour).rightJustified(2, QLatin1Char( '0' ), true);
60  return hour + QLatin1Char( ':' ) + min + QLatin1Char( ':' ) + sec;
61 }
62 
64 {
65  Q_EMIT timeChanged(timeString());
66 }
67 
69 {
70  d->timerClock->stop();
71  d->sec = 0;
72  d->min = 0;
73  d->hour = 0;
74  resume();
75  showTime();
76 }
77 
79 {
80  d->timerClock->start(1000); // 1 second
81 }
82 
84 {
85  d->timerClock->stop();
86 }
87 
88 uint KGameClock::seconds() const
89 {
90  return d->hour*3600 + d->min*60 + d->sec;
91 }
92 
93 void KGameClock::setTime(uint sec)
94 {
95  Q_ASSERT( sec<(3600*24) );
96  d->sec = sec % 60;
97  d->min = (sec / 60) % 60;
98  d->hour = sec / 1440 ;
99  showTime();
100 }
101 
103 {
104  Q_ASSERT( s.length()==8 && s[2]==QLatin1Char( ':' ) && s[5]==QLatin1Char( ':' ) );
105  uint hour = qMin(s.section(QLatin1Char( ':' ), 0, 0).toUInt(), uint(23));
106  uint min = qMin(s.section(QLatin1Char( ':' ), 1, 1).toUInt(), uint(59));
107  uint sec = qMin(s.section(QLatin1Char( ':' ), 2, 2).toUInt(), uint(59));
108  setTime(sec + min*60 + hour*3600);
109 }
virtual void restart()
Reset the clock and start again from zero.
Definition: kgameclock.cpp:68
uint seconds() const
Definition: kgameclock.cpp:88
void timeout()
QString number(int n, int base)
virtual void resume()
Resume counting time from the current position.
Definition: kgameclock.cpp:78
QString rightJustified(int width, QChar fill, bool truncate) const const
virtual void pause()
Pause the clock.
Definition: kgameclock.cpp:83
void showTime()
Refresh.
Definition: kgameclock.cpp:63
int length() const const
QString section(QChar sep, int start, int end, QString::SectionFlags flags) const const
void setTime(uint seconds)
Set the time.
Definition: kgameclock.cpp:93
KGameClock(QObject *parent=nullptr, ClockType clocktype=HourMinSec)
Definition: kgameclock.cpp:28
QString timeString() const
Definition: kgameclock.cpp:53
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
uint toUInt(bool *ok, int base) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Dec 7 2021 22:34:14 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.