Updated the web pages
[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     setWindowIcon( QIcon(":/rc/pic/chessclock.png"));
51     setWindowTitle( QString("%1 %2").arg(qApp->applicationName()).arg(qApp->applicationVersion()) );
52
53     // Start widget to select time control
54     start_ = new StartWidget;
55     clocks_ = 0;
56
57     initTimeControls();
58
59     stack_ = new QStackedWidget;
60     stack_->addWidget(start_);
61
62     setCentralWidget( stack_ );
63
64     connect( start_, SIGNAL(selected(TimeControl*)), this, SLOT(startGame(TimeControl*)));
65
66
67     // Set up menu
68 //    menuBar()->addAction( tr("Pause"), this, SLOT(pause()));   // UNUSED - Pause button
69     menuBar()->addAction( tr("New game"), this, SLOT(newGame()));
70
71     menuBar()->addAction( tr("Visit web page"), this, SLOT(visitWeb()));
72     menuBar()->addAction( tr("About"),this, SLOT(about()));
73     menuBar()->addAction(tr("About Qt"), this, SLOT(aboutQt()));
74
75     //set the event filter to grap window deactivate
76
77     installEventFilter(this);
78
79 }
80
81 void ChessClockWindow::pause()
82 {
83     if( clocks_ )
84         clocks_->pause();
85 }
86
87 void ChessClockWindow::newGame()
88 {
89     pause();
90     if(  clocks_ == 0 ||  !clocks_->isPlayStarted()  || QMessageBox::question(this, tr("Start new game"),
91                               tr("Really quit the current game and start a new one?"),
92                               QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
93     {
94         stack_->setCurrentWidget(start_);
95
96         if( clocks_ )
97         {   stack_->removeWidget(clocks_);
98             delete clocks_;
99         }
100         clocks_=0;
101     }
102 }
103
104 void ChessClockWindow::visitWeb()
105 {
106     pause();
107     QProcess* process = new QProcess(this);
108     process->start(QString("browser --url=chessclock.garage.maemo.org"));
109 }
110
111 void ChessClockWindow::about()
112 {
113     pause();
114     QMessageBox::about(this, tr("About ChessClock"),
115                        tr("<h1>Chess Clock %1</h1>"
116                           "&copy;Arto Hyv&auml;ttinen 2010"
117                           "<p>Chess Clock is free software under the terms of GNU General Public License 3"
118                           "<p>Bugtracker and instructions at <a>http://chessclock.garage.maemo.org</a>"
119                           ).arg(qApp->applicationVersion())) ;
120 }
121
122 void ChessClockWindow::aboutQt()
123 {
124     pause();
125     qApp->aboutQt();
126 }
127
128
129 void ChessClockWindow::initTimeControls()
130 {
131     start_->addTimeControl( new NoTimeControl );
132     start_->addTimeControl( new FischerTimeControl);
133     start_->addTimeControl( new FischerAfterTimeControl);
134     start_->addTimeControl( new DelayTimeControl );
135     start_->addTimeControl( new DelayAfterTimeControl);
136     start_->addTimeControl( new HourGlassTimeControl);
137 }
138
139 void ChessClockWindow::startGame(TimeControl *timecontrol)
140 {
141     ClocksWidget* newWidget = timecontrol->initGame(false);
142     if( newWidget )
143     {
144
145         clocks_ = newWidget;
146         stack_->addWidget(clocks_);
147         stack_->setCurrentWidget(clocks_);
148         connect( clocks_, SIGNAL(TurnFinished(TurnInformation*)), this, SLOT(dontEatMemory(TurnInformation*)));
149     }
150 }
151
152
153 ChessClockWindow::~ChessClockWindow()
154 {
155
156 }
157
158 bool ChessClockWindow::eventFilter(QObject *obj, QEvent *event)
159 {
160     if (event->type() == QEvent::WindowDeactivate) {
161         pause();
162         return QObject::eventFilter(obj,event);
163     } else {
164         // standard event processing
165         return QObject::eventFilter(obj, event);
166     }
167 }
168
169 void ChessClockWindow::dontEatMemory(TurnInformation *turnInformation)
170 {
171     delete turnInformation; // hopefully don't cause Segematation Fault
172 }