9882de566b0bdfae8d2ab13b5a6537e5514fa222
[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
96 void ClocksWidget::mouseReleaseEvent(QMouseEvent *event)
97 {
98     if( delayTimer_.elapsed() > CLICKDELAY )  // To avoid double clicks
99     {
100         switch( status_)
101         {
102         case Welcome :
103             // Start game!
104             welcomeLabel_->setVisible(false);
105             white_->startTurn();
106             status_ = WhiteTurn;
107             break;
108         case WhiteTurn:
109             // White turn finished, then black
110             emit TurnFinished( white_->endTurn());
111             black_->startTurn();
112             status_=BlackTurn;
113             break;
114         case BlackTurn:
115             // Black finished, then white
116             emit TurnFinished( black_->endTurn());
117             white_->startTurn();
118             status_=WhiteTurn;
119             break;
120
121
122         }
123     }
124 }
125
126 int const ClocksWidget::CLICKDELAY;