Screen keep lit when playing
[chessclock] / classes / clockswidget.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 "clockswidget.h"
23 #include "chessclock.h"
24
25 #include "screenlitkeeper.h"
26
27 #include <QLabel>
28 #include <QPixmap>
29 #include <QApplication>
30 #include <QHBoxLayout>
31 #include <QVBoxLayout>
32 #include <QFont>
33 #include <cstdlib>
34 #include <QMouseEvent>
35 #include <QToolButton>
36 #include <QSize>
37
38 ClocksWidget::ClocksWidget(ChessClock *white, ChessClock *black, QWidget *parent):
39     QWidget(parent)
40 {
41     // Set up clocks
42     white_ = white;
43     black_ = black;
44
45     // SET UP UI
46     // Make layout for clocks
47     QHBoxLayout* clockLayout = new QHBoxLayout;
48     clockLayout->addWidget(white_ );
49     clockLayout->addWidget( black_ );
50
51     // Pause information label
52     pauseLabel_ = new QLabel( tr("<font color=yellow>Paused. Touch to continue.</font>"));
53     pauseLabel_->setFont( QFont("Helvetica",25));
54     pauseLabel_->setAlignment( Qt::AlignCenter);
55     pauseLabel_->setVisible( false );
56
57     // Welcome label for first touch
58     welcomeLabel_ = new QLabel( tr("<font color=green>Welcome! Please touch to start game.<br>"
59                                    "Then touch to end turn.</font>"));
60     welcomeLabel_->setFont( QFont("Helvetica",25));
61     welcomeLabel_->setAlignment( Qt::AlignCenter);
62     welcomeLabel_->setVisible( true );  // Show welcome message
63
64     // Pause button
65     pauseButton_ = new QToolButton;
66     pauseButton_->setIcon( QIcon(":/rc/pic/pausebutton.png"));
67     pauseButton_->setIconSize(QSize(75,75));
68     connect(pauseButton_, SIGNAL(clicked()), this, SLOT(pause()));
69     pauseButton_->setVisible(false);
70
71     // Put all in layout
72     QVBoxLayout* mainLayout = new QVBoxLayout;
73     mainLayout->addLayout(clockLayout);
74     mainLayout->addWidget(pauseLabel_);
75     mainLayout->addWidget(welcomeLabel_);
76
77     QHBoxLayout* pbLayout = new QHBoxLayout;
78     pbLayout->addStretch();
79     pbLayout->addWidget(pauseButton_);
80     pbLayout->addStretch();
81     mainLayout->addLayout(pbLayout);
82
83     setLayout( mainLayout);
84     status_ = Welcome;
85
86     // First paint
87     white_->repaintClock();
88     black_->repaintClock();
89
90     // Set up others
91     white_->setAnother(black_);
92     black_->setAnother(white_);
93
94     delayTimer_.start(); // Initial start
95
96     recentX = recentY = -1;
97
98     // ScreenLitKeeper to keep screen lit when playing
99     keeper_ = new ScreenLitKeeper(this);
100 }
101
102 ClocksWidget::~ClocksWidget()
103 {
104     delete white_;
105     delete black_;
106 }
107
108 void ClocksWidget::pause()
109 {
110     if(status_ == WhiteTurn)
111     {
112         status_= WhitePause;
113         white_->pauseTurn();
114         pauseLabel_->setVisible(true);
115         pauseButton_->setVisible(false);
116         keeper_->keepScreenLit(false);
117
118     }
119     else if( status_ == BlackTurn)
120     {
121         status_ = BlackPause;
122         black_->pauseTurn();
123         pauseLabel_->setVisible(true);
124         pauseButton_->setVisible(false);
125         keeper_->keepScreenLit(false);
126     }
127 }
128
129 void ClocksWidget::stopPlay()
130 {
131     if( status_ == BlackTurn || status_ == BlackPause )
132        emit TurnFinished( black_->endTurn());
133     else if( status_ == WhiteTurn || status_ == WhitePause )
134         emit TurnFinished( white_->endTurn());
135     status_ = Stopped;
136 }
137
138
139 void ClocksWidget::mouseReleaseEvent(QMouseEvent *event)
140 {
141
142     // To avoid double clicks
143     // a) delay (default 1,2 secs) OR
144     // b) distance more than 90 pixels in axis.
145     if( delayTimer_.elapsed() > CLICKDELAY ||
146         std::abs( event->x() - recentX ) > 90 ||
147         std::abs( event->y() - recentY ) > 90
148         )
149     {
150         delayTimer_.start();    // to reset delay timer!
151         switch( status_)
152         {
153         case Welcome :
154             // Start game!
155             welcomeLabel_->setVisible(false);
156             pauseButton_->setVisible(true);
157             keeper_->keepScreenLit(true);
158             white_->startTurn();
159             status_ = WhiteTurn;
160             break;
161         case WhiteTurn:
162             // White turn finished, then black
163             emit TurnFinished( white_->endTurn());
164             black_->startTurn();
165             status_=BlackTurn;
166             break;
167         case BlackTurn:
168             // Black finished, then white
169             emit TurnFinished( black_->endTurn());
170             white_->startTurn();
171             status_=WhiteTurn;
172             break;
173         case WhitePause:
174             // Continue play
175             keeper_->keepScreenLit(true);
176             pauseLabel_->setVisible(false);
177             pauseButton_->setVisible(true);
178             white_->continueTurn();
179             status_=WhiteTurn;
180             break;
181         case BlackPause:
182             // Continue play
183             keeper_->keepScreenLit();
184             pauseLabel_->setVisible(false);
185             pauseButton_->setVisible(true);
186             black_->continueTurn();
187             status_=BlackTurn;
188             break;
189         case Stopped:
190             emit ClickedWhenStopped();
191
192
193         }
194     }
195     recentX = event->x();
196     recentY = event->y();
197 }
198
199 int const ClocksWidget::CLICKDELAY;