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):
50 // Make layout for clocks
51 QHBoxLayout* clockLayout = new QHBoxLayout;
52 clockLayout->addWidget(white_ );
53 clockLayout->addWidget( black_ );
55 // Pause information label
56 pauseLabel_ = new QLabel( tr("<font color=yellow>Paused. Touch to continue.</font>"),this);
57 pauseLabel_->setFont( QFont("Helvetica",25));
58 pauseLabel_->setAlignment( Qt::AlignCenter);
59 pauseLabel_->setVisible( false );
61 // Welcome label for first touch
62 welcomeLabel_ = new QLabel( tr("<font color=green>Welcome! Please touch to start game.<br>"
63 "Then touch to end turn.</font>"),this);
64 welcomeLabel_->setFont( QFont("Helvetica",25));
65 welcomeLabel_->setAlignment( Qt::AlignCenter);
66 welcomeLabel_->setVisible( true ); // Show welcome message
68 //For Harmattan pause button is moved to the toolbar (in QML)
71 // pauseButton_ = new QToolButton;
72 // pauseButton_->setIcon( QIcon(":/rc/pic/pausebutton.png"));
73 // pauseButton_->setIconSize(QSize(75,75));
74 // connect(pauseButton_, SIGNAL(clicked()), this, SLOT(pause()));
75 // pauseButton_->setVisible(false);
78 QVBoxLayout* mainLayout = new QVBoxLayout;
79 mainLayout->addLayout(clockLayout);
81 // Extra layout and widget for information
82 QVBoxLayout* extraLayout = new QVBoxLayout;
83 extraLayout->addWidget(pauseLabel_);
84 extraLayout->addWidget(welcomeLabel_);
86 //For Harmattan pause button is moved to the toolbar (in QML)
88 // QHBoxLayout* pbLayout = new QHBoxLayout;
89 // pbLayout->addStretch();
90 //// pbLayout->addWidget(pauseButton_);
91 // pbLayout->addStretch();
92 // extraLayout->addLayout(pbLayout);
94 QWidget* extraWidget = new QWidget(this);
95 extraWidget->setLayout(extraLayout);
96 // Some fun event filtering to grap clicking welcome and pause labels...
97 extraWidget->installEventFilter(this);
98 pauseLabel_->installEventFilter(this);
99 welcomeLabel_->installEventFilter(this);
101 mainLayout->addWidget(extraWidget);
102 setLayout( mainLayout);
106 white_->repaintClock();
107 black_->repaintClock();
110 white_->setAnother(black_);
111 black_->setAnother(white_);
113 delayTimer_.start(); // Initial start
115 recentX = recentY = -1;
117 // ScreenLitKeeper to keep screen lit when playing
118 keeper_ = new ScreenLitKeeper(this);
120 connect( white, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
121 connect( black, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
124 ClocksWidget::~ClocksWidget()
130 void ClocksWidget::pause()
132 if(status_ == WhiteTurn)
136 pauseLabel_->setVisible(true);
137 //For Harmattan pause button is moved to the toolbar (in QML)
138 // pauseButton_->setVisible(false);
139 keeper_->keepScreenLit(false);
142 else if( status_ == BlackTurn)
144 status_ = BlackPause;
146 pauseLabel_->setVisible(true);
147 //For Harmattan pause button is moved to the toolbar (in QML)
148 // pauseButton_->setVisible(false);
149 keeper_->keepScreenLit(false);
153 void ClocksWidget::stopPlay()
155 if( status_ == BlackTurn || status_ == BlackPause )
156 emit TurnFinished( black_->endTurn());
157 else if( status_ == WhiteTurn || status_ == WhitePause )
158 emit TurnFinished( white_->endTurn());
162 void ClocksWidget::saveScreen()
164 keeper_->keepScreenLit(false);
168 void ClocksWidget::mousePressEvent(QMouseEvent *event)
171 qDebug() << "Mouse released";
172 // To avoid double clicks
173 // a) delay (default 1,2 secs) OR
174 // b) distance more than 90 pixels in axis.
175 if( delayTimer_.elapsed() > CLICKDELAY ||
176 std::abs( event->x() - recentX ) > 90 ||
177 std::abs( event->y() - recentY ) > 90
180 delayTimer_.start(); // to reset delay timer!
185 welcomeLabel_->setVisible(false);
186 //For Harmattan pause button is moved to the toolbar (in QML)
187 // pauseButton_->setVisible(true);
188 keeper_->keepScreenLit(true);
193 // White turn finished, then black
194 emit TurnFinished( white_->endTurn());
199 // Black finished, then white
200 emit TurnFinished( black_->endTurn());
206 keeper_->keepScreenLit(true);
207 pauseLabel_->setVisible(false);
208 //For Harmattan pause button is moved to the toolbar (in QML)
209 // pauseButton_->setVisible(true);
210 white_->continueTurn();
215 keeper_->keepScreenLit(true);
216 pauseLabel_->setVisible(false);
217 //For Harmattan pause button is moved to the toolbar (in QML)
218 // pauseButton_->setVisible(true);
219 black_->continueTurn();
223 emit ClickedWhenStopped();
228 recentX = event->x();
229 recentY = event->y();
232 // to grap clicking pause or welcome label
233 bool ClocksWidget::eventFilter(QObject *obj, QEvent *event)
235 // qDebug() << "Event for pause button or welcome label received " << event->type();
236 if (event->type() == QEvent::MouseButtonPress) {
237 QMouseEvent *mEvent = static_cast<QMouseEvent *>(event);
238 mousePressEvent( mEvent );
241 // standard event processing
242 return QObject::eventFilter(obj, event);
247 int const ClocksWidget::CLICKDELAY;