~ubuntu-branches/ubuntu/utopic/picmi/utopic

« back to all changes in this revision

Viewing changes to src/gui/selectboardwindow.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-12-03 17:39:47 UTC
  • Revision ID: package-import@ubuntu.com-20121203173947-tt1kk5wp92zk1f2z
Tags: upstream-4.9.90
ImportĀ upstreamĀ versionĀ 4.9.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* *************************************************************************
 
2
 *  Copyright 2012 Jakob Gruber <jakob.gruber@gmail.com>                   *
 
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, see <http://www.gnu.org/licenses/>.  *
 
16
 ************************************************************************* */
 
17
 
 
18
#include "selectboardwindow.h"
 
19
 
 
20
#include <QAbstractTableModel>
 
21
#include <assert.h>
 
22
#include <klocale.h>
 
23
#include <kpushbutton.h>
 
24
 
 
25
#include "src/logic/elapsedtime.h"
 
26
#include "src/logic/levelloader.h"
 
27
 
 
28
class LevelTableModel : public QAbstractTableModel
 
29
{
 
30
public:
 
31
    LevelTableModel(const QList<QSharedPointer<Level> > &levels, QObject * parent = 0);
 
32
 
 
33
protected:
 
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;
 
38
 
 
39
private:
 
40
    const QList<QSharedPointer<Level> > &m_levels;
 
41
 
 
42
    enum Columns {
 
43
        Name,
 
44
        LevelSet,
 
45
        Difficulty,
 
46
        Size,
 
47
        Solved,
 
48
        ColumnCount /* not a real column */
 
49
    };
 
50
};
 
51
 
 
52
LevelTableModel::LevelTableModel(const QList<QSharedPointer<Level> > &levels, QObject *parent) :
 
53
    QAbstractTableModel(parent), m_levels(levels)
 
54
{
 
55
 
 
56
}
 
57
 
 
58
int LevelTableModel::rowCount(const QModelIndex &parent) const {
 
59
    Q_UNUSED(parent);
 
60
    return m_levels.size();
 
61
}
 
62
 
 
63
int LevelTableModel::columnCount(const QModelIndex &parent) const {
 
64
    Q_UNUSED(parent);
 
65
    return ColumnCount;
 
66
}
 
67
 
 
68
QVariant LevelTableModel::data(const QModelIndex &index, int role) const {
 
69
    if (!index.isValid() || (role != Qt::DisplayRole && role != Qt::TextAlignmentRole)) {
 
70
             return QVariant();
 
71
    }
 
72
 
 
73
    if (role == Qt::TextAlignmentRole) {
 
74
        return Qt::AlignCenter;
 
75
    }
 
76
 
 
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";
 
84
    default: assert(0);
 
85
    }
 
86
 
 
87
    return QVariant();
 
88
}
 
89
 
 
90
QVariant LevelTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
 
91
    if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
 
92
        switch (section) {
 
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());
 
98
        default: assert(0);
 
99
        }
 
100
    }
 
101
    return QAbstractTableModel::headerData(section, orientation, role);
 
102
}
 
103
 
 
104
SelectBoardWindow::SelectBoardWindow(QWidget *parent)
 
105
    : KDialog(parent)
 
106
{
 
107
    setModal(true);
 
108
    setCaption(i18n("Level Selection"));
 
109
    setMinimumSize(600, 350);
 
110
 
 
111
    ui = new Ui::LevelSelectUi;
 
112
    ui->setupUi(mainWidget());
 
113
 
 
114
    m_levels = LevelLoader::load();
 
115
    m_model = QSharedPointer<LevelTableModel>(new LevelTableModel(m_levels));
 
116
    ui->listView->setModel(m_model.data());
 
117
 
 
118
    if (m_levels.empty()) {
 
119
        button(KDialog::Ok)->setEnabled(false);
 
120
    } else {
 
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]);
 
125
    }
 
126
}
 
127
 
 
128
void SelectBoardWindow::showEvent(QShowEvent *event) {
 
129
    updateDetails(selectedBoard());
 
130
    KDialog::showEvent(event);
 
131
}
 
132
 
 
133
void SelectBoardWindow::resizeEvent(QResizeEvent *event) {
 
134
    updateDetails(selectedBoard());
 
135
    KDialog::resizeEvent(event);
 
136
}
 
137
 
 
138
SelectBoardWindow::~SelectBoardWindow() {
 
139
    delete ui;
 
140
}
 
141
 
 
142
void SelectBoardWindow::selectedLevelChanged(const QModelIndex &current, const QModelIndex &previous) {
 
143
    Q_UNUSED(previous);
 
144
    updateDetails(m_levels[current.row()]);
 
145
}
 
146
 
 
147
QString SelectBoardWindow::diffString(int difficulty) const {
 
148
    if (difficulty < 0) {
 
149
        return diffString(0);
 
150
    } else if (difficulty > 7) {
 
151
        return diffString(7);
 
152
    }
 
153
 
 
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();
 
164
    }
 
165
}
 
166
 
 
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(),
 
176
                                                 Qt::KeepAspectRatio,
 
177
                                                 Qt::FastTransformation);
 
178
        ui->labelImage->setPixmap(scaled);
 
179
    } else {
 
180
        ui->labelSolved->setText(i18nc("board not solved yet", "Solved: -"));
 
181
        ui->labelImage->setText("?");
 
182
    }
 
183
}
 
184
 
 
185
QSharedPointer<Level> SelectBoardWindow::selectedBoard() const {
 
186
    int index = ui->listView->selectionModel()->selectedIndexes().at(0).row();
 
187
    return m_levels[index];
 
188
}