OGS
TreeModelIterator.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ** INCLUDES **
5#include "TreeModelIterator.h"
6
7#include "Base/TreeItem.h"
8#include "Base/TreeModel.h"
9
11 : _current(nullptr), _model(model)
12{
13 if (_model->rootItem()->childCount() > 0)
14 {
15 _current = _model->rootItem();
16 }
17}
18
23
25{
26 if (_current)
27 {
29 }
30
31 return *this;
32}
33
35{
36 if (!current)
37 {
38 return nullptr;
39 }
40
41 TreeItem* next = nullptr;
42 if (current->childCount())
43 {
44 // walk the child
46 _currentIndex = 0;
47 next = current->child(0);
48 }
49 else
50 {
51 // walk the sibling
52 TreeItem* parent = current->parentItem();
53 next = parent ? parent->child(_currentIndex + 1)
54 : _model->rootItem()->child(_currentIndex + 1);
55 while (!next && parent)
56 {
57 // if we had no sibling walk up the parent
58 // and try the sibling of that
59 parent = parent->parentItem();
61 next = parent ? parent->child(_currentIndex + 1)
62 : _model->rootItem()->child(_currentIndex + 1);
63 }
64 if (next)
65 {
66 ++(_currentIndex);
67 }
68 }
69 return next;
70}
Objects nodes for the TreeModel.
Definition TreeItem.h:17
virtual int childCount() const
Definition TreeItem.cpp:54
TreeItem * parentItem() const
Definition TreeItem.cpp:104
TreeItem * child(int row) const
Definition TreeItem.cpp:41
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:19