00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "chessclockwidget.h"
00023
00024 #include <QLabel>
00025 #include <QFont>
00026 #include <QVBoxLayout>
00027 #include <QHBoxLayout>
00028
00029 ChessClockWidget::ChessClockWidget(bool white, QWidget *parent) :
00030 ChessClock(white, parent)
00031 {
00032 mainLayout = new QVBoxLayout;
00033
00034 initPictures();
00035 initLabels();
00036 initTop();
00037 initBottom();
00038
00039 setLayout( mainLayout );
00040 }
00041
00042
00043 void ChessClockWidget::initPictures()
00044 {
00045
00046 if( isWhite() )
00047 {
00048 picActive_.load(":/rc/pic/white_blue.png");
00049 picPassive_.load(":/rc/pic/white_gray.png");
00050 }
00051 else
00052 {
00053 picActive_.load(":/rc/pic/black_blue.png");
00054 picPassive_.load(":/rc/pic/black_gray.png");
00055 }
00056 picLoser_.load(":/rc/pic/loser.png");
00057 }
00058
00059 void ChessClockWidget::initLabels()
00060 {
00061 QFont normalFont("Helvetica",24);
00062
00063 pictureLabel_ = new QLabel;
00064 pictureLabel_->setPixmap( picPassive_ );
00065
00066 timeUsedLabel_ = new QLabel;
00067 timeAverageLabel_ = new QLabel;
00068 turnLabel_=new QLabel;
00069
00070 timeUsedLabel_->setFont(normalFont);
00071 timeAverageLabel_->setFont(normalFont);
00072 turnLabel_->setFont(normalFont);
00073
00074 loserLabel_ = new QLabel;
00075 loserLabel_->setPixmap(picLoser_);
00076 loserLabel_->setVisible(false);
00077
00078 }
00079
00080 void ChessClockWidget::initTop()
00081 {
00082 QVBoxLayout* details = new QVBoxLayout();
00083 details->addWidget(timeUsedLabel_);
00084 details->addWidget(timeAverageLabel_);
00085 details->addWidget(turnLabel_);
00086
00087 QHBoxLayout* topLayout = new QHBoxLayout();
00088 if( isWhite() )
00089 {
00090
00091
00092 topLayout->addWidget( pictureLabel_ );
00093 topLayout->addLayout( details );
00094 topLayout->addStretch();
00095 topLayout->addWidget(loserLabel_);
00096 }
00097 else
00098 {
00099
00100
00101 topLayout->addWidget(loserLabel_);
00102 topLayout->addStretch();
00103 topLayout->addLayout( details );
00104 topLayout->addWidget( pictureLabel_ );
00105 }
00106 mainLayout->addLayout(topLayout);
00107 }
00108
00109 void ChessClockWidget::initBottom()
00110 {
00111
00112 QFont bigfont("Helvetica",65,QFont::Bold);
00113 leftLabel_ = new QLabel("0.00.00");
00114 leftLabel_->setFont(bigfont);
00115
00116 if( isWhite() )
00117 leftLabel_->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
00118 mainLayout->addWidget(leftLabel_);
00119 }
00120
00121 void ChessClockWidget::repaintClock()
00122 {
00123
00124 if( getStatus() == Running )
00125 pictureLabel_->setPixmap( picActive_);
00126 else
00127 pictureLabel_->setPixmap(picPassive_);
00128
00129 timeUsedLabel_->setText( timeString( getTimePlayed() ));
00130
00131
00132 int timeAverage;
00133 if( getTurn() == 0)
00134 timeAverage = 0;
00135 else
00136 timeAverage = getTimePlayed() / getTurn();
00137 timeAverageLabel_->setText( tr("Average %1 per turn").arg( timeString( timeAverage ) ) );
00138
00139 turnLabel_->setText( tr("Turn %1").arg(getTurn()));
00140
00141
00142 loserLabel_->setVisible( isLoser());
00143
00144 leftLabel_->setText( timeString(getTimeAvailable()));
00145
00146 }
00147
00148 QString ChessClockWidget::timeString(int msecs)
00149 {
00150 int secs = msecs / 1000 % 60;
00151 int mins = msecs / ( 60 * 1000) % 60;
00152 int hours = msecs / ( 60 * 60 * 1000 );
00153 if( msecs < 0)
00154 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')));
00155 else
00156 return QString(tr("%1:%2:%3").arg(hours).arg(mins,2,10,QChar('0')).arg(secs,2,10,QChar('0')));
00157
00158 }