aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/models/tree_model_helpers.h
blob: 664415c5e0754baed4f512796e72cdc1728a6b64 (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
/* 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-or-later
 */

#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_.append(VariantPointer<Item>::asQVariant(child));
    }

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


    void insertChild(int row, Item* child)
    {
        childItems_.insert(row, VariantPointer<Item>::asQVariant(child));
    }

    void removeChild(int row)
    {
        delete VariantPointer<Item>::asPtr(childItems_.value(row));
        childItems_.removeAt(row);
    }

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

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

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

        return 0;
    }

    Item* parentItem() {return parent_; }

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

#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:
 */