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;
39 // Set clock timer calculating played time
43 updateTimer_.setInterval( UPDATEINTERVAL );
44 connect( &updateTimer_, SIGNAL(timeout()),this,SLOT(updateClock()));
47 void ChessClock::startTurn()
51 // Turn information for this new turn
52 currentTurn_ = new TurnInformation(turn_, isWhite_);
61 void ChessClock::pauseTurn()
65 currentTurn_->addTime( clockTime_.restart() );
70 void ChessClock::continueTurn()
72 // Continue paused game
73 // Add pause duration to information object
74 currentTurn_->addPause( clockTime_.restart() );
81 TurnInformation* ChessClock::endTurn()
88 timePlayedBeforeTurn_ = getTimePlayed();
90 // Count time available
91 // This update current turn
92 timeAvailableBeforeTurn_ = getTimeAvailable();
95 // Close and return turn information
96 currentTurn_->turnReady(timeAvailableBeforeTurn_ );
97 TurnInformation* information = currentTurn_;
104 void ChessClock::setAnother(ChessClock *another)
109 int ChessClock::getTimeAvailable()
111 // Most simple - will be overwritten in more complex time controls:
112 // subtract duration time!
116 return timeAvailableBeforeTurn_-currentTurnPlayed();
119 return timeAvailableBeforeTurn_;
123 int ChessClock::getTimePlayed()
125 return timePlayedBeforeTurn_ + currentTurnPlayed();
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_)