Pause button removed from ClocksWidget
[chessclock] / chessclockwindow.cpp
1 /**************************************************************************
2
3     Chess Clock
4
5     Copyright (c) Arto Hyvättinen 2010
6
7     This file is part of Chess Clock software.
8
9     Chess Clock is free software: you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation, either version 3 of the License, or
12     (at your option) any later version.
13
14     Chess Clock is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19
20 **************************************************************************/
21
22 #include "chessclockwindow.h"
23
24 #include "classes/clockswidget.h"
25 #include "classes/chessclockwidget.h"
26 #include "classes/startwidget.h"
27 #include "classes/timecontrol.h"
28 #include "classes/turninformation.h"
29
30 // Time controls
31 #include "classes/timecontrol/notimecontrol.h"
32 #include "classes/timecontrol/fischertimecontrol.h"
33 #include "classes/timecontrol/fischeraftertimecontrol.h"
34 #include "classes/timecontrol/delaytimecontrol.h"
35 #include "classes/timecontrol/delayaftertimecontrol.h"
36 #include "classes/timecontrol/hourglasstimecontrol.h"
37
38 #include <QIcon>
39 #include <QApplication>
40 #include <QMenuBar>
41 #include <QMessageBox>
42 #include <QStackedWidget>
43 #include <QProcess>
44
45
46 ChessClockWindow::ChessClockWindow(QWidget *parent)
47     : QMainWindow(parent)
48 {
49
50     showFullScreen();
51
52     setWindowIcon( QIcon(":/rc/pic/chessclock.png"));
53     setWindowTitle( QString("%1 %2").arg(qApp->applicationName()).arg(qApp->applicationVersion()) );
54
55     // Start widget to select time control
56     start_ = new StartWidget;
57     clocks_ = 0;
58
59     initTimeControls();
60
61     stack_ = new QStackedWidget;
62     stack_->addWidget(start_);
63
64     setCentralWidget( stack_ );
65
66     connect( start_, SIGNAL(selected(TimeControl*)), this, SLOT(startGame(TimeControl*)));
67
68
69     // Set up menu
70 //    menuBar()->addAction( tr("Pause"), this, SLOT(pause()));   // UNUSED - Pause button
71     menuBar()->addAction( tr("New game"), this, SLOT(newGame()));
72
73     menuBar()->addAction( tr("Visit web page"), this, SLOT(visitWeb()));
74     menuBar()->addAction( tr("About"),this, SLOT(about()));
75     menuBar()->addAction(tr("About Qt"), this, SLOT(aboutQt()));
76
77     //set the event filter to grap window deactivate
78
79     installEventFilter(this);
80
81 }
82
83 void ChessClockWindow::pause()
84 {
85     if( clocks_ )
86         clocks_->pause();
87 }
88
89 void ChessClockWindow::newGame()
90 {
91     pause();
92     if(  clocks_ == 0 ||  !clocks_->isPlayStarted()  || QMessageBox::question(this, tr("Start new game"),
93                               tr("Really quit the current game and start a new one?"),
94                               QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
95     {
96         stack_->setCurrentWidget(start_);
97
98         if( clocks_ )
99         {   stack_->removeWidget(clocks_);
100             delete clocks_;
101         }
102         clocks_=0;
103     }
104 }
105
106 void ChessClockWindow::visitWeb()
107 {
108     pause();
109     QProcess* process = new QProcess(this);
110     process->start(QString("browser --url=chessclock.garage.maemo.org"));
111 }
112
113 void ChessClockWindow::about()
114 {
115     pause();
116     QMessageBox::about(this, tr("About ChessClock"),
117                        tr("<h1>Chess Clock %1</h1>"
118                           "&copy;Arto Hyv&auml;ttinen 2010"
119                           "<p>Chess Clock is free software under the terms of GNU General Public License 3"
120                           "<p>Bugtracker and instructions at <a>http://chessclock.garage.maemo.org</a>"
121                           ).arg(qApp->applicationVersion())) ;
122 }
123
124 void ChessClockWindow::aboutQt()
125 {
126     pause();
127     qApp->aboutQt();
128 }
129
130
131 void ChessClockWindow::initTimeControls()
132 {
133     start_->addTimeControl( new NoTimeControl );
134     start_->addTimeControl( new FischerTimeControl);
135     start_->addTimeControl( new FischerAfterTimeControl);
136     start_->addTimeControl( new DelayTimeControl );
137     start_->addTimeControl( new DelayAfterTimeControl);
138     start_->addTimeControl( new HourGlassTimeControl);
139 }
140
141 void ChessClockWindow::startGame(TimeControl *timecontrol)
142 {
143     ClocksWidget* newWidget = timecontrol->initGame(false);
144     if( newWidget )
145     {
146
147         clocks_ = newWidget;
148         stack_->addWidget(clocks_);
149         stack_->setCurrentWidget(clocks_);
150         connect( clocks_, SIGNAL(TurnFinished(TurnInformation*)), this, SLOT(dontEatMemory(TurnInformation*)));
151     }
152 }
153
154
155 ChessClockWindow::~ChessClockWindow()
156 {
157
158 }
159
160 bool ChessClockWindow::eventFilter(QObject *obj, QEvent *event)
161 {
162     if (event->type() == QEvent::WindowDeactivate) {
163         pause();
164         return QObject::eventFilter(obj,event);
165     } else {
166         // standard event processing
167         return QObject::eventFilter(obj, event);
168     }
169 }
170
171 void ChessClockWindow::dontEatMemory(TurnInformation *turnInformation)
172 {
173     delete turnInformation; // hopefully don't cause Segematation Fault
174 }