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>
33 #include <QToolButton>
36 ClocksWidget::ClocksWidget(ChessClock *white, ChessClock *black, QWidget *parent):
44 // Make layout for clocks
45 QHBoxLayout* clockLayout = new QHBoxLayout;
46 clockLayout->addWidget(white_ );
47 clockLayout->addWidget( black_ );
49 // Pause information label
50 pauseLabel_ = new QLabel( tr("<font color=yellow>Paused. Touch to continue.</font>"));
51 pauseLabel_->setFont( QFont("Helvetica",25));
52 pauseLabel_->setAlignment( Qt::AlignCenter);
53 pauseLabel_->setVisible( false );
55 // Welcome label for first touch
56 welcomeLabel_ = new QLabel( tr("<font color=green>Welcome! Please touch to start game.<br>"
57 "Then touch to end turn.</font>"));
58 welcomeLabel_->setFont( QFont("Helvetica",25));
59 welcomeLabel_->setAlignment( Qt::AlignCenter);
60 welcomeLabel_->setVisible( true ); // Show welcome message
63 pauseButton_ = new QToolButton;
64 pauseButton_->setIcon( QIcon(":/rc/pic/pausebutton.png"));
65 pauseButton_->setIconSize(QSize(75,75));
66 connect(pauseButton_, SIGNAL(clicked()), this, SLOT(pause()));
67 pauseButton_->setVisible(false);
70 QVBoxLayout* mainLayout = new QVBoxLayout;
71 mainLayout->addLayout(clockLayout);
72 mainLayout->addWidget(pauseLabel_);
73 mainLayout->addWidget(welcomeLabel_);
75 QHBoxLayout* pbLayout = new QHBoxLayout;
76 pbLayout->addStretch();
77 pbLayout->addWidget(pauseButton_);
78 pbLayout->addStretch();
79 mainLayout->addLayout(pbLayout);
81 setLayout( mainLayout);
85 white_->repaintClock();
86 black_->repaintClock();
89 white_->setAnother(black_);
90 black_->setAnother(white_);
92 delayTimer_.start(); // Initial start
94 recentX = recentY = -1;
97 ClocksWidget::~ClocksWidget()
103 void ClocksWidget::pause()
105 if(status_ == WhiteTurn)
109 pauseLabel_->setVisible(true);
110 pauseButton_->setVisible(false);
112 else if( status_ == BlackTurn)
114 status_ = BlackPause;
116 pauseLabel_->setVisible(true);
117 pauseButton_->setVisible(false);
121 void ClocksWidget::stopPlay()
123 if( status_ == BlackTurn || status_ == BlackPause )
124 emit TurnFinished( black_->endTurn());
125 else if( status_ == WhiteTurn || status_ == WhitePause )
126 emit TurnFinished( white_->endTurn());
131 void ClocksWidget::mouseReleaseEvent(QMouseEvent *event)
134 // To avoid double clicks
135 // a) delay (default 1,2 secs) OR
136 // b) distance more than 90 pixels in axis.
137 if( delayTimer_.elapsed() > CLICKDELAY ||
138 std::abs( event->x() - recentX ) > 90 ||
139 std::abs( event->y() - recentY ) > 90
142 delayTimer_.start(); // to reset delay timer!
147 welcomeLabel_->setVisible(false);
148 pauseButton_->setVisible(true);
153 // White turn finished, then black
154 emit TurnFinished( white_->endTurn());
159 // Black finished, then white
160 emit TurnFinished( black_->endTurn());
166 pauseLabel_->setVisible(false);
167 pauseButton_->setVisible(true);
168 white_->continueTurn();
173 pauseLabel_->setVisible(false);
174 pauseButton_->setVisible(true);
175 black_->continueTurn();
179 emit ClickedWhenStopped();
184 recentX = event->x();
185 recentY = event->y();
188 int const ClocksWidget::CLICKDELAY;