1 /*
2 * Copyright (C) 2015 Ivan Romanov <drizt@land.ru>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 */
19
20 #include "abstracttreeitem.h"
21
22 #include <QDebug>
23
AbstractTreeItem(AbstractTreeItem * parent)24 AbstractTreeItem::AbstractTreeItem(AbstractTreeItem *parent)
25 : _parent(nullptr)
26 , _children(AbstractTreeItemList())
27 {
28 setParent(parent);
29 }
30
~AbstractTreeItem()31 AbstractTreeItem::~AbstractTreeItem()
32 {
33 qDeleteAll(_children);
34 if (_parent)
35 _parent->_children.removeOne(this);
36 }
37
setRow(int row)38 void AbstractTreeItem::setRow(int row)
39 {
40 Q_ASSERT(_parent);
41 if (!_parent)
42 return;
43
44 _parent->_children.move(this->row(), row);
45 }
46
row() const47 int AbstractTreeItem::row() const
48 {
49 return _parent ? _parent->_children.indexOf(const_cast<AbstractTreeItem*>(this)) : 0;
50 }
51
setParent(AbstractTreeItem * newParent)52 void AbstractTreeItem::setParent(AbstractTreeItem *newParent)
53 {
54 if (_parent)
55 _parent->_children.removeOne(this);
56
57 if (newParent)
58 newParent->_children.append(this);
59
60 _parent = newParent;
61 }
62
parent() const63 AbstractTreeItem *AbstractTreeItem::parent() const
64 {
65 return _parent;
66 }
67
insertChild(int row,AbstractTreeItem * child)68 void AbstractTreeItem::insertChild(int row, AbstractTreeItem *child)
69 {
70 Q_ASSERT(child);
71 Q_ASSERT(!child->_parent);
72
73 if (child->_parent)
74 child->_parent->removeChild(child);
75
76 child->_parent = this;
77 _children.insert(row, child);
78 }
79
appendChild(AbstractTreeItem * child)80 void AbstractTreeItem::appendChild(AbstractTreeItem *child)
81 {
82 Q_ASSERT(child);
83 Q_ASSERT(!child->_parent);
84
85 if (child->_parent)
86 child->_parent->removeChild(child);
87
88 child->_parent = this;
89 _children.append(child);
90 }
91
removeChild(AbstractTreeItem * child)92 void AbstractTreeItem::removeChild(AbstractTreeItem *child)
93 {
94 Q_ASSERT(child);
95 Q_ASSERT(_children.contains(child));
96
97 _children.removeOne(child);
98 }
99
child(int row) const100 AbstractTreeItem *AbstractTreeItem::child(int row) const
101 {
102 Q_ASSERT(row < childCount());
103 if (row < childCount())
104 return _children.at(row);
105 else
106 return 0;
107 }
108
childCount() const109 int AbstractTreeItem::childCount() const
110 {
111 return _children.size();
112 }
113
children() const114 AbstractTreeItemList AbstractTreeItem::children() const
115 {
116 return _children;
117 }
118
dump(int indent) const119 void AbstractTreeItem::dump(int indent) const
120 {
121 QString fill(indent, QLatin1Char(' '));
122 qDebug() << qPrintable(fill + QLatin1String("{"));
123 qDebug() << qPrintable(fill + QLatin1String(" ") + toString());
124 qDebug() << qPrintable(fill + QLatin1String(" ") + QLatin1String("this")) << this;
125 qDebug() << qPrintable(fill + QLatin1String(" ") + QLatin1String("parent")) << _parent;
126 qDebug() << qPrintable(fill + QLatin1String(" ") + QLatin1String("children")) << _children;
127
128 foreach (AbstractTreeItem *item, _children) {
129 item->dump(indent + 1);
130 }
131 qDebug() << qPrintable(fill + QLatin1String("}"));
132 }
133