Fischer time controller, start widget
authorArto Hyvättinen <arto.hyvattinen@gmail.com>
Mon, 16 Aug 2010 11:31:03 +0000 (14:31 +0300)
committerArto Hyvättinen <arto.hyvattinen@gmail.com>
Mon, 16 Aug 2010 11:31:03 +0000 (14:31 +0300)
0.1.0

16 files changed:
chessclock.pro
chessclockwindow.cpp
chessclockwindow.h
classes/chessclock.cpp
classes/clockswidget.cpp
classes/clockswidget.h
classes/startwidget.cpp [new file with mode: 0644]
classes/startwidget.h [new file with mode: 0644]
classes/timecontrol/basicdialog.cpp
classes/timecontrol/basicdialog.h
classes/timecontrol/fischerclock.cpp [new file with mode: 0644]
classes/timecontrol/fischerclock.h [new file with mode: 0644]
classes/timecontrol/fischertimecontrol.cpp [new file with mode: 0644]
classes/timecontrol/fischertimecontrol.h [new file with mode: 0644]
classes/timecontrol/notimecontrol.cpp
pic/logo.png [new file with mode: 0644]

index 4d0f3c7..21fd9f0 100644 (file)
@@ -18,7 +18,10 @@ SOURCES += main.cpp\
     classes/clockswidget.cpp \
     classes/welcomescreenwidget.cpp \
     classes/timecontrol/basicdialog.cpp \
-    classes/timecontrol/notimecontrol.cpp
+    classes/timecontrol/notimecontrol.cpp \
+    classes/startwidget.cpp \
+    classes/timecontrol/fischertimecontrol.cpp \
+    classes/timecontrol/fischerclock.cpp
 
 HEADERS  += chessclockwindow.h \
     classes/turninformation.h \
@@ -28,7 +31,10 @@ HEADERS  += chessclockwindow.h \
     classes/welcomescreenwidget.h \
     classes/timecontrol.h \
     classes/timecontrol/basicdialog.h \
-    classes/timecontrol/notimecontrol.h
+    classes/timecontrol/notimecontrol.h \
+    classes/startwidget.h \
+    classes/timecontrol/fischertimecontrol.h \
+    classes/timecontrol/fischerclock.h
 
 CONFIG += mobility
 MOBILITY = 
index 2bdecda..8bbd15a 100644 (file)
 
 #include "classes/clockswidget.h"
 #include "classes/chessclockwidget.h"
+#include "classes/startwidget.h"
+#include "classes/timecontrol.h"
+
+#include "classes/timecontrol/notimecontrol.h"
+#include "classes/timecontrol/fischertimecontrol.h"
 
 #include <QIcon>
 #include <QApplication>
+#include <QMenuBar>
+#include <QMessageBox>
+#include <QStackedWidget>
 
 ChessClockWindow::ChessClockWindow(QWidget *parent)
     : QMainWindow(parent)
@@ -34,16 +42,68 @@ ChessClockWindow::ChessClockWindow(QWidget *parent)
     setWindowIcon( QIcon(":/rc/pic/chessclock.png"));
     setWindowTitle( QString("%1 %2").arg(qApp->applicationName()).arg(qApp->applicationVersion()) );
 
-    ChessClockWidget* white = new ChessClockWidget(true, this);
-    white->setGreenTime(5000);
-    ChessClockWidget* black = new ChessClockWidget(false, this);
+    // Start widget to select time control
+    start_ = new StartWidget;
+    clocks_ = 0;
+
+    initTimeControls();
+
+    stack_ = new QStackedWidget;
+    stack_->addWidget(start_);
 
-    clocks_ = new ClocksWidget( white, black, this );
-    setCentralWidget( clocks_ );
+    setCentralWidget( stack_ );
 
+    connect( start_, SIGNAL(selected(TimeControl*)), this, SLOT(startGame(TimeControl*)));
+
+    // Set up menu
+    menuBar()->addAction( tr("Pause"), this, SLOT(pause()));
+    menuBar()->addAction( tr("New game"), this, SLOT(newGame()));
 
 }
 
