40 using TreeModel::TreeModel;
42 using node_type = NodeType;
44 virtual NodeType& rootNode() = 0;
45 virtual const NodeType& rootNode()
const = 0;
47 NodeType& nodeFromModelIndex(
const QModelIndex& index)
const
49 auto n = index.isValid() ?
static_cast<NodeType*
>(index.internalPointer())
50 :
const_cast<NodeType*
>(&rootNode());
56 QModelIndex parent(
const QModelIndex& index)
const final override
60 if(index.model() !=
this)
63 const auto& node = nodeFromModelIndex(index);
64 auto parentNode = node.parent();
69 auto grandparentNode = parentNode->parent();
74 const int rowParent = grandparentNode->indexOfChild(parentNode);
78 return createIndex(rowParent, 0, parentNode);
81 QModelIndex index(
int row,
int column,
const QModelIndex& parent)
const final override
83 if(!hasIndex(row, column, parent))
86 auto& parentItem = nodeFromModelIndex(parent);
88 if(parentItem.hasChild(row))
89 return createIndex(row, column, &parentItem.childAt(row));
94 int rowCount(
const QModelIndex& parent)
const final override
96 if(parent.column() > 0)
99 const auto& parentNode = nodeFromModelIndex(parent);
100 return parentNode.childCount();
103 bool hasChildren(
const QModelIndex& parent)
const final override
105 const auto& parentNode = nodeFromModelIndex(parent);
106 return parentNode.childCount() > 0;
void iterate(const QModelIndex &idx, const F &f)
idx: should be the root index of the view
Definition TreeNodeItemModel.hpp:20