1 | /* |
2 | Copyright (C) 2002-2005, Jason Katz-Brown <jasonkb@mit.edu> |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | |
9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
18 | |
19 | #include "scoreboard.h" |
20 | |
21 | #include <QHeaderView> |
22 | #include <KLocale> |
23 | |
24 | ScoreBoard::ScoreBoard(QWidget *parent) |
25 | : QTableWidget(1, 1, parent) |
26 | { |
27 | setVerticalHeaderItem(rowCount() -1, new QTableWidgetItem(i18n("Par" ))); |
28 | setHorizontalHeaderItem(columnCount() -1, new QTableWidgetItem(i18n("Total" ))); |
29 | |
30 | setFocusPolicy(Qt::NoFocus); |
31 | setEditTriggers(QAbstractItemView::NoEditTriggers); |
32 | |
33 | resizeColumnToContents(columnCount() - 1); |
34 | |
35 | verticalHeader()->setResizeMode(QHeaderView::Fixed); |
36 | |
37 | doUpdateHeight(); |
38 | } |
39 | |
40 | void ScoreBoard::newHole(int par) |
41 | { |
42 | int _columnCount = columnCount(); |
43 | insertColumn(_columnCount - 1); |
44 | setHorizontalHeaderItem(columnCount() -2, new QTableWidgetItem(QString::number(columnCount() - 1))); |
45 | //set each player's score to 0 |
46 | for (int i = 0; i < rowCount() - 1; i++) |
47 | setItem(i, columnCount() -2, new QTableWidgetItem(QString::number(0))); |
48 | setItem(rowCount() - 1, columnCount() - 2, new QTableWidgetItem(QString::number(par))); |
49 | |
50 | // update total |
51 | int tot = 0; |
52 | for (int i = 0; i < columnCount() - 1; ++i) |
53 | tot += item(rowCount() - 1, i)->text().toInt(); |
54 | setItem(rowCount() - 1, columnCount() - 1, new QTableWidgetItem(QString::number(tot))); |
55 | |
56 | resizeColumnToContents(columnCount() - 2); |
57 | } |
58 | |
59 | void ScoreBoard::newPlayer(const QString &name) |
60 | { |
61 | //kDebug(12007) << "name of new player is" << name; |
62 | insertRow(rowCount() - 1); |
63 | setVerticalHeaderItem(rowCount() -2, new QTableWidgetItem(name)); |
64 | |
65 | doUpdateHeight(); |
66 | } |
67 | |
68 | void ScoreBoard::setScore(int id, int hole, int score) |
69 | { |
70 | setItem(id - 1, hole - 1, new QTableWidgetItem(QString::number(score))); |
71 | |
72 | QString name; |
73 | setItem(id - 1, columnCount() - 1, new QTableWidgetItem(QString::number(total(id, name)))); |
74 | |
75 | resizeColumnToContents(hole -1); |
76 | |
77 | setCurrentCell(id - 1, hole - 1); |
78 | } |
79 | |
80 | void ScoreBoard::parChanged(int hole, int par) |
81 | { |
82 | setItem(rowCount() - 1, hole - 1, new QTableWidgetItem(QString::number(par))); |
83 | |
84 | // update total |
85 | int tot = 0; |
86 | for (int i = 0; i < columnCount() - 1; ++i) |
87 | tot += item(rowCount() - 1, i)->text().toInt(); |
88 | setItem(rowCount() - 1, columnCount() - 1, new QTableWidgetItem(QString::number(tot))); |
89 | } |
90 | |
91 | int ScoreBoard::total(int id, QString &name) |
92 | { |
93 | int tot = 0; |
94 | for (int i = 0; i < columnCount() - 1; i++) |
95 | tot += item(id - 1, i)->text().toInt(); |
96 | |
97 | name = verticalHeaderItem(id - 1)->text(); |
98 | |
99 | //kDebug(12007) << "tot is" << tot; |
100 | return tot; |
101 | } |
102 | |
103 | void ScoreBoard::doUpdateHeight() |
104 | { |
105 | int height = 0; |
106 | |
107 | height += horizontalHeader()->height(); |
108 | for (int i = 0; i < qMin(3, rowCount()); ++i) height += verticalHeader()->sectionSize(i); |
109 | height += size().height() - horizontalHeader()->height() - viewport()->height(); |
110 | setFixedHeight(height); |
111 | } |
112 | |
113 | #include "scoreboard.moc" |
114 | |