Pause button hidden when game paused
[chessclock] / classes / clockswidget.cpp
1  /**************************************************************************
2
3     Chess Clock
4
5     Copyright (c) Arto Hyvättinen 2010
6
7     Changes made for porting to Harmattan (c) Heli Hyvättinen 2011
8
9     This file is part of Chess Clock software.
10
11     Chess Clock is free software: you can redistribute it and/or modify
12     it under the terms of the GNU General Public License as published by
13     the Free Software Foundation, either version 3 of the License, or
14     (at your option) any later version.
15
16     Chess Clock is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU General Public License for more details.
20
21
22 **************************************************************************/
23
24 #include "clockswidget.h"
25 #include "chessclock.h"
26
27 #include "screenlitkeeper.h"
28
29 #include <QLabel>
30 #include <QPixmap>
31 #include <QApplication>
32 #include <QHBoxLayout>
33 #include <QVBoxLayout>
34 #include <QFont>
35 #include <cstdlib>
36 #include <QMouseEvent>
37 #include <QToolButton>
38 #include <QSize>
39 #include <QWidget>
40 #include <QDebug>
41
42 ClocksWidget::ClocksWidget(ChessClock *white, ChessClock *black, QWidget *parent):
43     QWidget(parent)
44 {
45     // Set up clocks
46     white_ = white;
47     black_ = black;
48
49     // SET UP UI
50
51
52     //Harmattan specific: brute force the width to a working value. (Adding a stretch didn't work.)
53
54     setMinimumWidth(860);
55
56     // Make layout for clocks
57     QHBoxLayout* clockLayout = new QHBoxLayout;
58     clockLayout->addWidget(white_ );
59     clockLayout->addWidget( black_ );
60
61     // Pause information label
62     pauseLabel_ = new QLabel( tr("<font color=yellow>Paused. Touch to continue.</font>"),this);
63     pauseLabel_->setFont( QFont("Helvetica",25));
64  //   For Fremantle:
65 //    pauseLabel_->setAlignment( Qt::AlignCenter);
66  //For Harmattan (QML)
67         pauseLabel_->setAlignment( Qt::AlignHCenter | Qt::AlignTop);
68
69     pauseLabel_->setVisible( false );
70
71     // Welcome label for first touch
72     welcomeLabel_ = new QLabel( tr("<font color=green>Welcome! Please touch to start game.<br>"
73                                    "Then touch to end turn.</font>"),this);
74     welcomeLabel_->setFont( QFont("Helvetica",25));
75     welcomeLabel_->setAlignment( Qt::AlignCenter);
76     welcomeLabel_->setVisible( true );  // Show welcome message
77
78 //For Harmattan pause button is moved to the toolbar (in QML)
79
80 //    // Pause button
81 //    pauseButton_ = new QToolButton;
82 //    pauseButton_->setIcon( QIcon(":/rc/pic/pausebutton.png"));
83 //    pauseButton_->setIconSize(QSize(75,75));
84 //    connect(pauseButton_, SIGNAL(clicked()), this, SLOT(pause()));
85 //    pauseButton_->setVisible(false);
86
87     // Put all in layout
88     QVBoxLayout* mainLayout = new QVBoxLayout;
89     mainLayout->addLayout(clockLayout);
90
91     // Extra layout and widget for information
92     QVBoxLayout* extraLayout = new QVBoxLayout;
93     extraLayout->addWidget(pauseLabel_);
94     extraLayout->addWidget(welcomeLabel_);
95
96 //For Harmattan pause button is moved to the toolbar (in QML)
97
98 //    QHBoxLayout* pbLayout = new QHBoxLayout;
99 //    pbLayout->addStretch();
100 ////    pbLayout->addWidget(pauseButton_);
101 //    pbLayout->addStretch();
102 //    extraLayout->addLayout(pbLayout);
103
104     QWidget* extraWidget = new QWidget(this);
105     extraWidget->setLayout(extraLayout);
106     // Some fun event filtering to grap clicking welcome and pause labels...
107     extraWidget->installEventFilter(this);
108     pauseLabel_->installEventFilter(this);
109     welcomeLabel_->installEventFilter(this);
110
111     mainLayout->addWidget(extraWidget);
112     setLayout( mainLayout);
113     status_ = Welcome;
114
115     // First paint
116     white_->repaintClock();
117     black_->repaintClock();
118
119     // Set up others
120     white_->setAnother(black_);
121     black_->setAnother(white_);
122
123     delayTimer_.start(); // Initial start
124
125     recentX = recentY = -1;
126
127     // ScreenLitKeeper to keep screen lit when playing
128     keeper_ = new ScreenLitKeeper(this);
129
130     connect( white, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
131     connect( black, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
132 }
133
134 ClocksWidget::~ClocksWidget()
135 {
136     delete white_;
137     delete black_;
138 }
139
140 void ClocksWidget::pause()
141 {
142     if(status_ == WhiteTurn)
143     {
144         status_= WhitePause;
145         white_->pauseTurn();
146         pauseLabel_->setVisible(true);
147  //For Harmattan pause button is moved to the toolbar (in QML)
148 //        pauseButton_->setVisible(false);
149         keeper_->keepScreenLit(false);
150
151     }
152     else if( status_ == BlackTurn)
153     {
154         status_ = BlackPause;
155         black_->pauseTurn();
156         pauseLabel_->setVisible(true);
157  //For Harmattan pause button is moved to the toolbar (in QML)
158 //        pauseButton_->setVisible(false);
159         keeper_->keepScreenLit(false);
160     }
161 }
162
163 void ClocksWidget::stopPlay()
164 {
165     if( status_ == BlackTurn || status_ == BlackPause )
166        emit TurnFinished( black_->endTurn());
167     else if( status_ == WhiteTurn || status_ == WhitePause )
168         emit TurnFinished( white_->endTurn());
169     status_ = Stopped;
170 }
171
172 void ClocksWidget::saveScreen()
173 {
174     keeper_->keepScreenLit(false);
175 }
176
177
178 void ClocksWidget::mousePressEvent(QMouseEvent *event)
179 {
180
181
182     // To avoid double clicks
183     // a) delay (default 1,2 secs) OR
184     // b) distance more than 90 pixels in axis.
185     if( delayTimer_.elapsed() > CLICKDELAY ||
186         std::abs( event->x() - recentX ) > 90 ||
187         std::abs( event->y() - recentY ) > 90
188         )
189     {
190         delayTimer_.start();    // to reset delay timer!
191         switch( status_)
192         {
193         case Welcome :
194             // Start game!
195             welcomeLabel_->setVisible(false);
196  //For Harmattan pause button is moved to the toolbar (in QML)
197 //            pauseButton_->setVisible(true);
198             keeper_->keepScreenLit(true);
199             white_->startTurn();
200             status_ = WhiteTurn;
201             break;
202         case WhiteTurn:
203             // White turn finished, then black
204             emit TurnFinished( white_->endTurn());
205             black_->startTurn();
206             status_=BlackTurn;
207             break;
208         case BlackTurn:
209             // Black finished, then white
210             emit TurnFinished( black_->endTurn());
211             white_->startTurn();
212             status_=WhiteTurn;
213             break;
214         case WhitePause:
215             // Continue play
216             keeper_->keepScreenLit(true);
217             pauseLabel_->setVisible(false);
218 //For Harmattan pause button is moved to the toolbar (in QML)
219 //            pauseButton_->setVisible(true);
220             white_->continueTurn();
221             status_=WhiteTurn;
222             emit unPaused(); //For Harmattan, to inform QML
223             break;
224         case BlackPause:
225             // Continue play
226             keeper_->keepScreenLit(true);
227             pauseLabel_->setVisible(false);
228 //For Harmattan pause button is moved to the toolbar (in QML)
229 //            pauseButton_->setVisible(true);
230             black_->continueTurn();
231             status_=BlackTurn;
232             emit unPaused(); //For Harmattan, to inform QML
233             break;
234         case Stopped:
235             emit ClickedWhenStopped();
236
237
238         }
239     }
240     recentX = event->x();
241     recentY = event->y();
242 }
243
244 // to grap clicking pause or welcome label
245 bool ClocksWidget::eventFilter(QObject *obj, QEvent *event)
246 {
247 //    qDebug() << "Event for pause button or welcome label received " << event->type();
248     if (event->type() == QEvent::MouseButtonPress) {
249         QMouseEvent *mEvent = static_cast<QMouseEvent *>(event);
250         mousePressEvent( mEvent );
251         return true;
252     } else {
253         // standard event processing
254         return QObject::eventFilter(obj, event);
255     }
256 }
257
258
259 int const ClocksWidget::CLICKDELAY;