File indexing completed on 2024-04-28 04:03:11

0001 /*
0002     This file is part of Knights, a chess board for KDE SC 4.
0003     SPDX-FileCopyrightText: 2009, 2010, 2011 Miha Čančula <miha@noughmad.eu>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "clockwidget.h"
0009 #include "knightsdebug.h"
0010 #include "ui_clockwidget.h"
0011 
0012 #include <QTime>
0013 
0014 using namespace Knights;
0015 
0016 ClockWidget::ClockWidget ( QWidget* parent, Qt::WindowFlags f ) : QWidget ( parent, f ),
0017     ui(new Ui::ClockWidget),
0018     m_activePlayer(NoColor) {
0019 
0020     ui->setupUi ( this );
0021 }
0022 
0023 ClockWidget::~ClockWidget() {
0024     delete ui;
0025 }
0026 
0027 void ClockWidget::setDisplayedPlayer ( Color color ) {
0028     bool w = ( color == White );
0029     ui->verticalLayout->addWidget ( w ? ui->groupB : ui->groupW );
0030     ui->verticalLayout->addWidget ( w ? ui->groupW : ui->groupB );
0031 }
0032 
0033 void ClockWidget::setPlayerName ( Color color, const QString& name ) {
0034     switch ( color ) {
0035     case White:
0036         ui->groupW->setTitle ( name );
0037         break;
0038     case Black:
0039         ui->groupB->setTitle ( name );
0040         break;
0041     default:
0042         break;
0043     }
0044 }
0045 
0046 void ClockWidget::setCurrentTime ( Color color, const QTime& time ) {
0047     m_currentTime[color] = time;
0048 
0049     const int miliSeconds = time.hour() * 3600 * 1000 + time.minute() * 60 * 1000 + time.second() * 1000 + time.msec();
0050     const int units = miliSeconds / 100;
0051     QProgressBar* bar = ( color == White ) ? ui->progressW : ui->progressB;
0052     if ( units > bar->maximum() ) {
0053         bar->setMaximum ( units );
0054         updateTimeFormat();
0055     }
0056     bar->setValue ( units );
0057     bar->setFormat ( time.toString( m_timeFormat ) );
0058 
0059     Clock* clock = ( color == White ) ? ui->clockW : ui->clockB;
0060     clock->setTime ( time );
0061 }
0062 
0063 void ClockWidget::setTimeLimit ( Color color, const QTime& time ) {
0064     qCDebug(LOG_KNIGHTS) << color << time;
0065     m_timeLimit[color] = time;
0066     int seconds = time.hour() * 3600 + time.minute() * 60 + time.second();
0067     switch ( color ) {
0068     case White:
0069         ui->progressW->setMaximum ( seconds * 10 );
0070         break;
0071     case Black:
0072         ui->progressB->setMaximum ( seconds * 10 );
0073         break;
0074     default:
0075         break;
0076     }
0077     updateTimeFormat();
0078     setCurrentTime( color, time );
0079 }
0080 
0081 void ClockWidget::updateTimeFormat() {
0082     if ( m_timeLimit[White] > QTime(1,0) || m_timeLimit[Black] > QTime(1,0) )
0083         m_timeFormat = QStringLiteral("h:mm:ss");
0084     else
0085         m_timeFormat = QStringLiteral("mm:ss");
0086 }
0087 
0088 #include "moc_clockwidget.cpp"