2 #include <QAbstractItemModel>
4 #include <score_lib_base_export.h>
14 class SCORE_LIB_BASE_EXPORT
TreeModel :
public QAbstractItemModel
17 using QAbstractItemModel::QAbstractItemModel;
20 void iterate(
const QModelIndex& idx,
const F& f)
28 const int rows = rowCount(idx);
29 for(
int i = 0; i < rows; ++i)
30 iterate(this->index(i, 0, idx), f);
33 QModelIndex convertPathToIndex(
const TreePath& path)
const;
36 template <
typename NodeType>
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;
Definition: TreeNodeItemModel.hpp:15
void iterate(const QModelIndex &idx, const F &f)
idx: should be the root index of the view
Definition: TreeNodeItemModel.hpp:20
Definition: TreeNodeItemModel.hpp:38
Path in a tree of QAbstractItemModel objects.
Definition: TreePath.hpp:34