/* * Strawberry Music Player * This file was part of Clementine. * Copyright 2010, David Sansome * Copyright 2018-2021, Jonas Kvinge * * Strawberry is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Strawberry is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Strawberry. If not, see . * */ #ifndef SIMPLETREEMODEL_H #define SIMPLETREEMODEL_H #include "config.h" #include #include template class SimpleTreeModel : public QAbstractItemModel { public: explicit SimpleTreeModel(T *root = nullptr, QObject *parent = nullptr); ~SimpleTreeModel() override {} // QAbstractItemModel int columnCount(const QModelIndex &parent) const override; QModelIndex index(int row, int, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &idx) const override; int rowCount(const QModelIndex &parent) const override; bool hasChildren(const QModelIndex &parent) const override; bool canFetchMore(const QModelIndex &parent) const override; void fetchMore(const QModelIndex &parent) override; T *IndexToItem(const QModelIndex &idx) 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(); void EmitDataChanged(T *item); protected: virtual void LazyPopulate(T *item) { item->lazy_loaded = true; } protected: T *root_; }; template SimpleTreeModel::SimpleTreeModel(T *root, QObject *parent) : QAbstractItemModel(parent), root_(root) {} template T *SimpleTreeModel::IndexToItem(const QModelIndex &idx) const { if (!idx.isValid()) return root_; return reinterpret_cast(idx.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 || row < 0 || parent_item->children.count() <= row) return QModelIndex(); return ItemToIndex(parent_item->children[row]); } template QModelIndex SimpleTreeModel::parent(const QModelIndex &idx) const { return ItemToIndex(IndexToItem(idx)->parent); } template int SimpleTreeModel::rowCount(const QModelIndex &parent) const { T *item = IndexToItem(parent); 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 bool SimpleTreeModel::canFetchMore(const QModelIndex &parent) const { T *item = IndexToItem(parent); return !item->lazy_loaded; } template void SimpleTreeModel::fetchMore(const QModelIndex &parent) { T *item = IndexToItem(parent); if (!item->lazy_loaded) { LazyPopulate(item); } } 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(); } template void SimpleTreeModel::EmitDataChanged(T *item) { QModelIndex index(ItemToIndex(item)); emit dataChanged(index, index); } #endif // SIMPLETREEMODEL_H