Add ClocksWidget
authorArto Hyvättinen <arto.hyvattinen@gmail.com>
Sat, 14 Aug 2010 13:53:54 +0000 (16:53 +0300)
committerArto Hyvättinen <arto.hyvattinen@gmail.com>
Sat, 14 Aug 2010 13:53:54 +0000 (16:53 +0300)
Seems needed to create new ClocksWidget for each game.

12 files changed:
chessclock.pro
chessclock.qrc
chessclockwindow.cpp
chessclockwindow.h
classes/chessclock.cpp
classes/chessclock.h
classes/chessclockwidget.cpp
classes/chessclockwidget.h
classes/clockswidget.cpp [new file with mode: 0644]
classes/clockswidget.h [new file with mode: 0644]
classes/welcomescreenwidget.cpp [new file with mode: 0644]
classes/welcomescreenwidget.h [new file with mode: 0644]

index c173879..82b2d01 100644 (file)
@@ -14,12 +14,16 @@ SOURCES += main.cpp\
         chessclockwindow.cpp \
     classes/turninformation.cpp \
     classes/chessclock.cpp \
-    classes/chessclockwidget.cpp
+    classes/chessclockwidget.cpp \
+    classes/clockswidget.cpp \
+    classes/welcomescreenwidget.cpp
 
 HEADERS  += chessclockwindow.h \
     classes/turninformation.h \
     classes/chessclock.h \
-    classes/chessclockwidget.h
+    classes/chessclockwidget.h \
+    classes/clockswidget.h \
+    classes/welcomescreenwidget.h
 
 CONFIG += mobility
 MOBILITY = 
index d0d6d4f..1b1983b 100644 (file)
@@ -6,5 +6,6 @@
         <file>pic/white_blue.png</file>
         <file>pic/white_gray.png</file>
         <file>pic/chessclock.png</file>
+        <file>pic/logo.png</file>
     </qresource>
 </RCC>
index 017819d..49e2aae 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "chessclockwindow.h"
 
+#include "classes/clockswidget.h"
 #include "classes/chessclockwidget.h"
 
 #include <QIcon>
@@ -33,9 +34,15 @@ ChessClockWindow::ChessClockWindow(QWidget *parent)
     setWindowIcon( QIcon(":/rc/pic/chessclock.png"));
     setWindowTitle( QString("%1 %2").arg(qApp->applicationName()).arg(qApp->applicationVersion()) );
 
-    ChessClockWidget* widget = new ChessClockWidget(true, this);
-    setCentralWidget(widget);
-    widget->startTurn();
+    ChessClockWidget* white = new ChessClockWidget(true, this);
+    white->setGreenTime(5000);
+    ChessClockWidget* black = new ChessClockWidget(false, this);
+    white->startTurn();
+    black->repaintClock();
+
+    clocks_ = new ClocksWidget( this );
+    setCentralWidget( clocks_ );
+    clocks_->setClocks(white, black);
 
 }
 
index 20c7ba2..82bd63d 100644 (file)
@@ -24,6 +24,8 @@
 
 #include <QtGui/QMainWindow>
 
+class ClocksWidget;
+
 class ChessClockWindow : public QMainWindow
 {
     Q_OBJECT
@@ -31,6 +33,10 @@ class ChessClockWindow : public QMainWindow
 public:
     ChessClockWindow(QWidget *parent = 0);
     ~ChessClockWindow();
+
+
+protected:
+    ClocksWidget*   clocks_;
 };
 
 #endif // CHESSCLOCKWINDOW_H
index a0d884f..308d82d 100644 (file)
@@ -79,14 +79,14 @@ void ChessClock::continueTurn()
 
 TurnInformation* ChessClock::endTurn()
 {
-    status_ = NotRunning;
     updateTimer_.stop();
-    // Update turn time
-    currentTurn_->addTime( clockTime_.restart());
     // Count time played
     timePlayedBeforeTurn_ = getTimePlayed();
     // Count time available
+    // This update current turn
     timeAvailableBeforeTurn_ = getTimeAvailable();
+
+    status_ = NotRunning;
     updateClock();
 
     // Close and return turn information
@@ -109,19 +109,18 @@ int ChessClock::getTimeAvailable()
     if( currentTurn_)
     {
         // Update turn time
-        currentTurn_->addTime( clockTime_.restart());
-        return timeAvailableBeforeTurn_-currentTurn_->getDuration();
+        return timeAvailableBeforeTurn_-currentTurnPlayed();
     }
     else
         return timeAvailableBeforeTurn_;
 }
 
 
