From: Arto Hyvättinen Date: Sat, 14 Aug 2010 13:53:54 +0000 (+0300) Subject: Add ClocksWidget X-Git-Tag: v1.9.0~113 X-Git-Url: http://git.maemo.org/git/?p=chessclock;a=commitdiff_plain;h=ca6853daf22068621981987588b9d670f03a5264 Add ClocksWidget Seems needed to create new ClocksWidget for each game. --- diff --git a/chessclock.pro b/chessclock.pro index c173879..82b2d01 100644 --- a/chessclock.pro +++ b/chessclock.pro @@ -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 = diff --git a/chessclock.qrc b/chessclock.qrc index d0d6d4f..1b1983b 100644 --- a/chessclock.qrc +++ b/chessclock.qrc @@ -6,5 +6,6 @@ pic/white_blue.png pic/white_gray.png pic/chessclock.png + pic/logo.png diff --git a/chessclockwindow.cpp b/chessclockwindow.cpp index 017819d..49e2aae 100644 --- a/chessclockwindow.cpp +++ b/chessclockwindow.cpp @@ -21,6 +21,7 @@ #include "chessclockwindow.h" +#include "classes/clockswidget.h" #include "classes/chessclockwidget.h" #include @@ -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); } diff --git a/chessclockwindow.h b/chessclockwindow.h index 20c7ba2..82bd63d 100644 --- a/chessclockwindow.h +++ b/chessclockwindow.h @@ -24,6 +24,8 @@ #include +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 diff --git a/classes/chessclock.cpp b/classes/chessclock.cpp index a0d884f..308d82d 100644 --- a/classes/chessclock.cpp +++ b/classes/chessclock.cpp @@ -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 diff --git a/classes/chessclock.h b/classes/chessclock.h index 4ab58cc..56f05bb 100644 --- a/classes/chessclock.h +++ b/classes/chessclock.h @@ -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(); diff --git a/classes/chessclockwidget.cpp b/classes/chessclockwidget.cpp index 46fff39..df29ae0 100644 --- a/classes/chessclockwidget.cpp +++ b/classes/chessclockwidget.cpp @@ -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(" %1 ") .arg(timeString( currentTurnPlayed()) ) ); + else + turnTimeLabel_->setText( timeString( currentTurnPlayed() ) ); + // Loser flag loserLabel_->setVisible( isLoser()); diff --git a/classes/chessclockwidget.h b/classes/chessclockwidget.h index 8de2136..356b8d6 100644 --- a/classes/chessclockwidget.h +++ b/classes/chessclockwidget.h @@ -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 index 0000000..ed1352b --- /dev/null +++ b/classes/clockswidget.cpp @@ -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 +#include +#include +#include +#include +#include + + +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("Paused. Touch to continue.")); + pauseLabel_->setFont( QFont("Helvetica",25)); + pauseLabel_->setAlignment( Qt::AlignCenter); + pauseLabel_->setVisible( false ); + + // Welcome label for first touch + welcomeLabel_ = new QLabel( tr("Welcome! Please touch to start game.
" + "Then touch to end turn.
")); + 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 index 0000000..72dd641 --- /dev/null +++ b/classes/clockswidget.h @@ -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 +#include + +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 index 0000000..29c9409 --- /dev/null +++ b/classes/welcomescreenwidget.cpp @@ -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 +#include +#include +#include + +#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("

Chess Clock


" + "Version %1
" + "© Arto Hyvättinen 2010
" + "Chess Clock is free software in terms of GNU General Public Licence v3" + "

Welcome to Chess Clock!
" + "Please touch to set up game clock.

" + ).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 index 0000000..434845b --- /dev/null +++ b/classes/welcomescreenwidget.h @@ -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 + +/*! 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