blob: 5734b66001c5a5282ff6d776b77e7844958cefad (
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
|
/** @file
*
* 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 static_cast<int>(childItems_.count());
}
int row()
{
if (parent_)
{
return static_cast<int>(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
|