308d82d19ddf44fe7dd541d749d12c7b4cd69053
[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
28 ChessClock::ChessClock(bool white, QWidget *parent) :
29     QWidget(parent)
30 {
31     isWhite_ = white;
32     loser_ = false;
33     turn_ = 0;
34     timePlayedBeforeTurn_ = 0;
35     status_ = NotRunning;
36     another_ = 0;
37
38     // Set clock timer calculating played time
39     clockTime_.start();
40
41     // Set updating timer
42     updateTimer_.setInterval( UPDATEINTERVAL );
43     connect( &updateTimer_, SIGNAL(timeout()),this,SLOT(updateClock()));
44 }
45
46 void ChessClock::startTurn()
47 {
48     turn_++;
49
50     // Turn information for this new turn
51     currentTurn_ = new TurnInformation(turn_, isWhite_);
52     clockTime_.restart();
53     updateTimer_.start();
54     status_=Running;
55
56     // Repaint clock
57     updateClock();
58 }
59
60 void ChessClock::pauseTurn()
61 {
62     updateTimer_.stop();
63     // Update turn time
64     currentTurn_->addTime( clockTime_.restart() );
65     status_ = Paused;
66     updateClock();
67 }
68
69 void ChessClock::continueTurn()
70 {
71     // Continue paused game
72     // Add pause duration to information object
73     currentTurn_->addPause( clockTime_.restart() );
74     status_ = Running;
75     updateTimer_.start();
76     updateClock();
77 }
78
79
80 TurnInformation* ChessClock::endTurn()
81 {
82     updateTimer_.stop();
83     // Count time played
84     timePlayedBeforeTurn_ = getTimePlayed();
85     // Count time available
86     // This update current turn
87     timeAvailableBeforeTurn_ = getTimeAvailable();
88
89     status_ = NotRunning;
90     updateClock();
91
92     // Close and return turn information
93     currentTurn_->turnReady(timeAvailableBeforeTurn_ );
94     TurnInformation* information = currentTurn_;
95     currentTurn_ = 0;
96     emit turnEnded();
97     return information;
98 }
99
100 void ChessClock::setAnother(ChessClock *another)
101 {
102     another_ = another;
103 }
104
105 int ChessClock::getTimeAvailable()
106 {
107     // Most simple - will be overwritten in more complex time controls:
108     // subtract duration time!
109     if( currentTurn_)
110     {
111         // Update turn time
112         return timeAvailableBeforeTurn_-currentTurnPlayed();
113     }
114     else
115         return timeAvailableBeforeTurn_;
116 }
117
118
119 int ChessClock::getTimePlayed()
120 {
121     // Count time played time
122     if( currentTurn_ )
123         return timePlayedBeforeTurn_ + currentTurnPlayed();
124     else
125         return timePlayedBeforeTurn_;
126 }
127
128
129 void ChessClock::setTimeAvailable(int msecs)
130 {
131     timeAvailableBeforeTurn_ = msecs;
132 }
133
134
135 void ChessClock::addTime(int msecs)
136 {
137    timeAvailableBeforeTurn_ += msecs;
138 }
139
140 int ChessClock::currentTurnPlayed()
141 {
142     if( currentTurn_ )
143     {
144         // Update current time
145         if( status_ == Running )
146             currentTurn_->addTime( clockTime_.restart());
147
148         // Return current time
149         return currentTurn_->getDuration();
150      }
151     else
152         // No current turn!
153         return 0;
154 }
155
156 void ChessClock::updateClock()
157 {
158     // Check loser
159     if( another_ && !another_->isLoser())
160     {
161         if( getTimeAvailable() < 0 && !loser_)
162         {
163             loser_ = true;
164             emit timeOutLoser();
165         }
166
167     }
168     repaintClock();
169
170 }
171