28 #include "treenodevisitor.h"
30 #include <tdeapplication.h>
34 #include <tqvaluelist.h>
38 class NodeList::NodeListPrivate
41 TQValueList<TreeNode*> flatList;
44 TQMap<int, TreeNode*> idMap;
45 AddNodeVisitor* addNodeVisitor;
46 RemoveNodeVisitor* removeNodeVisitor;
50 class NodeList::AddNodeVisitor :
public TreeNodeVisitor
53 AddNodeVisitor(NodeList* list) : m_list(list) {}
56 virtual bool visitTreeNode(TreeNode* node)
59 node->setId(m_list->generateID());
60 m_list->d->idMap[node->id()] = node;
61 m_list->d->flatList.append(node);
63 connect(node, TQ_SIGNAL(signalDestroyed(TreeNode*)), m_list, TQ_SLOT(slotNodeDestroyed(TreeNode*) ));
64 m_list->signalNodeAdded(node);
68 virtual bool visitFolder(Folder* node)
70 connect(node, TQ_SIGNAL(signalChildAdded(TreeNode*)), m_list, TQ_SLOT(slotNodeAdded(TreeNode*) ));
71 connect(node, TQ_SIGNAL(signalChildRemoved(Folder*, TreeNode*)), m_list, TQ_SLOT(slotNodeRemoved(Folder*, TreeNode*) ));
75 for (TreeNode* i = node->firstChild(); i && i != node; i = i->next() )
76 m_list->slotNodeAdded(i);
81 virtual void visit(TreeNode* node,
bool preserveID)
83 m_preserveID = preserveID;
84 TreeNodeVisitor::visit(node);
92 class NodeList::RemoveNodeVisitor :
public TreeNodeVisitor
95 RemoveNodeVisitor(NodeList* list) : m_list(list) {}
97 virtual bool visitTreeNode(TreeNode* node)
99 m_list->d->idMap.remove(node->id());
100 m_list->d->flatList.remove(node);
102 disconnect(node, TQ_SIGNAL(signalDestroyed(TreeNode*)), m_list, TQ_SLOT(slotNodeDestroyed(TreeNode*) ));
103 m_list->signalNodeRemoved(node);
108 virtual bool visitFolder(Folder* node)
111 disconnect(node, TQ_SIGNAL(signalChildAdded(TreeNode*)), m_list, TQ_SLOT(slotNodeAdded(TreeNode*) ));
112 disconnect(node, TQ_SIGNAL(signalChildRemoved(Folder*, TreeNode*)), m_list, TQ_SLOT(slotNodeRemoved(Folder*, TreeNode*) ));
121 NodeList::NodeList(TQObject *parent,
const char *name) : d(new NodeListPrivate)
124 d->addNodeVisitor =
new AddNodeVisitor(
this);
125 d->removeNodeVisitor =
new RemoveNodeVisitor(
this);
129 const TQString& NodeList::title()
const
134 TreeNode* NodeList::findByID(
int id)
const
139 void NodeList::setTitle(
const TQString& title)
144 Folder* NodeList::rootNode()
const
149 const TQValueList<TreeNode*>& NodeList::asFlatList()
const
154 bool NodeList::isEmpty()
const
156 return d->rootNode->firstChild() == 0;
159 TQValueList<TreeNode*>* NodeList::flatList()
const
161 return &(d->flatList);
164 void NodeList::clear()
166 Q_ASSERT(rootNode());
168 TQValueList<TreeNode*> children = rootNode()->children();
170 for (TQValueList<TreeNode*>::ConstIterator it = children.begin(); it != children.end(); ++it)
174 TQMap<int, TreeNode*>* NodeList::idMap()
const
179 void NodeList::setRootNode(Folder* folder)
182 d->rootNode = folder;
186 d->rootNode->setOpen(
true);
187 connect(d->rootNode, TQ_SIGNAL(signalChildAdded(TreeNode*)),
this, TQ_SLOT(slotNodeAdded(TreeNode*)));
188 connect(d->rootNode, TQ_SIGNAL(signalChildRemoved(Folder*, TreeNode*)),
this, TQ_SLOT(slotNodeRemoved(Folder*, TreeNode*)));
192 void NodeList::addNode(TreeNode* node,
bool preserveID)
194 d->addNodeVisitor->visit(node, preserveID);
197 void NodeList::removeNode(TreeNode* node)
199 d->removeNodeVisitor->visit(node);
202 NodeList::~NodeList()
204 emit signalDestroyed(
this);
205 delete d->addNodeVisitor;
206 delete d->removeNodeVisitor;
211 int NodeList::generateID()
213 return TDEApplication::random();
216 void NodeList::slotNodeAdded(TreeNode* node)
218 Folder* parent = node->parent();
219 if ( !node || !d->flatList.contains(parent) || d->flatList.contains(node) )
222 addNode(node,
false);
225 void NodeList::slotNodeDestroyed(TreeNode* node)
227 if ( !node || !d->flatList.contains(node) )
233 void NodeList::slotNodeRemoved(Folder* , TreeNode* node)
235 if ( !node || !d->flatList.contains(node) )
243 #include "nodelist.moc"