1
/* *************************************************************************
2
* Copyright 2012 Jakob Gruber <jakob.gruber@gmail.com> *
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. *
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. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
16
************************************************************************* */
18
#include "selectboardwindow.h"
20
#include <QAbstractTableModel>
23
#include <kpushbutton.h>
25
#include "src/logic/elapsedtime.h"
26
#include "src/logic/levelloader.h"
28
class LevelTableModel : public QAbstractTableModel
31
LevelTableModel(const QList<QSharedPointer<Level> > &levels, QObject * parent = 0);
34
int rowCount(const QModelIndex &parent) const;
35
int columnCount(const QModelIndex &parent) const;
36
QVariant data(const QModelIndex &index, int role) const;
37
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
40
const QList<QSharedPointer<Level> > &m_levels;
48
ColumnCount /* not a real column */
52
LevelTableModel::LevelTableModel(const QList<QSharedPointer<Level> > &levels, QObject *parent) :
53
QAbstractTableModel(parent), m_levels(levels)
58
int LevelTableModel::rowCount(const QModelIndex &parent) const {
60
return m_levels.size();
63
int LevelTableModel::columnCount(const QModelIndex &parent) const {
68
QVariant LevelTableModel::data(const QModelIndex &index, int role) const {
69
if (!index.isValid() || (role != Qt::DisplayRole && role != Qt::TextAlignmentRole)) {
73
if (role == Qt::TextAlignmentRole) {
74
return Qt::AlignCenter;
77
QSharedPointer<Level> level= m_levels[index.row()];
78
switch (index.column()) {
79
case Name: return level->visibleName();
80
case LevelSet: return level->levelset();
81
case Difficulty: return level->difficulty();
82
case Size: return QString("%1x%2").arg(level->width()).arg(level->height());
83
case Solved: return "TODO";
90
QVariant LevelTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
91
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
93
case Name: return QVariant(ki18n("Level").toString());
94
case LevelSet: return QVariant(ki18n("Level Set").toString());
95
case Difficulty: return QVariant(ki18n("Difficulty").toString());
96
case Size: return QVariant(ki18n("Size").toString());
97
case Solved: return QVariant(ki18nc("board solved", "Solved").toString());
101
return QAbstractTableModel::headerData(section, orientation, role);
104
SelectBoardWindow::SelectBoardWindow(QWidget *parent)
108
setCaption(i18n("Level Selection"));
109
setMinimumSize(600, 350);
111
ui = new Ui::LevelSelectUi;
112
ui->setupUi(mainWidget());
114
m_levels = LevelLoader::load();
115
m_model = QSharedPointer<LevelTableModel>(new LevelTableModel(m_levels));
116
ui->listView->setModel(m_model.data());
118
if (m_levels.empty()) {
119
button(KDialog::Ok)->setEnabled(false);
121
QModelIndex index = m_model->index(0, 0);
122
ui->listView->selectionModel()->select(index, QItemSelectionModel::Select);
123
connect(ui->listView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(selectedLevelChanged(QModelIndex,QModelIndex)));
124
updateDetails(m_levels[0]);
128
void SelectBoardWindow::showEvent(QShowEvent *event) {
129
updateDetails(selectedBoard());
130
KDialog::showEvent(event);
133
void SelectBoardWindow::resizeEvent(QResizeEvent *event) {
134
updateDetails(selectedBoard());
135
KDialog::resizeEvent(event);
138
SelectBoardWindow::~SelectBoardWindow() {
142
void SelectBoardWindow::selectedLevelChanged(const QModelIndex ¤t, const QModelIndex &previous) {
144
updateDetails(m_levels[current.row()]);
147
QString SelectBoardWindow::diffString(int difficulty) const {
148
if (difficulty < 0) {
149
return diffString(0);
150
} else if (difficulty > 7) {
151
return diffString(7);
154
switch (difficulty) {
155
case 0: return ki18n("Ridiculously Easy").toString();
156
case 1: return ki18n("Very Easy").toString();
157
case 2: return ki18n("Easy").toString();
158
case 3: return ki18n("Medium").toString();
159
case 4: return ki18n("Hard").toString();
160
case 5: return ki18n("Very Hard").toString();
161
case 6: return ki18n("Extremely Hard").toString();
162
case 7: return ki18n("Impossible").toString();
163
default: throw OutOfBoundsException();
167
void SelectBoardWindow::updateDetails(QSharedPointer<Level> level) {
168
ui->labelName->setText(i18n("Name: %1", level->visibleName()));
169
ui->labelAuthor->setText(i18n("Author: %1", level->author()));
170
ui->labelSize->setText(i18n("Size: %1x%2", level->width(), level->height()));
171
ui->labelDifficulty->setText(i18n("Difficulty: %1", diffString(level->difficulty())));
172
if (level->solved()) {
173
ui->labelSolved->setText(i18nc("board solve time", "Solved: %1",
174
Time(level->solvedTime()).toString()));
175
QPixmap scaled = level->preview().scaled(ui->labelImage->size(),
177
Qt::FastTransformation);
178
ui->labelImage->setPixmap(scaled);
180
ui->labelSolved->setText(i18nc("board not solved yet", "Solved: -"));
181
ui->labelImage->setText("?");
185
QSharedPointer<Level> SelectBoardWindow::selectedBoard() const {
186
int index = ui->listView->selectionModel()->selectedIndexes().at(0).row();
187
return m_levels[index];