Updated the web pages
[chessclock] / classes / chessclock.h
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 #ifndef CHESSCLOCK_H
23 #define CHESSCLOCK_H
24
25 #include <QWidget>
26 #include <QTime>
27 #include <QTimer>
28
29 class TurnInformation;
30
31
32 /*! Clock of a individual player
33
34   @author Arto Hyvättinen
35   @date 2010-08-13
36
37   Base class of chess clock.
38
39   ChessClock has not GUI itself, but it is subclass of QWidget
40   avoiding polymorphism.
41
42   */
43 class ChessClock : public QWidget
44 {
45     Q_OBJECT
46 public:
47     ChessClock(bool white, QWidget *parent = 0);
48
49 public:
50     enum RunningStatus {
51         NotRunning /*! Not turn  */ = 0,
52         Running /*! Turn running */ = 1,
53         Paused /*! Turn paused */ = 2
54     };
55
56     bool isLoser() const  { return loser_; }
57     int getTurn() const  { return turn_; }
58     bool isWhite() const { return isWhite_; }
59     RunningStatus getStatus() const { return status_ ; }
60
61
62     /*! Start new turn */
63     virtual void startTurn();
64
65     /*! End this turn.
66
67       Player has done his move.
68       @return Locked turn information */
69     virtual TurnInformation* endTurn();
70
71     /*! Pause clock */
72     virtual void pauseTurn();
73
74     /*! Continue paused game */
75     virtual void continueTurn();
76
77     /*! Set another chess clock for connecting
78       @param another Clock of opposite player */
79     void setAnother( ChessClock* another);
80
81     /*! Get total time available
82
83       Time does't contain delays.
84
85       @return Time available in msecs */
86     virtual int getTimeAvailable();
87
88     /*! Get total time played
89       @return Time played in msecs */
90     virtual int getTimePlayed();
91
92     /*! Set time available
93
94       @param msecs Time available in msecs */
95     void setTimeAvailable(int msecs);
96
97     /*! Add time
98
99       Add time to timeAvailableBeforeTurn_ total available time
100       counter.
101
102       @param msecs Time to add in msecs */
103     void addTime(int msecs);
104
105     /*! Get time played current turn.
106       @return Time in msecs */
107     int currentTurnPlayed();
108
109
110 signals:
111     void timeOutLoser();
112     void turnEnded();
113     /*! Emitted after 30 min played one turn.
114
115       Will cause screen not to keeped lit.
116       @since 1.1.2
117       */
118     void dontEatBattery();
119
120 public slots:    
121
122     /*! Refresh clock information */
123     virtual void repaintClock() = 0;
124
125     /*! Update clock information, check looser state and refresh */
126     virtual void updateClock();
127
128 protected:
129     ChessClock* another_; /*! Another player's clock */
130
131     bool loser_;        /*! Is player losed because of timeout */
132     int turn_;          /*! Current turn */
133     RunningStatus status_;
134     TurnInformation* currentTurn_;
135     bool dontEatBatteryEmitted_;
136
137     int timePlayedBeforeTurn_;    /*! Time played in this game BEFORE this turn msecs */
138     int timeAvailableBeforeTurn_; /*! Time available for play BEFORE this turn msecs !*/
139
140     bool isWhite_;      /*! True if white player */
141
142     QTime clockTime_;
143     QTimer updateTimer_;
144
145     static const int UPDATEINTERVAL = 1000; /** Clock updating interval in msecs */
146     static const int DONTEATBATTERYTIME = 30 * 60 * 1000; /*! Time after screen not to keep lit */
147 };
148
149 #endif // CHESSCLOCK_H