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