1 /**************************************************************************
5 Copyright (c) Arto Hyvättinen 2010
7 Changes made for porting to Harmattan (c) Heli Hyvättinen 2011
9 This file is part of Chess Clock software.
11 Chess Clock is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 Chess Clock is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
22 **************************************************************************/
24 #include "clockswidget.h"
25 #include "chessclock.h"
27 #include "screenlitkeeper.h"
31 #include <QApplication>
32 #include <QHBoxLayout>
33 #include <QVBoxLayout>
36 #include <QMouseEvent>
37 #include <QToolButton>
42 ClocksWidget::ClocksWidget(ChessClock *white, ChessClock *black, QWidget *parent):
52 //Harmattan specific: brute force the width to a working value. (Adding a stretch didn't work.)
56 // Make layout for clocks
57 QHBoxLayout* clockLayout = new QHBoxLayout;
58 clockLayout->addWidget(white_ );
59 clockLayout->addWidget( black_ );
61 // Pause information label
62 pauseLabel_ = new QLabel( tr("<font color=yellow>Paused. Touch to continue.</font>"),this);
63 pauseLabel_->setFont( QFont("Helvetica",25));
64 pauseLabel_->setAlignment( Qt::AlignCenter);
65 pauseLabel_->setVisible( false );
67 // Welcome label for first touch
68 welcomeLabel_ = new QLabel( tr("<font color=green>Welcome! Please touch to start game.<br>"
69 "Then touch to end turn.</font>"),this);
70 welcomeLabel_->setFont( QFont("Helvetica",25));
71 welcomeLabel_->setAlignment( Qt::AlignCenter);
72 welcomeLabel_->setVisible( true ); // Show welcome message
74 //For Harmattan pause button is moved to the toolbar (in QML)
77 // pauseButton_ = new QToolButton;
78 // pauseButton_->setIcon( QIcon(":/rc/pic/pausebutton.png"));
79 // pauseButton_->setIconSize(QSize(75,75));
80 // connect(pauseButton_, SIGNAL(clicked()), this, SLOT(pause()));
81 // pauseButton_->setVisible(false);
84 QVBoxLayout* mainLayout = new QVBoxLayout;
85 mainLayout->addLayout(clockLayout);
87 // Extra layout and widget for information
88 QVBoxLayout* extraLayout = new QVBoxLayout;
89 extraLayout->addWidget(pauseLabel_);
90 extraLayout->addWidget(welcomeLabel_);
92 //For Harmattan pause button is moved to the toolbar (in QML)
94 // QHBoxLayout* pbLayout = new QHBoxLayout;
95 // pbLayout->addStretch();
96 //// pbLayout->addWidget(pauseButton_);
97 // pbLayout->addStretch();
98 // extraLayout->addLayout(pbLayout);
100 QWidget* extraWidget = new QWidget(this);
101 extraWidget->setLayout(extraLayout);
102 // Some fun event filtering to grap clicking welcome and pause labels...
103 extraWidget->installEventFilter(this);
104 pauseLabel_->installEventFilter(this);
105 welcomeLabel_->installEventFilter(this);
107 mainLayout->addWidget(extraWidget);
108 setLayout( mainLayout);
112 white_->repaintClock();
113 black_->repaintClock();
116 white_->setAnother(black_);
117 black_->setAnother(white_);
119 delayTimer_.start(); // Initial start
121 recentX = recentY = -1;
123 // ScreenLitKeeper to keep screen lit when playing
124 keeper_ = new ScreenLitKeeper(this);
126 connect( white, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
127 connect( black, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
130 ClocksWidget::~ClocksWidget()
136 void ClocksWidget::pause()
138 if(status_ == WhiteTurn)
142 pauseLabel_->setVisible(true);
143 //For Harmattan pause button is moved to the toolbar (in QML)
144 // pauseButton_->setVisible(false);
145 keeper_->keepScreenLit(false);
148 else if( status_ == BlackTurn)
150 status_ = BlackPause;
152 pauseLabel_->setVisible(true);
153 //For Harmattan pause button is moved to the toolbar (in QML)
154 // pauseButton_->setVisible(false);
155 keeper_->keepScreenLit(false);
159 void ClocksWidget::stopPlay()
161 if( status_ == BlackTurn || status_ == BlackPause )
162 emit TurnFinished( black_->endTurn());
163 else if( status_ == WhiteTurn || status_ == WhitePause )
164 emit TurnFinished( white_->endTurn());
168 void ClocksWidget::saveScreen()
170 keeper_->keepScreenLit(false);
174 void ClocksWidget::mousePressEvent(QMouseEvent *event)
178 // To avoid double clicks
179 // a) delay (default 1,2 secs) OR
180 // b) distance more than 90 pixels in axis.
181 if( delayTimer_.elapsed() > CLICKDELAY ||
182 std::abs( event->x() - recentX ) > 90 ||
183 std::abs( event->y() - recentY ) > 90
186 delayTimer_.start(); // to reset delay timer!
191 welcomeLabel_->setVisible(false);
192 //For Harmattan pause button is moved to the toolbar (in QML)
193 // pauseButton_->setVisible(true);
194 keeper_->keepScreenLit(true);
199 // White turn finished, then black
200 emit TurnFinished( white_->endTurn());
205 // Black finished, then white
206 emit TurnFinished( black_->endTurn());
212 keeper_->keepScreenLit(true);
213 pauseLabel_->setVisible(false);
214 //For Harmattan pause button is moved to the toolbar (in QML)
215 // pauseButton_->setVisible(true);
216 white_->continueTurn();
221 keeper_->keepScreenLit(true);
222 pauseLabel_->setVisible(false);
223 //For Harmattan pause button is moved to the toolbar (in QML)
224 // pauseButton_->setVisible(true);
225 black_->continueTurn();
229 emit ClickedWhenStopped();
234 recentX = event->x();
235 recentY = event->y();
238 // to grap clicking pause or welcome label
239 bool ClocksWidget::eventFilter(QObject *obj, QEvent *event)
241 // qDebug() << "Event for pause button or welcome label received " << event->type();
242 if (event->type() == QEvent::MouseButtonPress) {
243 QMouseEvent *mEvent = static_cast<QMouseEvent *>(event);
244 mousePressEvent( mEvent );
247 // standard event processing
248 return QObject::eventFilter(obj, event);
253 int const ClocksWidget::CLICKDELAY;