From fba4f84fb6882b8d3321e25002a2463547758f9a Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Mon, 9 Jun 2025 02:24:53 +0200 Subject: [PATCH] CollectionModel: Move model reset to regular model updates --- src/collection/collectionmodel.cpp | 41 ++++++++++++-------------- src/collection/collectionmodel.h | 5 ++-- src/collection/collectionmodelupdate.h | 3 +- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/collection/collectionmodel.cpp b/src/collection/collectionmodel.cpp index c193ae9ec..e9b7c96a1 100644 --- a/src/collection/collectionmodel.cpp +++ b/src/collection/collectionmodel.cpp @@ -88,7 +88,6 @@ CollectionModel::CollectionModel(const SharedPtr backend, con albumcover_loader_(albumcover_loader), dir_model_(new CollectionDirectoryModel(backend, this)), filter_(new CollectionFilter(this)), - timer_reload_(new QTimer(this)), timer_update_(new QTimer(this)), icon_artist_(IconLoader::Load(u"folder-sound"_s)), use_disk_cache_(false), @@ -130,10 +129,6 @@ CollectionModel::CollectionModel(const SharedPtr backend, con backend_->UpdateTotalArtistCountAsync(); backend_->UpdateTotalAlbumCountAsync(); - timer_reload_->setSingleShot(true); - timer_reload_->setInterval(300ms); - QObject::connect(timer_reload_, &QTimer::timeout, this, &CollectionModel::Reload); - timer_update_->setSingleShot(false); timer_update_->setInterval(20ms); QObject::connect(timer_update_, &QTimer::timeout, this, &CollectionModel::ProcessUpdate); @@ -191,13 +186,9 @@ void CollectionModel::EndReset() { } -void CollectionModel::Reload() { +void CollectionModel::ResetInternal() { loading_ = true; - if (timer_reload_->isActive()) { - timer_reload_->stop(); - } - updates_.clear(); options_active_ = options_current_; @@ -211,14 +202,6 @@ void CollectionModel::Reload() { } -void CollectionModel::ScheduleReset() { - - if (!timer_reload_->isActive()) { - timer_reload_->start(); - } - -} - void CollectionModel::ReloadSettings() { Settings settings; @@ -421,10 +404,15 @@ void CollectionModel::RemoveSongs(const SongList &songs) { void CollectionModel::ScheduleUpdate(const CollectionModelUpdate::Type type, const SongList &songs) { - for (qint64 i = 0; i < songs.count(); i += 400LL) { - const qint64 number = std::min(songs.count() - i, 400LL); - const SongList songs_to_queue = songs.mid(i, number); - updates_.enqueue(CollectionModelUpdate(type, songs_to_queue)); + if (type == CollectionModelUpdate::Type::Reset) { + updates_.enqueue(CollectionModelUpdate(type)); + } + else { + for (qint64 i = 0; i < songs.count(); i += 400LL) { + const qint64 number = std::min(songs.count() - i, 400LL); + const SongList songs_to_queue = songs.mid(i, number); + updates_.enqueue(CollectionModelUpdate(type, songs_to_queue)); + } } if (!timer_update_->isActive()) { @@ -433,6 +421,12 @@ void CollectionModel::ScheduleUpdate(const CollectionModelUpdate::Type type, con } +void CollectionModel::ScheduleReset() { + + ScheduleUpdate(CollectionModelUpdate::Type::Reset); + +} + void CollectionModel::ScheduleAddSongs(const SongList &songs) { ScheduleUpdate(CollectionModelUpdate::Type::Add, songs); @@ -465,6 +459,9 @@ void CollectionModel::ProcessUpdate() { } switch (update.type) { + case CollectionModelUpdate::Type::Reset: + ResetInternal(); + break; case CollectionModelUpdate::Type::AddReAddOrUpdate: AddReAddOrUpdateSongsInternal(update.songs); break; diff --git a/src/collection/collectionmodel.h b/src/collection/collectionmodel.h index ed65663b3..1242a4fc0 100644 --- a/src/collection/collectionmodel.h +++ b/src/collection/collectionmodel.h @@ -228,7 +228,7 @@ class CollectionModel : public SimpleTreeModel { QVariant data(CollectionItem *item, const int role) const; - void ScheduleUpdate(const CollectionModelUpdate::Type type, const SongList &songs); + void ScheduleUpdate(const CollectionModelUpdate::Type type, const SongList &songs = SongList()); void ScheduleAddSongs(const SongList &songs); void ScheduleUpdateSongs(const SongList &songs); void ScheduleRemoveSongs(const SongList &songs); @@ -259,7 +259,7 @@ class CollectionModel : public SimpleTreeModel { static qint64 MaximumCacheSize(Settings *s, const char *size_id, const char *size_unit_id, const qint64 cache_size_default); private Q_SLOTS: - void Reload(); + void ResetInternal(); void ScheduleReset(); void ProcessUpdate(); void LoadSongsFromSqlAsyncFinished(); @@ -278,7 +278,6 @@ class CollectionModel : public SimpleTreeModel { const SharedPtr albumcover_loader_; CollectionDirectoryModel *dir_model_; CollectionFilter *filter_; - QTimer *timer_reload_; QTimer *timer_update_; QPixmap pixmap_no_cover_; diff --git a/src/collection/collectionmodelupdate.h b/src/collection/collectionmodelupdate.h index 721dc7d8b..c4d7efce9 100644 --- a/src/collection/collectionmodelupdate.h +++ b/src/collection/collectionmodelupdate.h @@ -25,12 +25,13 @@ class CollectionModelUpdate { public: enum class Type { + Reset, AddReAddOrUpdate, Add, Update, Remove, }; - explicit CollectionModelUpdate(const Type _type, const SongList &_songs); + explicit CollectionModelUpdate(const Type _type, const SongList &_songs = SongList()); Type type; SongList songs; };