-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreeModel.cpp
More file actions
245 lines (199 loc) · 6.25 KB
/
Copy pathTreeModel.cpp
File metadata and controls
245 lines (199 loc) · 6.25 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "TreeModel.h"
#include "TreeNode.h"
#include <QDataStream>
#include <QMimeData>
#include <QByteArray>
#include <QStack>
#include <algorithm>
using namespace std;
namespace {
static constexpr char mimeType[] = "MyNode";
struct MovableChild{
TreeNode::ChildPtr ptr;
QModelIndex parentIndex;
};
QList<MovableChild> convertIndexesToMovableChildren(const QModelIndexList &indexes){
QList<MovableChild> result;
result.reserve(indexes.count());
for (const QModelIndex &index: indexes){
if (!index.isValid()){
result.append({nullptr, {}});
continue;
}
TreeNode *child = static_cast<TreeNode *>(index.internalPointer());
result.append({child->shared_from_this(), index.parent()});
}
return result;
}
}
TreeModel::TreeModel(QObject *parent)
: QAbstractItemModel(parent)
, rootNode_{std::make_shared<TreeNode>("ROOT_NODE")}
{
fillTreeWithData();
}
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
{
TreeNode *parentNode = parent.isValid() ?
static_cast<TreeNode *>(parent.internalPointer()) :
rootNode_.get();
return createIndex(row, column, parentNode->child(row).get());
}
QModelIndex TreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid()){
return {};
}
TreeNode *node = static_cast<TreeNode *>(index.internalPointer());
TreeNode::ParentPtr parentNode = node->parent();
if (parentNode.expired()){
return {};
}
std::shared_ptr<TreeNode> lockedParentNode = parentNode.lock();
return createIndex(lockedParentNode->row(), 0, lockedParentNode.get());
}
int TreeModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid()){
return rootNode_->childrenCount();
}
TreeNode *parentNode = static_cast<TreeNode *>(parent.internalPointer());
return parentNode->childrenCount();
}
int TreeModel::columnCount(const QModelIndex &/*parent*/) const
{
return 1;
}
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() ||
role != Qt::DisplayRole) {
return QVariant();
}
return static_cast<TreeNode *>(index.internalPointer())->name;
}
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid()){
return {};
}
Qt::ItemFlags result = Qt::ItemIsEnabled |
Qt::ItemIsSelectable;
if (!index.parent().isValid()){ // Group nodes cann accept drop
result |= Qt::ItemIsDropEnabled;
}
else{ //leaves can be draged
result |= Qt::ItemIsDragEnabled;
}
return result;
}
Qt::DropActions TreeModel::supportedDropActions() const
{
return Qt::MoveAction;
}
QStringList TreeModel::mimeTypes() const
{
return {mimeType};
}
QMimeData *TreeModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *result = new QMimeData();
result->setData(mimeType, saveIndexes(indexes));
return result;
}
bool TreeModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int /*column*/, const QModelIndex &parent) const
{
if (!parent.isValid() || // root is not drop enabled
action != Qt::MoveAction ||
!data->hasFormat(mimeType) ||
row < 0 ||
row > rowCount(parent)){
return false;
}
return true;
}
bool TreeModel::dropMimeData(const QMimeData *data, Qt::DropAction /*action*/, int row, int /*column*/, const QModelIndex &parent)
{
QModelIndexList indexes = restoreIndexes(data->data(mimeType));
if (indexes.isEmpty()){
return false;
}
sortIndexes(indexes);
const QList<MovableChild> &childrenToMove = convertIndexesToMovableChildren(indexes);
TreeNode *parentNode = static_cast<TreeNode *>(parent.internalPointer());
for (const MovableChild &movableChild: childrenToMove){
const int srcRow = movableChild.ptr->row();
const bool interParentMove = movableChild.parentIndex == parent;
const bool incrementRow = !(interParentMove && srcRow < row);
beginMoveRows(movableChild.parentIndex, srcRow, srcRow, parent, row);
parentNode->insertChild(movableChild.ptr, row);
endMoveRows();
row += incrementRow;
}
return true;
}
void TreeModel::fillTreeWithData()
{
const std::vector<TreeNode::ChildPtr> groups = {
make_shared<TreeNode>("Group 0"),
make_shared<TreeNode>("Group 1"),
make_shared<TreeNode>("Group 2")
};
for (TreeNode::ChildPtr group: groups){
rootNode_->insertChild(group);
for(int i = 0; i < 5; ++i){
group->insertChild(
make_shared<TreeNode>(QStringLiteral("Item %1 of %2").arg(i).arg(group->name))
);
}
}
}
QByteArray TreeModel::saveIndexes(const QModelIndexList &indexes)
{
QByteArray result;
QDataStream stream(&result, QIODevice::WriteOnly);
for (const QModelIndex &index: indexes){
QModelIndex localIndex = index;
QStack<int> indexParentStack;
while (localIndex.isValid()){
indexParentStack << localIndex.row();
localIndex = localIndex.parent();
}
stream << indexParentStack.size();
while (!indexParentStack.isEmpty()){
stream << indexParentStack.pop();
}
}
return result;
}
QModelIndexList TreeModel::restoreIndexes(QByteArray data)
{
QModelIndexList result;
QDataStream stream(&data, QIODevice::ReadOnly);
while(!stream.atEnd()){
int childDepth = 0;
stream >> childDepth;
QModelIndex currentIndex = {};
for (int i = 0; i < childDepth; ++i){
int row = 0;
stream >> row;
currentIndex = index(row, 0, currentIndex);
}
result << currentIndex;
}
return result;
}
void TreeModel::sortIndexes(QModelIndexList &indexes)
{
std::sort(indexes.begin(), indexes.end(), [](const QModelIndex &left, const QModelIndex &right){
if (left.parent() < right.parent()){
return true;
}
else if (right.parent() < left.parent()){
return false;
}
else{
return left < right;
}
});
}