• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

libkdegames

  • sources
  • kde-4.14
  • kdegames
  • libkdegames
kgameclock.cpp
Go to the documentation of this file.
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 }
KGameClock::KGameClock
KGameClock(QObject *parent=0, ClockType clocktype=HourMinSec)
Definition: kgameclock.cpp:40
KGameClock::timeChanged
void timeChanged(const QString &)
KGameClock::restart
virtual void restart()
Reset the clock and start again from zero.
Definition: kgameclock.cpp:83
kgameclock.h
KGameClock::ClockType
ClockType
Definition: kgameclock.h:39
KGameClock::seconds
uint seconds() const
Definition: kgameclock.cpp:103
QString::number
QString number(int n, int base)
KGameClock::resume
virtual void resume()
Resume counting time from the current position.
Definition: kgameclock.cpp:93
QTimer
QString::rightJustified
QString rightJustified(int width, QChar fill, bool truncate) const
KGameClock::MinSecOnly
Definition: kgameclock.h:39
QObject
KGameClock::timeoutClock
virtual void timeoutClock()
Definition: kgameclock.cpp:53
QString
KGameClock::~KGameClock
virtual ~KGameClock()
Definition: kgameclock.cpp:48
KGameClock::pause
virtual void pause()
Pause the clock.
Definition: kgameclock.cpp:98
QLatin1Char
KGameClock::showTime
void showTime()
Refresh.
Definition: kgameclock.cpp:78
QString::length
int length() const
QString::section
QString section(QChar sep, int start, int end, QFlags< QString::SectionFlag > flags) const
KGameClock::setTime
void setTime(uint seconds)
Set the time.
Definition: kgameclock.cpp:108
KGameClock::timeString
QString timeString() const
Definition: kgameclock.cpp:68
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString::toUInt
uint toUInt(bool *ok, int base) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:42 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdegames

Skip menu "libkdegames"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal