-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreeNode.cpp
More file actions
85 lines (69 loc) · 1.59 KB
/
Copy pathTreeNode.cpp
File metadata and controls
85 lines (69 loc) · 1.59 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
#include "TreeNode.h"
#include <set>
TreeNode::TreeNode(const QString &name)
: name{name}
, parent_{}
{
}
TreeNode::ParentPtr TreeNode::parent() const
{
return parent_;
}
int TreeNode::row() const
{
if (parent_.expired()){
return -1;
}
return parent_.lock()->children_
.indexOf(std::const_pointer_cast<TreeNode>(shared_from_this()));
}
int TreeNode::childrenCount() const
{
return children_.count();
}
void TreeNode::insertChild(const TreeNode::ChildPtr &child, int position)
{
const ParentPtr lastParent = child->parent();
if (!lastParent.expired()){
const auto &lockedParent = lastParent.lock();
if (lockedParent.get() == this){
moveChild(child, position);
return;
}
lockedParent->removeChild(child);
}
child->setParent(shared_from_this());
if (position == -1){
children_ << child;
}
else{
children_.insert(position, child);
}
}
void TreeNode::removeChild(const TreeNode::ChildPtr &child)
{
const int pos = children_.indexOf(child);
if (pos != -1){
children_.takeAt(pos)->setParent({});
}
}
TreeNode::ChildPtr TreeNode::child(int row) const
{
return children_.at(row);
}
void TreeNode::setParent(const TreeNode::ParentPtr &parent)
{
parent_ = parent;
}
void TreeNode::moveChild(const TreeNode::ChildPtr &child, int newPosition)
{
int from = child->row();
if (from == newPosition){
return;
}
if (from < newPosition)
{
--newPosition;
}
children_.insert(newPosition, children_.takeAt(from));
}