All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
Public Member Functions | Protected Member Functions
Qtilities::Core::TreeIterator Class Reference

An iterator which iterates through an Observer tree (thus also Qtilities::CoreGui::TreeNode). More...

#include <TreeIterator.h>

Inheritance diagram for Qtilities::Core::TreeIterator:
Inheritance graph
[legend]

List of all members.

Public Member Functions

QObject * current () const
 Gets the current item in the context iterated through.
QObject * first ()
 The first item in the context iterated through.
QObject * last ()
 The last item in the context iterated through.
QObject * next ()
 The next item in the context iterated through.
QObject * previous ()
 The previous item in the context iterated through.
void setCurrent (const QObject *current)
 Sets the current item in the context iterated through.
 TreeIterator (const Observer *top_node=0, int iterator_id=-1)
 TreeIterator Constructs a new iterator.
- Public Member Functions inherited from Qtilities::Core::Interfaces::IIterator< QObject >
virtual bool hasNext ()
 Indicates if a next item exists.
virtual bool hasPrevious ()
 Indicates if a previous item exists.
virtual QObject * operator++ ()
 Prefix increment.
virtual QObject * operator++ (int x)
 Postfix increment.
virtual QObject * operator-- ()
 Prefix decrement.
virtual QObject * operator-- (int x)
 Postfix decrement.

Protected Member Functions

const QObject * findParentNext (const QObject *obj)
 Finds the next parent of an object.
const ObserverfindParentNextObserver (const QObject *obj)
 Finds the next parent of an object and cast it to an observer.
const QObject * findParentPrevious (const QObject *obj)
 Finds the previous parent of an object.
const ObserverfindParentPreviousObserver (const QObject *obj)
 Finds the previous parent of an object and cast it to an observer.

Detailed Description

An iterator which iterates through an Observer tree (thus also Qtilities::CoreGui::TreeNode).

The TreeIterator allows you to easily iterate over the items in a tree. Take the tree shown below as an example:

trees_full_iteration.jpg
Tree Iteration Order

Lets build this example tree and iterate over it:

TreeNode* rootNode = new TreeNode("1");
TreeNode* nodeA = rootNode->addNode("2");
nodeA->addItem("3");
nodeA->addItem("4");
TreeNode* nodeB = rootNode->addNode("5");
nodeB->addItem("6");
nodeB->addItem("7");
TreeIterator itr(rootNode);
qDebug() << itr.current()->objectName();
while (itr.hasNext()) {
qDebug() << itr.next()->objectName();
}
// In this case the result would be:
// >> 1
// >> 2
// >> 3
// >> 4
// >> 5
// >> 6
// >> 7
// We can also iterate backwards:
qDebug() << itr.current()->objectName();
while (itr.hasPrevious()) {
qDebug() << itr.previous()->objectName();
}
// In this case the result would be:
// >> 7
// >> 6
// >> 5
// >> 4
// >> 3
// >> 2
// >> 1

When subjects are attached to multiple parents

Because Qtilities allows tree items to be attached to multiple trees, TreeIterator needs the ability to iterate through trees where subjects can appear more than once. Lets look at an example:

trees_multiple_parent_iteration.jpg
Advanced Tree Iteration

The above tree is constructed using the code snippet below:

TreeNode* rootTop = new TreeNode("Root Node");
// This is the first tree (A):
TreeNode* rootNodeA = new TreeNode("A1");
rootNodeA->addItem("A2");
TreeItem* shared_item = rootNodeA->addItem("A3"); // This is the shared item.
QObject* lastA = rootNodeA->addItem("A4");
// This is the second tree (B):
TreeNode* rootNodeB = new TreeNode("B1");
rootNodeB->addItem("B2");
rootNodeB->attachSubject(shared_item); // Here we attach the shared item to another tree's node.
rootNodeB->addItem("B3");
rootTop->attachSubject(rootNodeA);
rootTop->attachSubject(rootNodeB);

When TreeIterator iterates through this tree two things can happen:

See also:
SubjectIterator, ConstSubjectIterator

This class was added in Qtilities v1.0.


Constructor & Destructor Documentation

Qtilities::Core::TreeIterator::TreeIterator ( const Observer top_node = 0,
int  iterator_id = -1 
)
inline

TreeIterator Constructs a new iterator.

Parameters:
top_nodeThe top node of the tree on which the iterator should operate.


Qtilities : Reference Documentation Back to top Copyright © 2009-2013, Jaco Naudé