-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoardModel.cpp
More file actions
81 lines (71 loc) · 2.22 KB
/
BoardModel.cpp
File metadata and controls
81 lines (71 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "BoardModel.h"
#include "Types.h"
BoardModel::BoardModel(QObject* parent): QAbstractListModel(parent) {}
int BoardModel::rowCount(const QModelIndex& parent) const {
if (parent.isValid()) {
return 0;
}
return static_cast<int>(tiles.size());
}
QHash<int, QByteArray> BoardModel::roleNames() const {
static QHash<int, QByteArray> mapping{
{BoardIndexRole, "index"},
{BoardValueRole, "value"},
{BoardToDeleteRole, "toDelete"}};
return mapping;
}
QVariant BoardModel::data(const QModelIndex& index, int role) const {
if (!index.isValid() || index.row() >= this->tiles.size()) {
return {};
}
const Tile& tile = this->tiles[index.row()];
switch (role) {
case BoardIndexRole:
return static_cast<int>(tile.index);
case BoardValueRole:
return static_cast<int>(tile.value);
case BoardToDeleteRole:
return tile.toDelete;
default:
return {};
}
}
void BoardModel::append(const Tile& tile) {
int size = static_cast<int>(this->tiles.size());
this->beginInsertRows(QModelIndex(), size, size);
this->tiles.append(tile);
this->endInsertRows();
}
void BoardModel::remove(uint8_t index) {
const auto& tileIndex = this->getIndexInList(index, true);
this->beginRemoveRows(QModelIndex(), tileIndex, tileIndex);
this->tiles.removeAt(tileIndex);
this->endRemoveRows();
}
void BoardModel::startRemove(uint8_t index) {
const auto& tileIndex = this->getIndexInList(index);
this->tiles[tileIndex].toDelete = true;
QModelIndex qIndex = this->index(tileIndex);
emit this->dataChanged(qIndex, qIndex);
}
void BoardModel::edit(uint8_t oldIndex, uint8_t newIndex, bool valueIncrement) {
const auto& tileIndex = this->getIndexInList(oldIndex);
if (valueIncrement) {
this->tiles[tileIndex].value++;
}
this->tiles[tileIndex].index = newIndex;
QModelIndex qIndex = this->index(tileIndex);
emit this->dataChanged(qIndex, qIndex);
}
int BoardModel::getIndexInList(uint8_t tileIndex, bool toDelete) const {
return static_cast<int>(
std::find_if(this->tiles.begin(), this->tiles.end(), [&tileIndex, &toDelete](const Tile& tile) {
return tile.index == tileIndex && tile.toDelete == toDelete;
}) - this->tiles.begin()
);
}
void BoardModel::reset() {
this->beginResetModel();
this->tiles.clear();
this->endResetModel();
}