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;
27 const int ChessClock::DONTEATBATTERYTIME;
29 ChessClock::ChessClock(bool white, QWidget *parent) :
35 dontEatBatteryEmitted_ = false;
36 timePlayedBeforeTurn_ = 0;
41 // Set clock timer calculating played time
45 updateTimer_.setInterval( UPDATEINTERVAL );
46 connect( &updateTimer_, SIGNAL(timeout()),this,SLOT(updateClock()));
49 void ChessClock::startTurn()
52 dontEatBatteryEmitted_ = false;
54 // Turn information for this new turn
55 currentTurn_ = new TurnInformation(turn_, isWhite_);
64 void ChessClock::pauseTurn()
68 currentTurn_->addTime( clockTime_.restart() );
73 void ChessClock::continueTurn()
75 // Continue paused game
76 // Add pause duration to information object
77 currentTurn_->addPause( clockTime_.restart() );
84 TurnInformation* ChessClock::endTurn()
91 timePlayedBeforeTurn_ = getTimePlayed();
93 // Count time available
94 // This update current turn
95 timeAvailableBeforeTurn_ = getTimeAvailable();
98 // Close and return turn information
99 currentTurn_->turnReady(timeAvailableBeforeTurn_ );
100 TurnInformation* information = currentTurn_;
107 void ChessClock::setAnother(ChessClock *another)
112 int ChessClock::getTimeAvailable()
114 // Most simple - will be overwritten in more complex time controls:
115 // subtract duration time!
119 return timeAvailableBeforeTurn_-currentTurnPlayed();
122 return timeAvailableBeforeTurn_;
126 int ChessClock::getTimePlayed()
128 return timePlayedBeforeTurn_ + currentTurnPlayed();
132 void ChessClock::setTimeAvailable(int msecs)
134 timeAvailableBeforeTurn_ = msecs;
138 void ChessClock::addTime(int msecs)
140 timeAvailableBeforeTurn_ += msecs;
143 int ChessClock::currentTurnPlayed()
147 // Update current time
148 if( status_ == Running )
150 currentTurn_->addTime( clockTime_.restart());
153 // emit dontEatBattery signal when screen should not to be keeped on
154 if ( currentTurn_->getDuration() > DONTEATBATTERYTIME && dontEatBatteryEmitted_ == false )
156 dontEatBatteryEmitted_ = true;
157 emit dontEatBattery();
161 // Return current time
162 return currentTurn_->getDuration();
169 void ChessClock::updateClock()
172 if( another_ && !another_->isLoser())
174 if( getTimeAvailable() < 0 && !loser_)