548132df3387f68e543735602d2b999ec3c2f977
[chessclock] / classes / timecontrol / hourglassclock.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 "hourglassclock.h"
23 #include "../turninformation.h"
24
25 HourGlassClock::HourGlassClock(bool white, QWidget *parent) :
26     ChessClockWidget(white, parent)
27 {
28 }
29
30
31 int HourGlassClock::getTimeAvailable()
32 {
33     if( !another_)
34         return timeAvailableBeforeTurn_;
35
36     if( currentTurn_ )
37     {
38         return timeAvailableBeforeTurn_ - currentTurnPlayed();
39     }
40     else
41     {
42         return timeAvailableBeforeTurn_ + another_->currentTurnPlayed();
43     }
44 }
45
46
47 TurnInformation* HourGlassClock::endTurn()
48 {
49     updateTimer_.stop();
50     status_ = NotRunning;
51
52     updateClock();
53
54     timePlayedBeforeTurn_ = getTimePlayed();
55
56     // Count available times of BOTH the clocks!
57     setTimeAvailable( getTimeAvailable() );
58     another_->setTimeAvailable( another_->getTimeAvailable());
59
60     // Close and return turn information
61     currentTurn_->turnReady(timeAvailableBeforeTurn_ );
62     TurnInformation* information = currentTurn_;
63     currentTurn_ = 0;
64
65     emit turnEnded();
66     return information;
67 }
68
69 void HourGlassClock::updateClock()
70 {
71     ChessClock::updateClock();
72     another_->ChessClock::updateClock();
73 }