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));
65 // pauseLabel_->setAlignment( Qt::AlignCenter);
67 pauseLabel_->setAlignment( Qt::AlignHCenter | Qt::AlignTop);
69 pauseLabel_->setVisible( false );
71 // Welcome label for first touch
72 welcomeLabel_ = new QLabel( tr("<font color=green>Welcome! Please touch to start game.<br>"
73 "Then touch to end turn.</font>"),this);
74 welcomeLabel_->setFont( QFont("Helvetica",25));
75 welcomeLabel_->setAlignment( Qt::AlignCenter);
76 welcomeLabel_->setVisible( true ); // Show welcome message
78 //For Harmattan pause button is moved to the toolbar (in QML)
81 // pauseButton_ = new QToolButton;
82 // pauseButton_->setIcon( QIcon(":/rc/pic/pausebutton.png"));
83 // pauseButton_->setIconSize(QSize(75,75));
84 // connect(pauseButton_, SIGNAL(clicked()), this, SLOT(pause()));
85 // pauseButton_->setVisible(false);
88 QVBoxLayout* mainLayout = new QVBoxLayout;
89 mainLayout->addLayout(clockLayout);
91 // Extra layout and widget for information
92 QVBoxLayout* extraLayout = new QVBoxLayout;
93 extraLayout->addWidget(pauseLabel_);
94 extraLayout->addWidget(welcomeLabel_);
96 //For Harmattan pause button is moved to the toolbar (in QML)
98 // QHBoxLayout* pbLayout = new QHBoxLayout;
99 // pbLayout->addStretch();
100 //// pbLayout->addWidget(pauseButton_);
101 // pbLayout->addStretch();
102 // extraLayout->addLayout(pbLayout);
104 QWidget* extraWidget = new QWidget(this);
105 extraWidget->setLayout(extraLayout);
106 // Some fun event filtering to grap clicking welcome and pause labels...
107 extraWidget->installEventFilter(this);
108 pauseLabel_->installEventFilter(this);
109 welcomeLabel_->installEventFilter(this);
111 mainLayout->addWidget(extraWidget);
112 setLayout( mainLayout);
116 white_->repaintClock();
117 black_->repaintClock();
120 white_->setAnother(black_);
121 black_->setAnother(white_);
123 delayTimer_.start(); // Initial start
125 recentX = recentY = -1;
127 // ScreenLitKeeper to keep screen lit when playing
128 keeper_ = new ScreenLitKeeper(this);
130 connect( white, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
131 connect( black, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
134 ClocksWidget::~ClocksWidget()
140 void ClocksWidget::pause()
142 if(status_ == WhiteTurn)
146 pauseLabel_->setVisible(true);
147 //For Harmattan pause button is moved to the toolbar (in QML)
148 // pauseButton_->setVisible(false);
149 keeper_->keepScreenLit(false);
152 else if( status_ == BlackTurn)
154 status_ = BlackPause;
156 pauseLabel_->setVisible(true);
157 //For Harmattan pause button is moved to the toolbar (in QML)
158 // pauseButton_->setVisible(false);
159 keeper_->keepScreenLit(false);
163 void ClocksWidget::stopPlay()
165 if( status_ == BlackTurn || status_ == BlackPause )
166 emit TurnFinished( black_->endTurn());
167 else if( status_ == WhiteTurn || status_ == WhitePause )
168 emit TurnFinished( white_->endTurn());
172 void ClocksWidget::saveScreen()
174 keeper_->keepScreenLit(false);
178 void ClocksWidget::mousePressEvent(QMouseEvent *event)
182 // To avoid double clicks
183 // a) delay (default 1,2 secs) OR
184 // b) distance more than 90 pixels in axis.
185 if( delayTimer_.elapsed() > CLICKDELAY ||
186 std::abs( event->x() - recentX ) > 90 ||
187 std::abs( event->y() - recentY ) > 90
190 delayTimer_.start(); // to reset delay timer!
195 welcomeLabel_->setVisible(false);
196 //For Harmattan pause button is moved to the toolbar (in QML)
197 // pauseButton_->setVisible(true);
198 keeper_->keepScreenLit(true);
203 // White turn finished, then black
204 emit TurnFinished( white_->endTurn());
209 // Black finished, then white
210 emit TurnFinished( black_->endTurn());
216 keeper_->keepScreenLit(true);
217 pauseLabel_->setVisible(false);
218 //For Harmattan pause button is moved to the toolbar (in QML)
219 // pauseButton_->setVisible(true);
220 white_->continueTurn();
222 emit unPaused(); //For Harmattan, to inform QML
226 keeper_->keepScreenLit(true);
227 pauseLabel_->setVisible(false);
228 //For Harmattan pause button is moved to the toolbar (in QML)
229 // pauseButton_->setVisible(true);
230 black_->continueTurn();
232 emit unPaused(); //For Harmattan, to inform QML
235 emit ClickedWhenStopped();
240 recentX = event->x();
241 recentY = event->y();
244 // to grap clicking pause or welcome label
245 bool ClocksWidget::eventFilter(QObject *obj, QEvent *event)
247 // qDebug() << "Event for pause button or welcome label received " << event->type();
248 if (event->type() == QEvent::MouseButtonPress) {
249 QMouseEvent *mEvent = static_cast<QMouseEvent *>(event);
250 mousePressEvent( mEvent );
253 // standard event processing
254 return QObject::eventFilter(obj, event);
259 int const ClocksWidget::CLICKDELAY;