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 const int ChessClock::DONTEATBATTERYTIME; 00028 00029 ChessClock::ChessClock(bool white, QWidget *parent) : 00030 QWidget(parent) 00031 { 00032 isWhite_ = white; 00033 loser_ = false; 00034 turn_ = 0; 00035 dontEatBatteryEmitted_ = false; 00036 timePlayedBeforeTurn_ = 0; 00037 status_ = NotRunning; 00038 another_ = 0; 00039 currentTurn_ = 0; 00040 00041 // Set clock timer calculating played time 00042 clockTime_.start(); 00043 00044 // Set updating timer 00045 updateTimer_.setInterval( UPDATEINTERVAL ); 00046 connect( &updateTimer_, SIGNAL(timeout()),this,SLOT(updateClock())); 00047 } 00048 00049 void ChessClock::startTurn() 00050 { 00051 turn_++; 00052 dontEatBatteryEmitted_ = false; 00053 00054 // Turn information for this new turn 00055 currentTurn_ = new TurnInformation(turn_, isWhite_); 00056 clockTime_.restart(); 00057 updateTimer_.start(); 00058 status_=Running; 00059 00060 // Repaint clock 00061 updateClock(); 00062 } 00063 00064 void ChessClock::pauseTurn() 00065 { 00066 updateTimer_.stop(); 00067 // Update turn time 00068 currentTurn_->addTime( clockTime_.restart() ); 00069 status_ = Paused; 00070 updateClock(); 00071 } 00072 00073 void ChessClock::continueTurn() 00074 { 00075 // Continue paused game 00076 // Add pause duration to information object 00077 currentTurn_->addPause( clockTime_.restart() ); 00078 status_ = Running; 00079 updateTimer_.start(); 00080 updateClock(); 00081 } 00082 00083 00084 TurnInformation* ChessClock::endTurn() 00085 { 00086 updateTimer_.stop(); 00087 status_ = NotRunning; 00088 00089 updateClock(); 00090 // Count time played 00091 timePlayedBeforeTurn_ = getTimePlayed(); 00092 00093 // Count time available 00094 // This update current turn 00095 timeAvailableBeforeTurn_ = getTimeAvailable(); 00096 00097 00098 // Close and return turn information 00099 currentTurn_->turnReady(timeAvailableBeforeTurn_ ); 00100 TurnInformation* information = currentTurn_; 00101 currentTurn_ = 0; 00102 00103 emit turnEnded(); 00104 return information; 00105 } 00106 00107 void ChessClock::setAnother(ChessClock *another) 00108 { 00109 another_ = another; 00110 } 00111 00112 int ChessClock::getTimeAvailable() 00113 { 00114 // Most simple - will be overwritten in more complex time controls: 00115 // subtract duration time! 00116 if( currentTurn_) 00117 { 00118 // Update turn time 00119 return timeAvailableBeforeTurn_-currentTurnPlayed(); 00120 } 00121 else 00122 return timeAvailableBeforeTurn_; 00123 } 00124 00125 00126 int ChessClock::getTimePlayed() 00127 { 00128 return timePlayedBeforeTurn_ + currentTurnPlayed(); 00129 } 00130 00131 00132 void ChessClock::setTimeAvailable(int msecs) 00133 { 00134 timeAvailableBeforeTurn_ = msecs; 00135 } 00136 00137 00138 void ChessClock::addTime(int msecs) 00139 { 00140 timeAvailableBeforeTurn_ += msecs; 00141 } 00142 00143 int ChessClock::currentTurnPlayed() 00144 { 00145 if( currentTurn_ ) 00146 { 00147 // Update current time 00148 if( status_ == Running ) 00149 { 00150 currentTurn_->addTime( clockTime_.restart()); 00151 00152 // since 1.1.2 00153 // emit dontEatBattery signal when screen should not to be keeped on 00154 if ( currentTurn_->getDuration() > DONTEATBATTERYTIME && dontEatBatteryEmitted_ == false ) 00155 { 00156 dontEatBatteryEmitted_ = true; 00157 emit dontEatBattery(); 00158 } 00159 } 00160 00161 // Return current time 00162 return currentTurn_->getDuration(); 00163 } 00164 else 00165 // No current turn! 00166 return 0; 00167 } 00168 00169 void ChessClock::updateClock() 00170 { 00171 // Check loser 00172 if( another_ && !another_->isLoser()) 00173 { 00174 if( getTimeAvailable() < 0 && !loser_) 00175 { 00176 loser_ = true; 00177 emit timeOutLoser(); 00178 } 00179 00180 } 00181 repaintClock(); 00182 00183 } 00184