| Wireshark 4.7.3 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
tree_model_helpers.h
Go to the documentation of this file.
1
11
12#ifndef TREE_MODEL_HELPERS_H
13#define TREE_MODEL_HELPERS_H
14
15#include <config.h>
16#include <ui/qt/utils/variant_pointer.h>
17
18#include <QAbstractItemModel>
19
23 template <typename Item>
24 class ModelHelperTreeItem
25{
26 public:
31 ModelHelperTreeItem(Item* parent)
32 : parent_(parent)
33 {
34 }
35
39 virtual ~ModelHelperTreeItem()
40 {
41 for (int row = 0; row < childItems_.count(); row++)
42 {
43 delete VariantPointer<Item>::asPtr(childItems_.value(row));
44 }
45
46 childItems_.clear();
47 }
48
53 void appendChild(Item* child)
54 {
55 childItems_.append(VariantPointer<Item>::asQVariant(child));
56 }
57
62 void prependChild(Item* child)
63 {
64 childItems_.prepend(VariantPointer<Item>::asQVariant(child));
65 }
66
72 void insertChild(int row, Item* child)
73 {
74 childItems_.insert(row, VariantPointer<Item>::asQVariant(child));
75 }
76
81 void removeChild(int row)
82 {
83 delete VariantPointer<Item>::asPtr(childItems_.value(row));
84 childItems_.removeAt(row);
85 }
86
92 Item* child(int row)
93 {
94 return VariantPointer<Item>::asPtr(childItems_.value(row));
95 }
96
101 int childCount() const
102 {
103 return static_cast<int>(childItems_.count());
104 }
105
110 int row()
111 {
112 if (parent_)
113 {
114 return static_cast<int>(parent_->childItems_.indexOf(VariantPointer<Item>::asQVariant((Item *)this)));
115 }
116
117 return 0;
118 }
119
124 Item* parentItem() {return parent_; }
125
126 protected:
128 Item* parent_;
129
131 QList<QVariant> childItems_;
132};
133
134#endif // TREE_MODEL_HELPERS_H
ModelHelperTreeItem::prependChild
void prependChild(Item *child)
Prepends a child item to the beginning of the children list.
Definition tree_model_helpers.h:62
ModelHelperTreeItem::appendChild
void appendChild(Item *child)
Appends a child item to the end of the children list.
Definition tree_model_helpers.h:53
ModelHelperTreeItem::parent_
Item * parent_
Definition tree_model_helpers.h:128
ModelHelperTreeItem::removeChild
void removeChild(int row)
Removes and deletes the child item at the specified row.
Definition tree_model_helpers.h:81
ModelHelperTreeItem::~ModelHelperTreeItem
virtual ~ModelHelperTreeItem()
Destroys the ModelHelperTreeItem and its children.
Definition tree_model_helpers.h:39
ModelHelperTreeItem::insertChild
void insertChild(int row, Item *child)
Inserts a child item at the specified row.
Definition tree_model_helpers.h:72
ModelHelperTreeItem::ModelHelperTreeItem
ModelHelperTreeItem(Item *parent)
Constructs a new ModelHelperTreeItem.
Definition tree_model_helpers.h:31
ModelHelperTreeItem::parentItem
Item * parentItem()
Retrieves the parent item.
Definition tree_model_helpers.h:124
ModelHelperTreeItem::childCount
int childCount() const
Gets the total number of child items.
Definition tree_model_helpers.h:101
ModelHelperTreeItem::childItems_
QList< QVariant > childItems_
Definition tree_model_helpers.h:131
ModelHelperTreeItem::child
Item * child(int row)
Retrieves the child item at the specified row.
Definition tree_model_helpers.h:92
ModelHelperTreeItem::row
int row()
Gets the row index of this item relative to its parent.
Definition tree_model_helpers.h:110
VariantPointer::asQVariant
static QVariant asQVariant(T *ptr)
Converts a typed pointer into a QVariant.
Definition variant_pointer.h:41
VariantPointer::asPtr
static T * asPtr(QVariant v)
Converts a QVariant containing a void pointer back to a typed pointer.
Definition variant_pointer.h:31
variant_pointer.h