/* This file is part of Clementine. Copyright 2010, David Sansome 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 . */ #ifndef SIMPLETREEITEM_H #define SIMPLETREEITEM_H #include "simpletreemodel.h" #include #include template class SimpleTreeItem { public: SimpleTreeItem(int _type, SimpleTreeModel* _model); // For the root item SimpleTreeItem(int _type, const QString& _key, T* _parent = NULL); SimpleTreeItem(int _type, T* _parent = NULL); virtual ~SimpleTreeItem(); void InsertNotify(T* _parent); void DeleteNotify(int child_row); void ClearNotify(); void ChangedNotify(); void Delete(int child_row); T* ChildByKey(const QString& key) const; QString DisplayText() const { return display_text.isNull() ? key : display_text; } QString SortText() const { return sort_text.isNull() ? key : sort_text; } int type; QString key; QString sort_text; QString display_text; int row; bool lazy_loaded; T* parent; QList children; QAbstractItemModel* child_model; SimpleTreeModel* model; }; template SimpleTreeItem::SimpleTreeItem(int _type, SimpleTreeModel* _model) : type(_type), row(0), lazy_loaded(true), parent(NULL), child_model(NULL), model(_model) {} template SimpleTreeItem::SimpleTreeItem(int _type, const QString& _key, T* _parent) : type(_type), key(_key), lazy_loaded(false), parent(_parent), child_model(NULL), model(_parent ? _parent->model : NULL) { if (parent) { row = parent->children.count(); parent->children << static_cast(this); } } template SimpleTreeItem::SimpleTreeItem(int _type, T* _parent) : type(_type), lazy_loaded(false), parent(_parent), child_model(NULL), model(_parent ? _parent->model : NULL) { if (parent) { row = parent->children.count(); parent->children << static_cast(this); } } template void SimpleTreeItem::InsertNotify(T* _parent) { parent = _parent; model = parent->model; row = parent->children.count(); model->BeginInsert(parent, row); parent->children << static_cast(this); model->EndInsert(); } template void SimpleTreeItem::DeleteNotify(int child_row) { model->BeginDelete(static_cast(this), child_row); delete children.takeAt(child_row); // Adjust row numbers of those below it :( for (int i = child_row; i < children.count(); ++i) children[i]->row--; model->EndDelete(); } template void SimpleTreeItem::ClearNotify() { if (children.count()) { model->BeginDelete(static_cast(this), 0, children.count() - 1); qDeleteAll(children); children.clear(); model->EndDelete(); } } template void SimpleTreeItem::ChangedNotify() { model->EmitDataChanged(static_cast(this)); } template SimpleTreeItem::~SimpleTreeItem() { qDeleteAll(children); } template void SimpleTreeItem::Delete(int child_row) { delete children.takeAt(child_row); // Adjust row numbers of those below it :( for (int i = child_row; i < children.count(); ++i) children[i]->row--; } template T* SimpleTreeItem::ChildByKey(const QString& key) const { for (T* child : children) { if (child->key == key) return child; } return NULL; } #endif // SIMPLETREEITEM_H