Screen will turn black after 30 min
[chessclock] / classes / chessclock.cpp
index d7df9d3..aedf531 100644 (file)
@@ -24,6 +24,7 @@
 #include "turninformation.h"
 
 const int ChessClock::UPDATEINTERVAL;
+const int ChessClock::DONTEATBATTERYTIME;
 
 ChessClock::ChessClock(bool white, QWidget *parent) :
     QWidget(parent)
@@ -31,36 +32,42 @@ ChessClock::ChessClock(bool white, QWidget *parent) :
     isWhite_ = white;
     loser_ = false;
     turn_ = 0;
-    timePlayed_ = 0;
+    dontEatBatteryEmitted_ = false;
+    timePlayedBeforeTurn_ = 0;
     status_ = NotRunning;
+    another_ = 0;
+    currentTurn_ = 0;
 
     // Set clock timer calculating played time
     clockTime_.start();
 
     // Set updating timer
     updateTimer_.setInterval( UPDATEINTERVAL );
-    connect( &updateTimer_, SIGNAL(timeout),this,SLOT(updateClock()));
+    connect( &updateTimer_, SIGNAL(timeout()),this,SLOT(updateClock()));
 }
 
 void ChessClock::startTurn()
 {
     turn_++;
+    dontEatBatteryEmitted_ = false;
 
     // Turn information for this new turn
     currentTurn_ = new TurnInformation(turn_, isWhite_);
     clockTime_.restart();
+    updateTimer_.start();
     status_=Running;
 
     // Repaint clock
-    repaintClock();
+    updateClock();
 }
 
 void ChessClock::pauseTurn()
 {
+    updateTimer_.stop();
     // Update turn time
     currentTurn_->addTime( clockTime_.restart() );
     status_ = Paused;
-    repaintClock();
+    updateClock();
 }
 
 void ChessClock::continueTurn()
@@ -69,23 +76,31 @@ void ChessClock::continueTurn()
     // Add pause duration to information object
     currentTurn_->addPause( clockTime_.restart() );
     status_ = Running;
-    repaintClock();
+    updateTimer_.start();
+    updateClock();
 }
 
 
 TurnInformation* ChessClock::endTurn()
 {
+    updateTimer_.stop();
     status_ = NotRunning;
-    // Update turn time
-    currentTurn_->addTime( clockTime_.restart());
+
+    updateClock();
+    // Count time played
+    timePlayedBeforeTurn_ = getTimePlayed();
+
     // Count time available
+    // This update current turn
     timeAvailableBeforeTurn_ = getTimeAvailable();
-    repaintClock();
+
 
     // Close and return turn information
     currentTurn_->turnReady(timeAvailableBeforeTurn_ );
     TurnInformation* information = currentTurn_;
     currentTurn_ = 0;
+
+    emit turnEnded();
     return information;
 }
 
@@ -99,7 +114,71 @@ int ChessClock::getTimeAvailable()
     // Most simple - will be overwritten in more complex time controls:
     // subtract duration time!
     if( currentTurn_)
-        return timeAvailableBeforeTurn_-currentTurn_->getDuration();
+    {
+        // Update turn time
+        return timeAvailableBeforeTurn_-currentTurnPlayed();
+    }
     else
         return timeAvailableBeforeTurn_;
 }
+
+
+int ChessClock::getTimePlayed()
+{
+     return timePlayedBeforeTurn_ + currentTurnPlayed();
+}
+
+
+void ChessClock::setTimeAvailable(int msecs)
+{
+    timeAvailableBeforeTurn_ = msecs;
+}
+
+
+void ChessClock::addTime(int msecs)
+{
+   timeAvailableBeforeTurn_ += msecs;
+}
+
+int ChessClock::currentTurnPlayed()
+{
+    if( currentTurn_ )
+    {
+        // Update current time
+        if( status_ == Running )
+        {
+            currentTurn_->addTime( clockTime_.restart());
+
+            // since 1.1.2
+            // emit dontEatBattery signal when screen should not to be keeped on
+            if ( currentTurn_->getDuration() > DONTEATBATTERYTIME  && dontEatBatteryEmitted_ == false )
+            {
+                dontEatBatteryEmitted_ = true;
+                emit dontEatBattery();
+            }
+        }
+
+        // Return current time
+        return currentTurn_->getDuration();
+     }
+    else
+        // No current turn!
+        return 0;
+}
+
+void ChessClock::updateClock()
+{
+    // Check loser
+    if( another_ && !another_->isLoser())
+    {
+        if( getTimeAvailable() < 0 && !loser_)
+        {
+            loser_ = true;
+            emit timeOutLoser();
+        }
+
+    }
+    repaintClock();
+
+}
+