aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/models/tree_model_helpers.h
blob: 440f7d5fa18afb818f8a658dc2bd3db582bd5339 (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
162
163
164
165
166
167
168
/* tree_model_helpers.h
 *
 * Utility template classes for basic tree model functionality
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0+
 */

#ifndef TREE_MODEL_HELPERS_H
#define TREE_MODEL_HELPERS_H

#include <config.h>
#include <ui/qt/utils/variant_pointer.h>

#include <QAbstractItemModel>

//Base class to inherit basic tree item from
template <typename Item>
class ModelHelperTreeItem
{
public:
    ModelHelperTreeItem(Item* parent)
        : parent_(parent)
    {
    }

    virtual ~ModelHelperTreeItem()
    {
        for (int row = 0; row < childItems_.count(); row++)
        {
            delete VariantPointer<Item>::asPtr(childItems_.value(row));
        }

        childItems_.clear();
    }

    void appendChild(Item* child)
    {
        childItems_.prepend(VariantPointer<Item>::asQVariant(child));
    }

    Item* child(int row)
    {
        return VariantPointer<Item>::asPtr(childItems_.value(row));
    }

    int childCount() const
    {
        return childItems_.count();
    }

    int row() const
    {
        if (parent_)
            return parent_->childItems_.indexOf(VariantPointer<Item>::asQVariant((Item*)this));

        return 0;
    }

    Item* parentItem() {return parent_; }

protected:
    Item* parent_;
    QList<QVariant> childItems_;
};

//XXX - Qt 4.8 doesn't work with these types of templated classes, so save the functionality for now.
#ifdef WIRESHARK_SUPPORTS_QT_5_0_MINIMUM

//Base class to inherit basic model for tree
template <typename Item>
class ModelHelperTreeModel : public QAbstractItemModel
{
public:
    explicit ModelHelperTreeModel(QObject * parent = Q_NULLPTR) : QAbstractItemModel(parent),
        root_(NULL)
    {
    }

    virtual ~ModelHelperTreeModel()
    {
        delete root_;
    }

    virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const
    {
        if (!hasIndex(row, column, parent))
            return QModelIndex();

        Item *parent_item, *child_item;

        if (!parent.isValid())
            parent_item = root_;
        else
            parent_item = static_cast<Item*>(parent.internalPointer());

        Q_ASSERT(parent_item);

        child_item = parent_item->child(row);
        if (child_item) {
            return createIndex(row, column, child_item);
        }

        return QModelIndex();
    }

    virtual QModelIndex parent(const QModelIndex& indexItem) const
    {
        if (!indexItem.isValid())
            return QModelIndex();

        Item* item = static_cast<Item*>(indexItem.internalPointer());
        if (item != NULL) {
            Item* parent_item = item->parentItem();
            if (parent_item != NULL) {
                if (parent_item == root_)
                    return QModelIndex();

                return createIndex(parent_item->row(), 0, parent_item);
            }
        }

        return QModelIndex();
    }

    virtual int rowCount(const QModelIndex& parent = QModelIndex()) const
    {
        Item *parent_item;

        if (parent.column() > 0)
            return 0;

        if (!parent.isValid())
            parent_item = root_;
        else
            parent_item = static_cast<Item*>(parent.internalPointer());

        if (parent_item == NULL)
            return 0;

        return parent_item->childCount();
    }

protected:
    Item* root_;

};
#endif



#endif // TREE_MODEL_HELPERS_H

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */