• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkdegames

kgameclock.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the KDE games library
00003     Copyright (C) 2007 Mauricio Piacentini (mauricio@tabuleiro.com)
00004     Portions reused from KGameLCDClock
00005     Copyright (C) 2001,2002,2003 Nicolas Hadacek (hadacek@kde.org)
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License version 2 as published by the Free Software Foundation.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "kgameclock.h"
00023 #include "kgameclock.moc"
00024 
00025 #include <QTimer>
00026 
00027 class KGameClockPrivate
00028 {
00029 public:
00030     KGameClockPrivate()
00031         : sec(0), min(0), hour(0)
00032     {
00033     }
00034 
00035     QTimer *timerClock;
00036     uint    sec, min, hour;
00037     KGameClock::ClockType clocktype;
00038 };
00039 
00040 KGameClock::KGameClock(QObject *parent, KGameClock::ClockType clocktype)
00041 : QObject(parent) , d(new KGameClockPrivate)
00042 {
00043     d->clocktype = clocktype;
00044     d->timerClock = new QTimer(this);
00045     connect(d->timerClock, SIGNAL(timeout()), SLOT(timeoutClock()));
00046 }
00047 
00048 KGameClock::~KGameClock()
00049 {
00050     delete d;
00051 }
00052 
00053 void KGameClock::timeoutClock()
00054 {
00055     if ( d->hour==23 && d->min==59 && d->sec==59 ) return;
00056     d->sec++;
00057     if (d->sec==60) {
00058         d->min++;
00059         d->sec = 0;
00060     }
00061     if (d->min==60) {
00062         d->hour++;
00063         d->min = 0;
00064     }
00065     showTime();
00066 }
00067 
00068 QString KGameClock::timeString() const
00069 {
00070     QString sec = QString::number(d->sec).rightJustified(2, '0', true);
00071     QString min = QString::number(d->min).rightJustified(2, '0', true);
00072     if (d->clocktype==MinSecOnly) return min + ':' + sec;
00073     //else return hour as well
00074     QString hour = QString::number(d->hour).rightJustified(2, '0', true);
00075     return hour + ':' + min + ':' + sec;
00076 }
00077 
00078 void KGameClock::showTime()
00079 {
00080     emit timeChanged(timeString());
00081 }
00082 
00083 void KGameClock::restart()
00084 {
00085     d->timerClock->stop();
00086     d->sec = 0;
00087     d->min = 0;
00088     d->hour = 0;
00089     resume();
00090     showTime();
00091 }
00092 
00093 void KGameClock::resume()
00094 {
00095     d->timerClock->start(1000); // 1 second
00096 }
00097 
00098 void KGameClock::pause()
00099 {
00100     d->timerClock->stop();
00101 }
00102 
00103 uint KGameClock::seconds() const
00104 {
00105     return d->hour*3600 + d->min*60 + d->sec;
00106 }
00107 
00108 void KGameClock::setTime(uint sec)
00109 {
00110     Q_ASSERT( sec<(3600*24) );
00111     d->sec = sec % 60;
00112     d->min = (sec / 60) % 60;
00113     d->hour = sec / 1440 ;
00114     showTime();
00115 }
00116 
00117 void KGameClock::setTime(const QString &s)
00118 {
00119     Q_ASSERT( s.length()==8 && s[2]==':' && s[5]==':' );
00120     uint hour = qMin(s.section(':', 0, 0).toUInt(), uint(23));
00121     uint min = qMin(s.section(':', 1, 1).toUInt(), uint(59));
00122     uint sec = qMin(s.section(':', 2, 2).toUInt(), uint(59));
00123     setTime(sec + min*60 + hour*3600);
00124 }

libkdegames

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

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal