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 greenTime_ = 0;
00033
00034 mainLayout = new QVBoxLayout;
00035
00036 initPictures();
00037 initLabels();
00038 initTop();
00039 initBottom();
00040
00041 setLayout( mainLayout );
00042 }
00043
00044
00045 void ChessClockWidget::setGreenTime(int msecs)
00046 {
00047 greenTime_ = msecs;
00048 }
00049
00050 void ChessClockWidget::initPictures()
00051 {
00052
00053 if( isWhite() )
00054 {
00055 picActive_.load(":/rc/pic/white_blue.png");
00056 picPassive_.load(":/rc/pic/white_gray.png");
00057 }
00058 else
00059 {
00060 picActive_.load(":/rc/pic/black_blue.png");
00061 picPassive_.load(":/rc/pic/black_gray.png");
00062 }
00063 picLoser_.load(":/rc/pic/loser.png");
00064 }
00065
00066 void ChessClockWidget::initLabels()
00067 {
00068 QFont normalFont("Helvetica",24);
00069
00070 pictureLabel_ = new QLabel;
00071 pictureLabel_->setPixmap( picPassive_ );
00072
00073 timeUsedLabel_ = new QLabel;
00074 timeAverageLabel_ = new QLabel;
00075 turnLabel_=new QLabel;
00076 turnTimeLabel_=new QLabel;
00077
00078 timeUsedLabel_->setFont(normalFont);
00079 timeAverageLabel_->setFont(normalFont);
00080 turnLabel_->setFont(normalFont);
00081
00082 QFont turnTimeFont("Helvetica",36,QFont::Bold);
00083 turnTimeLabel_->setFont(turnTimeFont);
00084
00085 loserLabel_ = new QLabel;
00086 loserLabel_->setPixmap(picLoser_);
00087 loserLabel_->setVisible(false);
00088
00089 }
00090
00091 void ChessClockWidget::initTop()
00092 {
00093 QVBoxLayout* details = new QVBoxLayout();
00094 details->addWidget(timeUsedLabel_);
00095 details->addWidget(timeAverageLabel_);
00096 details->addWidget(turnLabel_);
00097 details->addWidget(turnTimeLabel_);
00098
00099 QHBoxLayout* topLayout = new QHBoxLayout();
00100 if( isWhite() )
00101 {
00102
00103
00104 topLayout->addWidget( pictureLabel_ );
00105 topLayout->addLayout( details );
00106 topLayout->addStretch();
00107 topLayout->addWidget(loserLabel_);
00108 }
00109 else
00110 {
00111
00112
00113 topLayout->addWidget(loserLabel_);
00114 topLayout->addStretch();
00115 topLayout->addLayout( details );
00116 topLayout->addWidget( pictureLabel_ );
00117 }
00118 mainLayout->addLayout(topLayout);
00119 }
00120
00121 void ChessClockWidget::initBottom()
00122 {
00123
00124 QFont bigfont("Helvetica",65,QFont::Bold);
00125 leftLabel_ = new QLabel("0.00.00");
00126 leftLabel_->setFont(bigfont);
00127
00128 if( !isWhite() )
00129 leftLabel_->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
00130 mainLayout->addWidget(leftLabel_);
00131 }
00132
00133 void ChessClockWidget::repaintClock()
00134 {
00135
00136 if( getStatus() == Running || getStatus() == Paused)
00137 pictureLabel_->setPixmap( picActive_);
00138 else
00139 pictureLabel_->setPixmap(picPassive_);
00140
00141 timeUsedLabel_->setText( timeString( getTimePlayed() ));
00142
00143
00144 int timeAverage;
00145 if( getTurn() == 0)
00146 timeAverage = 0;
00147 else
00148 timeAverage = getTimePlayed() / getTurn();
00149 timeAverageLabel_->setText( tr("Avg %1").arg( timeString( timeAverage ) ) );
00150
00151 turnLabel_->setText( tr("Turn %1").arg(getTurn()));
00152
00153
00154
00155 if( currentTurnPlayed() < greenTime_ )
00156 turnTimeLabel_->setText( QString("<font color=green> %1 </font>") .arg(timeString( currentTurnPlayed()) ) );
00157 else
00158 turnTimeLabel_->setText( timeString( currentTurnPlayed() ) );
00159
00160
00161 loserLabel_->setVisible( isLoser());
00162
00163 leftLabel_->setText( timeString(getTimeAvailable()));
00164
00165 }
00166
00167 QString ChessClockWidget::timeString(int msecs)
00168 {
00169 int secs = msecs / 1000 % 60;
00170 int mins = msecs / ( 60 * 1000) % 60;
00171 int hours = msecs / ( 60 * 60 * 1000 );
00172 if( msecs < 0)
00173 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')));
00174 else
00175 return QString(tr("%1:%2:%3").arg(hours).arg(mins,2,10,QChar('0')).arg(secs,2,10,QChar('0')));
00176
00177 }