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