strawberry-audio-player-win.../src/core/simpletreemodel.h

157 lines
4.4 KiB
C
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* 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 <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#ifndef SIMPLETREEMODEL_H
#define SIMPLETREEMODEL_H
#include "config.h"
#include <QObject>
2018-02-27 18:06:05 +01:00
#include <QAbstractItemModel>
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
class SimpleTreeModel : public QAbstractItemModel {
public:
2020-06-15 21:55:05 +02:00
explicit SimpleTreeModel(T *root = nullptr, QObject *parent = nullptr);
~SimpleTreeModel() override {}
2018-02-27 18:06:05 +01:00
// QAbstractItemModel
2020-06-15 21:55:05 +02:00
int columnCount(const QModelIndex &parent) const override;
QModelIndex index(int row, int, const QModelIndex &parent = QModelIndex()) const override;
2021-01-26 16:48:04 +01:00
QModelIndex parent(const QModelIndex &idx) const override;
2020-06-15 21:55:05 +02:00
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;
2018-02-27 18:06:05 +01:00
2021-01-26 16:48:04 +01:00
T *IndexToItem(const QModelIndex &idx) const;
2018-02-27 18:06:05 +01:00
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:
2021-06-28 00:19:17 +02:00
virtual void LazyPopulate(T *item) { item->lazy_loaded = true; }
2018-02-27 18:06:05 +01:00
protected:
T *root_;
};
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
SimpleTreeModel<T>::SimpleTreeModel(T *root, QObject *parent)
: QAbstractItemModel(parent), root_(root) {}
2022-03-22 21:09:05 +01:00
template<typename T>
2021-01-26 16:48:04 +01:00
T *SimpleTreeModel<T>::IndexToItem(const QModelIndex &idx) const {
if (!idx.isValid()) return root_;
return reinterpret_cast<T*>(idx.internalPointer());
2018-02-27 18:06:05 +01:00
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
QModelIndex SimpleTreeModel<T>::ItemToIndex(T *item) const {
if (!item || !item->parent) return QModelIndex();
return createIndex(item->row, 0, item);
}
template <typename T>
int SimpleTreeModel<T>::columnCount(const QModelIndex&) const {
return 1;
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
QModelIndex SimpleTreeModel<T>::index(int row, int, const QModelIndex &parent) const {
2021-01-26 16:48:04 +01:00
2018-02-27 18:06:05 +01:00
T *parent_item = IndexToItem(parent);
if (!parent_item || row < 0 || parent_item->children.count() <= row)
return QModelIndex();
return ItemToIndex(parent_item->children[row]);
2022-03-22 21:19:59 +01:00
2018-02-27 18:06:05 +01:00
}
2022-03-22 21:09:05 +01:00
template<typename T>
2021-01-26 16:48:04 +01:00
QModelIndex SimpleTreeModel<T>::parent(const QModelIndex &idx) const {
return ItemToIndex(IndexToItem(idx)->parent);
2018-02-27 18:06:05 +01:00
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
int SimpleTreeModel<T>::rowCount(const QModelIndex &parent) const {
T *item = IndexToItem(parent);
return item->children.count();
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
bool SimpleTreeModel<T>::hasChildren(const QModelIndex &parent) const {
T *item = IndexToItem(parent);
if (item->lazy_loaded)
return !item->children.isEmpty();
else
return true;
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
bool SimpleTreeModel<T>::canFetchMore(const QModelIndex &parent) const {
T *item = IndexToItem(parent);
return !item->lazy_loaded;
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
void SimpleTreeModel<T>::fetchMore(const QModelIndex &parent) {
T *item = IndexToItem(parent);
if (!item->lazy_loaded) {
LazyPopulate(item);
}
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
void SimpleTreeModel<T>::BeginInsert(T *parent, int start, int end) {
if (end == -1) end = start;
beginInsertRows(ItemToIndex(parent), start, end);
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
void SimpleTreeModel<T>::EndInsert() {
endInsertRows();
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
void SimpleTreeModel<T>::BeginDelete(T *parent, int start, int end) {
if (end == -1) end = start;
beginRemoveRows(ItemToIndex(parent), start, end);
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
void SimpleTreeModel<T>::EndDelete() {
endRemoveRows();
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
void SimpleTreeModel<T>::EmitDataChanged(T *item) {
QModelIndex index(ItemToIndex(item));
emit dataChanged(index, index);
}
#endif // SIMPLETREEMODEL_H