summaryrefslogtreecommitdiffstats
path: root/examples/treeModel/test.cpp
blob: 30d9900307c8df9d7ca89b66c66e9f94a971992c (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
/****************************************************************************
**
** 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 "test.h"
#include <QDebug>

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

Test::~Test()
{
}

Node *Test::firstChild(Node *node) const
{
    if (!hiddenRoot)
        return 0;
    if (node)
        return node->children.isEmpty() ? 0 : node->children.first();
    return hiddenRoot->children.isEmpty() ? 0 : hiddenRoot->children.first();
}

Node *Test::nextSibling(Node *node) const
{
    Node *next = 0;
    if (node && node->parent) { // find a faster way
        int i = node->parent->children.indexOf(node);
        if (i != -1 && i < node->parent->children.count() - 1)
            next = node->parent->children.at(i + 1);
    }
    return next;
}

Node *Test::parentItem(Node *node) const
{
    return (node && node->parent != hiddenRoot? node->parent : 0);
}

Node *Test::previousSibling(Node *node) const
{
    Node *prev = 0;
    if (node && node->parent) { // find a faster way
        int i = node->parent->children.indexOf(node);
        if (i != -1 && i > 1)
            prev = node->parent->children.at(i - 1);
    }
    return prev;
}

QHash<QByteArray,QVariant> Test::data(Node *node, int column, const QList<QByteArray> &roles) const
{
    QHash<QByteArray,QVariant> values;
    if (node && column == 0)
        for (int i = 0; i < roles.count(); ++i)
            if (roles.at(i) == "DisplayRole")
                values.insert("DisplayRole", node->text);
    return values;
}

bool Test::setData(const QVariant &data, Node *node, int column, const QByteArray &role)
{
    if (node && column == 0 && role == "DisplayRole") {
        node->text = data.toString();
        Test::iterator it = iterator(node, this);
        emit itemsChanged(it, 1, QList<QByteArray>() << "DisplayRole");
    }
    return !!node;
}

bool Test::isValid(Node *node) const
{
    return !!node;
}

// Model specific API

void Test::insertNode(Node *parent, int index, Node *node)
{
    if (parent)
        parent->children.insert(index, node);
    else
        hiddenRoot->children.insert(index, node);
    node->parent = parent;
    
    Test::iterator it = iterator(node, this);
    emit itemsInserted(it, 1);
}

Node *Test::node(Node *parent, int index)
{
    if (parent)
        return parent->children.at(index);
    return hiddenRoot->children.at(index);
}

void Test::removeNode(Node *node)
{
    if (node->parent)
        node->parent->children.removeAll(node);
    else
        hiddenRoot->children.removeAll(node);
    node->parent = 0;
    
    // ### doesn't make sense to send out a signal with an interator _after_ removal
    //change(node, 0); // invalidate

    Test::iterator it = iterator(node, this);
    emit itemsRemoved(it, 1);
}

// Setup tree

void Test::init()
{
    hiddenRoot = new Node;
    for (int i = 0; i < 1000; ++i) {
        Node *node = new Node;
        node->parent = hiddenRoot;
        node->text = QString("node ") + QString::number(i);
        hiddenRoot->children.append(node);
        for (int j = 0; j < 10; ++j) {
            Node *child = new Node;
            child->parent = node;
            child->text = QString("child ") + QString::number(j);
            node->children.append(child);
        }
    }
}