2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2014-11-02 19:36:21 +01:00
|
|
|
Copyright 2009-2010, David Sansome <davidsansome@gmail.com>
|
|
|
|
Copyright 2010, 2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.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/>.
|
|
|
|
*/
|
|
|
|
|
2014-11-01 19:26:05 +01:00
|
|
|
#ifndef CORE_SIMPLETREEITEM_H_
|
|
|
|
#define CORE_SIMPLETREEITEM_H_
|
2009-12-26 14:49:18 +01:00
|
|
|
|
2009-12-30 01:31:00 +01:00
|
|
|
#include "simpletreemodel.h"
|
|
|
|
|
2009-12-26 14:49:18 +01:00
|
|
|
#include <QString>
|
|
|
|
#include <QList>
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class SimpleTreeItem {
|
|
|
|
public:
|
2014-02-07 16:34:20 +01:00
|
|
|
SimpleTreeItem(int _type, SimpleTreeModel<T>* _model); // For the root item
|
2014-02-21 17:24:49 +01:00
|
|
|
SimpleTreeItem(int _type, const QString& _key, T* _parent = nullptr);
|
2014-11-02 21:37:15 +01:00
|
|
|
explicit SimpleTreeItem(int _type, T* _parent = nullptr);
|
2009-12-26 14:49:18 +01:00
|
|
|
virtual ~SimpleTreeItem();
|
|
|
|
|
2009-12-30 01:31:00 +01:00
|
|
|
void InsertNotify(T* _parent);
|
|
|
|
void DeleteNotify(int child_row);
|
|
|
|
void ClearNotify();
|
2010-05-28 00:53:07 +02:00
|
|
|
void ChangedNotify();
|
2009-12-30 01:31:00 +01:00
|
|
|
|
2009-12-26 14:49:18 +01:00
|
|
|
void Delete(int child_row);
|
|
|
|
T* ChildByKey(const QString& key) const;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString DisplayText() const {
|
|
|
|
return display_text.isNull() ? key : display_text;
|
|
|
|
}
|
2009-12-26 14:49:18 +01:00
|
|
|
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<T*> children;
|
2010-05-09 17:51:04 +02:00
|
|
|
QAbstractItemModel* child_model;
|
2009-12-30 01:31:00 +01:00
|
|
|
|
|
|
|
SimpleTreeModel<T>* model;
|
2009-12-26 14:49:18 +01:00
|
|
|
};
|
|
|
|
|
2009-12-30 01:31:00 +01:00
|
|
|
template <typename T>
|
|
|
|
SimpleTreeItem<T>::SimpleTreeItem(int _type, SimpleTreeModel<T>* _model)
|
2014-02-07 16:34:20 +01:00
|
|
|
: type(_type),
|
|
|
|
row(0),
|
|
|
|
lazy_loaded(true),
|
2014-02-21 17:24:49 +01:00
|
|
|
parent(nullptr),
|
|
|
|
child_model(nullptr),
|
2014-02-07 16:34:20 +01:00
|
|
|
model(_model) {}
|
2009-12-30 01:31:00 +01:00
|
|
|
|
2009-12-26 14:49:18 +01:00
|
|
|
template <typename T>
|
|
|
|
SimpleTreeItem<T>::SimpleTreeItem(int _type, const QString& _key, T* _parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: type(_type),
|
|
|
|
key(_key),
|
|
|
|
lazy_loaded(false),
|
|
|
|
parent(_parent),
|
2014-02-21 17:24:49 +01:00
|
|
|
child_model(nullptr),
|
|
|
|
model(_parent ? _parent->model : nullptr) {
|
2009-12-26 14:49:18 +01:00
|
|
|
if (parent) {
|
|
|
|
row = parent->children.count();
|
|
|
|
parent->children << static_cast<T*>(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-31 02:30:57 +02:00
|
|
|
template <typename T>
|
|
|
|
SimpleTreeItem<T>::SimpleTreeItem(int _type, T* _parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: type(_type),
|
|
|
|
lazy_loaded(false),
|
|
|
|
parent(_parent),
|
2014-02-21 17:24:49 +01:00
|
|
|
child_model(nullptr),
|
|
|
|
model(_parent ? _parent->model : nullptr) {
|
2010-03-31 02:30:57 +02:00
|
|
|
if (parent) {
|
|
|
|
row = parent->children.count();
|
|
|
|
parent->children << static_cast<T*>(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-30 01:31:00 +01:00
|
|
|
template <typename T>
|
|
|
|
void SimpleTreeItem<T>::InsertNotify(T* _parent) {
|
|
|
|
parent = _parent;
|
|
|
|
model = parent->model;
|
|
|
|
row = parent->children.count();
|
|
|
|
|
|
|
|
model->BeginInsert(parent, row);
|
|
|
|
parent->children << static_cast<T*>(this);
|
|
|
|
model->EndInsert();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void SimpleTreeItem<T>::DeleteNotify(int child_row) {
|
|
|
|
model->BeginDelete(static_cast<T*>(this), child_row);
|
|
|
|
delete children.takeAt(child_row);
|
|
|
|
|
|
|
|
// Adjust row numbers of those below it :(
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = child_row; i < children.count(); ++i) children[i]->row--;
|
2009-12-30 01:31:00 +01:00
|
|
|
model->EndDelete();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void SimpleTreeItem<T>::ClearNotify() {
|
2010-07-26 19:32:16 +02:00
|
|
|
if (children.count()) {
|
|
|
|
model->BeginDelete(static_cast<T*>(this), 0, children.count() - 1);
|
2009-12-30 01:31:00 +01:00
|
|
|
|
2010-07-26 19:32:16 +02:00
|
|
|
qDeleteAll(children);
|
|
|
|
children.clear();
|
2009-12-30 01:31:00 +01:00
|
|
|
|
2010-07-26 19:32:16 +02:00
|
|
|
model->EndDelete();
|
|
|
|
}
|
2009-12-30 01:31:00 +01:00
|
|
|
}
|
|
|
|
|
2010-05-28 00:53:07 +02:00
|
|
|
template <typename T>
|
|
|
|
void SimpleTreeItem<T>::ChangedNotify() {
|
|
|
|
model->EmitDataChanged(static_cast<T*>(this));
|
|
|
|
}
|
|
|
|
|
2009-12-26 14:49:18 +01:00
|
|
|
template <typename T>
|
|
|
|
SimpleTreeItem<T>::~SimpleTreeItem() {
|
|
|
|
qDeleteAll(children);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void SimpleTreeItem<T>::Delete(int child_row) {
|
|
|
|
delete children.takeAt(child_row);
|
|
|
|
|
|
|
|
// Adjust row numbers of those below it :(
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = child_row; i < children.count(); ++i) children[i]->row--;
|
2009-12-26 14:49:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T* SimpleTreeItem<T>::ChildByKey(const QString& key) const {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (T* child : children) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (child->key == key) return child;
|
2009-12-26 14:49:18 +01:00
|
|
|
}
|
2014-02-21 17:24:49 +01:00
|
|
|
return nullptr;
|
2009-12-26 14:49:18 +01:00
|
|
|
}
|
|
|
|
|
2014-11-01 19:26:05 +01:00
|
|
|
#endif // CORE_SIMPLETREEITEM_H_
|