Move some bits out of LibraryItem

This commit is contained in:
David Sansome 2009-12-26 13:49:18 +00:00
parent 1d9270bba5
commit 9b2a5b1535
5 changed files with 81 additions and 55 deletions

View File

@ -20,6 +20,8 @@ Library::Library(EngineBase* engine, QObject* parent)
album_icon_(":album.png"), album_icon_(":album.png"),
config_(new LibraryConfig) config_(new LibraryConfig)
{ {
root_->lazy_loaded = true;
connect(backend_, SIGNAL(Initialised()), SLOT(BackendInitialised())); connect(backend_, SIGNAL(Initialised()), SLOT(BackendInitialised()));
connect(watcher_, SIGNAL(Initialised()), SLOT(WatcherInitialised())); connect(watcher_, SIGNAL(Initialised()), SLOT(WatcherInitialised()));
waiting_for_threads_ = 2; waiting_for_threads_ = 2;
@ -143,6 +145,7 @@ LibraryItem* Library::CreateArtistNode(bool signal, const QString& name) {
LibraryItem* divider = LibraryItem* divider =
new LibraryItem(LibraryItem::Type_Divider, QString(divider_char), root_); new LibraryItem(LibraryItem::Type_Divider, QString(divider_char), root_);
divider->lazy_loaded = true;
if (divider_char.isDigit()) if (divider_char.isDigit())
divider->display_text = "0-9"; divider->display_text = "0-9";
@ -180,6 +183,7 @@ LibraryItem* Library::CreateSongNode(bool signal, const Song& song, LibraryItem*
beginInsertRows(ItemToIndex(parent), parent->children.count(), parent->children.count()); beginInsertRows(ItemToIndex(parent), parent->children.count(), parent->children.count());
LibraryItem* ret = new LibraryItem(LibraryItem::Type_Song, song.title(), parent); LibraryItem* ret = new LibraryItem(LibraryItem::Type_Song, song.title(), parent);
ret->lazy_loaded = true;
ret->display_text = song.PrettyTitleWithArtist(); ret->display_text = song.PrettyTitleWithArtist();
ret->song = song; ret->song = song;
@ -398,6 +402,7 @@ void Library::Reset() {
compilation_artist_node_ = NULL; compilation_artist_node_ = NULL;
root_ = new LibraryItem(LibraryItem::Type_Root); root_ = new LibraryItem(LibraryItem::Type_Root);
root_->lazy_loaded = true;
// Various artists? // Various artists?
if (backend_->Worker()->HasCompilations(query_options_)) if (backend_->Worker()->HasCompilations(query_options_))

View File

@ -1,35 +0,0 @@
#include "libraryitem.h"
LibraryItem::LibraryItem(Type _type, const QString& _key, LibraryItem* _parent)
: type(_type),
key(_key),
lazy_loaded(_type == Type_Song || _type == Type_Root || _type == Type_Divider),
parent(_parent)
{
if (parent) {
row = parent->children.count();
parent->children << this;
} else {
row = 0;
}
}
LibraryItem::~LibraryItem() {
qDeleteAll(children);
}
void LibraryItem::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 --;
}
LibraryItem* LibraryItem::ChildByKey(const QString& key) const {
foreach (LibraryItem* child, children) {
if (child->key == key)
return child;
}
return NULL;
}

View File

@ -5,8 +5,10 @@
#include <QList> #include <QList>
#include "song.h" #include "song.h"
#include "simpletreeitem.h"
struct LibraryItem { class LibraryItem : public SimpleTreeItem<LibraryItem> {
public:
enum Type { enum Type {
Type_Root, Type_Root,
Type_Divider, Type_Divider,
@ -17,25 +19,10 @@ struct LibraryItem {
Type_Song, Type_Song,
}; };
LibraryItem(Type _type, const QString& _key = QString::null, LibraryItem* _parent = NULL); LibraryItem(Type type, const QString& key = QString::null, LibraryItem* parent = NULL)
~LibraryItem(); : SimpleTreeItem<LibraryItem>(type, key, parent) {}
void Delete(int child_row);
LibraryItem* 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; }
Type type;
QString key;
QString sort_text;
QString display_text;
int row;
bool lazy_loaded;
Song song; Song song;
LibraryItem* parent;
QList<LibraryItem*> children;
}; };
#endif // LIBRARYITEM_H #endif // LIBRARYITEM_H

69
src/simpletreeitem.h Normal file
View File

@ -0,0 +1,69 @@
#ifndef SIMPLETREEITEM_H
#define SIMPLETREEITEM_H
#include <QString>
#include <QList>
template <typename T>
class SimpleTreeItem {
public:
SimpleTreeItem(int _type, const QString& _key = QString::null, T* _parent = NULL);
virtual ~SimpleTreeItem();
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<T*> children;
};
template <typename T>
SimpleTreeItem<T>::SimpleTreeItem(int _type, const QString& _key, T* _parent)
: type(_type),
key(_key),
lazy_loaded(false),
parent(_parent)
{
if (parent) {
row = parent->children.count();
parent->children << static_cast<T*>(this);
} else {
row = 0;
}
}
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 :(
for (int i=child_row ; i<children.count() ; ++i)
children[i]->row --;
}
template <typename T>
T* SimpleTreeItem<T>::ChildByKey(const QString& key) const {
foreach (T* child, children) {
if (child->key == key)
return child;
}
return NULL;
}
#endif // SIMPLETREEITEM_H

View File

@ -27,7 +27,6 @@ SOURCES += main.cpp \
songmimedata.cpp \ songmimedata.cpp \
songplaylistitem.cpp \ songplaylistitem.cpp \
libraryview.cpp \ libraryview.cpp \
libraryitem.cpp \
libraryconfig.cpp \ libraryconfig.cpp \
systemtrayicon.cpp \ systemtrayicon.cpp \
libraryquery.cpp \ libraryquery.cpp \
@ -63,7 +62,8 @@ HEADERS += mainwindow.h \
libraryquery.h \ libraryquery.h \
fileview.h \ fileview.h \
fileviewlist.h \ fileviewlist.h \
playlistheader.h playlistheader.h \
simpletreeitem.h
FORMS += mainwindow.ui \ FORMS += mainwindow.ui \
libraryconfig.ui \ libraryconfig.ui \
fileview.ui fileview.ui