Clocks now occupy full screen width
[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
51
52     //Harmattan specific: brute force the width to a working value. (Adding a stretch didn't work.)
53
54     setMinimumWidth(860);
55
56     // Make layout for clocks
57     QHBoxLayout* clockLayout = new QHBoxLayout;
58     clockLayout->addWidget(white_ );
59     clockLayout->addWidget( black_ );
60
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 );
66
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
73
74 //For Harmattan pause button is moved to the toolbar (in QML)
75
76 //    // Pause button
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);
82
83     // Put all in layout
84     QVBoxLayout* mainLayout = new QVBoxLayout;
85     mainLayout->addLayout(clockLayout);
86
87     // Extra layout and widget for information
88     QVBoxLayout* extraLayout = new QVBoxLayout;
89     extraLayout->addWidget(pauseLabel_);
90     extraLayout->addWidget(welcomeLabel_);
91
92 //For Harmattan pause button is moved to the toolbar (in QML)
93
94 //    QHBoxLayout* pbLayout = new QHBoxLayout;
95 //    pbLayout->addStretch();
96 ////    pbLayout->addWidget(pauseButton_);
97 //    pbLayout->addStretch();
98 //    extraLayout->addLayout(pbLayout);
99
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);
106
107     mainLayout->addWidget(extraWidget);
108     setLayout( mainLayout);
109     status_ = Welcome;
110
111     // First paint
112     white_->repaintClock();
113     black_->repaintClock();
114
115     // Set up others
116     white_->setAnother(black_);
117     black_->setAnother(white_);
118
119     delayTimer_.start(); // Initial start
120
121     recentX = recentY = -1;
122
123     // ScreenLitKeeper to keep screen lit when playing
124     keeper_ = new ScreenLitKeeper(this);
125
126     connect( white, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
127     connect( black, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
128 }
129
130 ClocksWidget::~ClocksWidget()
131 {
132     delete white_;
133     delete black_;
134 }
135
136 void ClocksWidget::pause()
137 {
138     if(status_ == WhiteTurn)
139     {
140         status_= WhitePause;
141         white_->pauseTurn();
142         pauseLabel_->setVisible(true);
143  //For Harmattan pause button is moved to the toolbar (in QML)
144 //        pauseButton_->setVisible(false);
145         keeper_->keepScreenLit(false);
146
147     }
148     else if( status_ == BlackTurn)
149     {
150         status_ = BlackPause;
151         black_->pauseTurn();
152         pauseLabel_->setVisible(true);
153  //For Harmattan pause button is moved to the toolbar (in QML)
154 //        pauseButton_->setVisible(false);
155         keeper_->keepScreenLit(false);
156     }
157 }
158
159 void ClocksWidget::stopPlay()
160 {
161     if( status_ == BlackTurn || status_ == BlackPause )
162        emit TurnFinished( black_->endTurn());
163     else if( status_ == WhiteTurn || status_ == WhitePause )
164         emit TurnFinished( white_->endTurn());
165     status_ = Stopped;
166 }
167
168 void ClocksWidget::saveScreen()
169 {
170     keeper_->keepScreenLit(false);
171 }
172
173
174 void ClocksWidget::mousePressEvent(QMouseEvent *event)
175 {
176
177
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
184         )
185     {
186         delayTimer_.start();    // to reset delay timer!
187         switch( status_)
188         {
189         case Welcome :
190             // Start game!
191             welcomeLabel_->setVisible(false);
192  //For Harmattan pause button is moved to the toolbar (in QML)
193 //            pauseButton_->setVisible(true);
194             keeper_->keepScreenLit(true);
195             white_->startTurn();
196             status_ = WhiteTurn;
197             break;
198         case WhiteTurn:
199             // White turn finished, then black
200             emit TurnFinished( white_->endTurn());
201             black_->startTurn();
202             status_=BlackTurn;
203             break;
204         case BlackTurn:
205             // Black finished, then white
206             emit TurnFinished( black_->endTurn());
207             white_->startTurn();
208             status_=WhiteTurn;
209             break;
210         case WhitePause:
211             // Continue play
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();
217             status_=WhiteTurn;
218             break;
219         case BlackPause:
220             // Continue play
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();
226             status_=BlackTurn;
227             break;
228         case Stopped:
229             emit ClickedWhenStopped();
230
231
232         }
233     }
234     recentX = event->x();
235     recentY = event->y();
236 }
237
238 // to grap clicking pause or welcome label
239 bool ClocksWidget::eventFilter(QObject *obj, QEvent *event)
240 {
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 );
245         return true;
246     } else {
247         // standard event processing
248         return QObject::eventFilter(obj, event);
249     }
250 }
251
252
253 int const ClocksWidget::CLICKDELAY;