ChessClock class
authorArto Hyvättinen <arto.hyvattinen@gmail.com>
Fri, 13 Aug 2010 21:01:48 +0000 (00:01 +0300)
committerArto Hyvättinen <arto.hyvattinen@gmail.com>
Fri, 13 Aug 2010 21:01:48 +0000 (00:01 +0300)
New png images

chessclock.pro
classes/chessclock.cpp [new file with mode: 0644]
classes/chessclock.h [new file with mode: 0644]
classes/turninformation.cpp
classes/turninformation.h
pic/black_blue.png [new file with mode: 0644]
pic/black_gray.png [new file with mode: 0644]
pic/white_blue.png [new file with mode: 0644]
pic/white_gray.png [new file with mode: 0644]

index 81c5831..4248040 100644 (file)
@@ -12,10 +12,12 @@ TEMPLATE = app
 
 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 = 
diff --git a/classes/chessclock.cpp b/classes/chessclock.cpp
new file mode 100644 (file)
index 0000000..d7df9d3
--- /dev/null
@@ -0,0 +1,105 @@
+ /**************************************************************************
+
+    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_;
+}
diff --git a/classes/chessclock.h b/classes/chessclock.h
new file mode 100644 (file)
index 0000000..528b58b
--- /dev/null
@@ -0,0 +1,112 @@
+ /**************************************************************************
+
+    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
index a1e4411..96d95b8 100644 (file)
@@ -40,7 +40,8 @@ void TurnInformation::addTime(int msecs)
 void TurnInformation::addPause(int msecs)
 {
     if( !turnReady_ )
-        pause_ += msecs;
+        paused_ += msecs;
+
 }
 
 void TurnInformation::turnReady(int msecs)
@@ -49,7 +50,7 @@ void TurnInformation::turnReady(int msecs)
     turnReady_ = true;
 }
 
-int TurnInformation::getTimeAfter()
+int TurnInformation::getTimeAfter() const
 {
     if( turnReady_ )
         return timeAfter_;
index 63d21ad..90291c9 100644 (file)
@@ -40,20 +40,20 @@ public:
       @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 );
diff --git a/pic/black_blue.png b/pic/black_blue.png
new file mode 100644 (file)
index 0000000..b189459
Binary files /dev/null and b/pic/black_blue.png differ
diff --git a/pic/black_gray.png b/pic/black_gray.png
new file mode 100644 (file)
index 0000000..25953dc
Binary files /dev/null and b/pic/black_gray.png differ
diff --git a/pic/white_blue.png b/pic/white_blue.png
new file mode 100644 (file)
index 0000000..e971074
Binary files /dev/null and b/pic/white_blue.png differ
diff --git a/pic/white_gray.png b/pic/white_gray.png
new file mode 100644 (file)
index 0000000..c033fc0
Binary files /dev/null and b/pic/white_gray.png differ