File: kgameclock.cpp

package info (click to toggle)
libkdegames-kde4 4:14.12.3-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 36,372 kB
  • ctags: 2,337
  • sloc: cpp: 16,003; perl: 5,276; sh: 58; makefile: 3
file content (124 lines) | stat: -rw-r--r-- 3,252 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
    This file is part of the KDE games library
    Copyright (C) 2007 Mauricio Piacentini (mauricio@tabuleiro.com)
    Portions reused from KGameLCDClock
    Copyright (C) 2001,2002,2003 Nicolas Hadacek (hadacek@kde.org)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kgameclock.h"
#include "kgameclock.moc"

#include <QTimer>

class KGameClockPrivate
{
public:
    KGameClockPrivate()
        : sec(0), min(0), hour(0)
    {
    }

    QTimer *timerClock;
    uint    sec, min, hour;
    KGameClock::ClockType clocktype;
};

KGameClock::KGameClock(QObject *parent, KGameClock::ClockType clocktype)
: QObject(parent) , d(new KGameClockPrivate)
{
    d->clocktype = clocktype;
    d->timerClock = new QTimer(this);
    connect(d->timerClock, SIGNAL(timeout()), SLOT(timeoutClock()));
}

KGameClock::~KGameClock()
{
    delete d;
}

void KGameClock::timeoutClock()
{
    if ( d->hour==23 && d->min==59 && d->sec==59 ) return;
    d->sec++;
    if (d->sec==60) {
        d->min++;
        d->sec = 0;
    }
    if (d->min==60) {
        d->hour++;
        d->min = 0;
    }
    showTime();
}

QString KGameClock::timeString() const
{
    QString sec = QString::number(d->sec).rightJustified(2, QLatin1Char( '0' ), true);
    QString min = QString::number(d->min).rightJustified(2, QLatin1Char( '0' ), true);
    if (d->clocktype==MinSecOnly) return min + QLatin1Char( ':' ) + sec;
    //else return hour as well
    QString hour = QString::number(d->hour).rightJustified(2, QLatin1Char( '0' ), true);
    return hour + QLatin1Char( ':' ) + min + QLatin1Char( ':' ) + sec;
}

void KGameClock::showTime()
{
    emit timeChanged(timeString());
}

void KGameClock::restart()
{
    d->timerClock->stop();
    d->sec = 0;
    d->min = 0;
    d->hour = 0;
    resume();
    showTime();
}

void KGameClock::resume()
{
    d->timerClock->start(1000); // 1 second
}

void KGameClock::pause()
{
    d->timerClock->stop();
}

uint KGameClock::seconds() const
{
    return d->hour*3600 + d->min*60 + d->sec;
}

void KGameClock::setTime(uint sec)
{
    Q_ASSERT( sec<(3600*24) );
    d->sec = sec % 60;
    d->min = (sec / 60) % 60;
    d->hour = sec / 1440 ;
    showTime();
}

void KGameClock::setTime(const QString &s)
{
    Q_ASSERT( s.length()==8 && s[2]==QLatin1Char( ':' ) && s[5]==QLatin1Char( ':' ) );
    uint hour = qMin(s.section(QLatin1Char( ':' ), 0, 0).toUInt(), uint(23));
    uint min = qMin(s.section(QLatin1Char( ':' ), 1, 1).toUInt(), uint(59));
    uint sec = qMin(s.section(QLatin1Char( ':' ), 2, 2).toUInt(), uint(59));
    setTime(sec + min*60 + hour*3600);
}