12 #ifndef TREE_MODEL_HELPERS_H 13 #define TREE_MODEL_HELPERS_H 16 #include <ui/qt/utils/variant_pointer.h> 18 #include <QAbstractItemModel> 21 template <
typename Item>
32 for (
int row = 0; row < childItems_.count(); row++)
40 void appendChild(Item* child)
45 void prependChild(Item* child)
51 void insertChild(
int row, Item* child)
56 void removeChild(
int row)
59 childItems_.removeAt(row);
67 int childCount()
const 69 return childItems_.count();
80 Item* parentItem() {
return parent_; }
84 QList<QVariant> childItems_;
88 #ifdef WIRESHARK_SUPPORTS_QT_5_0_MINIMUM 91 template <
typename Item>
92 class ModelHelperTreeModel :
public QAbstractItemModel
95 explicit ModelHelperTreeModel(QObject * parent = Q_NULLPTR) : QAbstractItemModel(parent),
100 virtual ~ModelHelperTreeModel()
105 virtual QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex())
const 107 if (!hasIndex(row, column, parent))
108 return QModelIndex();
110 Item *parent_item, *child_item;
112 if (!parent.isValid())
115 parent_item =
static_cast<Item*
>(parent.internalPointer());
117 Q_ASSERT(parent_item);
119 child_item = parent_item->child(row);
121 return createIndex(row, column, child_item);
124 return QModelIndex();
127 virtual QModelIndex parent(
const QModelIndex& indexItem)
const 129 if (!indexItem.isValid())
130 return QModelIndex();
132 Item* item =
static_cast<Item*
>(indexItem.internalPointer());
134 Item* parent_item = item->parentItem();
135 if (parent_item != NULL) {
136 if (parent_item == root_)
137 return QModelIndex();
139 return createIndex(parent_item->row(), 0, parent_item);
143 return QModelIndex();
146 virtual int rowCount(
const QModelIndex& parent = QModelIndex())
const 150 if (parent.column() > 0)
153 if (!parent.isValid())
156 parent_item =
static_cast<Item*
>(parent.internalPointer());
158 if (parent_item == NULL)
161 return parent_item->childCount();
172 #endif // TREE_MODEL_HELPERS_H Definition: tree_model_helpers.h:22
Definition: variant_pointer.h:19