#ifndef SIMPLETREEMODEL_H #define SIMPLETREEMODEL_H #include class QModelIndex; template class SimpleTreeModel : public QAbstractItemModel { public: SimpleTreeModel(T* root = 0, QObject* parent = 0); virtual ~SimpleTreeModel() {} // QAbstractItemModel int columnCount(const QModelIndex& parent) const; QModelIndex index(int row, int, const QModelIndex& parent) const; QModelIndex parent(const QModelIndex& index) const; int rowCount(const QModelIndex& parent) const; bool hasChildren(const QModelIndex& parent) const; T* IndexToItem(const QModelIndex& index) const; QModelIndex ItemToIndex(T* item) const; // Called by items void BeginInsert(T* parent, int start, int end = -1); void EndInsert(); void BeginDelete(T* parent, int start, int end = -1); void EndDelete(); protected: virtual void LazyPopulate(T* item) = 0; protected: T* root_; }; template SimpleTreeModel::SimpleTreeModel(T* root, QObject* parent) : QAbstractItemModel(parent), root_(root) { } template T* SimpleTreeModel::IndexToItem(const QModelIndex& index) const { if (!index.isValid()) return root_; return reinterpret_cast(index.internalPointer()); } template QModelIndex SimpleTreeModel::ItemToIndex(T* item) const { if (!item || !item->parent) return QModelIndex(); return createIndex(item->row, 0, item); } template int SimpleTreeModel::columnCount(const QModelIndex &) const { return 1; } template QModelIndex SimpleTreeModel::index(int row, int, const QModelIndex& parent) const { T* parent_item = IndexToItem(parent); if (!parent_item || parent_item->children.count() <= row) return QModelIndex(); return ItemToIndex(parent_item->children[row]); } template QModelIndex SimpleTreeModel::parent(const QModelIndex& index) const { return ItemToIndex(IndexToItem(index)->parent); } template int SimpleTreeModel::rowCount(const QModelIndex & parent) const { T* item = IndexToItem(parent); if (!item->lazy_loaded) const_cast*>(this)->LazyPopulate(item); // Ahem return item->children.count(); } template bool SimpleTreeModel::hasChildren(const QModelIndex &parent) const { T* item = IndexToItem(parent); if (item->lazy_loaded) return !item->children.isEmpty(); else return true; } template void SimpleTreeModel::BeginInsert(T* parent, int start, int end) { if (end == -1) end = start; beginInsertRows(ItemToIndex(parent), start, end); } template void SimpleTreeModel::EndInsert() { endInsertRows(); } template void SimpleTreeModel::BeginDelete(T* parent, int start, int end) { if (end == -1) end = start; beginRemoveRows(ItemToIndex(parent), start, end); } template void SimpleTreeModel::EndDelete() { endRemoveRows(); } #endif // SIMPLETREEMODEL_H