Updated the web pages
[chessclock] / classes / chessclockwidget.cpp
1  /**************************************************************************
2
3     Chess Clock
4
5     Copyright (c) Arto Hyvättinen 2010
6
7     This file is part of Chess Clock software.
8
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.
13
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.
18
19
20 **************************************************************************/
21
22 #include "chessclockwidget.h"
23
24 #include <QLabel>
25 #include <QFont>
26 #include <QVBoxLayout>
27 #include <QHBoxLayout>
28
29 ChessClockWidget::ChessClockWidget(bool white, QWidget *parent) :
30     ChessClock(white, parent)
31 {
32     greenTime_ = 0;
33
34     mainLayout = new QVBoxLayout;
35
36     initPictures();
37     initLabels();
38     initTop();
39     initBottom();
40
41     setLayout( mainLayout );
42 }
43
44
45 void ChessClockWidget::setGreenTime(int msecs)
46 {
47     greenTime_ = msecs;
48 }
49
50 void ChessClockWidget::initPictures()
51 {
52     // Load pictures from resources.
53     if(  isWhite() )
54       {
55           picActive_.load(":/rc/pic/white_blue.png");
56           picPassive_.load(":/rc/pic/white_gray.png");
57       }
58       else
59       {
60           picActive_.load(":/rc/pic/black_blue.png");
61           picPassive_.load(":/rc/pic/black_gray.png");
62       }
63       picLoser_.load(":/rc/pic/loser.png");
64 }
65
66 void ChessClockWidget::initLabels()
67 {
68     QFont normalFont("Helvetica",24);
69
70     pictureLabel_ = new QLabel;
71     pictureLabel_->setPixmap( picPassive_ );
72
73     timeUsedLabel_ = new QLabel;
74     timeAverageLabel_ = new QLabel;
75     turnLabel_=new QLabel;
76     turnTimeLabel_=new QLabel;
77
78     timeUsedLabel_->setFont(normalFont);
79     timeAverageLabel_->setFont(normalFont);
80     turnLabel_->setFont(normalFont);
81
82     QFont turnTimeFont("Helvetica",36,QFont::Bold);
83     turnTimeLabel_->setFont(turnTimeFont);
84
85     loserLabel_ = new QLabel;
86     loserLabel_->setPixmap(picLoser_);
87     loserLabel_->setVisible(false);
88
89 }
90
91 void ChessClockWidget::initTop()
92 {
93     QVBoxLayout* details = new QVBoxLayout();
94     details->addWidget(timeUsedLabel_);
95     details->addWidget(timeAverageLabel_);
96     details->addWidget(turnLabel_);
97     details->addWidget(turnTimeLabel_);
98
99     QHBoxLayout* topLayout = new QHBoxLayout();
100     if( isWhite() )
101     {
102         // White player
103         //  Picture  |  Details | .. | LOSER
104         topLayout->addWidget( pictureLabel_ );
105         topLayout->addLayout( details );
106         topLayout->addStretch();
107         topLayout->addWidget(loserLabel_);
108     }
109     else
110     {
111         // Black player
112         // LOSER | ... | Details | Picture
113         topLayout->addWidget(loserLabel_);
114         topLayout->addStretch();
115         topLayout->addLayout( details );
116         topLayout->addWidget( pictureLabel_ );
117     }
118     mainLayout->addLayout(topLayout);
119 }
120
121 void ChessClockWidget::initBottom()
122 {
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
128     if( !isWhite() )
129        leftLabel_->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
130     mainLayout->addWidget(leftLabel_);
131 }
132
133 void ChessClockWidget::repaintClock()
134 {
135     // Set picture
136     if( getStatus() == Running || getStatus() == Paused)
137         pictureLabel_->setPixmap( picActive_);
138     else
139         pictureLabel_->setPixmap(picPassive_);
140
141     timeUsedLabel_->setText( timeString( getTimePlayed() ));
142
143     // Time average per turn
144     int timeAverage;
145     if( getTurn() == 0)
146         timeAverage = 0;
147     else
148         timeAverage = getTimePlayed() / getTurn();
149     timeAverageLabel_->setText( tr("Avg %1").arg( timeString( timeAverage ) ) );
150
151     turnLabel_->setText( tr("Turn %1").arg(getTurn()));
152
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()) ) );
157     else
158         turnTimeLabel_->setText( timeString( currentTurnPlayed() ) );
159
160     // Loser flag
161     loserLabel_->setVisible( isLoser());
162
163     leftLabel_->setText( timeString(getTimeAvailable()));
164
165 }
166
167 QString ChessClockWidget::timeString(int msecs)
168 {
169     int secs = msecs / 1000 % 60;
170     int mins = msecs / ( 60 * 1000) % 60;
171     int hours = msecs / ( 60 * 60 * 1000 );
172     if( msecs < 0)
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')));
174     else
175         return QString(tr("%1:%2:%3").arg(hours).arg(mins,2,10,QChar('0')).arg(secs,2,10,QChar('0')));
176
177 }