KDEGames

kgameclock.cpp
1/*
2 This file is part of the KDE games library
3 SPDX-FileCopyrightText: 2007 Mauricio Piacentini <mauricio@tabuleiro.com>
4 Portions reused from KGameLCDClock
5 SPDX-FileCopyrightText: 2001, 2002, 2003 Nicolas Hadacek <hadacek@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-only
8*/
9
10#include "kgameclock.h"
11
12// Qt
13#include <QString>
14#include <QTimer>
15
16class KGameClockPrivate
17{
18public:
19 KGameClockPrivate() = default;
20
21public:
22 QTimer *timerClock;
23 uint totalSeconds = 0;
24 KGameClock::ClockType clocktype;
25};
26
27namespace
28{
29QString timeSectionString(uint x)
30{
31 return QString::number(x).rightJustified(2, QLatin1Char('0'), true);
32}
33
34QString timeStringHourMinSec(uint hour, uint min, uint sec)
35{
36 return timeSectionString(hour) + QLatin1Char(':') + timeSectionString(min) + QLatin1Char(':') + timeSectionString(sec);
37}
38
39QString timeStringMinSec(uint min, uint sec)
40{
41 return timeSectionString(min) + QLatin1Char(':') + timeSectionString(sec);
42}
43
44} // namespace
45
47 : QObject(parent)
48 , d_ptr(new KGameClockPrivate)
49{
51
52 d->clocktype = clocktype;
53 d->timerClock = new QTimer(this);
54 connect(d->timerClock, &QTimer::timeout, this, &KGameClock::timeoutClock);
55}
56
57KGameClock::~KGameClock() = default;
58
59void KGameClock::timeoutClock()
60{
62
63 d->totalSeconds++;
64 showTime();
65}
66
68{
69 Q_D(const KGameClock);
70
71 uint sec = d->totalSeconds;
72 if (d->clocktype == MinSecOnly) {
73 return timeStringMinSec((sec / 60) % 60, sec % 60);
74 }
75 if (d->clocktype == FlexibleHourMinSec) {
76 if (sec < 3600)
77 return timeStringMinSec(sec / 60, sec % 60);
78 return timeStringHourMinSec(sec / 3600, (sec / 60) % 60, sec % 60);
79 }
80 if (d->clocktype == LongMinSec) {
81 return timeStringMinSec(sec / 60, sec % 60);
82 }
83 // default is HourMinSec
84 return timeStringHourMinSec(sec / 3600, (sec / 60) % 60, sec % 60);
85}
86
88{
89 Q_EMIT timeChanged(timeString());
90}
91
93{
95
96 d->timerClock->stop();
97 d->totalSeconds = 0;
98 resume();
99 showTime();
100}
101
103{
105
106 d->timerClock->start(1000); // 1 second
107}
108
110{
112
113 d->timerClock->stop();
114}
115
117{
118 Q_D(const KGameClock);
119
120 return d->totalSeconds;
121}
122
124{
126
127 d->totalSeconds = sec;
128 showTime();
129}
130
132{
133 const QList<QStringView> sections = QStringView(s).split(QLatin1Char(':'));
134 Q_ASSERT(sections.size() <= 3);
135 uint sec = 0;
136 for (const QStringView &x : sections) {
137 sec = sec * 60 + x.toUInt();
138 }
139 setTime(sec);
140}
141
142#include "moc_kgameclock.cpp"
Class representing a game clock.
Definition kgameclock.h:28
KGameClock(QObject *parent=nullptr, ClockType clocktype=HourMinSec)
virtual void resume()
Resume counting time from the current position.
void setTime(uint seconds)
Set the time.
void showTime()
Refresh.
ClockType
Controls the format of return value of timeString()
Definition kgameclock.h:41
@ FlexibleHourMinSec
Definition kgameclock.h:44
virtual void restart()
Reset the clock and start again from zero.
uint seconds() const
QString timeString() const
virtual void pause()
Pause the clock.
qsizetype size() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString number(double n, char format, int precision)
QString rightJustified(qsizetype width, QChar fill, bool truncate) const const
QList< QStringView > split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
void timeout()
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.