-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHierarchyModel.cpp
337 lines (285 loc) · 10.3 KB
/
HierarchyModel.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "HierarchyModel.h"
#include "HierarchyObject.h"
#include "widget.h"
#include "qapplication.h"
#include "qmimedata.h"
HierarchyModel::HierarchyModel()
{
root = new HierarchyObject("root");
}
HierarchyModel::~HierarchyModel()
{
}
HierarchyObject * HierarchyModel::createObject(const QString& name)
{
Q_ASSERT(widget); // 此时必须已经添加到widget上
// 默认挂到root上
beginInsertRows(QModelIndex(), root->childrenCount(), root->childrenCount() + 1);
HierarchyObject * res = new HierarchyObject(name);
//res->widget = widget;
//res->hierarchy = this;
objects.push_back(res);
root->insertChild(root->childrenCount(), res);
endInsertRows();
return res;
}
void HierarchyModel::moveObject(HierarchyObject* obj, HierarchyObject* toParent, int toIndex) {
Q_ASSERT(obj->getParent() && toParent);
Q_ASSERT(toIndex >= 0 && toIndex <= toParent->childrenCount());
if (obj->getParent() == toParent) {
// 同级移动
//assert(toIndex < toParent->childrenCount());
int fromIndex = toParent->findChild(obj);
int realToIndex = (fromIndex < toIndex) ? (toIndex - 1) : toIndex; // 目标在后,向前挪一个
if (fromIndex == realToIndex) {
// 不用移
return;
}
//QModelIndex changeBegin = obj2index(obj), changeEnd = obj2index(toParent->getChildren(toIndex));
// https://bugreports.qt.io/browse/QTBUG-6940
QModelIndex parentIndex = obj2index(toParent);
beginMoveRows(parentIndex, fromIndex, fromIndex, parentIndex, toIndex);
toParent->moveChild(fromIndex, realToIndex);
endMoveRows();
//emit(dataChanged(changeBegin, changeEnd, { Qt::EditRole }));
}
else {
// 移动到别的子树上
// 先移除,再添加
QModelIndex originalParentIndex = obj2index(obj->getParent());
int objIndex = obj->getParent()->findChild(obj);
beginRemoveRows(originalParentIndex, objIndex, objIndex);
obj->getParent()->popChild(objIndex);
endRemoveRows();
// 添加
QModelIndex toParentIndex = obj2index(toParent);
beginInsertRows(toParentIndex, toIndex, toIndex);
toParent->insertChild(toIndex, obj);
endInsertRows();
}
}
void HierarchyModel::removeObject(HierarchyObject* obj) {
QModelIndex originalParentIndex = obj2index(obj->getParent());
int objIndex = obj->getParent()->findChild(obj);
beginRemoveRows(originalParentIndex, objIndex, objIndex);
obj->getParent()->popChild(objIndex);
endRemoveRows();
}
HierarchyObject* HierarchyModel::index2obj(const QModelIndex & index) const
{
HierarchyObject *res;
if (!index.isValid()) {
res = root;
}
else {
res = static_cast<HierarchyObject*>(index.internalPointer());
}
return res;
}
QModelIndex HierarchyModel::obj2index(HierarchyObject* obj) const
{
assert(obj);
if (obj == root) return QModelIndex();
assert(obj->getParent());
return createIndex(obj->getParent()->findChild(obj), 0, obj);
}
void HierarchyModel::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
QModelIndexList selectedIndices = selected.indexes();
QModelIndexList deselectedIndices = deselected.indexes();
//// 取消所有高亮
//root->callRecursively([](HierarchyObject* o) -> void {
// Renderer* renderer = o->getComponent<Renderer>();
// if (renderer != NULL) {
// renderer->highlight = false;
// }
//});
for (auto& index : deselectedIndices) {
HierarchyObject* obj = index2obj(index);
assert(obj);
obj->callRecursively([](HierarchyObject* o) -> void {
Renderer* renderer = o->getComponent<Renderer>();
if (renderer != NULL) {
renderer->highlight = false;
}
});
if (obj == lastSelected) lastSelected = NULL;
}
// 重新高亮
for (auto& index : selectedIndices) {
HierarchyObject* obj = index2obj(index);
assert(obj);
obj->callRecursively([](HierarchyObject* o) -> void {
Renderer* renderer = o->getComponent<Renderer>();
if (renderer != NULL) {
renderer->highlight = true;
}
});
lastSelected = obj;
}
}
// ---------abstract item model--------------------
QModelIndex HierarchyModel::index(int row, int column, const QModelIndex & parent) const
{
HierarchyObject *parentItem = index2obj(parent);
HierarchyObject *childItem = parentItem->getChildren(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex HierarchyModel::parent(const QModelIndex & index) const
{
if (!index.isValid()) //传入节点是根节点
return QModelIndex();
HierarchyObject *childItem = static_cast<HierarchyObject*>(index.internalPointer());
HierarchyObject *parentItem = childItem->getParent();
if (parentItem == root)
return QModelIndex();
return createIndex(parentItem->getParent()->findChild(parentItem), 0, parentItem);
}
int HierarchyModel::rowCount(const QModelIndex & parent) const
{
HierarchyObject * parentItem;
if (!parent.isValid())
parentItem = root;
else
parentItem = static_cast<HierarchyObject*>(parent.internalPointer());
return parentItem->childrenCount();
}
int HierarchyModel::columnCount(const QModelIndex & parent) const
{
return 1;
}
QVariant HierarchyModel::data(const QModelIndex & index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole && role != Qt::EditRole)
return QVariant();
HierarchyObject *item = static_cast<HierarchyObject*>(index.internalPointer());
assert(item);
return item->name;
}
bool HierarchyModel::setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) {
//qDebug() << "setData: " << value << "|" << role;
HierarchyObject * item;
if (!index.isValid()) return false;
else {
item = static_cast<HierarchyObject*>(index.internalPointer());
item->name = value.toString();
emit(dataChanged(index, index, { role }));
return true;
}
}
Qt::ItemFlags HierarchyModel::flags(const QModelIndex & index) const
{
return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
}
static const char s_treeNodeMimeType[] = "application/x-treenode";
bool HierarchyModel::canDropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) const
{
if (!data->hasFormat(s_treeNodeMimeType)) {
return false;
}
//std::vector<HierarchyObject*> droppedNodes;
//// 获取失败,不是同一个进程
//if (!retrieveDroppedObjects(data, droppedNodes)) {
// return false;
//}
//// 拖放目标
//HierarchyObject *parentNode = index2obj(parent);
//Q_ASSERT(parentNode);
// 经测试已经自带了判断是不是拖到自己的子树的功能
// 暂时不用加别的检测
return true;
}
QMimeData * HierarchyModel::mimeData(const QModelIndexList & indexes) const
{
QMimeData *mimeData = new QMimeData;
QByteArray data; //a kind of RAW format for datas
QDataStream stream(&data, QIODevice::WriteOnly);
QList<HierarchyObject *> nodes;
// 取出所有选中项的指针
foreach(const QModelIndex &index, indexes) {
HierarchyObject *node = index2obj(index);
if (!nodes.contains(node))
nodes << node;
}
// 写入进程pid
stream << QCoreApplication::applicationPid();
// 写入选中项数量
stream << nodes.count();
// 写入选中项指针
foreach(HierarchyObject *node, nodes) {
stream << reinterpret_cast<qlonglong>(node);
}
mimeData->setData(s_treeNodeMimeType, data);
return mimeData;
}
bool HierarchyModel::retrieveDroppedObjects(const QMimeData * data, std::vector<HierarchyObject*>& out_objects) const {
QByteArray bytes = data->data(s_treeNodeMimeType);
QDataStream stream(&bytes, QIODevice::ReadOnly);// 准备读取
qint64 senderPid;
stream >> senderPid; // 读出进程pid
if (senderPid != QCoreApplication::applicationPid()) {
// Let's not cast pointers that come from another process...
return false;
}
int count;
stream >> count; // 读出拖入项数量
// 以longlong读出指针并转换回来
for (int i = 0; i < count; ++i) {
// Decode data from the QMimeData
qlonglong nodePtr;
stream >> nodePtr;
HierarchyObject* objPoiner = reinterpret_cast<HierarchyObject *>(nodePtr);
assert(objPoiner);
out_objects.push_back(objPoiner);
}
return true;
}
// https://forum.qt.io/topic/76708/full-drag-and-drop-support-in-qtreeview/3
bool HierarchyModel::dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
{
Q_ASSERT(action == Qt::MoveAction);
Q_UNUSED(column);
//test if the data type is the good one
if (!data->hasFormat(s_treeNodeMimeType)) {
return false;
}
std::vector<HierarchyObject*> droppedNodes;
// 获取失败,不是同一个进程
if (!retrieveDroppedObjects(data, droppedNodes)) {
return false;
}
// 拖放目标
HierarchyObject *parentNode = index2obj(parent);
Q_ASSERT(parentNode);
// 处理行数的特殊情形
if (row == -1) {
// valid index means: drop onto item. -> insert at begin
if (parent.isValid()) row = 0;
// invalid index means: append at bottom, after last toplevel
else row = rowCount(parent);
}
qDebug() << "parent: " << parentNode->name << " row" << row << " mime contents: " << droppedNodes.size();
for (auto objPoiner : droppedNodes) {
qDebug() << objPoiner->name;
}
for (auto& droppedNode : droppedNodes) {
moveObject(droppedNode, parentNode, row);
}
return true;
}
QStringList HierarchyModel::mimeTypes() const
{
return QStringList() << s_treeNodeMimeType;
}
Qt::DropActions HierarchyModel::supportedDropActions() const
{
return Qt::MoveAction;
}
QVariant HierarchyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
return QVariant(QString::fromUtf8("sdf啊 "));
}