-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtreemodel.cpp
185 lines (167 loc) · 5.4 KB
/
treemodel.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "treemodel.hpp"
#include "node.hpp"
#include "io.hpp"
TreeModel::TreeModel(NodePtr root_,QObject* const parent) :
QAbstractItemModel(parent),
root(std::move(root_))
{
}
void TreeModel::reset() const
{
rawToIndices.clear();
}
std::vector<QPersistentModelIndex> TreeModel::indices(const NodePtr& node) const
{
if (node==nullptr)
return std::vector<QPersistentModelIndex>(1);
const auto& pair=rawToIndices.find(node.get());
if (pair==rawToIndices.end()) {
const auto childIndex=node->childIndex();
const auto numColumns=columnCount(node);
std::vector<QPersistentModelIndex> indices;
indices.reserve(numColumns);
for (int column=0;column<numColumns;++column)
indices.emplace_back(createIndex(childIndex,column,node));
return indices;
}
else
return pair->second;
}
QPersistentModelIndex TreeModel::index(const NodePtr& node,const int column) const
{
if (node==nullptr)
return QPersistentModelIndex();
const auto& pair=rawToIndices.find(node.get());
if (pair!=rawToIndices.end() && column<int(pair->second.size())) {
const auto result=pair->second[column];
if (result.isValid())
return result;
}
return createIndex(node->childIndex(),column,node);
}
QPersistentModelIndex TreeModel::lastIndex(const NodePtr& node) const
{
return index(node,columnCount(node)-1);
}
NodePtr TreeModel::getItem(const QModelIndex& index) const
{
if (index.isValid())
if (const auto item=rawToWeak[static_cast<Node*>(index.internalPointer())].lock())
return item;
return root;
}
inline QModelIndex TreeModel::createIndex(const int row,const int column,const NodePtr& node) const
{
rawToWeak.emplace(node.get(),std::weak_ptr<Node>(node));
const auto index=QAbstractItemModel::createIndex(row,column,node.get());
safeAt(rawToIndices[node.get()],column)=index;
return index;
}
int TreeModel::columnCount(const NodePtr& node) const
{
if (node->previousNode==nullptr)
return 1;
else if (node->move.empty())
return numStartingPieces+1;
else
return moveColumnCount(node->move.size());
}
int TreeModel::moveColumnCount(const int numMoves) const
{
return numMoves==MAX_STEPS_PER_MOVE ? MAX_STEPS_PER_MOVE+1 : numMoves+2;
}
bool TreeModel::hasChildren(const QModelIndex& parent) const
{
const auto parentItem=getItem(parent);
return parentItem!=nullptr && parentItem->hasChild();
}
int TreeModel::rowCount(const QModelIndex& parent) const
{
const auto parentItem=getItem(parent);
return parentItem==nullptr ? 0 : parentItem->numChildren();
}
int TreeModel::columnCount(const QModelIndex& index) const
{
const auto item=getItem(index);
if (item==nullptr || !item->hasChild())
return 0;
else if (item->inSetup())
return numStartingPieces+1;
else
return moveColumnCount(index.parent().isValid() ? item->maxChildSteps() : item->maxDescendantSteps());
}
QModelIndex TreeModel::index(const int row,const int column,const QModelIndex& parent) const
{
if (column>=0 && (!parent.isValid() || parent.column()==0)) {
const auto parentItem=getItem(parent);
if (parentItem!=nullptr)
if (const auto child=parentItem->child(row))
if (column<columnCount(child))
return createIndex(row,column,child);
}
return QModelIndex();
}
QModelIndex TreeModel::parent(const QModelIndex& index) const
{
if (index.isValid()) {
const auto item=getItem(index);
if (item!=nullptr) {
const auto parent=item->previousNode;
if (parent!=nullptr)
return createIndex(parent->childIndex(),0,parent);
}
}
return QModelIndex();
}
QVariant TreeModel::data(const QModelIndex& index,const int role) const
{
if (index.isValid()) {
const auto& node=getItem(index);
if (node==nullptr)
return QVariant();
if (role==Qt::DisplayRole) {
const unsigned int column=index.column();
if (column==0) {
const auto moveNumber=QString::fromStdString(node->toPlyString(*root));
const auto numChildren=rowCount(index);
switch (numChildren) {
case 0: return moveNumber;
case 1: return moveNumber+" *";
default: return moveNumber+" ("+QString::number(numChildren)+')';
}
}
const auto& move=node->move;
const unsigned int moveIndex=column-1;
if (move.empty()) {
const auto& placements=node->playedPlacements();
assert(placements.size()==numStartingPieces);
const auto begin=next(placements.cbegin(),moveIndex);
return QString::fromStdString(toString(begin,next(begin)));
}
else {
assert(moveIndex<MAX_STEPS_PER_MOVE);
QString step;
if (moveIndex<move.size()) {
step=QString::fromStdString(toString(move[moveIndex]));
if (moveIndex<MAX_STEPS_PER_MOVE-1)
return step;
}
else if (moveIndex==move.size())
step="pass";
const auto result=node->result;
if (node->result.endCondition!=NO_END)
step+=QString(' ')+(result.winner==node->gameState.sideToMove ? '-' : '+')+toupper(toChar(result.endCondition));
return step;
}
}
else if (role==Qt::BackgroundRole)
return node->cumulativeChildIndex()%2==0 ? QPalette().base() : QPalette().alternateBase();
}
return QVariant();
}
Qt::ItemFlags TreeModel::flags(const QModelIndex& index) const
{
if (!index.isValid() || (exclusive.isValid() && exclusive!=index))
return Qt::NoItemFlags;
return QAbstractItemModel::flags(index);
}