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()
85 timePlayedBeforeTurn_ = getTimePlayed();
86 // Count time available
87 // This update current turn
88 timeAvailableBeforeTurn_ = getTimeAvailable();
93 // Close and return turn information
94 currentTurn_->turnReady(timeAvailableBeforeTurn_ );
95 TurnInformation* information = currentTurn_;
101 void ChessClock::setAnother(ChessClock *another)
106 int ChessClock::getTimeAvailable()
108 // Most simple - will be overwritten in more complex time controls:
109 // subtract duration time!
113 return timeAvailableBeforeTurn_-currentTurnPlayed();
116 return timeAvailableBeforeTurn_;
120 int ChessClock::getTimePlayed()
122 // Count time played time
124 return timePlayedBeforeTurn_ + currentTurnPlayed();
126 return timePlayedBeforeTurn_;
130 void ChessClock::setTimeAvailable(int msecs)
132 timeAvailableBeforeTurn_ = msecs;
136 void ChessClock::addTime(int msecs)
138 timeAvailableBeforeTurn_ += msecs;
141 int ChessClock::currentTurnPlayed()
145 // Update current time
146 if( status_ == Running )
147 currentTurn_->addTime( clockTime_.restart());
149 // Return current time
150 return currentTurn_->getDuration();
157 void ChessClock::updateClock()
160 if( another_ && !another_->isLoser())
162 if( getTimeAvailable() < 0 && !loser_)