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