Screen keep lit when playing
[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
29 // Time controls
30 #include "classes/timecontrol/notimecontrol.h"
31 #include "classes/timecontrol/fischertimecontrol.h"
32 #include "classes/timecontrol/fischeraftertimecontrol.h"
33 #include "classes/timecontrol/delaytimecontrol.h"
34 #include "classes/timecontrol/delayaftertimecontrol.h"
35 #include "classes/timecontrol/hourglasstimecontrol.h"
36
37 #include <QIcon>
38 #include <QApplication>
39 #include <QMenuBar>
40 #include <QMessageBox>
41 #include <QStackedWidget>
42 #include <QProcess>
43 #include <QAction>
44
45 ChessClockWindow::ChessClockWindow(QWidget *parent)
46     : QMainWindow(parent)
47 {
48
49     setWindowIcon( QIcon(":/rc/pic/chessclock.png"));
50     setWindowTitle( QString("%1 %2").arg(qApp->applicationName()).arg(qApp->applicationVersion()) );
51
52     // Start widget to select time control
53     start_ = new StartWidget;
54     clocks_ = 0;
55
56     initTimeControls();
57
58     stack_ = new QStackedWidget;
59     stack_->addWidget(start_);
60
61     setCentralWidget( stack_ );
62
63     connect( start_, SIGNAL(selected(TimeControl*)), this, SLOT(startGame(TimeControl*)));
64
65
66     // Set up menu
67 //    menuBar()->addAction( tr("Pause"), this, SLOT(pause()));   // UNUSED - Pause button
68     menuBar()->addAction( tr("New game"), this, SLOT(newGame()));
69     menuBar()->addAction( keepAction );
70     menuBar()->addAction( tr("Visit web page"), this, SLOT(visitWeb()));
71     menuBar()->addAction( tr("About"),this, SLOT(about()));
72     menuBar()->addAction(tr("About Qt"), this, SLOT(aboutQt()));
73
74 }
75
76 void ChessClockWindow::pause()
77 {
78     if( clocks_ )
79         clocks_->pause();
80 }
81
82 void ChessClockWindow::newGame()
83 {
84     pause();
85     if(  clocks_ == 0 ||  !clocks_->isPlayStarted()  || QMessageBox::question(this, tr("Start new game"),
86                               tr("Really quit the current game and start a new one?"),
87                               QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
88     {
89         stack_->setCurrentWidget(start_);
90
91         if( clocks_ )
92         {   stack_->removeWidget(clocks_);
93             delete clocks_;
94         }
95         clocks_=0;
96     }
97 }
98
99 void ChessClockWindow::visitWeb()
100 {
101     pause();
102     QProcess* process = new QProcess(this);
103     process->start(QString("browser --url=chessclock.garage.maemo.org"));
104 }
105
106 void ChessClockWindow::about()
107 {
108     pause();
109     QMessageBox::about(this, tr("About ChessClock"),
110                        tr("<h1>Chess Clock %1</h1>"
111                           "&copy;Arto Hyv&auml;ttinen 2010"
112                           "<p>Chess Clock is free software under the terms of GNU General Public License 3"
113                           "<p>Bugtracker and instructions at <a>http://chessclock.garage.maemo.org</a>"
114                           ).arg(qApp->applicationVersion())) ;
115 }
116
117 void ChessClockWindow::aboutQt()
118 {
119     pause();
120     qApp->aboutQt();
121 }
122
123
124 void ChessClockWindow::initTimeControls()
125 {
126     start_->addTimeControl( new NoTimeControl );
127     start_->addTimeControl( new FischerTimeControl);
128     start_->addTimeControl( new FischerAfterTimeControl);
129     start_->addTimeControl( new DelayTimeControl );
130     start_->addTimeControl( new DelayAfterTimeControl);
131     start_->addTimeControl( new HourGlassTimeControl);
132 }
133
134 void ChessClockWindow::startGame(TimeControl *timecontrol)
135 {
136     ClocksWidget* newWidget = timecontrol->initGame(false);
137     if( newWidget )
138     {
139
140         clocks_ = newWidget;
141         stack_->addWidget(clocks_);
142         stack_->setCurrentWidget(clocks_);
143     }
144 }
145
146
147 ChessClockWindow::~ChessClockWindow()
148 {
149
150 }