1 /**************************************************************************
5 Copyright (c) Arto Hyvättinen 2010
7 This file is part of Chess Clock software.
9 Chess Clock is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Chess Clock is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 **************************************************************************/
22 #include "chessclock.h"
24 #include "turninformation.h"
26 const int ChessClock::UPDATEINTERVAL;
28 ChessClock::ChessClock(bool white, QWidget *parent) :
34 timePlayedBeforeTurn_ = 0;
38 // Set clock timer calculating played time
42 updateTimer_.setInterval( UPDATEINTERVAL );
43 connect( &updateTimer_, SIGNAL(timeout()),this,SLOT(updateClock()));
46 void ChessClock::startTurn()
50 // Turn information for this new turn
51 currentTurn_ = new TurnInformation(turn_, isWhite_);
60 void ChessClock::pauseTurn()
64 currentTurn_->addTime( clockTime_.restart() );
69 void ChessClock::continueTurn()
71 // Continue paused game
72 // Add pause duration to information object
73 currentTurn_->addPause( clockTime_.restart() );
80 TurnInformation* ChessClock::endTurn()
85 currentTurn_->addTime( clockTime_.restart());
87 timePlayedBeforeTurn_ = getTimePlayed();
88 // Count time available
89 timeAvailableBeforeTurn_ = getTimeAvailable();
92 // Close and return turn information
93 currentTurn_->turnReady(timeAvailableBeforeTurn_ );
94 TurnInformation* information = currentTurn_;
100 void ChessClock::setAnother(ChessClock *another)
105 int ChessClock::getTimeAvailable()
107 // Most simple - will be overwritten in more complex time controls:
108 // subtract duration time!
112 currentTurn_->addTime( clockTime_.restart());
113 return timeAvailableBeforeTurn_-currentTurn_->getDuration();
116 return timeAvailableBeforeTurn_;
120 int ChessClock::getTimePlayed() const
122 // Count time played time
124 return timePlayedBeforeTurn_ + currentTurn_->getDuration();
126 return timePlayedBeforeTurn_;
130 void ChessClock::setTimeAvailable(int msecs)
132 timeAvailableBeforeTurn_ = msecs;
136 void ChessClock::addTime(int msecs)
138 timeAvailableBeforeTurn_ += msecs;
141 void ChessClock::updateClock()
144 if( another_ && !another_->isLoser())
146 if( getTimeAvailable() < 0 && !loser_)