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"
25 #include "screenlitkeeper.h"
29 #include <QApplication>
30 #include <QHBoxLayout>
31 #include <QVBoxLayout>
34 #include <QMouseEvent>
35 #include <QToolButton>
38 ClocksWidget::ClocksWidget(ChessClock *white, ChessClock *black, QWidget *parent):
46 // Make layout for clocks
47 QHBoxLayout* clockLayout = new QHBoxLayout;
48 clockLayout->addWidget(white_ );
49 clockLayout->addWidget( black_ );
51 // Pause information label
52 pauseLabel_ = new QLabel( tr("<font color=yellow>Paused. Touch to continue.</font>"));
53 pauseLabel_->setFont( QFont("Helvetica",25));
54 pauseLabel_->setAlignment( Qt::AlignCenter);
55 pauseLabel_->setVisible( false );
57 // Welcome label for first touch
58 welcomeLabel_ = new QLabel( tr("<font color=green>Welcome! Please touch to start game.<br>"
59 "Then touch to end turn.</font>"));
60 welcomeLabel_->setFont( QFont("Helvetica",25));
61 welcomeLabel_->setAlignment( Qt::AlignCenter);
62 welcomeLabel_->setVisible( true ); // Show welcome message
65 pauseButton_ = new QToolButton;
66 pauseButton_->setIcon( QIcon(":/rc/pic/pausebutton.png"));
67 pauseButton_->setIconSize(QSize(75,75));
68 connect(pauseButton_, SIGNAL(clicked()), this, SLOT(pause()));
69 pauseButton_->setVisible(false);
72 QVBoxLayout* mainLayout = new QVBoxLayout;
73 mainLayout->addLayout(clockLayout);
74 mainLayout->addWidget(pauseLabel_);
75 mainLayout->addWidget(welcomeLabel_);
77 QHBoxLayout* pbLayout = new QHBoxLayout;
78 pbLayout->addStretch();
79 pbLayout->addWidget(pauseButton_);
80 pbLayout->addStretch();
81 mainLayout->addLayout(pbLayout);
83 setLayout( mainLayout);
87 white_->repaintClock();
88 black_->repaintClock();
91 white_->setAnother(black_);
92 black_->setAnother(white_);
94 delayTimer_.start(); // Initial start
96 recentX = recentY = -1;
98 // ScreenLitKeeper to keep screen lit when playing
99 keeper_ = new ScreenLitKeeper(this);
102 ClocksWidget::~ClocksWidget()
108 void ClocksWidget::pause()
110 if(status_ == WhiteTurn)
114 pauseLabel_->setVisible(true);
115 pauseButton_->setVisible(false);
116 keeper_->keepScreenLit(false);
119 else if( status_ == BlackTurn)
121 status_ = BlackPause;
123 pauseLabel_->setVisible(true);
124 pauseButton_->setVisible(false);
125 keeper_->keepScreenLit(false);
129 void ClocksWidget::stopPlay()
131 if( status_ == BlackTurn || status_ == BlackPause )
132 emit TurnFinished( black_->endTurn());
133 else if( status_ == WhiteTurn || status_ == WhitePause )
134 emit TurnFinished( white_->endTurn());
139 void ClocksWidget::mouseReleaseEvent(QMouseEvent *event)
142 // To avoid double clicks
143 // a) delay (default 1,2 secs) OR
144 // b) distance more than 90 pixels in axis.
145 if( delayTimer_.elapsed() > CLICKDELAY ||
146 std::abs( event->x() - recentX ) > 90 ||
147 std::abs( event->y() - recentY ) > 90
150 delayTimer_.start(); // to reset delay timer!
155 welcomeLabel_->setVisible(false);
156 pauseButton_->setVisible(true);
157 keeper_->keepScreenLit(true);
162 // White turn finished, then black
163 emit TurnFinished( white_->endTurn());
168 // Black finished, then white
169 emit TurnFinished( black_->endTurn());
175 keeper_->keepScreenLit(true);
176 pauseLabel_->setVisible(false);
177 pauseButton_->setVisible(true);
178 white_->continueTurn();
183 keeper_->keepScreenLit();
184 pauseLabel_->setVisible(false);
185 pauseButton_->setVisible(true);
186 black_->continueTurn();
190 emit ClickedWhenStopped();
195 recentX = event->x();
196 recentY = event->y();
199 int const ClocksWidget::CLICKDELAY;