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