Pause button hidden when game paused
[chessclock] / classes / wrappedclockswidget.cpp
1 /**************************************************************************
2
3    Chess Clock
4
5    This file is part of Chess Clock software.
6
7    (This file) Copyright (c) Heli Hyvättinen 2011
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
23 #include "wrappedclockswidget.h"
24 #include "classes/timecontrol/hourglassclock.h"
25 #include "classes/timecontrol/delayclock.h"
26 #include "classes/timecontrol/delayafterclock.h"
27 #include "classes/timecontrol/fischerclock.h"
28 #include "classes/timecontrol/fischerafterclock.h"
29 #include "classes/chessclockwidget.h"
30 #include <QDebug>
31
32
33 WrappedClocksWidget::WrappedClocksWidget(QObject *parent) :
34     QGraphicsProxyWidget()
35 {
36     pClocksWidget_ = NULL;
37     pWhiteClock_ = NULL;
38     pBlackClock_ = NULL;
39 }
40
41 void WrappedClocksWidget::startGame(TimeControlType timeControl, int whiteInitialTime, int whiteAdditionalTime, int whiteTurnsPerAddition, int blackInitialTime, int blackAdditionalTime, int blackTurnsPerAddition)
42 {
43
44     deleteOldWidgets();
45
46
47     switch (timeControl)
48     {
49         case NormalClock:
50
51             pWhiteClock_ = new ChessClockWidget (true);
52             pWhiteClock_->setTimeAvailable(whiteInitialTime);
53
54             pBlackClock_ = new ChessClockWidget (false);
55             pBlackClock_->setTimeAvailable(blackInitialTime);
56
57             break;
58
59         case AdditionBefore:
60
61             pWhiteClock_ = new FischerClock (true,whiteAdditionalTime,whiteTurnsPerAddition);
62             pWhiteClock_->setTimeAvailable(whiteInitialTime);
63
64             pBlackClock_ = new FischerClock (false,blackAdditionalTime,blackTurnsPerAddition);
65             pBlackClock_->setTimeAvailable(blackInitialTime);
66
67             break;
68
69         case AdditionAfter:
70
71             pWhiteClock_ = new FischerAfterClock (true,whiteAdditionalTime,whiteTurnsPerAddition);
72             pWhiteClock_->setTimeAvailable(whiteInitialTime);
73
74             pBlackClock_ = new FischerAfterClock (false,blackAdditionalTime,blackTurnsPerAddition);
75             pBlackClock_->setTimeAvailable(blackInitialTime);
76
77             break;
78
79         case Delay:
80
81             pWhiteClock_ = new DelayClock (true,whiteAdditionalTime);
82             pWhiteClock_->setTimeAvailable(whiteInitialTime);
83
84             pBlackClock_ = new DelayClock (false,blackAdditionalTime);
85             pBlackClock_->setTimeAvailable(blackInitialTime);
86
87             break;
88
89         case DelayAfter:
90
91             pWhiteClock_ = new DelayAfterClock (true,whiteAdditionalTime);
92             pWhiteClock_->setTimeAvailable(whiteInitialTime);
93
94             pBlackClock_ = new DelayAfterClock (false,blackAdditionalTime);
95             pBlackClock_->setTimeAvailable(blackInitialTime);
96
97             break;
98
99         case HourGlass:
100
101             pWhiteClock_ = new HourGlassClock (true);
102             pWhiteClock_->setTimeAvailable(whiteInitialTime);
103
104             pBlackClock_ = new HourGlassClock (false);
105             pBlackClock_->setTimeAvailable(blackInitialTime);
106
107             break;
108
109
110         default:
111
112         //QML has no type safety for enums (using int for real!)
113         //So there is possibility of getting invalid values if there is a bug in QML files
114
115         //Defaulting to normal clock
116
117
118         qDebug() << "Invalid time control type. Defaulting to Normal Clock.";
119
120         pWhiteClock_ = new ChessClockWidget (true);
121         pWhiteClock_->setTimeAvailable(whiteInitialTime);
122
123         pBlackClock_ = new ChessClockWidget (false);
124         pBlackClock_->setTimeAvailable(blackInitialTime);
125
126         break;
127
128
129     }
130
131
132     pClocksWidget_ = new ClocksWidget(pWhiteClock_, pBlackClock_);
133
134     connect(pClocksWidget_,SIGNAL(unPaused()),this,(SIGNAL(unPaused())));
135
136     pClocksWidget_->setAttribute(Qt::WA_NoSystemBackground);
137     setWidget(pClocksWidget_);
138
139 }
140
141  WrappedClocksWidget::~WrappedClocksWidget()
142 {
143      deleteOldWidgets();
144 }
145
146 bool WrappedClocksWidget::isPlayStarted()
147 {
148     if (!pClocksWidget_)
149         return false;
150
151     return  pClocksWidget_->isPlayStarted();
152 }
153
154 void WrappedClocksWidget::deleteOldWidgets()
155 {
156
157
158     if (pClocksWidget_)
159     {
160         pClocksWidget_->deleteLater();
161         pClocksWidget_ = NULL;
162     }
163
164     if (pWhiteClock_)
165     {
166         pWhiteClock_->deleteLater();
167         pWhiteClock_ = NULL;
168     }
169
170     if (pBlackClock_)
171     {
172         pBlackClock_->deleteLater();
173         pBlackClock_ = NULL;
174     }
175 }
176
177 void WrappedClocksWidget::pause()
178 {
179     if (pClocksWidget_)
180         pClocksWidget_->pause();
181 }
182
183 void WrappedClocksWidget::stopPlay()
184 {
185     if (pClocksWidget_)
186         pClocksWidget_->stopPlay();
187 }
188
189 void WrappedClocksWidget::saveScreen()
190 {
191     if (pClocksWidget_)
192         pClocksWidget_->saveScreen();
193 }
194