00001 /************************************************************************** 00002 00003 Chess Clock 00004 00005 Copyright (c) Arto Hyvättinen 2010 00006 00007 This file is part of Chess Clock software. 00008 00009 Chess Clock is free software: you can redistribute it and/or modify 00010 it under the terms of the GNU General Public License as published by 00011 the Free Software Foundation, either version 3 of the License, or 00012 (at your option) any later version. 00013 00014 Chess Clock is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU General Public License for more details. 00018 00019 00020 **************************************************************************/ 00021 00022 #include "chessclock.h" 00023 00024 #include "turninformation.h" 00025 00026 const int ChessClock::UPDATEINTERVAL; 00027 00028 ChessClock::ChessClock(bool white, QWidget *parent) : 00029 QWidget(parent) 00030 { 00031 isWhite_ = white; 00032 loser_ = false; 00033 turn_ = 0; 00034 timePlayedBeforeTurn_ = 0; 00035 status_ = NotRunning; 00036 another_ = 0; 00037 00038 // Set clock timer calculating played time 00039 clockTime_.start(); 00040 00041 // Set updating timer 00042 updateTimer_.setInterval( UPDATEINTERVAL ); 00043 connect( &updateTimer_, SIGNAL(timeout),this,SLOT(updateClock())); 00044 } 00045 00046 void ChessClock::startTurn() 00047 { 00048 turn_++; 00049 00050 // Turn information for this new turn 00051 currentTurn_ = new TurnInformation(turn_, isWhite_); 00052 clockTime_.restart(); 00053 status_=Running; 00054 00055 // Repaint clock 00056 updateClock(); 00057 } 00058 00059 void ChessClock::pauseTurn() 00060 { 00061 // Update turn time 00062 currentTurn_->addTime( clockTime_.restart() ); 00063 status_ = Paused; 00064 updateClock(); 00065 } 00066 00067 void ChessClock::continueTurn() 00068 { 00069 // Continue paused game 00070 // Add pause duration to information object 00071 currentTurn_->addPause( clockTime_.restart() ); 00072 status_ = Running; 00073 updateClock(); 00074 } 00075 00076 00077 TurnInformation* ChessClock::endTurn() 00078 { 00079 status_ = NotRunning; 00080 // Update turn time 00081 currentTurn_->addTime( clockTime_.restart()); 00082 // Count time played 00083 timePlayedBeforeTurn_ = getTimePlayed(); 00084 // Count time available 00085 timeAvailableBeforeTurn_ = getTimeAvailable(); 00086 updateClock(); 00087 00088 // Close and return turn information 00089 currentTurn_->turnReady(timeAvailableBeforeTurn_ ); 00090 TurnInformation* information = currentTurn_; 00091 currentTurn_ = 0; 00092 return information; 00093 } 00094 00095 void ChessClock::setAnother(ChessClock *another) 00096 { 00097 another_ = another; 00098 } 00099 00100 int ChessClock::getTimeAvailable() 00101 { 00102 // Most simple - will be overwritten in more complex time controls: 00103 // subtract duration time! 00104 if( currentTurn_) 00105 return timeAvailableBeforeTurn_-currentTurn_->getDuration(); 00106 else 00107 return timeAvailableBeforeTurn_; 00108 } 00109 00110 00111 int ChessClock::getTimePlayed() const 00112 { 00113 // Count time played time 00114 if( currentTurn_ ) 00115 return timePlayedBeforeTurn_ + currentTurn_->getDuration(); 00116 else 00117 return timePlayedBeforeTurn_; 00118 } 00119 00120 void ChessClock::updateClock() 00121 { 00122 // Check loser 00123 if( another_ && !another_->isLoser()) 00124 { 00125 if( getTimeAvailable() < 0 && !loser_) 00126 { 00127 loser_ = true; 00128 emit timeOutLoser(); 00129 } 00130 00131 } 00132 repaintClock(); 00133 00134 } 00135