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