2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-12-26 16:13:38 +01:00
|
|
|
#ifndef SIMPLETREEMODEL_H
|
|
|
|
#define SIMPLETREEMODEL_H
|
|
|
|
|
|
|
|
#include <QAbstractItemModel>
|
|
|
|
|
|
|
|
class QModelIndex;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class SimpleTreeModel : public QAbstractItemModel {
|
|
|
|
public:
|
|
|
|
SimpleTreeModel(T* root = 0, QObject* parent = 0);
|
|
|
|
virtual ~SimpleTreeModel() {}
|
|
|
|
|
|
|
|
// QAbstractItemModel
|
|
|
|
int columnCount(const QModelIndex& parent) const;
|
2010-05-11 21:03:29 +02:00
|
|
|
QModelIndex index(int row, int, const QModelIndex& parent = QModelIndex()) const;
|
2009-12-26 16:13:38 +01:00
|
|
|
QModelIndex parent(const QModelIndex& index) const;
|
|
|
|
int rowCount(const QModelIndex& parent) const;
|
|
|
|
bool hasChildren(const QModelIndex& parent) const;
|
2010-05-11 20:17:41 +02:00
|
|
|
bool canFetchMore(const QModelIndex& parent) const;
|
|
|
|
void fetchMore(const QModelIndex& parent);
|
2009-12-26 16:13:38 +01:00
|
|
|
|
|
|
|
T* IndexToItem(const QModelIndex& index) const;
|
|
|
|
QModelIndex ItemToIndex(T* item) const;
|
|
|
|
|
2009-12-30 01:31:00 +01:00
|
|
|
// 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();
|
2010-05-28 00:53:07 +02:00
|
|
|
void EmitDataChanged(T* item);
|
2009-12-30 01:31:00 +01:00
|
|
|
|
2009-12-30 00:01:07 +01:00
|
|
|
protected:
|
2009-12-26 16:13:38 +01:00
|
|
|
virtual void LazyPopulate(T* item) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
T* root_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
SimpleTreeModel<T>::SimpleTreeModel(T* root, QObject* parent)
|
|
|
|
: QAbstractItemModel(parent),
|
|
|
|
root_(root)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T* SimpleTreeModel<T>::IndexToItem(const QModelIndex& index) const {
|
|
|
|
if (!index.isValid())
|
|
|
|
return root_;
|
|
|
|
return reinterpret_cast<T*>(index.internalPointer());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
QModelIndex SimpleTreeModel<T>::index(int row, int, const QModelIndex& parent) const {
|
|
|
|
T* parent_item = IndexToItem(parent);
|
2010-03-17 18:16:05 +01:00
|
|
|
if (!parent_item || row < 0 || parent_item->children.count() <= row)
|
2009-12-26 16:13:38 +01:00
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
return ItemToIndex(parent_item->children[row]);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
QModelIndex SimpleTreeModel<T>::parent(const QModelIndex& index) const {
|
|
|
|
return ItemToIndex(IndexToItem(index)->parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
int SimpleTreeModel<T>::rowCount(const QModelIndex & parent) const {
|
|
|
|
T* item = IndexToItem(parent);
|
|
|
|
return item->children.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool SimpleTreeModel<T>::hasChildren(const QModelIndex &parent) const {
|
|
|
|
T* item = IndexToItem(parent);
|
|
|
|
if (item->lazy_loaded)
|
|
|
|
return !item->children.isEmpty();
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-11 20:17:41 +02:00
|
|
|
template <typename T>
|
|
|
|
bool SimpleTreeModel<T>::canFetchMore(const QModelIndex& parent) const {
|
|
|
|
T* item = IndexToItem(parent);
|
|
|
|
return !item->lazy_loaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void SimpleTreeModel<T>::fetchMore(const QModelIndex& parent) {
|
|
|
|
T* item = IndexToItem(parent);
|
|
|
|
if (!item->lazy_loaded) {
|
|
|
|
LazyPopulate(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-30 01:31:00 +01:00
|
|
|
template <typename T>
|
|
|
|
void SimpleTreeModel<T>::BeginInsert(T* parent, int start, int end) {
|
|
|
|
if (end == -1) end = start;
|
|
|
|
beginInsertRows(ItemToIndex(parent), start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void SimpleTreeModel<T>::EndInsert() {
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void SimpleTreeModel<T>::BeginDelete(T* parent, int start, int end) {
|
|
|
|
if (end == -1) end = start;
|
|
|
|
beginRemoveRows(ItemToIndex(parent), start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void SimpleTreeModel<T>::EndDelete() {
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2010-05-28 00:53:07 +02:00
|
|
|
template <typename T>
|
|
|
|
void SimpleTreeModel<T>::EmitDataChanged(T *item) {
|
|
|
|
QModelIndex index(ItemToIndex(item));
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
}
|
|
|
|
|
2009-12-26 16:13:38 +01:00
|
|
|
#endif // SIMPLETREEMODEL_H
|