From efb5fddf5828e4dad0eefc538cda513361623ddc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= <ipkiss@gmail.com>
Date: Tue, 21 Feb 2012 08:17:29 +0100
Subject: [PATCH] Reset the timer when the current turn changes

---
 qt/main_window.cpp | 15 +++++++++++++--
 qt/main_window.h   |  4 ++++
 qt/timer_widget.h  |  4 +++-
 3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/qt/main_window.cpp b/qt/main_window.cpp
index 4f14e1d..636b54c 100644
--- a/qt/main_window.cpp
+++ b/qt/main_window.cpp
@@ -76,7 +76,8 @@ MainWindow::MainWindow(QWidget *iParent)
     m_playersWidget(NULL), m_trainingWidget(NULL), m_scoresWidget(NULL),
     m_bagWindow(NULL), m_boardWindow(NULL),
     m_historyWindow(NULL), m_timerWindow(NULL),
-    m_dicToolsWindow(NULL), m_dicNameLabel(NULL), m_timerModel(NULL)
+    m_dicToolsWindow(NULL), m_dicNameLabel(NULL), m_timerModel(NULL),
+    m_currentTurn(0)
 {
 #ifdef DEBUG
     // Check that the string conversion routines are not buggy
@@ -104,6 +105,9 @@ MainWindow::MainWindow(QWidget *iParent)
     int timerTotal = qs.value(PrefsDialog::kINTF_TIMER_TOTAL_DURATION, 180).toInt();
     int timerAlert = qs.value(PrefsDialog::kINTF_TIMER_ALERT_DURATION, 30).toInt();
     m_timerModel = new TimerModel(timerTotal, timerAlert);
+    // Reset the timer when the turn changes
+    QObject::connect(this, SIGNAL(turnChanged(int, bool)),
+                     m_timerModel, SLOT(resetTimer()));
     // TODO: connect to some of the timer signals (alert() and expired())
 
     QObject::connect(this, SIGNAL(gameChangedNonConst(PublicGame*)),
@@ -231,8 +235,9 @@ void MainWindow::refresh()
                                 .arg(bag.getNbConsonants())
                                 .arg(bag.getNbVowels())
                                 .arg(bag.in(Tile::Joker())));
+        unsigned currTurn = m_game->getCurrTurn();
         m_turnLabel->setText(_q("Turn %1/%2")
-                             .arg(m_game->getCurrTurn())
+                             .arg(currTurn)
                              .arg(m_game->getNbTurns()));
         bool isFirstTurn = m_game->isFirstTurn();
         bool isLastTurn = m_game->isLastTurn();
@@ -243,6 +248,12 @@ void MainWindow::refresh()
         m_actionHistoryReplayTurn->setEnabled(!isLastTurn);
         if (m_game->isFinished())
             displayInfoMsg(_q("End of the game"));
+        // Emit the turnChanged() sign if needed.
+        if (currTurn != m_currentTurn)
+        {
+            m_currentTurn = currTurn;
+            emit turnChanged(currTurn, isLastTurn);
+        }
 #ifdef DEBUG
         //m_game->printTurns();
 #endif
diff --git a/qt/main_window.h b/qt/main_window.h
index 8531a10..ed7b74d 100644
--- a/qt/main_window.h
+++ b/qt/main_window.h
@@ -57,6 +57,7 @@ signals:
     void gameChanged(const PublicGame *iGame);
     void gameChangedNonConst(PublicGame *iGame);
     void gameUpdated();
+    void turnChanged(int iCurrTurn, bool isLastTurn);
 
 public slots:
     /// Display an error message to the user
@@ -176,6 +177,9 @@ private:
     /// Model for the timer widgets
     TimerModel *m_timerModel;
 
+    /// Last known turn number. Used to emit turnChanged().
+    unsigned m_currentTurn;
+
     /// Save window state
     void writeSettings() const;
     /// Restore window state
diff --git a/qt/timer_widget.h b/qt/timer_widget.h
index 3c564fe..8457f05 100644
--- a/qt/timer_widget.h
+++ b/qt/timer_widget.h
@@ -51,11 +51,13 @@ public:
     bool wasAlertTriggered() const { return m_alertTriggered; }
     bool isExpired() const { return m_remaining == 0; }
 
+    bool isActiveTimer() const;
+
+public slots:
     // Timer handling
     void startTimer();
     void pauseTimer();
     void resetTimer();
-    bool isActiveTimer() const;
 
 signals:
     void valueChanged(int iNewValue);