1 /***********************************************************************
2 *
3 * Copyright (C) 2008, 2010 Graeme Gott <graeme@gottcode.org>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ***********************************************************************/
19
20 #include "score_board.h"
21
22 #include <QHeaderView>
23 #include <QPushButton>
24 #include <QSettings>
25 #include <QTreeWidget>
26 #include <QVBoxLayout>
27
28 /*****************************************************************************/
29
ScoreBoard(QWidget * parent)30 ScoreBoard::ScoreBoard(QWidget* parent)
31 : QDialog(parent)
32 {
33 setWindowTitle(tr("Gottet Scores"));
34
35 // Create tree view
36 m_scores = new QTreeWidget(this);
37 m_scores->setHeaderLabels(QStringList() << tr("Level") << tr("Lines") << tr("Score"));
38 m_scores->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
39 m_scores->setRootIsDecorated(false);
40
41 // Load scores
42 QStringList scores = QSettings().value("Scores").toString().split(";", QString::SkipEmptyParts);
43 QStringList values;
44 for (const QString& score : scores) {
45 values = score.split(",", QString::SkipEmptyParts);
46 if (values.count() != 3)
47 continue;
48 m_scores->addTopLevelItem( createItem(values[0].toInt(), values[1].toInt(), values[2].toInt()) );
49 }
50
51 // Create OK button
52 QPushButton* ok_button = new QPushButton(tr("OK"), this);
53 connect(ok_button, &QPushButton::clicked, this, &ScoreBoard::accept);
54
55 // Layout window
56 QVBoxLayout* layout = new QVBoxLayout(this);
57 layout->setSpacing(24);
58 layout->addWidget(m_scores);
59 layout->addWidget(ok_button, 0, Qt::AlignRight);
60
61 // Resize window
62 resize(QSettings().value("ScoresSize", sizeHint()).toSize());
63 }
64
65 /*****************************************************************************/
66
highScorePosition(int score) const67 int ScoreBoard::highScorePosition(int score) const
68 {
69 // Empty scores can't go on scoreboard
70 if (score == 0)
71 return 10;
72
73 // Return position
74 int position = 0;
75 for (position = 0; position < m_scores->topLevelItemCount(); ++position) {
76 if (score > m_scores->topLevelItem(position)->data(2, Qt::UserRole).toInt())
77 break;
78 }
79 return position;
80 }
81
82 /*****************************************************************************/
83
addHighScore(int level,int lines,int score)84 void ScoreBoard::addHighScore(int level, int lines, int score)
85 {
86 // Find position in high score list
87 int position = highScorePosition(score);
88 if (position >= 10)
89 return;
90
91 // Add score to list
92 if (m_scores->topLevelItemCount() == 10)
93 delete m_scores->takeTopLevelItem(9);
94 QTreeWidgetItem* item = createItem(level, lines, score);
95 m_scores->insertTopLevelItem(position, item);
96 m_scores->clearSelection();
97 item->setSelected(true);
98
99 // Save scores
100 QStringList scores;
101 for (int i = 0; i < m_scores->topLevelItemCount(); ++i) {
102 item = m_scores->topLevelItem(i);
103 scores.append(QString("%1,%2,%3").arg(item->text(0)).arg(item->text(1)).arg(item->data(2, Qt::UserRole).toInt()));
104 }
105 QSettings().setValue("Scores", scores.join(";"));
106
107 exec();
108 }
109
110 /*****************************************************************************/
111
hideEvent(QHideEvent * event)112 void ScoreBoard::hideEvent(QHideEvent* event)
113 {
114 QSettings().setValue("ScoresSize", size());
115 QDialog::hideEvent(event);
116 }
117
118 /*****************************************************************************/
119
createItem(int level,int lines,int score) const120 QTreeWidgetItem* ScoreBoard::createItem(int level, int lines, int score) const
121 {
122 QTreeWidgetItem* item = new QTreeWidgetItem(QStringList() << QString::number(level) << QString::number(lines) << QString("%L1").arg(score));
123 item->setData(2, Qt::UserRole, score);
124 item->setTextAlignment(0, Qt::AlignRight | Qt::AlignVCenter);
125 item->setTextAlignment(1, Qt::AlignRight | Qt::AlignVCenter);
126 item->setTextAlignment(2, Qt::AlignRight | Qt::AlignVCenter);
127 return item;
128 }
129
130 /*****************************************************************************/
131