| 1 | /*  This file is part of the KDE project.
 | 
|---|
| 2 | 
 | 
|---|
| 3 | Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 | 
|---|
| 4 | 
 | 
|---|
| 5 | This library is free software: you can redistribute it and/or modify
 | 
|---|
| 6 | it under the terms of the GNU Lesser General Public License as published by
 | 
|---|
| 7 | the Free Software Foundation, either version 2.1 or 3 of the License.
 | 
|---|
| 8 | 
 | 
|---|
| 9 | This library is distributed in the hope that it will be useful,
 | 
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 12 | GNU Lesser General Public License for more details.
 | 
|---|
| 13 | 
 | 
|---|
| 14 | You should have received a copy of the GNU Lesser General Public License
 | 
|---|
| 15 | along with this library.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 16 | 
 | 
|---|
| 17 | */
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include <QTextStream>
 | 
|---|
| 20 | #include <QWidget>
 | 
|---|
| 21 | #include "objecttree.h"
 | 
|---|
| 22 | 
 | 
|---|
| 23 | QT_BEGIN_NAMESPACE
 | 
|---|
| 24 | 
 | 
|---|
| 25 | namespace ObjectTree
 | 
|---|
| 26 | {
 | 
|---|
| 27 | 
 | 
|---|
| 28 | DepthFirstConstIterator::DepthFirstConstIterator()
 | 
|---|
| 29 |     :   m_pointee(0)
 | 
|---|
| 30 | {
 | 
|---|
| 31 | 
 | 
|---|
| 32 | }
 | 
|---|
| 33 | 
 | 
|---|
| 34 | DepthFirstConstIterator::DepthFirstConstIterator
 | 
|---|
| 35 |     (const QObject& root)
 | 
|---|
| 36 |     :   m_pointee(&root)
 | 
|---|
| 37 | {
 | 
|---|
| 38 | 
 | 
|---|
| 39 | }
 | 
|---|
| 40 | 
 | 
|---|
| 41 | DepthFirstConstIterator&
 | 
|---|
| 42 |     DepthFirstConstIterator::operator++()
 | 
|---|
| 43 | {
 | 
|---|
| 44 |     const QObjectList& children = m_pointee->children();
 | 
|---|
| 45 | 
 | 
|---|
| 46 |     if (children.count() == 0) {
 | 
|---|
| 47 |         backtrack();
 | 
|---|
| 48 |     }
 | 
|---|
| 49 |     else {
 | 
|---|
| 50 |         m_history.push(0);
 | 
|---|
| 51 |         m_pointee = children.first();
 | 
|---|
| 52 |     }
 | 
|---|
| 53 | 
 | 
|---|
| 54 |     return *this;
 | 
|---|
| 55 | }
 | 
|---|
| 56 | 
 | 
|---|
| 57 | void DepthFirstConstIterator::backtrack()
 | 
|---|
| 58 | {
 | 
|---|
| 59 |     if (m_history.count()) {
 | 
|---|
| 60 |         const int index = m_history.top();
 | 
|---|
| 61 |         m_history.pop();
 | 
|---|
| 62 | 
 | 
|---|
| 63 |         const QObjectList& siblings = m_pointee->parent()->children();
 | 
|---|
| 64 |         if (siblings.count() > index + 1) {
 | 
|---|
| 65 |             m_history.push(index + 1);
 | 
|---|
| 66 |             m_pointee = siblings[index + 1];
 | 
|---|
| 67 |         }
 | 
|---|
| 68 |         else {
 | 
|---|
| 69 |             m_pointee = m_pointee->parent();
 | 
|---|
| 70 |             backtrack();
 | 
|---|
| 71 |         }
 | 
|---|
| 72 |     }
 | 
|---|
| 73 |     else {
 | 
|---|
| 74 |         // Reached end of search
 | 
|---|
| 75 |         m_pointee = 0;
 | 
|---|
| 76 |     }
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | 
 | 
|---|
| 80 | 
 | 
|---|
| 81 | AncestorConstIterator::AncestorConstIterator()
 | 
|---|
| 82 | {
 | 
|---|
| 83 | 
 | 
|---|
| 84 | }
 | 
|---|
| 85 | 
 | 
|---|
| 86 | AncestorConstIterator::AncestorConstIterator(const QObject& leaf)
 | 
|---|
| 87 | {
 | 
|---|
| 88 |     m_ancestors.push(&leaf);
 | 
|---|
| 89 |     QObject* ancestor = leaf.parent();
 | 
|---|
| 90 |     while(ancestor)
 | 
|---|
| 91 |     {
 | 
|---|
| 92 |         m_ancestors.push(ancestor);
 | 
|---|
| 93 |         ancestor = ancestor->parent();
 | 
|---|
| 94 |     }
 | 
|---|
| 95 | }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | } // namespace ObjectTree
 | 
|---|
| 98 | 
 | 
|---|
| 99 | QT_END_NAMESPACE
 | 
|---|
| 100 | 
 | 
|---|