Added ChessClockWizard class
[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     status_=Running;
54
55     // Repaint clock
56     updateClock();
57 }
58
59 void ChessClock::pauseTurn()
60 {
61     // Update turn time
62     currentTurn_->addTime( clockTime_.restart() );
63     status_ = Paused;
64     updateClock();
65 }
66
67 void ChessClock::continueTurn()
68 {
69     // Continue paused game
70     // Add pause duration to information object
71     currentTurn_->addPause( clockTime_.restart() );
72     status_ = Running;
73     updateClock();
74 }
75
76
77 TurnInformation* ChessClock::endTurn()
78 {
79     status_ = NotRunning;
80     // Update turn time
81     currentTurn_->addTime( clockTime_.restart());
82     // Count time played
83     timePlayedBeforeTurn_ = getTimePlayed();
84     // Count time available
85     timeAvailableBeforeTurn_ = getTimeAvailable();
86     updateClock();
87
88     // Close and return turn information
89     currentTurn_->turnReady(timeAvailableBeforeTurn_ );
90     TurnInformation* information = currentTurn_;
91     currentTurn_ = 0;
92     return information;
93 }
94
95 void ChessClock::setAnother(ChessClock *another)
96 {
97     another_ = another;
98 }
99
100 int ChessClock::getTimeAvailable()
101 {
102     // Most simple - will be overwritten in more complex time controls:
103     // subtract duration time!
104     if( currentTurn_)
105         return timeAvailableBeforeTurn_-currentTurn_->getDuration();
106     else
107         return timeAvailableBeforeTurn_;
108 }
109
110
111 int ChessClock::getTimePlayed() const
112 {
113     // Count time played time
114     if( currentTurn_ )
115         return timePlayedBeforeTurn_ + currentTurn_->getDuration();
116     else
117         return timePlayedBeforeTurn_;
118 }
119
120 void ChessClock::updateClock()
121 {
122     // Check loser
123     if( another_ && !another_->isLoser())
124     {
125         if( getTimeAvailable() < 0 && !loser_)
126         {
127             loser_ = true;
128             emit timeOutLoser();
129         }
130
131     }
132     repaintClock();
133
134 }
135