-int ChessClock::getTimePlayed() const
+int ChessClock::getTimePlayed()
 {
     // Count time played time
     if( currentTurn_ )
-        return timePlayedBeforeTurn_ + currentTurn_->getDuration();
+        return timePlayedBeforeTurn_ + currentTurnPlayed();
     else
         return timePlayedBeforeTurn_;
 }
@@ -138,6 +137,22 @@ void ChessClock::addTime(int msecs)
    timeAvailableBeforeTurn_ += msecs;
 }
 
+int ChessClock::currentTurnPlayed()
+{
+    if( currentTurn_ )
+    {
+        // Update current time
+        if( status_ == Running )
+            currentTurn_->addTime( clockTime_.restart());
+
+        // Return current time
+        return currentTurn_->getDuration();
+     }
+    else
+        // No current turn!
+        return 0;
+}
+
 void ChessClock::updateClock()
 {
     // Check loser
index 4ab58cc..56f05bb 100644 (file)
@@ -87,7 +87,7 @@ public:
 
     /*! Get total time played
       @return Time played in msecs */
-    virtual int getTimePlayed() const;
+    virtual int getTimePlayed();
 
     /*! Set time available
 
@@ -102,6 +102,10 @@ public:
       @param msecs Time to add in msecs */
     void addTime(int msecs);
 
+    /*! Get time played current turn.
+      @return Time in msecs */
+    int currentTurnPlayed();
+
 
 signals:
     void timeOutLoser();
index 46fff39..df29ae0 100644 (file)
@@ -29,6 +29,8 @@
 ChessClockWidget::ChessClockWidget(bool white, QWidget *parent) :
     ChessClock(white, parent)
 {
+    greenTime_ = 0;
+
     mainLayout = new QVBoxLayout;
 
     initPictures();
@@ -40,6 +42,11 @@ ChessClockWidget::ChessClockWidget(bool white, QWidget *parent) :
 }
 
 
+void ChessClockWidget::setGreenTime(int msecs)
+{
+    greenTime_ = msecs;
+}
+
 void ChessClockWidget::initPictures()
 {
     // Load pictures from resources.
@@ -66,11 +73,15 @@ void ChessClockWidget::initLabels()
     timeUsedLabel_ = new QLabel;
     timeAverageLabel_ = new QLabel;
     turnLabel_=new QLabel;
+    turnTimeLabel_=new QLabel;
 
     timeUsedLabel_->setFont(normalFont);
     timeAverageLabel_->setFont(normalFont);
     turnLabel_->setFont(normalFont);
 
+    QFont turnTimeFont("Helvetica",36,QFont::Bold);
+    turnTimeLabel_->setFont(turnTimeFont);
+
     loserLabel_ = new QLabel;
     loserLabel_->setPixmap(picLoser_);
     loserLabel_->setVisible(false);
@@ -83,6 +94,7 @@ void ChessClockWidget::initTop()
     details->addWidget(timeUsedLabel_);
     details->addWidget(timeAverageLabel_);
     details->addWidget(turnLabel_);
+    details->addWidget(turnTimeLabel_);
 
     QHBoxLayout* topLayout = new QHBoxLayout();
     if( isWhite() )
@@ -113,7 +125,7 @@ void ChessClockWidget::initBottom()
     leftLabel_ = new QLabel("0.00.00");
     leftLabel_->setFont(bigfont);
     // Black player: right alignment
-    if( isWhite() )
+    if( !isWhite() )
        leftLabel_->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
     mainLayout->addWidget(leftLabel_);
 }
@@ -138,6 +150,13 @@ void ChessClockWidget::repaintClock()
 
     turnLabel_->setText( tr("Turn %1").arg(getTurn()));
 
+    // Current turn played
+    // Extra time of this turn is shown in green.
+    if( currentTurnPlayed() < greenTime_ )
+        turnTimeLabel_->setText( QString("<font color=green> %1 </font>") .arg(timeString( currentTurnPlayed()) ) );
+    else
+        turnTimeLabel_->setText( timeString( currentTurnPlayed() ) );
+
     // Loser flag
     loserLabel_->setVisible( isLoser());
 
index 8de2136..356b8d6 100644 (file)
@@ -41,6 +41,12 @@ class ChessClockWidget : public ChessClock
     Q_OBJECT
 public:
     ChessClockWidget(bool white, QWidget *parent = 0);
+    /*! Set time begin of turn shown green.
+
+      Extra time of turn is shown green.
+      @param msecs Green time in msecs
+      */
+    void setGreenTime(int msecs);
 
 signals:
 
@@ -71,11 +77,14 @@ protected:
     QLabel* timeUsedLabel_;
     QLabel* timeAverageLabel_;
     QLabel* turnLabel_;
+    QLabel* turnTimeLabel_;
     QLabel* loserLabel_;
     QLabel* leftLabel_;
 
     QVBoxLayout* mainLayout;
 
+    int greenTime_; /*! Green time in begin of turn */
+
 
 };
 
