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