From 9b2a5b15351fe402246514c8905abb1a38d09dad Mon Sep 17 00:00:00 2001 From: David Sansome Date: Sat, 26 Dec 2009 13:49:18 +0000 Subject: [PATCH] Move some bits out of LibraryItem --- src/library.cpp | 5 ++++ src/libraryitem.cpp | 35 ---------------------- src/libraryitem.h | 23 ++++----------- src/simpletreeitem.h | 69 ++++++++++++++++++++++++++++++++++++++++++++ src/src.pro | 4 +-- 5 files changed, 81 insertions(+), 55 deletions(-) delete mode 100644 src/libraryitem.cpp create mode 100644 src/simpletreeitem.h diff --git a/src/library.cpp b/src/library.cpp index 07cf3b8fe..c1af818d5 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -20,6 +20,8 @@ Library::Library(EngineBase* engine, QObject* parent) album_icon_(":album.png"), config_(new LibraryConfig) { + root_->lazy_loaded = true; + connect(backend_, SIGNAL(Initialised()), SLOT(BackendInitialised())); connect(watcher_, SIGNAL(Initialised()), SLOT(WatcherInitialised())); waiting_for_threads_ = 2; @@ -143,6 +145,7 @@ LibraryItem* Library::CreateArtistNode(bool signal, const QString& name) { LibraryItem* divider = new LibraryItem(LibraryItem::Type_Divider, QString(divider_char), root_); + divider->lazy_loaded = true; if (divider_char.isDigit()) 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()); LibraryItem* ret = new LibraryItem(LibraryItem::Type_Song, song.title(), parent); + ret->lazy_loaded = true; ret->display_text = song.PrettyTitleWithArtist(); ret->song = song; @@ -398,6 +402,7 @@ void Library::Reset() { compilation_artist_node_ = NULL; root_ = new LibraryItem(LibraryItem::Type_Root); + root_->lazy_loaded = true; // Various artists? if (backend_->Worker()->HasCompilations(query_options_)) diff --git a/src/libraryitem.cpp b/src/libraryitem.cpp deleted file mode 100644 index a0e1358ad..000000000 --- a/src/libraryitem.cpp +++ /dev/null @@ -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 ; irow --; -} - -LibraryItem* LibraryItem::ChildByKey(const QString& key) const { - foreach (LibraryItem* child, children) { - if (child->key == key) - return child; - } - return NULL; -} diff --git a/src/libraryitem.h b/src/libraryitem.h index 6767a99a9..aba92eb21 100644 --- a/src/libraryitem.h +++ b/src/libraryitem.h @@ -5,8 +5,10 @@ #include #include "song.h" +#include "simpletreeitem.h" -struct LibraryItem { +class LibraryItem : public SimpleTreeItem { + public: enum Type { Type_Root, Type_Divider, @@ -17,25 +19,10 @@ struct LibraryItem { Type_Song, }; - LibraryItem(Type _type, const QString& _key = QString::null, LibraryItem* _parent = NULL); - ~LibraryItem(); + LibraryItem(Type type, const QString& key = QString::null, LibraryItem* parent = NULL) + : SimpleTreeItem(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; - - LibraryItem* parent; - QList children; }; #endif // LIBRARYITEM_H diff --git a/src/simpletreeitem.h b/src/simpletreeitem.h new file mode 100644 index 000000000..e9fc55117 --- /dev/null +++ b/src/simpletreeitem.h @@ -0,0 +1,69 @@ +#ifndef SIMPLETREEITEM_H +#define SIMPLETREEITEM_H + +#include +#include + +template +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 children; +}; + +template +SimpleTreeItem::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(this); + } else { + row = 0; + } +} + +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 ; irow --; +} + +template +T* SimpleTreeItem::ChildByKey(const QString& key) const { + foreach (T* child, children) { + if (child->key == key) + return child; + } + return NULL; +} + +#endif // SIMPLETREEITEM_H diff --git a/src/src.pro b/src/src.pro index 66ce74cd8..4b8bea9d6 100644 --- a/src/src.pro +++ b/src/src.pro @@ -27,7 +27,6 @@ SOURCES += main.cpp \ songmimedata.cpp \ songplaylistitem.cpp \ libraryview.cpp \ - libraryitem.cpp \ libraryconfig.cpp \ systemtrayicon.cpp \ libraryquery.cpp \ @@ -63,7 +62,8 @@ HEADERS += mainwindow.h \ libraryquery.h \ fileview.h \ fileviewlist.h \ - playlistheader.h + playlistheader.h \ + simpletreeitem.h FORMS += mainwindow.ui \ libraryconfig.ui \ fileview.ui