diff --git a/classes/clockswidget.cpp b/classes/clockswidget.cpp
new file mode 100644 (file)
index 0000000..ed1352b
--- /dev/null
@@ -0,0 +1,105 @@
+ /**************************************************************************
+
+    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 "clockswidget.h"
+#include "chessclock.h"
+#include "welcomescreenwidget.h"
+
+#include <QLabel>
+#include <QPixmap>
+#include <QApplication>
+#include <QHBoxLayout>
+#include <QVBoxLayout>
+#include <QFont>
+
+
+ClocksWidget::ClocksWidget(QWidget *parent) :
+    QWidget(parent)
+{
+    white_ = 0;
+    black_ = 0;
+    status_ = NoClocks;
+
+    // Make layout for clocks
+    QHBoxLayout* clockLayout_ = new QHBoxLayout;
+
+    // Pause information label
+    pauseLabel_ = new QLabel( tr("<font color=yellow>Paused. Touch to continue.</font>"));
+    pauseLabel_->setFont( QFont("Helvetica",25));
+    pauseLabel_->setAlignment( Qt::AlignCenter);
+    pauseLabel_->setVisible( false );
+
+    // Welcome label for first touch
+    welcomeLabel_ = new QLabel( tr("<font color=yellow>Welcome! Please touch to start game.<br>"
+                                   "Then touch to end turn.</font>"));
+    welcomeLabel_->setFont( QFont("Helvetica",25));
+    welcomeLabel_->setAlignment( Qt::AlignCenter);
+    welcomeLabel_->setVisible( false );
+
+    // Welcome screen if no clocks set
+    welcomeScreen_ = new WelcomeScreenWidget(this);
+
+    // Put all in layout
+    QVBoxLayout* mainLayout = new QVBoxLayout;
+    mainLayout->addLayout(clockLayout_);
+    mainLayout->addWidget(pauseLabel_);
+    mainLayout->addWidget(welcomeLabel_);
+    mainLayout->addWidget(welcomeScreen_);
+
+    setLayout( mainLayout);
+
+}
+
+
+
+void ClocksWidget::setClocks(ChessClock *white, ChessClock *black)
+{
+    // Remove old clocks
+    if( white_ )
+    {
+        clockLayout_->removeWidget( white_ );
+        delete white_;
+    }
+    if( black_ )
+    {
+        clockLayout_->removeWidget( black_ );
+        delete black_;
+    }
+
+    // Set up new ones
+    white_ = white;
+    black_ = black;
+
+    clockLayout_->addWidget(white_);
+    clockLayout_->addWidget( black_ );
+
+    // First paint
+    white_->repaintClock();
+    black_->repaintClock();
+
+    // Welcome status for first touch
+    welcomeLabel_->setVisible(true);
+    status_ = Welcome;
+
+
+}
+
+int const ClocksWidget::CLICKDELAY;
diff --git a/classes/clockswidget.h b/classes/clockswidget.h
new file mode 100644 (file)
index 0000000..72dd641
--- /dev/null
@@ -0,0 +1,94 @@
+ /**************************************************************************
+
+    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 CLOCKSWIDGET_H
+#define CLOCKSWIDGET_H
+
+#include <QWidget>
+#include <QTime>
+
+class QHBoxLayout;
+class QVBoxLayout;
+class QLabel;
+class WelcomeScreenWidget;
+
+class ChessClock;
+
+/*! Widget with two clocks
+
+    @author Arto Hyvättinen
+    @date 2010-08-14
+
+    Central widget of Chess Clock
+
+  */
+class ClocksWidget : public QWidget
+{
+    Q_OBJECT
+public:
+    ClocksWidget(QWidget *parent = 0);
+
+    /*! Set clocks and paint them.
+
+      */
+    void setClocks(ChessClock* white, ChessClock* black);
+
+
+protected:
+//    void mouseReleaseEvent(QMouseEvent *event);
+
+    void initEmptyScreen();
+
+signals:
+    void setupTouch();
+
+public slots:
+
+protected:
+    ChessClock* white_;
+    ChessClock* black_;
+
+    QHBoxLayout* clockLayout_;
+
+    QLabel* pauseLabel_;
+    QLabel* welcomeLabel_;
+    WelcomeScreenWidget* welcomeScreen_;
+
+    enum GameStatus {
+        Stopped  /*! Not running */,
+        WhiteTurn,
+        BlackTurn,
+        WhitePause,
+        BlackPause,
+        NoClocks,    /*! Clocks not set ! */
+        Welcome
+        };
+
+    GameStatus status_;
+    QTime delayTimer_; /*! To avoid double clicks */
+
+    static int const CLICKDELAY = 500 ; /*! Click delay in msecs */
+
+
+
+};
+
+#endif // CLOCKSWIDGET_H
diff --git a/classes/welcomescreenwidget.cpp b/classes/welcomescreenwidget.cpp
new file mode 100644 (file)
index 0000000..29c9409
--- /dev/null
@@ -0,0 +1,49 @@
+ /**************************************************************************
+
+    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 <QLabel>
+#include <QPixmap>
+#include <QApplication>
+#include <QHBoxLayout>
+
+#include "welcomescreenwidget.h"
+
+WelcomeScreenWidget::WelcomeScreenWidget(QWidget *parent) :
+    QWidget(parent)
+{
+    QLabel* logoLabel = new QLabel;
+    logoLabel->setPixmap( QPixmap(":/rc/pic/logo.png"));
+    QLabel* introLabel = new QLabel(
+            tr("<h1>Chess Clock</h1><br>"
+               "Version %1<br>"
+               "&copy; Arto Hyv&auml;ttinen 2010<br>"
+               "Chess Clock is free software in terms of GNU General Public Licence v3"
+               "<p>Welcome to Chess Clock!<br>"
+               "Please touch to set up game clock.</p>"
+               ).arg(qApp->applicationVersion())
+
+            );
+    introLabel->setWordWrap(true);
+    QHBoxLayout* layout = new QHBoxLayout;
+    layout->addWidget(logoLabel);
+    layout->addWidget(introLabel);
+    setLayout( layout );
+}
diff --git a/classes/welcomescreenwidget.h b/classes/welcomescreenwidget.h
new file mode 100644 (file)
index 0000000..434845b
--- /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 WELCOMESCREENWIDGET_H
+#define WELCOMESCREENWIDGET_H
+
+#include <QWidget>
+
+/*! Welcome screen
+
+  @author Arto Hyvättinen
+  @date 2010-08-14
+
+  */
+
+class WelcomeScreenWidget : public QWidget
+{
+    Q_OBJECT
+public:
+    WelcomeScreenWidget(QWidget *parent = 0);
+
+signals:
+
+public slots:
+
+};
+
+#endif // WELCOMESCREENWIDGET_H