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

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     SPDX-FileCopyrightText: 2016 Alexander Semke <alexander.semke@web.de>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "historywidget.h"
0010 #include "ui_historywidget.h"
0011 #include "gamemanager.h"
0012 #include "knightsdebug.h"
0013 
0014 #include <QScrollBar>
0015 
0016 #include <math.h>
0017 
0018 using namespace Knights;
0019 
0020 HistoryWidget::HistoryWidget(QWidget* parent, Qt::WindowFlags f): QWidget(parent, f) {
0021     ui = new Ui::HistoryWidget;
0022     ui->setupUi(this);
0023 
0024     ui->twMoves->setSelectionBehavior(QAbstractItemView::SelectItems);
0025     ui->twMoves->setSelectionMode(QAbstractItemView::SingleSelection);
0026     ui->twMoves->horizontalHeaderItem(0)->setText(i18n("white"));
0027     ui->twMoves->horizontalHeaderItem(1)->setText(i18n("black"));
0028 
0029     connect( ui->notationComboBox, static_cast<void (QComboBox::*)(int)> (&QComboBox::currentIndexChanged),
0030              this, &HistoryWidget::updateHistory );
0031     connect( Manager::self(), &Manager::historyChanged, this, &HistoryWidget::updateHistory );
0032 
0033     qCDebug(LOG_KNIGHTS);
0034 }
0035 
0036 HistoryWidget::~HistoryWidget() {
0037     delete ui;
0038 }
0039 
0040 void HistoryWidget::updateHistory() {
0041     Move::Notation notation;
0042     switch ( ui->notationComboBox->currentIndex() ) {
0043     case 0:
0044         notation = Move::Algebraic;
0045         break;
0046     case 1:
0047         notation = Move::LongAlgebraic;
0048         break;
0049     case 2:
0050         notation = Move::Coordinate;
0051         break;
0052     default:
0053         notation = Move::Algebraic;
0054         break;
0055     }
0056 
0057     bool bottom = ui->twMoves->verticalScrollBar()->value() == ui->twMoves->verticalScrollBar()->maximum();
0058 
0059     ui->twMoves->clearContents();
0060 
0061     const QStack<Move> moveHistory = Manager::self()->moveHistory();
0062     ui->twMoves->setRowCount(ceil(double(moveHistory.size())/2));
0063     for (int i=1; i<=moveHistory.size(); ++i) {
0064         const Move& move = moveHistory.at(i-1);
0065         QTableWidgetItem* item = new QTableWidgetItem(move.stringForNotation(notation));
0066         const int row = ceil(double(i)/2)-1;
0067         const int column = i%2 ? 0 : 1;
0068         ui->twMoves->setItem(row, column, item);
0069     }
0070 
0071     if (bottom)
0072         ui->twMoves->scrollToBottom();
0073 }
0074 
0075 #include "moc_historywidget.cpp"