1 | /* |
2 | This file is part of the KDE games library |
3 | Copyright (C) 2007 Mauricio Piacentini (mauricio@tabuleiro.com) |
4 | Portions reused from KGameLCDClock |
5 | Copyright (C) 2001,2002,2003 Nicolas Hadacek (hadacek@kde.org) |
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 | #include "kgameclock.moc" |
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, SIGNAL(timeout()), SLOT(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 | |
68 | QString KGameClock::timeString() const |
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 | |
78 | void KGameClock::showTime() |
79 | { |
80 | emit timeChanged(timeString()); |
81 | } |
82 | |
83 | void KGameClock::restart() |
84 | { |
85 | d->timerClock->stop(); |
86 | d->sec = 0; |
87 | d->min = 0; |
88 | d->hour = 0; |
89 | resume(); |
90 | showTime(); |
91 | } |
92 | |
93 | void KGameClock::resume() |
94 | { |
95 | d->timerClock->start(1000); // 1 second |
96 | } |
97 | |
98 | void KGameClock::pause() |
99 | { |
100 | d->timerClock->stop(); |
101 | } |
102 | |
103 | uint KGameClock::seconds() const |
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 | |
117 | void KGameClock::setTime(const QString &s) |
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 | } |
125 | |