00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "chessclockwindow.h"
00023
00024 #include "classes/clockswidget.h"
00025 #include "classes/chessclockwidget.h"
00026 #include "classes/startwidget.h"
00027 #include "classes/timecontrol.h"
00028 #include "classes/turninformation.h"
00029
00030
00031 #include "classes/timecontrol/notimecontrol.h"
00032 #include "classes/timecontrol/fischertimecontrol.h"
00033 #include "classes/timecontrol/fischeraftertimecontrol.h"
00034 #include "classes/timecontrol/delaytimecontrol.h"
00035 #include "classes/timecontrol/delayaftertimecontrol.h"
00036 #include "classes/timecontrol/hourglasstimecontrol.h"
00037
00038 #include <QIcon>
00039 #include <QApplication>
00040 #include <QMenuBar>
00041 #include <QMessageBox>
00042 #include <QStackedWidget>
00043 #include <QProcess>
00044
00045
00046 ChessClockWindow::ChessClockWindow(QWidget *parent)
00047 : QMainWindow(parent)
00048 {
00049
00050 setWindowIcon( QIcon(":/rc/pic/chessclock.png"));
00051 setWindowTitle( QString("%1 %2").arg(qApp->applicationName()).arg(qApp->applicationVersion()) );
00052
00053
00054 start_ = new StartWidget;
00055 clocks_ = 0;
00056
00057 initTimeControls();
00058
00059 stack_ = new QStackedWidget;
00060 stack_->addWidget(start_);
00061
00062 setCentralWidget( stack_ );
00063
00064 connect( start_, SIGNAL(selected(TimeControl*)), this, SLOT(startGame(TimeControl*)));
00065
00066
00067
00068
00069 menuBar()->addAction( tr("New game"), this, SLOT(newGame()));
00070
00071 menuBar()->addAction( tr("Visit web page"), this, SLOT(visitWeb()));
00072 menuBar()->addAction( tr("About"),this, SLOT(about()));
00073 menuBar()->addAction(tr("About Qt"), this, SLOT(aboutQt()));
00074
00075
00076
00077 installEventFilter(this);
00078
00079 }
00080
00081 void ChessClockWindow::pause()
00082 {
00083 if( clocks_ )
00084 clocks_->pause();
00085 }
00086
00087 void ChessClockWindow::newGame()
00088 {
00089 pause();
00090 if( clocks_ == 0 || !clocks_->isPlayStarted() || QMessageBox::question(this, tr("Start new game"),
00091 tr("Really quit the current game and start a new one?"),
00092 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
00093 {
00094 stack_->setCurrentWidget(start_);
00095
00096 if( clocks_ )
00097 { stack_->removeWidget(clocks_);
00098 delete clocks_;
00099 }
00100 clocks_=0;
00101 }
00102 }
00103
00104 void ChessClockWindow::visitWeb()
00105 {
00106 pause();
00107 QProcess* process = new QProcess(this);
00108 process->start(QString("browser --url=chessclock.garage.maemo.org"));
00109 }
00110
00111 void ChessClockWindow::about()
00112 {
00113 pause();
00114 QMessageBox::about(this, tr("About ChessClock"),
00115 tr("<h1>Chess Clock %1</h1>"
00116 "©Arto Hyvättinen 2010"
00117 "<p>Chess Clock is free software under the terms of GNU General Public License 3"
00118 "<p>Bugtracker and instructions at <a>http://chessclock.garage.maemo.org</a>"
00119 ).arg(qApp->applicationVersion())) ;
00120 }
00121
00122 void ChessClockWindow::aboutQt()
00123 {
00124 pause();
00125 qApp->aboutQt();
00126 }
00127
00128
00129 void ChessClockWindow::initTimeControls()
00130 {
00131 start_->addTimeControl( new NoTimeControl );
00132 start_->addTimeControl( new FischerTimeControl);
00133 start_->addTimeControl( new FischerAfterTimeControl);
00134 start_->addTimeControl( new DelayTimeControl );
00135 start_->addTimeControl( new DelayAfterTimeControl);
00136 start_->addTimeControl( new HourGlassTimeControl);
00137 }
00138
00139 void ChessClockWindow::startGame(TimeControl *timecontrol)
00140 {
00141 ClocksWidget* newWidget = timecontrol->initGame(false);
00142 if( newWidget )
00143 {
00144
00145 clocks_ = newWidget;
00146 stack_->addWidget(clocks_);
00147 stack_->setCurrentWidget(clocks_);
00148 connect( clocks_, SIGNAL(TurnFinished(TurnInformation*)), this, SLOT(dontEatMemory(TurnInformation*)));
00149 }
00150 }
00151
00152
00153 ChessClockWindow::~ChessClockWindow()
00154 {
00155
00156 }
00157
00158 bool ChessClockWindow::eventFilter(QObject *obj, QEvent *event)
00159 {
00160 if (event->type() == QEvent::WindowDeactivate) {
00161 pause();
00162 return QObject::eventFilter(obj,event);
00163 } else {
00164
00165 return QObject::eventFilter(obj, event);
00166 }
00167 }
00168
00169 void ChessClockWindow::dontEatMemory(TurnInformation *turnInformation)
00170 {
00171 delete turnInformation;
00172 }