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_);
59 void ChessClock::pauseTurn()
62 currentTurn_->addTime( clockTime_.restart() );
67 void ChessClock::continueTurn()
69 // Continue paused game
70 // Add pause duration to information object
71 currentTurn_->addPause( clockTime_.restart() );
77 TurnInformation* ChessClock::endTurn()
81 currentTurn_->addTime( clockTime_.restart());
83 timePlayedBeforeTurn_ = getTimePlayed();
84 // Count time available
85 timeAvailableBeforeTurn_ = getTimeAvailable();
88 // Close and return turn information
89 currentTurn_->turnReady(timeAvailableBeforeTurn_ );
90 TurnInformation* information = currentTurn_;
95 void ChessClock::setAnother(ChessClock *another)
100 int ChessClock::getTimeAvailable()
102 // Most simple - will be overwritten in more complex time controls:
103 // subtract duration time!
105 return timeAvailableBeforeTurn_-currentTurn_->getDuration();
107 return timeAvailableBeforeTurn_;
111 int ChessClock::getTimePlayed() const
113 // Count time played time
115 return timePlayedBeforeTurn_ + currentTurn_->getDuration();
117 return timePlayedBeforeTurn_;
120 void ChessClock::updateClock()
123 if( another_ && !another_->isLoser())
125 if( getTimeAvailable() < 0 && !loser_)