cc9269ea08ca96886d1b6460cc9f3c5c5c676468
[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     currentTurn_ = 0;
38
39     // Set clock timer calculating played time
40     clockTime_.start();
41
42     // Set updating timer
43     updateTimer_.setInterval( UPDATEINTERVAL );
44     connect( &updateTimer_, SIGNAL(timeout()),this,SLOT(updateClock()));
45 }
46
47 void ChessClock::startTurn()
48 {
49     turn_++;
50
51     // Turn information for this new turn
52     currentTurn_ = new TurnInformation(turn_, isWhite_);
53     clockTime_.restart();
54     updateTimer_.start();
55     status_=Running;
56
57     // Repaint clock
58     updateClock();
59 }
60
61 void ChessClock::pauseTurn()
62 {
63     updateTimer_.stop();
64     // Update turn time
65     currentTurn_->addTime( clockTime_.restart() );
66     status_ = Paused;
67     updateClock();
68 }
69
70 void ChessClock::continueTurn()
71 {
72     // Continue paused game
73     // Add pause duration to information object
74     currentTurn_->addPause( clockTime_.restart() );
75     status_ = Running;
76     updateTimer_.start();
77     updateClock();
78 }
79
80
81 TurnInformation* ChessClock::endTurn()
82 {
83     updateTimer_.stop();
84     status_ = NotRunning;
85
86     updateClock();
87     // Count time played
88     timePlayedBeforeTurn_ = getTimePlayed();
89
90     // Count time available
91     // This update current turn
92     timeAvailableBeforeTurn_ = getTimeAvailable();
93
94
95     // Close and return turn information
96     currentTurn_->turnReady(timeAvailableBeforeTurn_ );
97     TurnInformation* information = currentTurn_;
98     currentTurn_ = 0;
99
100     emit turnEnded();
101     return information;
102 }
103
104 void ChessClock::setAnother(ChessClock *another)
105 {
106     another_ = another;
107 }
108
109 int ChessClock::getTimeAvailable()
110 {
111     // Most simple - will be overwritten in more complex time controls:
112     // subtract duration time!
113     if( currentTurn_)
114     {
115         // Update turn time
116         return timeAvailableBeforeTurn_-currentTurnPlayed();
117     }
118     else
119         return timeAvailableBeforeTurn_;
120 }
121
122
123 int ChessClock::getTimePlayed()
124 {
125      return timePlayedBeforeTurn_ + currentTurnPlayed();
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