Added Visit web page action
[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
44 ChessClockWindow::ChessClockWindow(QWidget *parent)
45     : QMainWindow(parent)
46 {
47
48     setWindowIcon( QIcon(":/rc/pic/chessclock.png"));
49     setWindowTitle( QString("%1 %2").arg(qApp->applicationName()).arg(qApp->applicationVersion()) );
50
51     // Start widget to select time control
52     start_ = new StartWidget;
53     clocks_ = 0;
54
55     initTimeControls();
56
57     stack_ = new QStackedWidget;
58     stack_->addWidget(start_);
59
60     setCentralWidget( stack_ );
61
62     connect( start_, SIGNAL(selected(TimeControl*)), this, SLOT(startGame(TimeControl*)));
63
64     // Set up menu
65     menuBar()->addAction( tr("Pause"), this, SLOT(pause()));
66     menuBar()->addAction( tr("New game"), this, SLOT(newGame()));
67     menuBar()->addAction( tr("Visit web page"), this, SLOT(visitWeb()));
68
69 }
70
71 void ChessClockWindow::pause()
72 {
73     if( clocks_ )
74         clocks_->pause();
75 }
76
77 void ChessClockWindow::newGame()
78 {
79     pause();
80     if(  clocks_ == 0 ||  !clocks_->isPlayStarted()  || QMessageBox::question(this, tr("Start new game"),
81                               tr("Really quit current game and start new one with current settings?"),
82                               QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
83     {
84         stack_->setCurrentWidget(start_);
85
86         if( clocks_ )
87         {   stack_->removeWidget(clocks_);
88             delete clocks_;
89         }
90         clocks_=0;
91     }
92 }
93
94 void ChessClockWindow::visitWeb()
95 {
96     QProcess::execute(QString("browser --url=chessclock.garage.maemo.org"));
97 }
98
99
100 void ChessClockWindow::initTimeControls()
101 {
102     start_->addTimeControl( new NoTimeControl );
103     start_->addTimeControl( new FischerTimeControl);
104     start_->addTimeControl( new FischerAfterTimeControl);
105     start_->addTimeControl( new DelayTimeControl );
106     start_->addTimeControl( new DelayAfterTimeControl);
107     start_->addTimeControl( new HourGlassTimeControl);
108 }
109
110 void ChessClockWindow::startGame(TimeControl *timecontrol)
111 {
112     ClocksWidget* newWidget = timecontrol->initGame(false);
113     if( newWidget )
114     {
115         if( clocks_ )
116         {
117             stack_->removeWidget(clocks_);
118             delete clocks_;
119         }
120         clocks_ = newWidget;
121         stack_->addWidget(clocks_);
122         stack_->setCurrentWidget(clocks_);
123     }
124 }
125
126
127 ChessClockWindow::~ChessClockWindow()
128 {
129
130 }