1 /**************************************************************************
5 Copyright (c) Arto Hyvättinen 2010
7 This file is part of Chess Clock software.
9 Chess Clock is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Chess Clock is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 **************************************************************************/
22 #include "chessclockwidget.h"
26 #include <QVBoxLayout>
27 #include <QHBoxLayout>
29 ChessClockWidget::ChessClockWidget(bool white, QWidget *parent) :
30 ChessClock(white, parent)
34 mainLayout = new QVBoxLayout;
41 setLayout( mainLayout );
45 void ChessClockWidget::setGreenTime(int msecs)
50 void ChessClockWidget::initPictures()
52 // Load pictures from resources.
55 picActive_.load(":/rc/pic/white_blue.png");
56 picPassive_.load(":/rc/pic/white_gray.png");
60 picActive_.load(":/rc/pic/black_blue.png");
61 picPassive_.load(":/rc/pic/black_gray.png");
63 picLoser_.load(":/rc/pic/loser.png");
66 void ChessClockWidget::initLabels()
68 QFont normalFont("Helvetica",24);
70 pictureLabel_ = new QLabel;
71 pictureLabel_->setPixmap( picPassive_ );
73 timeUsedLabel_ = new QLabel;
74 timeAverageLabel_ = new QLabel;
75 turnLabel_=new QLabel;
76 turnTimeLabel_=new QLabel;
78 timeUsedLabel_->setFont(normalFont);
79 timeAverageLabel_->setFont(normalFont);
80 turnLabel_->setFont(normalFont);
82 QFont turnTimeFont("Helvetica",36,QFont::Bold);
83 turnTimeLabel_->setFont(turnTimeFont);
85 loserLabel_ = new QLabel;
86 loserLabel_->setPixmap(picLoser_);
87 loserLabel_->setVisible(false);
91 void ChessClockWidget::initTop()
93 QVBoxLayout* details = new QVBoxLayout();
94 details->addWidget(timeUsedLabel_);
95 details->addWidget(timeAverageLabel_);
96 details->addWidget(turnLabel_);
97 details->addWidget(turnTimeLabel_);
99 QHBoxLayout* topLayout = new QHBoxLayout();
103 // Picture | Details | .. | LOSER
104 topLayout->addWidget( pictureLabel_ );
105 topLayout->addLayout( details );
106 topLayout->addStretch();
107 topLayout->addWidget(loserLabel_);
112 // LOSER | ... | Details | Picture
113 topLayout->addWidget(loserLabel_);
114 topLayout->addStretch();
115 topLayout->addLayout( details );
116 topLayout->addWidget( pictureLabel_ );
118 mainLayout->addLayout(topLayout);
121 void ChessClockWidget::initBottom()
123 // At bottom, time left in BIG font!
124 QFont bigfont("Helvetica",65,QFont::Bold);
125 leftLabel_ = new QLabel("0.00.00");
126 leftLabel_->setFont(bigfont);
127 // Black player: right alignment
129 leftLabel_->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
130 mainLayout->addWidget(leftLabel_);
133 void ChessClockWidget::repaintClock()
136 if( getStatus() == Running || getStatus() == Paused)
137 pictureLabel_->setPixmap( picActive_);
139 pictureLabel_->setPixmap(picPassive_);
141 timeUsedLabel_->setText( timeString( getTimePlayed() ));
143 // Time average per turn
148 timeAverage = getTimePlayed() / getTurn();
149 timeAverageLabel_->setText( tr("Avg %1").arg( timeString( timeAverage ) ) );
151 turnLabel_->setText( tr("Turn %1").arg(getTurn()));
153 // Current turn played
154 // Extra time of this turn is shown in green.
155 if( currentTurnPlayed() < greenTime_ )
156 turnTimeLabel_->setText( QString("<font color=green> %1 </font>") .arg(timeString( currentTurnPlayed()) ) );
158 turnTimeLabel_->setText( timeString( currentTurnPlayed() ) );
161 loserLabel_->setVisible( isLoser());
163 leftLabel_->setText( timeString(getTimeAvailable()));
167 QString ChessClockWidget::timeString(int msecs)
169 int secs = msecs / 1000 % 60;
170 int mins = msecs / ( 60 * 1000) % 60;
171 int hours = msecs / ( 60 * 60 * 1000 );
173 return QString(tr("<font color=red> %1:%2:%3 <font>").arg(0-hours).arg(0-mins,2,10,QChar('0')).arg(0-secs,2,10,QChar('0')));
175 return QString(tr("%1:%2:%3").arg(hours).arg(mins,2,10,QChar('0')).arg(secs,2,10,QChar('0')));