46fff39366cd3d9dcb310bbd618cdeef8f2d3f61
[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     mainLayout = new QVBoxLayout;
33
34     initPictures();
35     initLabels();
36     initTop();
37     initBottom();
38
39     setLayout( mainLayout );
40 }
41
42
43 void ChessClockWidget::initPictures()
44 {
45     // Load pictures from resources.
46     if(  isWhite() )
47       {
48           picActive_.load(":/rc/pic/white_blue.png");
49           picPassive_.load(":/rc/pic/white_gray.png");
50       }
51       else
52       {
53           picActive_.load(":/rc/pic/black_blue.png");
54           picPassive_.load(":/rc/pic/black_gray.png");
55       }
56       picLoser_.load(":/rc/pic/loser.png");
57 }
58
59 void ChessClockWidget::initLabels()
60 {
61     QFont normalFont("Helvetica",24);
62
63     pictureLabel_ = new QLabel;
64     pictureLabel_->setPixmap( picPassive_ );
65
66     timeUsedLabel_ = new QLabel;
67     timeAverageLabel_ = new QLabel;
68     turnLabel_=new QLabel;
69
70     timeUsedLabel_->setFont(normalFont);
71     timeAverageLabel_->setFont(normalFont);
72     turnLabel_->setFont(normalFont);
73
74     loserLabel_ = new QLabel;
75     loserLabel_->setPixmap(picLoser_);
76     loserLabel_->setVisible(false);
77
78 }
79
80 void ChessClockWidget::initTop()
81 {
82     QVBoxLayout* details = new QVBoxLayout();
83     details->addWidget(timeUsedLabel_);
84     details->addWidget(timeAverageLabel_);
85     details->addWidget(turnLabel_);
86
87     QHBoxLayout* topLayout = new QHBoxLayout();
88     if( isWhite() )
89     {
90         // White player
91         //  Picture  |  Details | .. | LOSER
92         topLayout->addWidget( pictureLabel_ );
93         topLayout->addLayout( details );
94         topLayout->addStretch();
95         topLayout->addWidget(loserLabel_);
96     }
97     else
98     {
99         // Black player
100         // LOSER | ... | Details | Picture
101         topLayout->addWidget(loserLabel_);
102         topLayout->addStretch();
103         topLayout->addLayout( details );
104         topLayout->addWidget( pictureLabel_ );
105     }
106     mainLayout->addLayout(topLayout);
107 }
108
109 void ChessClockWidget::initBottom()
110 {
111     // At bottom, time left in BIG font!
112     QFont bigfont("Helvetica",65,QFont::Bold);
113     leftLabel_ = new QLabel("0.00.00");
114     leftLabel_->setFont(bigfont);
115     // Black player: right alignment
116     if( isWhite() )
117        leftLabel_->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
118     mainLayout->addWidget(leftLabel_);
119 }
120
121 void ChessClockWidget::repaintClock()
122 {
123     // Set picture
124     if( getStatus() == Running )
125         pictureLabel_->setPixmap( picActive_);
126     else
127         pictureLabel_->setPixmap(picPassive_);
128
129     timeUsedLabel_->setText( timeString( getTimePlayed() ));
130
131     // Time average per turn
132     int timeAverage;
133     if( getTurn() == 0)
134         timeAverage = 0;
135     else
136         timeAverage = getTimePlayed() / getTurn();
137     timeAverageLabel_->setText( tr("Avg %1").arg( timeString( timeAverage ) ) );
138
139     turnLabel_->setText( tr("Turn %1").arg(getTurn()));
140
141     // Loser flag
142     loserLabel_->setVisible( isLoser());
143
144     leftLabel_->setText( timeString(getTimeAvailable()));
145
146 }
147
148 QString ChessClockWidget::timeString(int msecs)
149 {
150     int secs = msecs / 1000 % 60;
151     int mins = msecs / ( 60 * 1000) % 60;
152     int hours = msecs / ( 60 * 60 * 1000 );
153     if( msecs < 0)
154         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')));
155     else
156         return QString(tr("%1:%2:%3").arg(hours).arg(mins,2,10,QChar('0')).arg(secs,2,10,QChar('0')));
157
158 }