Fischer time controller, start widget
[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 void ClocksWidget::pause()
80 {
81     if(status_ == WhiteTurn)
82     {
83         status_= WhitePause;
84         white_->pauseTurn();
85         pauseLabel_->setVisible(true);
86     }
87     else if( status_ == BlackTurn)
88     {
89         status_ = BlackPause;
90         black_->pauseTurn();
91         pauseLabel_->setVisible(true);
92     }
93 }
94
95 void ClocksWidget::stopPlay()
96 {
97     if( status_ == BlackTurn || status_ == BlackPause )
98        emit TurnFinished( black_->endTurn());
99     else if( status_ == WhiteTurn || status_ == WhitePause )
100         emit TurnFinished( white_->endTurn());
101     status_ = Stopped;
102 }
103
104
105 void ClocksWidget::mouseReleaseEvent(QMouseEvent *event)
106 {
107     if( delayTimer_.elapsed() > CLICKDELAY )  // To avoid double clicks
108     {
109         switch( status_)
110         {
111         case Welcome :
112             // Start game!
113             welcomeLabel_->setVisible(false);
114             white_->startTurn();
115             status_ = WhiteTurn;
116             break;
117         case WhiteTurn:
118             // White turn finished, then black
119             emit TurnFinished( white_->endTurn());
120             black_->startTurn();
121             status_=BlackTurn;
122             break;
123         case BlackTurn:
124             // Black finished, then white
125             emit TurnFinished( black_->endTurn());
126             white_->startTurn();
127             status_=WhiteTurn;
128             break;
129         case WhitePause:
130             // Continue play
131             pauseLabel_->setVisible(false);
132             white_->continueTurn();
133             status_=WhiteTurn;
134             break;
135         case BlackPause:
136             // Continue play
137             pauseLabel_->setVisible(false);
138             black_->continueTurn();
139             status_=BlackTurn;
140             break;
141         case Stopped:
142             emit ClickedWhenStopped();
143
144
145         }
146     }
147 }
148
149 int const ClocksWidget::CLICKDELAY;