1 /**************************************************************************
5 Copyright (c) Arto Hyvättinen 2010
7 This file is part of Chess Clock software.
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.
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.
20 **************************************************************************/
22 #include "clockswidget.h"
23 #include "chessclock.h"
27 #include <QApplication>
28 #include <QHBoxLayout>
29 #include <QVBoxLayout>
32 #include <QMouseEvent>
34 ClocksWidget::ClocksWidget(ChessClock *white, ChessClock *black, QWidget *parent):
42 // Make layout for clocks
43 QHBoxLayout* clockLayout = new QHBoxLayout;
44 clockLayout->addWidget(white_ );
45 clockLayout->addWidget( black_ );
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 );
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
61 QVBoxLayout* mainLayout = new QVBoxLayout;
62 mainLayout->addLayout(clockLayout);
63 mainLayout->addWidget(pauseLabel_);
64 mainLayout->addWidget(welcomeLabel_);
66 setLayout( mainLayout);
70 white_->repaintClock();
71 black_->repaintClock();
74 white_->setAnother(black_);
75 black_->setAnother(white_);
77 delayTimer_.start(); // Initial start
79 recentX = recentY = -1;
82 ClocksWidget::~ClocksWidget()
88 void ClocksWidget::pause()
90 if(status_ == WhiteTurn)
94 pauseLabel_->setVisible(true);
96 else if( status_ == BlackTurn)
100 pauseLabel_->setVisible(true);
104 void ClocksWidget::stopPlay()
106 if( status_ == BlackTurn || status_ == BlackPause )
107 emit TurnFinished( black_->endTurn());
108 else if( status_ == WhiteTurn || status_ == WhitePause )
109 emit TurnFinished( white_->endTurn());
114 void ClocksWidget::mouseReleaseEvent(QMouseEvent *event)
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
125 delayTimer_.start(); // to reset delay timer!
130 welcomeLabel_->setVisible(false);
135 // White turn finished, then black
136 emit TurnFinished( white_->endTurn());
141 // Black finished, then white
142 emit TurnFinished( black_->endTurn());
148 pauseLabel_->setVisible(false);
149 white_->continueTurn();
154 pauseLabel_->setVisible(false);
155 black_->continueTurn();
159 emit ClickedWhenStopped();
164 recentX = event->x();
165 recentY = event->y();
168 int const ClocksWidget::CLICKDELAY;