OGS
TreeModelIterator.cpp
Go to the documentation of this file.
1
15// ** INCLUDES **
16#include "TreeModelIterator.h"
17
18#include "Base/TreeItem.h"
19#include "Base/TreeModel.h"
20
22 : _current(nullptr), _model(model)
23{
24 if (_model->rootItem()->childCount() > 0)
25 {
27 }
28}
29
34
36{
37 if (_current)
38 {
40 }
41
42 return *this;
43}
44
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}
Definition of the TreeItem class.
Definition of the TreeModelIterator class.
Definition of the TreeModel class.
Objects nodes for the TreeModel.
Definition TreeItem.h:28
virtual int childCount() const
Definition TreeItem.cpp:65
TreeItem * parentItem() const
Definition TreeItem.cpp:115
TreeItem * child(int row) const
Definition TreeItem.cpp:52
TreeModelIterator provides a way to iterate over TreeItems in a TreeModel. Usage:
TreeItem * next(const TreeItem *current)
The traversal implementation.
TreeModelIterator(TreeModel *model)
Constructor. Provide a tree model to iterate over.
QStack< int > _parentIndex
Stack to save the child indices of the parent TreeItems.
TreeModel * _model
The model to iterate over.
TreeItem * operator*() const
Dereferencing the iterator to retrieve the current TreeItem. Returns nullptr if the iterator is at th...
int _currentIndex
The current child index.
TreeItem * _current
The current TreeItem.
TreeModelIterator & operator++()
Advance the iterator to the next TreeItem.
A hierarchical model for a tree implemented as a double-linked list.
Definition TreeModel.h:30
TreeItem * rootItem() const