Wireshark  2.9.0-477-g68ec514b
The Wireshark network protocol analyzer
tree_model_helpers.h
1 /* tree_model_helpers.h
2  *
3  * Utility template classes for basic tree model functionality
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
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 
20 //Base class to inherit basic tree item from
21 template <typename Item>
23 {
24 public:
25  ModelHelperTreeItem(Item* parent)
26  : parent_(parent)
27  {
28  }
29 
30  virtual ~ModelHelperTreeItem()
31  {
32  for (int row = 0; row < childItems_.count(); row++)
33  {
34  delete VariantPointer<Item>::asPtr(childItems_.value(row));
35  }
36 
37  childItems_.clear();
38  }
39 
40  void appendChild(Item* child)
41  {
42  childItems_.append(VariantPointer<Item>::asQVariant(child));
43  }
44 
45  void prependChild(Item* child)
46  {
47  childItems_.prepend(VariantPointer<Item>::asQVariant(child));
48  }
49 
50 
51  void insertChild(int row, Item* child)
52  {
53  childItems_.insert(row, VariantPointer<Item>::asQVariant(child));
54  }
55 
56  void removeChild(int row)
57  {
58  delete VariantPointer<Item>::asPtr(childItems_.value(row));
59  childItems_.removeAt(row);
60  }
61 
62  Item* child(int row)
63  {
64  return VariantPointer<Item>::asPtr(childItems_.value(row));
65  }
66 
67  int childCount() const
68  {
69  return childItems_.count();
70  }
71 
72  int row() const
73  {
74  if (parent_)
75  return parent_->childItems_.indexOf(VariantPointer<Item>::asQVariant((Item*)this));
76 
77  return 0;
78  }
79 
80  Item* parentItem() {return parent_; }
81 
82 protected:
83  Item* parent_;
84  QList<QVariant> childItems_;
85 };
86 
87 //XXX - Qt 4.8 doesn't work with these types of templated classes, so save the functionality for now.
88 #ifdef WIRESHARK_SUPPORTS_QT_5_0_MINIMUM
89 
90 //Base class to inherit basic model for tree
91 template <typename Item>
92 class ModelHelperTreeModel : public QAbstractItemModel
93 {
94 public:
95  explicit ModelHelperTreeModel(QObject * parent = Q_NULLPTR) : QAbstractItemModel(parent),
96  root_(NULL)
97  {
98  }
99 
100  virtual ~ModelHelperTreeModel()
101  {
102  delete root_;
103  }
104 
105  virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const
106  {
107  if (!hasIndex(row, column, parent))
108  return QModelIndex();
109 
110  Item *parent_item, *child_item;
111 
112  if (!parent.isValid())
113  parent_item = root_;
114  else
115  parent_item = static_cast<Item*>(parent.internalPointer());
116 
117  Q_ASSERT(parent_item);
118 
119  child_item = parent_item->child(row);
120  if (child_item) {
121  return createIndex(row, column, child_item);
122  }
123 
124  return QModelIndex();
125  }
126 
127  virtual QModelIndex parent(const QModelIndex& indexItem) const
128  {
129  if (!indexItem.isValid())
130  return QModelIndex();
131 
132  Item* item = static_cast<Item*>(indexItem.internalPointer());
133  if (item != NULL) {
134  Item* parent_item = item->parentItem();
135  if (parent_item != NULL) {
136  if (parent_item == root_)
137  return QModelIndex();
138 
139  return createIndex(parent_item->row(), 0, parent_item);
140  }
141  }
142 
143  return QModelIndex();
144  }
145 
146  virtual int rowCount(const QModelIndex& parent = QModelIndex()) const
147  {
148  Item *parent_item;
149 
150  if (parent.column() > 0)
151  return 0;
152 
153  if (!parent.isValid())
154  parent_item = root_;
155  else
156  parent_item = static_cast<Item*>(parent.internalPointer());
157 
158  if (parent_item == NULL)
159  return 0;
160 
161  return parent_item->childCount();
162  }
163 
164 protected:
165  Item* root_;
166 
167 };
168 #endif
169 
170 
171 
172 #endif // TREE_MODEL_HELPERS_H
173 
174 /*
175  * Editor modelines
176  *
177  * Local Variables:
178  * c-basic-offset: 4
179  * tab-width: 8
180  * indent-tabs-mode: nil
181  * End:
182  *
183  * ex: set shiftwidth=4 tabstop=8 expandtab:
184  * :indentSize=4:tabSize=8:noTabs=true:
185  */
Definition: tree_model_helpers.h:22
Definition: variant_pointer.h:19