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)
32 mainLayout = new QVBoxLayout;
39 setLayout( mainLayout );
43 void ChessClockWidget::initPictures()
45 // Load pictures from resources.
48 picActive_.load(":/rc/pic/white_blue.png");
49 picPassive_.load(":/rc/pic/white_gray.png");
53 picActive_.load(":/rc/pic/black_blue.png");
54 picPassive_.load(":/rc/pic/black_gray.png");
56 picLoser_.load(":/rc/pic/loser.png");
59 void ChessClockWidget::initLabels()
61 QFont normalFont("Helvetica",24);
63 pictureLabel_ = new QLabel;
64 pictureLabel_->setPixmap( picPassive_ );
66 timeUsedLabel_ = new QLabel;
67 timeAverageLabel_ = new QLabel;
68 turnLabel_=new QLabel;
70 timeUsedLabel_->setFont(normalFont);
71 timeAverageLabel_->setFont(normalFont);
72 turnLabel_->setFont(normalFont);
74 loserLabel_ = new QLabel;
75 loserLabel_->setPixmap(picLoser_);
76 loserLabel_->setVisible(false);
80 void ChessClockWidget::initTop()
82 QVBoxLayout* details = new QVBoxLayout();
83 details->addWidget(timeUsedLabel_);
84 details->addWidget(timeAverageLabel_);
85 details->addWidget(turnLabel_);
87 QHBoxLayout* topLayout = new QHBoxLayout();
91 // Picture | Details | .. | LOSER
92 topLayout->addWidget( pictureLabel_ );
93 topLayout->addLayout( details );
94 topLayout->addStretch();
95 topLayout->addWidget(loserLabel_);
100 // LOSER | ... | Details | Picture
101 topLayout->addWidget(loserLabel_);
102 topLayout->addStretch();
103 topLayout->addLayout( details );
104 topLayout->addWidget( pictureLabel_ );
106 mainLayout->addLayout(topLayout);
109 void ChessClockWidget::initBottom()
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
117 leftLabel_->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
118 mainLayout->addWidget(leftLabel_);
121 void ChessClockWidget::repaintClock()
124 if( getStatus() == Running )
125 pictureLabel_->setPixmap( picActive_);
127 pictureLabel_->setPixmap(picPassive_);
129 timeUsedLabel_->setText( timeString( getTimePlayed() ));
131 // Time average per turn
136 timeAverage = getTimePlayed() / getTurn();
137 timeAverageLabel_->setText( tr("Avg %1").arg( timeString( timeAverage ) ) );
139 turnLabel_->setText( tr("Turn %1").arg(getTurn()));
142 loserLabel_->setVisible( isLoser());
144 leftLabel_->setText( timeString(getTimeAvailable()));
148 QString ChessClockWidget::timeString(int msecs)
150 int secs = msecs / 1000 % 60;
151 int mins = msecs / ( 60 * 1000) % 60;
152 int hours = msecs / ( 60 * 60 * 1000 );
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')));
156 return QString(tr("%1:%2:%3").arg(hours).arg(mins,2,10,QChar('0')).arg(secs,2,10,QChar('0')));