Add RDTreeWidgetItemIterator for depth-first forward-only iteration

This commit is contained in:
baldurk
2017-08-24 16:03:01 +01:00
parent 9cf53473b2
commit 1f13a1466e
2 changed files with 71 additions and 0 deletions
@@ -412,6 +412,48 @@ void RDTreeWidgetItem::clear()
m_widget->m_model->endRemoveChildren();
}
RDTreeWidgetItemIterator::RDTreeWidgetItemIterator(RDTreeWidget *widget)
{
if(widget->topLevelItemCount() == 0)
m_Current = NULL;
else
m_Current = widget->topLevelItem(0);
}
RDTreeWidgetItemIterator &RDTreeWidgetItemIterator::operator++()
{
// depth first
if(m_Current->childCount() > 0)
{
m_Current = m_Current->child(0);
return *this;
}
// otherwise check if we have siblings, recursively up
RDTreeWidgetItem *parent = m_Current->parent();
RDTreeWidgetItem *child = m_Current;
while(parent)
{
// if there's a sibling at this level, move to it
int idx = parent->indexOfChild(child);
if(idx + 1 < parent->childCount())
{
m_Current = parent->child(idx + 1);
return *this;
}
// if there are no more siblings at this level, move up
child = parent;
parent = parent->parent();
// if we just exhausted siblings at the top-level, parent will now be NULL, so we abort.
}
// no more siblings, stop.
m_Current = NULL;
return *this;
}
RDTreeWidget::RDTreeWidget(QWidget *parent) : RDTreeView(parent)
{
// we'll call this ourselves in drawBranches()
@@ -145,6 +145,35 @@ private:
QVariant m_tag;
};
class RDTreeWidgetItemIterator
{
public:
RDTreeWidgetItemIterator(RDTreeWidget *widget);
RDTreeWidgetItemIterator &operator++();
inline const RDTreeWidgetItemIterator operator++(int)
{
RDTreeWidgetItemIterator it = *this;
++(*this);
return it;
}
// TODO implement operator-- if we need it.
/*
RDTreeWidgetItemIterator &operator--();
inline const RDTreeWidgetItemIterator operator--(int)
{
RDTreeWidgetItemIterator it = *this;
--(*this);
return it;
}
*/
inline RDTreeWidgetItem *operator*() const { return m_Current; }
private:
RDTreeWidgetItem *m_Current;
};
class RDTreeWidget : public RDTreeView
{
Q_OBJECT