00001 /************************************************************************** 00002 00003 Chess Clock 00004 00005 Copyright (c) Arto Hyvättinen 2010 00006 00007 This file is part of Chess Clock software. 00008 00009 Chess Clock is free software: you can redistribute it and/or modify 00010 it under the terms of the GNU General Public License as published by 00011 the Free Software Foundation, either version 3 of the License, or 00012 (at your option) any later version. 00013 00014 Chess Clock is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU General Public License for more details. 00018 00019 00020 **************************************************************************/ 00021 00022 #include "clockswidget.h" 00023 #include "chessclock.h" 00024 00025 #include <QLabel> 00026 #include <QPixmap> 00027 #include <QApplication> 00028 #include <QHBoxLayout> 00029 #include <QVBoxLayout> 00030 #include <QFont> 00031 00032 00033 ClocksWidget::ClocksWidget(ChessClock *white, ChessClock *black, QWidget *parent): 00034 QWidget(parent) 00035 { 00036 // Set up clocks 00037 white_ = white; 00038 black_ = black; 00039 00040 // SET UP UI 00041 // Make layout for clocks 00042 QHBoxLayout* clockLayout = new QHBoxLayout; 00043 clockLayout->addWidget(white_ ); 00044 clockLayout->addWidget( black_ ); 00045 00046 // Pause information label 00047 pauseLabel_ = new QLabel( tr("<font color=yellow>Paused. Touch to continue.</font>")); 00048 pauseLabel_->setFont( QFont("Helvetica",25)); 00049 pauseLabel_->setAlignment( Qt::AlignCenter); 00050 pauseLabel_->setVisible( false ); 00051 00052 // Welcome label for first touch 00053 welcomeLabel_ = new QLabel( tr("<font color=green>Welcome! Please touch to start game.<br>" 00054 "Then touch to end turn.</font>")); 00055 welcomeLabel_->setFont( QFont("Helvetica",25)); 00056 welcomeLabel_->setAlignment( Qt::AlignCenter); 00057 welcomeLabel_->setVisible( true ); // Show welcome message 00058 00059 // Put all in layout 00060 QVBoxLayout* mainLayout = new QVBoxLayout; 00061 mainLayout->addLayout(clockLayout); 00062 mainLayout->addWidget(pauseLabel_); 00063 mainLayout->addWidget(welcomeLabel_); 00064 00065 setLayout( mainLayout); 00066 status_ = Welcome; 00067 00068 // First paint 00069 white_->repaintClock(); 00070 black_->repaintClock(); 00071 00072 // Set up others 00073 white_->setAnother(black_); 00074 black_->setAnother(white_); 00075 00076 delayTimer_.start(); // Initial start 00077 } 00078 00079 ClocksWidget::~ClocksWidget() 00080 { 00081 delete white_; 00082 delete black_; 00083 } 00084 00085 void ClocksWidget::pause() 00086 { 00087 if(status_ == WhiteTurn) 00088 { 00089 status_= WhitePause; 00090 white_->pauseTurn(); 00091 pauseLabel_->setVisible(true); 00092 } 00093 else if( status_ == BlackTurn) 00094 { 00095 status_ = BlackPause; 00096 black_->pauseTurn(); 00097 pauseLabel_->setVisible(true); 00098 } 00099 } 00100 00101 void ClocksWidget::stopPlay() 00102 { 00103 if( status_ == BlackTurn || status_ == BlackPause ) 00104 emit TurnFinished( black_->endTurn()); 00105 else if( status_ == WhiteTurn || status_ == WhitePause ) 00106 emit TurnFinished( white_->endTurn()); 00107 status_ = Stopped; 00108 } 00109 00110 00111 void ClocksWidget::mouseReleaseEvent(QMouseEvent *event) 00112 { 00113 if( delayTimer_.elapsed() > CLICKDELAY ) // To avoid double clicks 00114 { 00115 switch( status_) 00116 { 00117 case Welcome : 00118 // Start game! 00119 welcomeLabel_->setVisible(false); 00120 white_->startTurn(); 00121 status_ = WhiteTurn; 00122 break; 00123 case WhiteTurn: 00124 // White turn finished, then black 00125 emit TurnFinished( white_->endTurn()); 00126 black_->startTurn(); 00127 status_=BlackTurn; 00128 break; 00129 case BlackTurn: 00130 // Black finished, then white 00131 emit TurnFinished( black_->endTurn()); 00132 white_->startTurn(); 00133 status_=WhiteTurn; 00134 break; 00135 case WhitePause: 00136 // Continue play 00137 pauseLabel_->setVisible(false); 00138 white_->continueTurn(); 00139 status_=WhiteTurn; 00140 break; 00141 case BlackPause: 00142 // Continue play 00143 pauseLabel_->setVisible(false); 00144 black_->continueTurn(); 00145 status_=BlackTurn; 00146 break; 00147 case Stopped: 00148 emit ClickedWhenStopped(); 00149 00150 00151 } 00152 } 00153 } 00154 00155 int const ClocksWidget::CLICKDELAY;