+void ChessClockWindow::pause()
+{
+    if( clocks_ )
+        clocks_->pause();
+}
+
+void ChessClockWindow::newGame()
+{
+    pause();
+    if(  clocks_ == 0 ||  !clocks_->isPlayStarted()  || QMessageBox::question(this, tr("Start new game"),
+                              tr("Really quit current game and start new one with current settings?"),
+                              QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
+    {
+        stack_->setCurrentWidget(start_);
+
+        if( clocks_ )
+        {   stack_->removeWidget(clocks_);
+            delete clocks_;
+        }
+        clocks_=0;
+    }
+}
+
+void ChessClockWindow::initTimeControls()
+{
+    start_->addTimeControl( new NoTimeControl );
+    start_->addTimeControl( new FischerTimeControl);
+}
+
+void ChessClockWindow::startGame(TimeControl *timecontrol)
+{
+    ClocksWidget* newWidget = timecontrol->initGame(false);
+    if( newWidget )
+    {
+        if( clocks_ )
+            delete clocks_;
+        clocks_ = newWidget;
+        stack_->addWidget(clocks_);
+        stack_->setCurrentWidget(clocks_);
+    }
+}
+
+
 ChessClockWindow::~ChessClockWindow()
 {
 
index 82bd63d..0e7f1c9 100644 (file)
@@ -25,6 +25,9 @@
 #include <QtGui/QMainWindow>
 
 class ClocksWidget;
+class StartWidget;
+class TimeControl;
+class QStackedWidget;
 
 class ChessClockWindow : public QMainWindow
 {
@@ -34,9 +37,19 @@ public:
     ChessClockWindow(QWidget *parent = 0);
     ~ChessClockWindow();
 
+public slots:
+    void startGame( TimeControl* timecontrol);
+    void pause();
+    void newGame();
+
+
+protected:
+    void initTimeControls();
 
 protected:
     ClocksWidget*   clocks_;
+    StartWidget* start_;
+    QStackedWidget* stack_;
 };
 
 #endif // CHESSCLOCKWINDOW_H
index 5ae23b0..cc9269e 100644 (file)
@@ -81,19 +81,22 @@ void ChessClock::continueTurn()
 TurnInformation* ChessClock::endTurn()
 {
     updateTimer_.stop();
+    status_ = NotRunning;
+
+    updateClock();
     // Count time played
     timePlayedBeforeTurn_ = getTimePlayed();
+
     // Count time available
     // This update current turn
     timeAvailableBeforeTurn_ = getTimeAvailable();
 
-    status_ = NotRunning;
-    updateClock();
 
     // Close and return turn information
     currentTurn_->turnReady(timeAvailableBeforeTurn_ );
     TurnInformation* information = currentTurn_;
     currentTurn_ = 0;
+
     emit turnEnded();
     return information;
 }
@@ -119,11 +122,7 @@ int ChessClock::getTimeAvailable()
 
 int ChessClock::getTimePlayed()
 {
-    // Count time played time
-    if( currentTurn_ )
-        return timePlayedBeforeTurn_ + currentTurnPlayed();
-    else
-        return timePlayedBeforeTurn_;
+     return timePlayedBeforeTurn_ + currentTurnPlayed();
 }
 
 
index 95baa86..d39c1f2 100644 (file)
@@ -128,11 +128,13 @@ void ClocksWidget::mouseReleaseEvent(QMouseEvent *event)
             break;
         case WhitePause:
             // Continue play
+            pauseLabel_->setVisible(false);
             white_->continueTurn();
             status_=WhiteTurn;
             break;
         case BlackPause:
             // Continue play
+            pauseLabel_->setVisible(false);
             black_->continueTurn();
             status_=BlackTurn;
             break;
index 7872915..1283d32 100644 (file)
@@ -47,7 +47,7 @@ class ClocksWidget : public QWidget
     Q_OBJECT
 public:
     ClocksWidget(ChessClock* white, ChessClock* black, QWidget *parent = 0);
-
+    bool isPlayStarted()  { return (status_!=Welcome); }
 
 protected:
     void mouseReleaseEvent(QMouseEvent *event);
diff --git a/classes/startwidget.cpp b/classes/startwidget.cpp
new file mode 100644 (file)
index 0000000..b790a92
--- /dev/null
@@ -0,0 +1,88 @@
+ /**************************************************************************
+
+    Chess Clock
+
+    Copyright (c) Arto Hyvättinen 2010
+
+    This file is part of Chess Clock software.
+
+    Chess Clock is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Chess Clock is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#include "startwidget.h"
+#include "timecontrol.h"
+
+#include <QVBoxLayout>
+#include <QHBoxLayout>
+#include <QApplication>
+#include <QFont>
+#include <QLabel>
+#include <QListWidgetItem>
+
+StartWidget::StartWidget(QWidget *parent) :
+    QWidget(parent)
+{
+    QLabel* titleLabel = new QLabel( qApp->applicationName() );
+    titleLabel->setFont(QFont("Helvetica",32,QFont::Bold));
+
+    QLabel* copyLabel = new QLabel( tr("&copy; Arto Hyv&auml;ttinen 2010"));
+    copyLabel->setTextFormat(Qt::RichText);
+    copyLabel->setWordWrap(true);
+
+    QLabel* logoLabel = new QLabel;
+    logoLabel->setPixmap( QPixmap(":/rc/pic/logo.png"));
+
+    QLabel* introLabel = new QLabel( tr("Select game mode"));
+    introLabel->setWordWrap(true);
+
+    QVBoxLayout* leftLayout = new QVBoxLayout;
+    leftLayout->addWidget(titleLabel);
+    leftLayout->addWidget(copyLabel);
+    leftLayout->addWidget(logoLabel);
+    leftLayout->addWidget(introLabel);
+
+    modeSelect_ = new QListWidget();
+    modeSelect_->setViewMode(QListView::IconMode);
+    modeSelect_->setMovement(QListView::Static);
+    modeSelect_->setSelectionMode(QAbstractItemView::NoSelection);
+    modeSelect_->setIconSize(QSize(64,64 ));
+
+    connect( modeSelect_, SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(selectControl(QListWidgetItem*)));
+
+    QHBoxLayout* layout = new QHBoxLayout;
+    layout->addLayout(leftLayout);
+    layout->addWidget(modeSelect_);
+
+
+    setLayout( layout );
+}
+
+void StartWidget::addTimeControl(TimeControl *tc)
+{
+    timeControls_.append(tc);
+    QListWidgetItem* item = new QListWidgetItem(modeSelect_);
+    item->setText( tc->getName());
+    item->setIcon( tc->getIcon());
+    // Store index to UserRole
+    item->setData(Qt::UserRole, timeControls_.size()-1);
+
+}
+
+void StartWidget::selectControl(QListWidgetItem *item)
+{
+    int index=item->data(Qt::UserRole).toInt();
+    TimeControl* tc=timeControls_.at(index);
+    emit selected(tc);
+
+}
+
diff --git a/classes/startwidget.h b/classes/startwidget.h
new file mode 100644 (file)
index 0000000..f3660f3
--- /dev/null
@@ -0,0 +1,57 @@
+ /**************************************************************************
+
+    Chess Clock
+
+    Copyright (c) Arto Hyvättinen 2010
+
+    This file is part of Chess Clock software.
+
+    Chess Clock is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Chess Clock is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#ifndef STARTWIDGET_H
+#define STARTWIDGET_H
+
+#include <QWidget>
+#include <QListWidget>
+
+
+class QListWidget;
+class TimeControl;
+
+/*! Start widget to ask for Time control
+
+  @author Arto Hyvättinen
+  @date 2010-08-16
+
+  Time Controls are stored inside Start Widget */
+class StartWidget : public QWidget
+{
+    Q_OBJECT
+public:
+    StartWidget(QWidget *parent = 0);
+    void addTimeControl( TimeControl* tc);
+
+signals:
+    void selected(TimeControl* timeControl);
+
+public slots:
+    void selectControl(QListWidgetItem* item);
+
+protected:
+    QListWidget* modeSelect_;
+    QList<TimeControl*> timeControls_;
+
+};
+
+#endif // STARTWIDGET_H
index ebfdf8a..436f2f6 100644 (file)
@@ -55,11 +55,10 @@ BasicDialog::BasicDialog(QString timeControlName, QWidget *parent) :
     // Equal times
     equals_ = new QCheckBox( tr("Equal times"));
 
-    connect( equals_, SIGNAL(stateChanged(bool)),blackInitial_,SLOT(setDisabled(bool)));
-    connect( equals_, SIGNAL(stateChanged(bool)),blackAddition_,SLOT(setDisabled(bool)));
-    connect( equals_, SIGNAL(stateChanged(bool)),blackTurns_,SLOT(setDisabled(bool)));
+    connect( equals_, SIGNAL(stateChanged(int)),this,SLOT(setEquals()));
 
     equals_->setChecked(true);
+    setEquals();
 
     whiteLabel_ = new QLabel;
     whiteLabel_->setPixmap(QPixmap(":/rc/pic/white_small.png"));
@@ -86,12 +85,22 @@ BasicDialog::BasicDialog(QString timeControlName, QWidget *parent) :
 
     QPushButton* button = new QPushButton( tr("Start game"));
     connect( button, SIGNAL(clicked()), this, SLOT(accept()));
+    layout->addWidget(button,4,2);
 
     setLayout( layout );
     setWindowTitle( timeControlName_);
 
 }
 
+void BasicDialog::setEquals()
+{
+    blackInitial_->setDisabled( equals_->isChecked());
+    blackAddition_->setDisabled( equals_->isChecked());
+    blackTurns_->setDisabled( equals_->isChecked());
+
+}
+
+
 void BasicDialog::disablePerTurns()
 {
     perTurnLabel_->setVisible(false);
@@ -141,6 +150,7 @@ void BasicDialog::init(QTime whiteInitial, QTime blackInitial, QTime whiteAdditi
     s.beginGroup(timeControlName_);
 
     equals_->setChecked(s.value("Equals",true).toBool() );
+    setEquals();
     whiteInitial_->setTime( s.value("WhiteInitial",whiteInitial).toTime());
     blackInitial_->setTime(s.value("BlackInitial",blackInitial).toTime());
     whiteAddition_->setTime(s.value("WhiteAddition",whiteAddition).toTime());
@@ -196,7 +206,8 @@ int BasicDialog::getBlackPerTurns()
 int BasicDialog::toMsecs(QTimeEdit *timeEdit)
 {
     QTime qtime=timeEdit->time();
-    return 0-qtime.msecsTo(QTime(0,0,0));
+    int msecs=qtime.msecsTo(QTime(0,0,0));;
+    return 0-msecs;
 }
 
 
index 54ab82b..8343a71 100644 (file)
@@ -75,6 +75,7 @@ public:
 signals:
 
 public slots:
+    void setEquals();
 
 protected:
     static QTimeEdit* initTimeEdit();
diff --git a/classes/timecontrol/fischerclock.cpp b/classes/timecontrol/fischerclock.cpp
new file mode 100644 (file)
index 0000000..13da69c
--- /dev/null
@@ -0,0 +1,38 @@
+ /**************************************************************************
+
+    Chess Clock
+
+    Copyright (c) Arto Hyvättinen 2010
+
+    This file is part of Chess Clock software.
+
+    Chess Clock is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Chess Clock is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#include "fischerclock.h"
+
+FischerClock::FischerClock(bool white, int addition, int perTurns,  QWidget *parent) :
+    ChessClockWidget(white, parent)
+{
+    addition_ = addition;
+    perTurns_ = perTurns;
+
+    setGreenTime( addition );
+}
+
+void FischerClock::startTurn()
+{
+    if(getTurn() % perTurns_ == 0 )
+        addTime( addition_ );
+    ChessClock::startTurn();
+}
diff --git a/classes/timecontrol/fischerclock.h b/classes/timecontrol/fischerclock.h
new file mode 100644 (file)
index 0000000..a941cc3
--- /dev/null
@@ -0,0 +1,46 @@
+ /**************************************************************************
+
+    Chess Clock
+
+    Copyright (c) Arto Hyvättinen 2010
+
+    This file is part of Chess Clock software.
+
+    Chess Clock is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Chess Clock is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#ifndef FISCHERCLOCK_H
+#define FISCHERCLOCK_H
+
+#include "../chessclockwidget.h"
+
+class FischerClock : public ChessClockWidget
+{
+    Q_OBJECT
+public:
+    FischerClock(bool white, int addition, int perTurns,  QWidget *parent = 0);
+
+
+    void startTurn();
+
+signals:
+
+public slots:
+
+protected:
+    int addition_;
+    int perTurns_;
+
+};
+
+#endif // FISCHERCLOCK_H
diff --git a/classes/timecontrol/fischertimecontrol.cpp b/classes/timecontrol/fischertimecontrol.cpp
new file mode 100644 (file)
index 0000000..cc29ad0
--- /dev/null
@@ -0,0 +1,58 @@
+ /**************************************************************************;
+
+    Chess Clock
+
+    Copyright (c) Arto Hyvättinen 2010
+
+    This file is part of Chess Clock software.
+
+    Chess Clock is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Chess Clock is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#include "fischertimecontrol.h"
+#include "fischerclock.h"
+#include "../clockswidget.h"
+#include "basicdialog.h"
+
+#include <QApplication>
+
+
+FischerTimeControl::FischerTimeControl()
+{
+}
+
+QString FischerTimeControl::getDescription()
+{
+    return qApp->translate("Fischer","Specified time increment is added to clock before turn.");
+}
+
+ClocksWidget* FischerTimeControl::initGame(bool useLastSettings)
+{
+    BasicDialog dialog(getName());
+    dialog.init();
+
+    if( useLastSettings || dialog.exec() == QDialog::Accepted)
+    {
+        dialog.store();
+        FischerClock* white = new FischerClock( true, dialog.getWhiteAddition(), dialog.getWhitePerTurns());
+        white->addTime(dialog.getWhiteInitial());
+
+        FischerClock* black = new FischerClock( false, dialog.getBlackAddition(), dialog.getBlackPerTurns());
+        black->addTime( dialog.getBlackInitial());
+
+        return( new ClocksWidget(white,black));
+
+    }
+    else
+        return 0;
+}
diff --git a/classes/timecontrol/fischertimecontrol.h b/classes/timecontrol/fischertimecontrol.h
new file mode 100644 (file)
index 0000000..5127f92
--- /dev/null
@@ -0,0 +1,38 @@
+ /**************************************************************************
+
+    Chess Clock
+
+    Copyright (c) Arto Hyvättinen 2010
+
+    This file is part of Chess Clock software.
+
+    Chess Clock is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Chess Clock is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#ifndef FISCHERTIMECONTROL_H
+#define FISCHERTIMECONTROL_H
+
+#include "../timecontrol.h"
+#include <QApplication>
+
+class FischerTimeControl : public TimeControl
+{
+public:
+    FischerTimeControl();
+
+    QString getName() { return qApp->translate("Fischer","Addition before"); }
+    QString getDescription();
+    ClocksWidget* initGame(bool useLastSettings);
+};
+
+#endif // FISCHERTIMECONTROL_H
index 3163070..d5918a8 100644 (file)
@@ -49,10 +49,10 @@ ClocksWidget* NoTimeControl::initGame(bool useLastSettings)
     {
         dialog.store();
         ChessClockWidget* white = new ChessClockWidget(true);
-        white->addTime(dialog.getWhiteInitial());
+        white->setTimeAvailable(dialog.getWhiteInitial());
 
         ChessClockWidget* black = new ChessClockWidget(false);
-        black->addTime( dialog.getBlackInitial());
+        black->setTimeAvailable( dialog.getBlackInitial());
 
         return( new ClocksWidget(white,black));
 
diff --git a/pic/logo.png b/pic/logo.png
new file mode 100644 (file)
index 0000000..abcd3ba
Binary files /dev/null and b/pic/logo.png differ