bdafcfd40bf6c35139cd1229965d1fd3c3fa4209
[chessclock] / classes / clockswidget.cpp
1  /**************************************************************************
2
3     Chess Clock
4
5     Copyright (c) Arto Hyvättinen 2010
6
7     Changes made for porting to Harmattan (c) Heli Hyvättinen 2011
8
9     This file is part of Chess Clock software.
10
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.
15
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.
20
21
22 **************************************************************************/
23
24 #include "clockswidget.h"
25 #include "chessclock.h"
26
27 #include "screenlitkeeper.h"
28
29 #include <QLabel>
30 #include <QPixmap>
31 #include <QApplication>
32 #include <QHBoxLayout>
33 #include <QVBoxLayout>
34 #include <QFont>
35 #include <cstdlib>
36 #include <QMouseEvent>
37 #include <QToolButton>
38 #include <QSize>
39 #include <QWidget>
40 #include <QDebug>
41
42 ClocksWidget::ClocksWidget(ChessClock *white, ChessClock *black, QWidget *parent):
43     QWidget(parent)
44 {
45     // Set up clocks
46     white_ = white;
47     black_ = black;
48
49     // SET UP UI
50     // Make layout for clocks
51     QHBoxLayout* clockLayout = new QHBoxLayout;
52     clockLayout->addWidget(white_ );
53     clockLayout->addWidget( black_ );
54
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 );
60
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
67
68 //For Harmattan pause button is moved to the toolbar (in QML)
69
70 //    // Pause button
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);
76
77     // Put all in layout
78     QVBoxLayout* mainLayout = new QVBoxLayout;
79     mainLayout->addLayout(clockLayout);
80
81     // Extra layout and widget for information
82     QVBoxLayout* extraLayout = new QVBoxLayout;
83     extraLayout->addWidget(pauseLabel_);
84     extraLayout->addWidget(welcomeLabel_);
85
86 //For Harmattan pause button is moved to the toolbar (in QML)
87
88 //    QHBoxLayout* pbLayout = new QHBoxLayout;
89 //    pbLayout->addStretch();
90 ////    pbLayout->addWidget(pauseButton_);
91 //    pbLayout->addStretch();
92 //    extraLayout->addLayout(pbLayout);
93
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);
100
101     mainLayout->addWidget(extraWidget);
102     setLayout( mainLayout);
103     status_ = Welcome;
104
105     // First paint
106     white_->repaintClock();
107     black_->repaintClock();
108
109     // Set up others
110     white_->setAnother(black_);
111     black_->setAnother(white_);
112
113     delayTimer_.start(); // Initial start
114
115     recentX = recentY = -1;
116
117     // ScreenLitKeeper to keep screen lit when playing
118     keeper_ = new ScreenLitKeeper(this);
119
120     connect( white, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
121     connect( black, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
122 }
123
124 ClocksWidget::~ClocksWidget()
125 {
126     delete white_;
127     delete black_;
128 }
129
130 void ClocksWidget::pause()
131 {
132     if(status_ == WhiteTurn)
133     {
134         status_= WhitePause;
135         white_->pauseTurn();
136         pauseLabel_->setVisible(true);
137  //For Harmattan pause button is moved to the toolbar (in QML)
138 //        pauseButton_->setVisible(false);
139         keeper_->keepScreenLit(false);
140
141     }
142     else if( status_ == BlackTurn)
143     {
144         status_ = BlackPause;
145         black_->pauseTurn();
146         pauseLabel_->setVisible(true);
147  //For Harmattan pause button is moved to the toolbar (in QML)
148 //        pauseButton_->setVisible(false);
149         keeper_->keepScreenLit(false);
150     }
151 }
152
153 void ClocksWidget::stopPlay()
154 {
155     if( status_ == BlackTurn || status_ == BlackPause )
156        emit TurnFinished( black_->endTurn());
157     else if( status_ == WhiteTurn || status_ == WhitePause )
158         emit TurnFinished( white_->endTurn());
159     status_ = Stopped;
160 }
161
162 void ClocksWidget::saveScreen()
163 {
164     keeper_->keepScreenLit(false);
165 }
166
167
168 void ClocksWidget::mousePressEvent(QMouseEvent *event)
169 {
170
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
178         )
179     {
180         delayTimer_.start();    // to reset delay timer!
181         switch( status_)
182         {
183         case Welcome :
184             // Start game!
185             welcomeLabel_->setVisible(false);
186  //For Harmattan pause button is moved to the toolbar (in QML)
187 //            pauseButton_->setVisible(true);
188             keeper_->keepScreenLit(true);
189             white_->startTurn();
190             status_ = WhiteTurn;
191             break;
192         case WhiteTurn:
193             // White turn finished, then black
194             emit TurnFinished( white_->endTurn());
195             black_->startTurn();
196             status_=BlackTurn;
197             break;
198         case BlackTurn:
199             // Black finished, then white
200             emit TurnFinished( black_->endTurn());
201             white_->startTurn();
202             status_=WhiteTurn;
203             break;
204         case WhitePause:
205             // Continue play
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();
211             status_=WhiteTurn;
212             break;
213         case BlackPause:
214             // Continue play
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();
220             status_=BlackTurn;
221             break;
222         case Stopped:
223             emit ClickedWhenStopped();
224
225
226         }
227     }
228     recentX = event->x();
229     recentY = event->y();
230 }
231
232 // to grap clicking pause or welcome label
233 bool ClocksWidget::eventFilter(QObject *obj, QEvent *event)
234 {
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 );
239         return true;
240     } else {
241         // standard event processing
242         return QObject::eventFilter(obj, event);
243     }
244 }
245
246
247 int const ClocksWidget::CLICKDELAY;