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()
84 timePlayedBeforeTurn_ = getTimePlayed();
85 // Count time available
86 // This update current turn
87 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 return timeAvailableBeforeTurn_-currentTurnPlayed();
115 return timeAvailableBeforeTurn_;
119 int ChessClock::getTimePlayed()
121 // Count time played time
123 return timePlayedBeforeTurn_ + currentTurnPlayed();
125 return timePlayedBeforeTurn_;
129 void ChessClock::setTimeAvailable(int msecs)
131 timeAvailableBeforeTurn_ = msecs;
135 void ChessClock::addTime(int msecs)
137 timeAvailableBeforeTurn_ += msecs;
140 int ChessClock::currentTurnPlayed()
144 // Update current time
145 if( status_ == Running )
146 currentTurn_->addTime( clockTime_.restart());
148 // Return current time
149 return currentTurn_->getDuration();
156 void ChessClock::updateClock()
159 if( another_ && !another_->isLoser())
161 if( getTimeAvailable() < 0 && !loser_)