Updated the web pages
[chessclock] / classes / chessclock.cpp
1  /**************************************************************************
2
3     Chess Clock
4
5     Copyright (c) Arto Hyvättinen 2010
6
7     This file is part of Chess Clock software.
8
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.
13
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.
18
19
20 **************************************************************************/
21
22 #include "chessclock.h"
23
24 #include "turninformation.h"
25
26 const int ChessClock::UPDATEINTERVAL;
27 const int ChessClock::DONTEATBATTERYTIME;
28
29 ChessClock::ChessClock(bool white, QWidget *parent) :
30     QWidget(parent)
31 {
32     isWhite_ = white;
33     loser_ = false;
34     turn_ = 0;
35     dontEatBatteryEmitted_ = false;
36     timePlayedBeforeTurn_ = 0;
37     status_ = NotRunning;
38     another_ = 0;
39     currentTurn_ = 0;
40
41     // Set clock timer calculating played time
42     clockTime_.start();
43
44     // Set updating timer
45     updateTimer_.setInterval( UPDATEINTERVAL );
46     connect( &updateTimer_, SIGNAL(timeout()),this,SLOT(updateClock()));
47 }
48
49 void ChessClock::startTurn()
50 {
51     turn_++;
52     dontEatBatteryEmitted_ = false;
53
54     // Turn information for this new turn
55     currentTurn_ = new TurnInformation(turn_, isWhite_);
56     clockTime_.restart();
57     updateTimer_.start();
58     status_=Running;
59
60     // Repaint clock
61     updateClock();
62 }
63
64 void ChessClock::pauseTurn()
65 {
66     updateTimer_.stop();
67     // Update turn time
68     currentTurn_->addTime( clockTime_.restart() );
69     status_ = Paused;
70     updateClock();
71 }
72
73 void ChessClock::continueTurn()
74 {
75     // Continue paused game
76     // Add pause duration to information object
77     currentTurn_->addPause( clockTime_.restart() );
78     status_ = Running;
79     updateTimer_.start();
80     updateClock();
81 }
82
83
84 TurnInformation* ChessClock::endTurn()
85 {
86     updateTimer_.stop();
87     status_ = NotRunning;
88
89     updateClock();
90     // Count time played
91     timePlayedBeforeTurn_ = getTimePlayed();
92
93     // Count time available
94     // This update current turn
95     timeAvailableBeforeTurn_ = getTimeAvailable();
96
97
98     // Close and return turn information
99     currentTurn_->turnReady(timeAvailableBeforeTurn_ );
100     TurnInformation* information = currentTurn_;
101     currentTurn_ = 0;
102
103     emit turnEnded();
104     return information;
105 }
106
107 void ChessClock::setAnother(ChessClock *another)
108 {
109     another_ = another;
110 }
111
112 int ChessClock::getTimeAvailable()
113 {
114     // Most simple - will be overwritten in more complex time controls:
115     // subtract duration time!
116     if( currentTurn_)
117     {
118         // Update turn time
119         return timeAvailableBeforeTurn_-currentTurnPlayed();
120     }
121     else
122         return timeAvailableBeforeTurn_;
123 }
124
125
126 int ChessClock::getTimePlayed()
127 {
128      return timePlayedBeforeTurn_ + currentTurnPlayed();
129 }
130
131
132 void ChessClock::setTimeAvailable(int msecs)
133 {
134     timeAvailableBeforeTurn_ = msecs;
135 }
136
137
138 void ChessClock::addTime(int msecs)
139 {
140    timeAvailableBeforeTurn_ += msecs;
141 }
142
143 int ChessClock::currentTurnPlayed()
144 {
145     if( currentTurn_ )
146     {
147         // Update current time
148         if( status_ == Running )
149         {
150             currentTurn_->addTime( clockTime_.restart());
151
152             // since 1.1.2
153             // emit dontEatBattery signal when screen should not to be keeped on
154             if ( currentTurn_->getDuration() > DONTEATBATTERYTIME  && dontEatBatteryEmitted_ == false )
155             {
156                 dontEatBatteryEmitted_ = true;
157                 emit dontEatBattery();
158             }
159         }
160
161         // Return current time
162         return currentTurn_->getDuration();
163      }
164     else
165         // No current turn!
166         return 0;
167 }
168
169 void ChessClock::updateClock()
170 {
171     // Check loser
172     if( another_ && !another_->isLoser())
173     {
174         if( getTimeAvailable() < 0 && !loser_)
175         {
176             loser_ = true;
177             emit timeOutLoser();
178         }
179
180     }
181     repaintClock();
182
183 }
184