KDEGames

kgameclock.cpp
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2007 Mauricio Piacentini ([email protected])
4  Portions reused from KGameLCDClock
5  Copyright (C) 2001,2002,2003 Nicolas Hadacek ([email protected])
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License version 2 as published by the Free Software Foundation.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "kgameclock.h"
23 
24 
25 #include <QTimer>
26 
27 class KGameClockPrivate
28 {
29 public:
30  KGameClockPrivate()
31  : sec(0), min(0), hour(0)
32  {
33  }
34 
35  QTimer *timerClock;
36  uint sec, min, hour;
37  KGameClock::ClockType clocktype;
38 };
39 
40 KGameClock::KGameClock(QObject *parent, KGameClock::ClockType clocktype)
41 : QObject(parent) , d(new KGameClockPrivate)
42 {
43  d->clocktype = clocktype;
44  d->timerClock = new QTimer(this);
45  connect(d->timerClock, &QTimer::timeout, this, &KGameClock::timeoutClock);
46 }
47 
48 KGameClock::~KGameClock()
49 {
50  delete d;
51 }
52 
53 void KGameClock::timeoutClock()
54 {
55  if ( d->hour==23 && d->min==59 && d->sec==59 ) return;
56  d->sec++;
57  if (d->sec==60) {
58  d->min++;
59  d->sec = 0;
60  }
61  if (d->min==60) {
62  d->hour++;
63  d->min = 0;
64  }
65  showTime();
66 }
67 
69 {
70  QString sec = QString::number(d->sec).rightJustified(2, QLatin1Char( '0' ), true);
71  QString min = QString::number(d->min).rightJustified(2, QLatin1Char( '0' ), true);
72  if (d->clocktype==MinSecOnly) return min + QLatin1Char( ':' ) + sec;
73  //else return hour as well
74  QString hour = QString::number(d->hour).rightJustified(2, QLatin1Char( '0' ), true);
75  return hour + QLatin1Char( ':' ) + min + QLatin1Char( ':' ) + sec;
76 }
77 
79 {
80  Q_EMIT timeChanged(timeString());
81 }
82 
84 {
85  d->timerClock->stop();
86  d->sec = 0;
87  d->min = 0;
88  d->hour = 0;
89  resume();
90  showTime();
91 }
92 
94 {
95  d->timerClock->start(1000); // 1 second
96 }
97 
99 {
100  d->timerClock->stop();
101 }
102 
104 {
105  return d->hour*3600 + d->min*60 + d->sec;
106 }
107 
108 void KGameClock::setTime(uint sec)
109 {
110  Q_ASSERT( sec<(3600*24) );
111  d->sec = sec % 60;
112  d->min = (sec / 60) % 60;
113  d->hour = sec / 1440 ;
114  showTime();
115 }
116 
118 {
119  Q_ASSERT( s.length()==8 && s[2]==QLatin1Char( ':' ) && s[5]==QLatin1Char( ':' ) );
120  uint hour = qMin(s.section(QLatin1Char( ':' ), 0, 0).toUInt(), uint(23));
121  uint min = qMin(s.section(QLatin1Char( ':' ), 1, 1).toUInt(), uint(59));
122  uint sec = qMin(s.section(QLatin1Char( ':' ), 2, 2).toUInt(), uint(59));
123  setTime(sec + min*60 + hour*3600);
124 }
virtual void restart()
Reset the clock and start again from zero.
Definition: kgameclock.cpp:83
uint seconds() const
Definition: kgameclock.cpp:103
void timeout()
QString number(int n, int base)
virtual void resume()
Resume counting time from the current position.
Definition: kgameclock.cpp:93
QString rightJustified(int width, QChar fill, bool truncate) const const
virtual void pause()
Pause the clock.
Definition: kgameclock.cpp:98
void showTime()
Refresh.
Definition: kgameclock.cpp:78
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:108
KGameClock(QObject *parent=nullptr, ClockType clocktype=HourMinSec)
Definition: kgameclock.cpp:40
QString timeString() const
Definition: kgameclock.cpp:68
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-2020 The KDE developers.
Generated on Thu Nov 26 2020 22:36:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.