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

163 lines
4.1 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 SIMPLETREEITEM_H
#define SIMPLETREEITEM_H
#include "config.h"
#include <QList>
#include <QString>
#include <QAbstractItemModel>
#include "simpletreemodel.h"
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
class SimpleTreeItem {
public:
explicit SimpleTreeItem(int _type, SimpleTreeModel<T> *_model); // For the root item
explicit SimpleTreeItem(int _type, const QString &_key, T *_parent = nullptr);
explicit SimpleTreeItem(int _type, T *_parent = nullptr);
2018-02-27 18:06:05 +01:00
virtual ~SimpleTreeItem();
void InsertNotify(T *_parent);
2018-02-27 18:06:05 +01:00
void DeleteNotify(int child_row);
void ClearNotify();
void ChangedNotify();
void Delete(int child_row);
2021-06-12 20:53:23 +02:00
T *ChildByKey(const QString &key) const;
2018-02-27 18:06:05 +01:00
QString DisplayText() const { return display_text; }
QString SortText() const { return sort_text; }
2018-02-27 18:06:05 +01:00
int type;
QString key;
QString sort_text;
QString display_text;
int row;
bool lazy_loaded;
2021-06-12 20:53:23 +02:00
T *parent;
2022-03-22 21:19:59 +01:00
QList<T*> children;
2021-06-12 20:53:23 +02:00
QAbstractItemModel *child_model;
2018-02-27 18:06:05 +01:00
2021-06-12 20:53:23 +02:00
SimpleTreeModel<T> *model;
2018-02-27 18:06:05 +01:00
};
2022-03-22 21:09:05 +01:00
template<typename T>
SimpleTreeItem<T>::SimpleTreeItem(int _type, SimpleTreeModel<T> *_model)
2018-02-27 18:06:05 +01:00
: type(_type),
row(0),
lazy_loaded(true),
parent(nullptr),
child_model(nullptr),
model(_model) {}
2022-03-22 21:09:05 +01:00
template<typename T>
SimpleTreeItem<T>::SimpleTreeItem(int _type, const QString &_key, T *_parent)
2018-02-27 18:06:05 +01:00
: type(_type),
key(_key),
lazy_loaded(false),
parent(_parent),
child_model(nullptr),
model(_parent ? _parent->model : nullptr) {
if (parent) {
row = parent->children.count();
parent->children << static_cast<T*>(this);
}
}
2022-03-22 21:09:05 +01:00
template<typename T>
SimpleTreeItem<T>::SimpleTreeItem(int _type, T *_parent)
2018-02-27 18:06:05 +01:00
: type(_type),
lazy_loaded(false),
parent(_parent),
child_model(nullptr),
model(_parent ? _parent->model : nullptr) {
if (parent) {
row = parent->children.count();
parent->children << static_cast<T*>(this);
}
}
2022-03-22 21:09:05 +01:00
template<typename T>
void SimpleTreeItem<T>::InsertNotify(T *_parent) {
2018-02-27 18:06:05 +01:00
parent = _parent;
model = parent->model;
row = parent->children.count();
model->BeginInsert(parent, row);
parent->children << static_cast<T*>(this);
model->EndInsert();
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
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 :(
for (int i = child_row; i < children.count(); ++i) children[i]->row--;
model->EndDelete();
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
void SimpleTreeItem<T>::ClearNotify() {
if (children.count()) {
model->BeginDelete(static_cast<T*>(this), 0, children.count() - 1);
qDeleteAll(children);
children.clear();
model->EndDelete();
}
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
void SimpleTreeItem<T>::ChangedNotify() {
model->EmitDataChanged(static_cast<T*>(this));
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
SimpleTreeItem<T>::~SimpleTreeItem() {
qDeleteAll(children);
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
void SimpleTreeItem<T>::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--;
}
2022-03-22 21:09:05 +01:00
template<typename T>
T *SimpleTreeItem<T>::ChildByKey(const QString &_key) const {
for (T *child : children) {
2020-04-23 21:08:28 +02:00
if (child->key == _key) return child;
2018-02-27 18:06:05 +01:00
}
return nullptr;
}
#endif // SIMPLETREEITEM_H