File indexing completed on 2025-02-02 07:28:25
0001 /* 0002 Copyright (C) 2002-2005, Jason Katz-Brown <jasonkb@mit.edu> 0003 0004 This program is free software; you can redistribute it and/or modify 0005 it under the terms of the GNU General Public License as published by 0006 the Free Software Foundation; either version 2 of the License, or 0007 (at your option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0012 GNU General Public License for more details. 0013 0014 You should have received a copy of the GNU General Public License 0015 along with this program; if not, write to the Free Software 0016 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 0017 */ 0018 0019 #include "scoreboard.h" 0020 0021 #include <QHeaderView> 0022 #include <KLocalizedString> 0023 0024 ScoreBoard::ScoreBoard(QWidget *parent) 0025 : QTableWidget(1, 1, parent) 0026 { 0027 setVerticalHeaderItem(rowCount() -1, new QTableWidgetItem(i18n("Par"))); 0028 setHorizontalHeaderItem(columnCount() -1, new QTableWidgetItem(i18n("Total"))); 0029 0030 setFocusPolicy(Qt::NoFocus); 0031 setEditTriggers(QAbstractItemView::NoEditTriggers); 0032 0033 resizeColumnToContents(columnCount() - 1); 0034 0035 verticalHeader()->setSectionResizeMode(QHeaderView::Fixed); 0036 0037 doUpdateHeight(); 0038 } 0039 0040 void ScoreBoard::newHole(int par) 0041 { 0042 int _columnCount = columnCount(); 0043 insertColumn(_columnCount - 1); 0044 setHorizontalHeaderItem(columnCount() -2, new QTableWidgetItem(QString::number(columnCount() - 1))); 0045 //set each player's score to 0 0046 for (int i = 0; i < rowCount() - 1; i++) 0047 setItem(i, columnCount() -2, new QTableWidgetItem(QString::number(0))); 0048 setItem(rowCount() - 1, columnCount() - 2, new QTableWidgetItem(QString::number(par))); 0049 0050 // update total 0051 int tot = 0; 0052 for (int i = 0; i < columnCount() - 1; ++i) 0053 tot += item(rowCount() - 1, i)->text().toInt(); 0054 setItem(rowCount() - 1, columnCount() - 1, new QTableWidgetItem(QString::number(tot))); 0055 0056 resizeColumnToContents(columnCount() - 2); 0057 } 0058 0059 void ScoreBoard::newPlayer(const QString &name) 0060 { 0061 //kDebug(12007) << "name of new player is" << name; 0062 insertRow(rowCount() - 1); 0063 setVerticalHeaderItem(rowCount() -2, new QTableWidgetItem(name)); 0064 0065 doUpdateHeight(); 0066 } 0067 0068 void ScoreBoard::setScore(int id, int hole, int score) 0069 { 0070 setItem(id - 1, hole - 1, new QTableWidgetItem(QString::number(score))); 0071 0072 QString name; 0073 setItem(id - 1, columnCount() - 1, new QTableWidgetItem(QString::number(total(id, name)))); 0074 0075 resizeColumnToContents(hole -1); 0076 0077 setCurrentCell(id - 1, hole - 1); 0078 } 0079 0080 void ScoreBoard::parChanged(int hole, int par) 0081 { 0082 setItem(rowCount() - 1, hole - 1, new QTableWidgetItem(QString::number(par))); 0083 0084 // update total 0085 int tot = 0; 0086 for (int i = 0; i < columnCount() - 1; ++i) 0087 tot += item(rowCount() - 1, i)->text().toInt(); 0088 setItem(rowCount() - 1, columnCount() - 1, new QTableWidgetItem(QString::number(tot))); 0089 } 0090 0091 int ScoreBoard::total(int id, QString &name) 0092 { 0093 int tot = 0; 0094 for (int i = 0; i < columnCount() - 1; i++) 0095 tot += item(id - 1, i)->text().toInt(); 0096 0097 name = verticalHeaderItem(id - 1)->text(); 0098 0099 //kDebug(12007) << "tot is" << tot; 0100 return tot; 0101 } 0102 0103 void ScoreBoard::doUpdateHeight() 0104 { 0105 int height = 0; 0106 0107 height += horizontalHeader()->height(); 0108 for (int i = 0; i < qMin(3, rowCount()); ++i) height += verticalHeader()->sectionSize(i); 0109 height += size().height() - horizontalHeader()->height() - viewport()->height(); 0110 setFixedHeight(height); 0111 } 0112 0113 #include "moc_scoreboard.cpp"