CollectionModel: Reset model before deletion

This commit is contained in:
Jonas Kvinge 2024-04-03 21:16:28 +02:00
parent f9acfbc224
commit 4c1a5168f0
2 changed files with 34 additions and 4 deletions

View File

@ -142,7 +142,9 @@ CollectionModel::~CollectionModel() {
qLog(Debug) << "Collection model" << this << "for" << Song::TextForSource(backend_->source()) << "deleted";
delete root_;
beginResetModel();
Clear();
endResetModel();
}
@ -199,6 +201,8 @@ void CollectionModel::ReloadSettings() {
void CollectionModel::Init(const bool async) {
if (!root_) return;
if (async) {
// Show a loading indicator in the model.
CollectionItem *loading = new CollectionItem(CollectionItem::Type_LoadingIndicator, root_);
@ -222,6 +226,8 @@ void CollectionModel::Init(const bool async) {
void CollectionModel::SongsDiscovered(const SongList &songs) {
if (!root_) return;
for (const Song &song : songs) {
// Sanity check to make sure we don't add songs that are outside the user's filter
@ -519,6 +525,8 @@ QString CollectionModel::DividerDisplayText(const GroupBy group_by, const QStrin
void CollectionModel::SongsDeleted(const SongList &songs) {
if (!root_) return;
// Delete the actual song nodes first, keeping track of each parent so we might check to see if they're empty later.
QSet<CollectionItem*> parents;
for (const Song &song : songs) {
@ -923,6 +931,8 @@ CollectionModel::QueryResult CollectionModel::RunQuery(const CollectionFilterOpt
void CollectionModel::PostQuery(CollectionItem *parent, const CollectionModel::QueryResult &result, const bool signal) {
if (!root_) return;
// Information about what we want the children to be
int child_level = parent == root_ ? 0 : parent->container_level + 1;
GroupBy child_group_by = child_level >= 3 ? GroupBy::None : group_by_[child_level];
@ -949,6 +959,8 @@ void CollectionModel::PostQuery(CollectionItem *parent, const CollectionModel::Q
void CollectionModel::LazyPopulate(CollectionItem *parent, const bool signal) {
if (!root_) return;
if (parent->lazy_loaded) return;
parent->lazy_loaded = true;
@ -960,6 +972,8 @@ void CollectionModel::LazyPopulate(CollectionItem *parent, const bool signal) {
void CollectionModel::ResetAsync() {
if (!root_) return;
CollectionQueryOptions query_options = PrepareQuery(root_);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
@ -975,6 +989,8 @@ void CollectionModel::ResetAsync() {
void CollectionModel::ResetAsyncQueryFinished() {
if (!root_) return;
QFutureWatcher<CollectionModel::QueryResult> *watcher = static_cast<QFutureWatcher<CollectionModel::QueryResult>*>(sender());
const struct QueryResult result = watcher->result();
watcher->deleteLater();
@ -995,10 +1011,12 @@ void CollectionModel::ResetAsyncQueryFinished() {
}
void CollectionModel::BeginReset() {
void CollectionModel::Clear() {
beginResetModel();
delete root_;
if (root_) {
delete root_;
root_ = nullptr;
}
song_nodes_.clear();
container_nodes_[0].clear();
container_nodes_[1].clear();
@ -1007,6 +1025,13 @@ void CollectionModel::BeginReset() {
pending_art_.clear();
pending_cache_keys_.clear();
}
void CollectionModel::BeginReset() {
beginResetModel();
Clear();
root_ = new CollectionItem(this);
root_->compilation_artist_node_ = nullptr;
root_->lazy_loaded = false;
@ -1641,6 +1666,8 @@ CollectionItem *CollectionModel::ItemFromSong(const GroupBy group_by, const bool
void CollectionModel::FinishItem(const GroupBy group_by, const bool signal, const bool create_divider, CollectionItem *parent, CollectionItem *item) {
if (!root_) return;
if (group_by == GroupBy::None) item->lazy_loaded = true;
if (signal) {
@ -1982,6 +2009,8 @@ void CollectionModel::ClearDiskCache() {
void CollectionModel::ExpandAll(CollectionItem *item) const {
if (!root_) return;
if (!item) item = root_;
const_cast<CollectionModel*>(this)->LazyPopulate(const_cast<CollectionItem*>(item), false);
for (CollectionItem *child : item->children) {

View File

@ -245,6 +245,7 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
bool HasCompilations(const QSqlDatabase &db, const CollectionFilterOptions &filter_options, const CollectionQueryOptions &query_options);
void Clear();
void BeginReset();
// Functions for working with queries and creating items.