SOURCES += main.cpp\
chessclockwindow.cpp \
- classes/turninformation.cpp
+ classes/turninformation.cpp \
+ classes/chessclock.cpp
HEADERS += chessclockwindow.h \
- classes/turninformation.h
+ classes/turninformation.h \
+ classes/chessclock.h
CONFIG += mobility
MOBILITY =
--- /dev/null
+ /**************************************************************************
+
+ Chess Clock
+
+ Copyright (c) Arto Hyvättinen 2010
+
+ This file is part of Chess Clock software.
+
+ Chess Clock is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Chess Clock 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 General Public License for more details.
+
+
+**************************************************************************/
+
+#include "chessclock.h"
+
+#include "turninformation.h"
+
+const int ChessClock::UPDATEINTERVAL;
+
+ChessClock::ChessClock(bool white, QWidget *parent) :
+ QWidget(parent)
+{
+ isWhite_ = white;
+ loser_ = false;
+ turn_ = 0;
+ timePlayed_ = 0;
+ status_ = NotRunning;
+
+ // Set clock timer calculating played time
+ clockTime_.start();
+
+ // Set updating timer
+ updateTimer_.setInterval( UPDATEINTERVAL );
+ connect( &updateTimer_, SIGNAL(timeout),this,SLOT(updateClock()));
+}
+
+void ChessClock::startTurn()
+{
+ turn_++;
+
+ // Turn information for this new turn
+ currentTurn_ = new TurnInformation(turn_, isWhite_);
+ clockTime_.restart();
+ status_=Running;
+
+ // Repaint clock
+ repaintClock();
+}
+
+void ChessClock::pauseTurn()
+{
+ // Update turn time
+ currentTurn_->addTime( clockTime_.restart() );
+ status_ = Paused;
+ repaintClock();
+}
+
+void ChessClock::continueTurn()
+{
+ // Continue paused game
+ // Add pause duration to information object
+ currentTurn_->addPause( clockTime_.restart() );
+ status_ = Running;
+ repaintClock();
+}
+
+
+TurnInformation* ChessClock::endTurn()
+{
+ status_ = NotRunning;
+ // Update turn time
+ currentTurn_->addTime( clockTime_.restart());
+ // Count time available
+ timeAvailableBeforeTurn_ = getTimeAvailable();
+ repaintClock();
+
+ // Close and return turn information
+ currentTurn_->turnReady(timeAvailableBeforeTurn_ );
+ TurnInformation* information = currentTurn_;
+ currentTurn_ = 0;
+ return information;
+}
+
+void ChessClock::setAnother(ChessClock *another)
+{
+ another_ = another;
+}
+
+int ChessClock::getTimeAvailable()
+{
+ // Most simple - will be overwritten in more complex time controls:
+ // subtract duration time!
+ if( currentTurn_)
+ return timeAvailableBeforeTurn_-currentTurn_->getDuration();
+ else
+ return timeAvailableBeforeTurn_;
+}
--- /dev/null
+ /**************************************************************************
+
+ Chess Clock
+
+ Copyright (c) Arto Hyvättinen 2010
+
+ This file is part of Chess Clock software.
+
+ Chess Clock is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Chess Clock 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 General Public License for more details.
+
+
+**************************************************************************/
+
+#ifndef CHESSCLOCK_H
+#define CHESSCLOCK_H
+
+#include <QWidget>
+#include <QTime>
+#include <QTimer>
+
+class TurnInformation;
+
+
+/*! Clock of a individual player
+
+ @author Arto Hyvättinen
+ @date 2010-08-13
+
+ Base class of chess clock.
+
+ */
+class ChessClock : public QWidget
+{
+ Q_OBJECT
+public:
+ ChessClock(bool white, QWidget *parent = 0);
+
+public:
+ enum RunningStatus {
+ NotRunning /*! Not turn */ = 0,
+ Running /*! Turn running */ = 1,
+ Paused /*! Turn paused */ = 2
+ };
+
+ bool isLoser() const { return loser_; }
+ int getTurn() const { return turn_; }
+
+
+ /*! Start new turn */
+ virtual void startTurn();
+
+ /*! End this turn.
+
+ Player has done his move.
+ @return Locked turn information */
+ virtual TurnInformation* endTurn();
+
+ /*! Pause clock */
+ virtual void pauseTurn();
+
+ /*! Continue paused game */
+ virtual void continueTurn();
+
+ /*! Set another chess clock for connecting
+ @param another Clock of opposite player */
+ void setAnother( ChessClock* another);
+
+ /*! Get total time available
+
+ Time does't contain delays.
+
+ @return Time available in msecs */
+ virtual int getTimeAvailable();
+
+signals:
+ void timeOut();
+
+public slots:
+
+ /*! Refresh clock information */
+ virtual void repaintClock() = 0;
+
+
+protected:
+ ChessClock* another_; /*! Another player's clock */
+
+ bool loser_; /*! Is player losed because of timeout */
+ int turn_; /*! Current turn */
+ RunningStatus status_;
+ TurnInformation* currentTurn_;
+
+ int timePlayed_; /*! Time played in this game */
+ int timeAvailableBeforeTurn_; /*! Time available for play BEFORE this turn!*/
+
+ bool isWhite_; /*! True if white player */
+
+ QTime clockTime_;
+ QTimer updateTimer_;
+
+ static const int UPDATEINTERVAL = 1000; /** Clock updating interval in msecs */
+
+};
+
+#endif // CHESSCLOCK_H
void TurnInformation::addPause(int msecs)
{
if( !turnReady_ )
- pause_ += msecs;
+ paused_ += msecs;
+
}
void TurnInformation::turnReady(int msecs)
turnReady_ = true;
}
-int TurnInformation::getTimeAfter()
+int TurnInformation::getTimeAfter() const
{
if( turnReady_ )
return timeAfter_;
@param white true if turn on white, false id black */
TurnInformation(int turnId, bool white);
- int getTurnId() { return turnId_; }
+ int getTurnId() const { return turnId_; }
/*! Duration of turn
@return Duration of turn in msecs */
- int getDuration() { return duration_; }
+ int getDuration() const { return duration_; }
/*! Duration of pauses during this turn
@return Duration of pauses in msecs */
- int getPaused() { return paused_; }
+ int getPaused() const { return paused_; }
/*! Time available for this player after this turn
@return Time available in msecs */
- int getTimeAfter();
- bool isWhiteTurn() { return white_; }
+ int getTimeAfter() const;
+ bool isWhiteTurn() const { return white_; }
void addTime( int msecs );
void addPause( int msecs );