OGS
TreeModelIterator Class Reference

Detailed Description

TreeModelIterator provides a way to iterate over TreeItems in a TreeModel. Usage:

while(*it)
{
QVariant var = (*it)->data(0);
std::cout << var.toString().toStdString() << std::endl;
++it;
}
TreeModelIterator provides a way to iterate over TreeItems in a TreeModel. Usage:

Definition at line 34 of file TreeModelIterator.h.

#include <TreeModelIterator.h>

Collaboration diagram for TreeModelIterator:
[legend]

Public Member Functions

 TreeModelIterator (TreeModel *model)
 Constructor. Provide a tree model to iterate over.
 
TreeItemoperator* () const
 Dereferencing the iterator to retrieve the current TreeItem. Returns nullptr if the iterator is at the end.
 
TreeModelIteratoroperator++ ()
 Advance the iterator to the next TreeItem.
 

Private Member Functions

TreeItemnext (const TreeItem *current)
 The traversal implementation.
 

Private Attributes

TreeItem_current
 The current TreeItem.
 
int _currentIndex
 The current child index.
 
QStack< int > _parentIndex
 Stack to save the child indices of the parent TreeItems.
 
TreeModel_model
 The model to iterate over.
 

Constructor & Destructor Documentation

◆ TreeModelIterator()

TreeModelIterator::TreeModelIterator ( TreeModel model)
explicit

Constructor. Provide a tree model to iterate over.

Definition at line 21 of file TreeModelIterator.cpp.

22 : _current(nullptr), _model(model)
23{
24 if (_model->rootItem()->childCount() > 0)
25 {
27 }
28}
virtual int childCount() const
Definition: TreeItem.cpp:65
TreeModel * _model
The model to iterate over.
TreeItem * _current
The current TreeItem.
TreeItem * rootItem() const
Definition: TreeModel.cpp:294

References _current, _model, TreeItem::childCount(), and TreeModel::rootItem().

Member Function Documentation

◆ next()

TreeItem * TreeModelIterator::next ( const TreeItem current)
private

The traversal implementation.

Definition at line 45 of file TreeModelIterator.cpp.

46{
47 if (!current)
48 {
49 return nullptr;
50 }
51
52 TreeItem* next = nullptr;
53 if (current->childCount())
54 {
55 // walk the child
57 _currentIndex = 0;
58 next = current->child(0);
59 }
60 else
61 {
62 // walk the sibling
63 TreeItem* parent = current->parentItem();
64 next = parent ? parent->child(_currentIndex + 1)
66 while (!next && parent)
67 {
68 // if we had no sibling walk up the parent
69 // and try the sibling of that
70 parent = parent->parentItem();
72 next = parent ? parent->child(_currentIndex + 1)
74 }
75 if (next)
76 {
77 ++(_currentIndex);
78 }
79 }
80 return next;
81}
Objects nodes for the TreeModel.
Definition: TreeItem.h:28
TreeItem * parentItem() const
Definition: TreeItem.cpp:115
TreeItem * child(int row) const
Definition: TreeItem.cpp:52
TreeItem * next(const TreeItem *current)
The traversal implementation.
QStack< int > _parentIndex
Stack to save the child indices of the parent TreeItems.
int _currentIndex
The current child index.

References _currentIndex, _model, _parentIndex, TreeItem::child(), TreeItem::childCount(), next(), TreeItem::parentItem(), and TreeModel::rootItem().

Referenced by next(), and operator++().

◆ operator*()

TreeItem * TreeModelIterator::operator* ( ) const

Dereferencing the iterator to retrieve the current TreeItem. Returns nullptr if the iterator is at the end.

Definition at line 30 of file TreeModelIterator.cpp.

31{
32 return _current;
33}

References _current.

◆ operator++()

TreeModelIterator & TreeModelIterator::operator++ ( )

Advance the iterator to the next TreeItem.

Definition at line 35 of file TreeModelIterator.cpp.

36{
37 if (_current)
38 {
40 }
41
42 return *this;
43}

References _current, and next().

Member Data Documentation

◆ _current

TreeItem* TreeModelIterator::_current
private

The current TreeItem.

Definition at line 49 of file TreeModelIterator.h.

Referenced by TreeModelIterator(), operator*(), and operator++().

◆ _currentIndex

int TreeModelIterator::_currentIndex
private

The current child index.

Definition at line 52 of file TreeModelIterator.h.

Referenced by next().

◆ _model

TreeModel* TreeModelIterator::_model
private

The model to iterate over.

Definition at line 58 of file TreeModelIterator.h.

Referenced by TreeModelIterator(), and next().

◆ _parentIndex

QStack<int> TreeModelIterator::_parentIndex
private

Stack to save the child indices of the parent TreeItems.

Definition at line 55 of file TreeModelIterator.h.

Referenced by next().


The documentation for this class was generated from the following files: