summaryrefslogtreecommitdiffstats
path: root/examples/treeModel/test2.cpp
blob: 8af1e2db53c56c9e9df55a3bd717d15b578048fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/****************************************************************************
**
** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Itemviews NG project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "test2.h"
#include <QDebug>

Test2::Test2(QObject *parent)
    : QtTreeModelInterface<Path>(parent), hiddenRoot(0)
{
    init();
}

Test2::~Test2()
{
}

Path Test2::firstChild(Path path) const
{
    Node2 *node = lookup(path);
    if (node && !node->children.isEmpty()) {
        if (cachedPath == path && !cachedFirstChild.isEmpty())
            return cachedFirstChild;
        cachedPath = path; // ### cached
        path.append(0);
        cachedFirstChild = path; // ### cached
        cachedNextSibling = Path();
        cachedPreviousSibling = Path();
        return path;
    }
    return Path();
}

Path Test2::nextSibling(Path path) const
{
    if (!path.isEmpty()) {
        if (cachedPath == path && !cachedNextSibling.isEmpty())
            return cachedNextSibling;
        cachedPath = path; // ### cached

        int last = path.count() - 1;

        Node2 *parent = lookup(path, last);
        if (!parent || path.at(last) >= parent->children.count())
            return Path();
 
       ++path[last];

        cachedFirstChild = Path();
        cachedNextSibling = path; // ### cached
        cachedPreviousSibling = Path();
    }
    return path;
}

Path Test2::parentItem(Path path) const
{
    if (!path.isEmpty())
        path.removeLast();
    return path;
}

Path Test2::previousSibling(Path path) const
{
    if (!path.isEmpty()) {
        if (cachedPath == path && !cachedPreviousSibling.isEmpty())
            return cachedPreviousSibling;
        cachedPath = path; // ### cached
        int last = path.count() - 1;
        if (path.at(last) <= 0)
            return Path();
        --path[last];
        cachedFirstChild = Path();
        cachedNextSibling = Path();
        cachedPreviousSibling = path; // cached
    }
    return path;
}

QHash<int,QVariant> Test2::data(Path path, int column, const QList<int> &roles) const
{
    QHash<int, QVariant> values;
    if (column == 0)
        for (int i = 0; i < roles.count(); ++i)
            if (roles.at(i) == Qt::DisplayRole) 
                if (Node2 *node = lookup(path))
                    values.insert(Qt::DisplayRole, node->text);
    return values;
}

bool Test2::isValid(Path path) const
{
    if (path.isEmpty())
        return false;
    return !!lookup(path);
}

// Internal mapping

Node2 *Test2::lookup(Path path, int depth) const
{
    Node2 *node = hiddenRoot;
    int c = depth >= 0 ? qMin(depth, path.count()) : path.count();
    for (int i = 0; i < c; ++i) {
        int j = path.at(i);
        if (j >= 0 && j < node->children.count())
            node = node->children.at(j);
        else
            return 0; // ### something went wrong!
    }
    return node;
}

// Setup tree

void Test2::init()
{
    // It seems that most itunes collections are less that 20000 songs, and
    // only in very extreme cases does it go over 50000.
    // Over 50k songs, is still only 5000 albums. So organized in a tree, it's
    // still not that much.
    
    hiddenRoot = new Node2;
    for (int i = 0; i < 5000; ++i) {
        Node2 *node = new Node2;
        node->parent = hiddenRoot;
        node->text = QString("text ") + QString::number(i);
        hiddenRoot->children.append(node);
        for (int j = 0; j < 10; ++j) {
            Node2 *child = new Node2;
            child->parent = node;
            child->text = QString("child ") + QString::number(j);
            node->children.append(child);
            for (int k = 0; k < 10; ++k) {
                Node2 *grandchild = new Node2;
                grandchild->parent = child;
                grandchild->text = QString("grand ") + QString::number(k);
                child->children.append(grandchild);
            }
        }
    }
}