Added pause button and screen lit function
[chessclock] / classes / clockswidget.cpp
index d39c1f2..f1cc66c 100644 (file)
 #include <QHBoxLayout>
 #include <QVBoxLayout>
 #include <QFont>
-
+#include <cstdlib>
+#include <QMouseEvent>
+#include <QToolButton>
+#include <QSize>
 
 ClocksWidget::ClocksWidget(ChessClock *white, ChessClock *black, QWidget *parent):
     QWidget(parent)
@@ -56,12 +59,25 @@ ClocksWidget::ClocksWidget(ChessClock *white, ChessClock *black, QWidget *parent
     welcomeLabel_->setAlignment( Qt::AlignCenter);
     welcomeLabel_->setVisible( true );  // Show welcome message
 
+    // Pause button
+    pauseButton_ = new QToolButton;
+    pauseButton_->setIcon( QIcon(":/rc/pic/pausebutton.png"));
+    pauseButton_->setIconSize(QSize(75,75));
+    connect(pauseButton_, SIGNAL(clicked()), this, SLOT(pause()));
+    pauseButton_->setVisible(false);
+
     // Put all in layout
     QVBoxLayout* mainLayout = new QVBoxLayout;
     mainLayout->addLayout(clockLayout);
     mainLayout->addWidget(pauseLabel_);
     mainLayout->addWidget(welcomeLabel_);
 
+    QHBoxLayout* pbLayout = new QHBoxLayout;
+    pbLayout->addStretch();
+    pbLayout->addWidget(pauseButton_);
+    pbLayout->addStretch();
+    mainLayout->addLayout(pbLayout);
+
     setLayout( mainLayout);
     status_ = Welcome;
 
@@ -74,6 +90,14 @@ ClocksWidget::ClocksWidget(ChessClock *white, ChessClock *black, QWidget *parent
     black_->setAnother(white_);
 
     delayTimer_.start(); // Initial start
+
+    recentX = recentY = -1;
+}
+
+ClocksWidget::~ClocksWidget()
+{
+    delete white_;
+    delete black_;
 }
 
 void ClocksWidget::pause()
@@ -83,12 +107,14 @@ void ClocksWidget::pause()
         status_= WhitePause;
         white_->pauseTurn();
         pauseLabel_->setVisible(true);
+        pauseButton_->setVisible(false);
     }
     else if( status_ == BlackTurn)
     {
         status_ = BlackPause;
         black_->pauseTurn();
         pauseLabel_->setVisible(true);
+        pauseButton_->setVisible(false);
     }
 }
 
@@ -104,13 +130,22 @@ void ClocksWidget::stopPlay()
 
 void ClocksWidget::mouseReleaseEvent(QMouseEvent *event)
 {
-    if( delayTimer_.elapsed() > CLICKDELAY )  // To avoid double clicks
+
+    // To avoid double clicks
+    // a) delay (default 1,2 secs) OR
+    // b) distance more than 90 pixels in axis.
+    if( delayTimer_.elapsed() > CLICKDELAY ||
+        std::abs( event->x() - recentX ) > 90 ||
+        std::abs( event->y() - recentY ) > 90
+        )
     {
+        delayTimer_.start();    // to reset delay timer!
         switch( status_)
         {
         case Welcome :
             // Start game!
             welcomeLabel_->setVisible(false);
+            pauseButton_->setVisible(true);
             white_->startTurn();
             status_ = WhiteTurn;
             break;
@@ -129,12 +164,14 @@ void ClocksWidget::mouseReleaseEvent(QMouseEvent *event)
         case WhitePause:
             // Continue play
             pauseLabel_->setVisible(false);
+            pauseButton_->setVisible(true);
             white_->continueTurn();
             status_=WhiteTurn;
             break;
         case BlackPause:
             // Continue play
             pauseLabel_->setVisible(false);
+            pauseButton_->setVisible(true);
             black_->continueTurn();
             status_=BlackTurn;
             break;
@@ -144,6 +181,8 @@ void ClocksWidget::mouseReleaseEvent(QMouseEvent *event)
 
         }
     }
+    recentX = event->x();
+    recentY = event->y();
 }
 
 int const ClocksWidget::CLICKDELAY;