File indexing completed on 2024-04-28 04:05:02

0001 /*
0002     This file is part of the KDE games library
0003     SPDX-FileCopyrightText: 2007 Mauricio Piacentini <mauricio@tabuleiro.com>
0004     Portions reused from KGameLCDClock
0005     SPDX-FileCopyrightText: 2001, 2002, 2003 Nicolas Hadacek <hadacek@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-only
0008 */
0009 
0010 #include "kgameclock.h"
0011 
0012 // Qt
0013 #include <QString>
0014 #include <QTimer>
0015 
0016 class KGameClockPrivate
0017 {
0018 public:
0019     KGameClockPrivate() = default;
0020 
0021 public:
0022     QTimer *timerClock;
0023     uint totalSeconds = 0;
0024     KGameClock::ClockType clocktype;
0025 };
0026 
0027 namespace
0028 {
0029 QString timeSectionString(uint x)
0030 {
0031     return QString::number(x).rightJustified(2, QLatin1Char('0'), true);
0032 }
0033 
0034 QString timeStringHourMinSec(uint hour, uint min, uint sec)
0035 {
0036     return timeSectionString(hour) + QLatin1Char(':') + timeSectionString(min) + QLatin1Char(':') + timeSectionString(sec);
0037 }
0038 
0039 QString timeStringMinSec(uint min, uint sec)
0040 {
0041     return timeSectionString(min) + QLatin1Char(':') + timeSectionString(sec);
0042 }
0043 
0044 } // namespace
0045 
0046 KGameClock::KGameClock(QObject *parent, KGameClock::ClockType clocktype)
0047     : QObject(parent)
0048     , d_ptr(new KGameClockPrivate)
0049 {
0050     Q_D(KGameClock);
0051 
0052     d->clocktype = clocktype;
0053     d->timerClock = new QTimer(this);
0054     connect(d->timerClock, &QTimer::timeout, this, &KGameClock::timeoutClock);
0055 }
0056 
0057 KGameClock::~KGameClock() = default;
0058 
0059 void KGameClock::timeoutClock()
0060 {
0061     Q_D(KGameClock);
0062 
0063     d->totalSeconds++;
0064     showTime();
0065 }
0066 
0067 QString KGameClock::timeString() const
0068 {
0069     Q_D(const KGameClock);
0070 
0071     uint sec = d->totalSeconds;
0072     if (d->clocktype == MinSecOnly) {
0073         return timeStringMinSec((sec / 60) % 60, sec % 60);
0074     }
0075     if (d->clocktype == FlexibleHourMinSec) {
0076         if (sec < 3600)
0077             return timeStringMinSec(sec / 60, sec % 60);
0078         return timeStringHourMinSec(sec / 3600, (sec / 60) % 60, sec % 60);
0079     }
0080     if (d->clocktype == LongMinSec) {
0081         return timeStringMinSec(sec / 60, sec % 60);
0082     }
0083     // default is HourMinSec
0084     return timeStringHourMinSec(sec / 3600, (sec / 60) % 60, sec % 60);
0085 }
0086 
0087 void KGameClock::showTime()
0088 {
0089     Q_EMIT timeChanged(timeString());
0090 }
0091 
0092 void KGameClock::restart()
0093 {
0094     Q_D(KGameClock);
0095 
0096     d->timerClock->stop();
0097     d->totalSeconds = 0;
0098     resume();
0099     showTime();
0100 }
0101 
0102 void KGameClock::resume()
0103 {
0104     Q_D(KGameClock);
0105 
0106     d->timerClock->start(1000); // 1 second
0107 }
0108 
0109 void KGameClock::pause()
0110 {
0111     Q_D(KGameClock);
0112 
0113     d->timerClock->stop();
0114 }
0115 
0116 uint KGameClock::seconds() const
0117 {
0118     Q_D(const KGameClock);
0119 
0120     return d->totalSeconds;
0121 }
0122 
0123 void KGameClock::setTime(uint sec)
0124 {
0125     Q_D(KGameClock);
0126 
0127     d->totalSeconds = sec;
0128     showTime();
0129 }
0130 
0131 void KGameClock::setTime(const QString &s)
0132 {
0133     const QList<QStringView> sections = QStringView(s).split(QLatin1Char(':'));
0134     Q_ASSERT(sections.size() <= 3);
0135     uint sec = 0;
0136     for (const QStringView &x : sections) {
0137         sec = sec * 60 + x.toUInt();
0138     }
0139     setTime(sec);
0140 }
0141 
0142 #include "moc_kgameclock.cpp"