From 8699790e784bcc27718eb691e267e94e3db82f9b Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Wed, 16 Jun 2021 00:30:21 +0200 Subject: [PATCH] Always call QFutureWatcher::setFuture after connects --- src/collection/collectionmodel.cpp | 2 +- src/covermanager/albumcoverchoicecontroller.cpp | 4 ++-- src/device/deviceproperties.cpp | 2 +- src/dialogs/edittagdialog.cpp | 6 +++--- src/dialogs/trackselectiondialog.cpp | 2 +- src/engine/gstengine.cpp | 2 +- src/moodbar/moodbaritemdelegate.cpp | 4 ++-- src/musicbrainz/tagfetcher.cpp | 2 +- src/organize/organizedialog.cpp | 2 +- src/playlist/playlist.cpp | 4 ++-- src/playlist/playlistdelegates.cpp | 2 +- src/playlist/playlistmanager.cpp | 2 +- src/smartplaylists/playlistgeneratorinserter.cpp | 2 +- .../smartplaylistsearchpreview.cpp | 2 +- tests/src/concurrentrun_test.cpp | 16 ++++++++-------- 15 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/collection/collectionmodel.cpp b/src/collection/collectionmodel.cpp index 36f7746f..d02a6aba 100644 --- a/src/collection/collectionmodel.cpp +++ b/src/collection/collectionmodel.cpp @@ -930,8 +930,8 @@ void CollectionModel::ResetAsync() { QFuture future = QtConcurrent::run(this, &CollectionModel::RunQuery, root_); #endif QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, &CollectionModel::ResetAsyncQueryFinished); + watcher->setFuture(future); } diff --git a/src/covermanager/albumcoverchoicecontroller.cpp b/src/covermanager/albumcoverchoicecontroller.cpp index 139301e2..58ba2024 100644 --- a/src/covermanager/albumcoverchoicecontroller.cpp +++ b/src/covermanager/albumcoverchoicecontroller.cpp @@ -617,7 +617,6 @@ void AlbumCoverChoiceController::SaveCoverEmbeddedAutomatic(const Song &song, co QFuture future = QtConcurrent::run(app_->collection_backend(), &CollectionBackend::GetAlbumSongs, song.effective_albumartist(), song.effective_album(), QueryOptions()); #endif QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, [=]() { SongList songs = watcher->result(); watcher->deleteLater(); @@ -634,6 +633,7 @@ void AlbumCoverChoiceController::SaveCoverEmbeddedAutomatic(const Song &song, co cover_save_tasks_.insert(id, song); } }); + watcher->setFuture(future); } else { if (result.is_jpeg()) { @@ -661,7 +661,6 @@ void AlbumCoverChoiceController::SaveCoverEmbeddedAutomatic(const Song &song, co QFuture future = QtConcurrent::run(app_->collection_backend(), &CollectionBackend::GetAlbumSongs, song.effective_albumartist(), song.effective_album(), QueryOptions()); #endif QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, [=]() { SongList songs = watcher->result(); watcher->deleteLater(); @@ -671,6 +670,7 @@ void AlbumCoverChoiceController::SaveCoverEmbeddedAutomatic(const Song &song, co QMutexLocker l(&mutex_cover_save_tasks_); cover_save_tasks_.insert(id, song); }); + watcher->setFuture(future); } else { app_->album_cover_loader()->SaveEmbeddedCoverAsync(song.url().toLocalFile(), cover_filename); diff --git a/src/device/deviceproperties.cpp b/src/device/deviceproperties.cpp index c56c71fc..19b737a8 100644 --- a/src/device/deviceproperties.cpp +++ b/src/device/deviceproperties.cpp @@ -249,8 +249,8 @@ void DeviceProperties::UpdateFormats() { QFuture future = QtConcurrent::run(std::bind(&ConnectedDevice::GetSupportedFiletypes, device, &supported_formats_)); #endif QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, &DeviceProperties::UpdateFormatsFinished); + watcher->setFuture(future); ui_->formats_stack->setCurrentWidget(ui_->formats_page_loading); updating_formats_ = true; diff --git a/src/dialogs/edittagdialog.cpp b/src/dialogs/edittagdialog.cpp index 996e64fb..f0bc0db9 100644 --- a/src/dialogs/edittagdialog.cpp +++ b/src/dialogs/edittagdialog.cpp @@ -415,8 +415,8 @@ void EditTagDialog::SetSongs(const SongList &s, const PlaylistItemList &items) { QFuture> future = QtConcurrent::run(this, &EditTagDialog::LoadData, s); #endif QFutureWatcher> *watcher = new QFutureWatcher>(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher>::finished, this, &EditTagDialog::SetSongsFinished); + watcher->setFuture(future); } @@ -1178,7 +1178,6 @@ void EditTagDialog::SaveData() { ++save_art_pending_; QFuture future = QtConcurrent::run(&ImageUtils::SaveImageToJpegData, ref.cover_result_.image); QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, [=]() { TagReaderReply *reply = TagReaderClient::Instance()->SaveEmbeddedArt(ref.current_.url().toLocalFile(), watcher->result()); QObject::connect(reply, &TagReaderReply::Finished, this, [this, reply, ref]() { @@ -1186,12 +1185,12 @@ void EditTagDialog::SaveData() { }, Qt::QueuedConnection); watcher->deleteLater(); }); + watcher->setFuture(future); } else if (!embedded_cover_from_file.isEmpty()) { // Save existing file on disk as embedded cover. ++save_art_pending_; QFuture future = QtConcurrent::run(&ImageUtils::FileToJpegData, embedded_cover_from_file); QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, [=]() { TagReaderReply *reply = TagReaderClient::Instance()->SaveEmbeddedArt(ref.current_.url().toLocalFile(), watcher->result()); QObject::connect(reply, &TagReaderReply::Finished, this, [this, reply, ref]() { @@ -1199,6 +1198,7 @@ void EditTagDialog::SaveData() { }, Qt::QueuedConnection); watcher->deleteLater(); }); + watcher->setFuture(future); } } else if (ref.cover_action_ == UpdateCoverAction_Delete) { diff --git a/src/dialogs/trackselectiondialog.cpp b/src/dialogs/trackselectiondialog.cpp index dda72f1c..f5a09768 100644 --- a/src/dialogs/trackselectiondialog.cpp +++ b/src/dialogs/trackselectiondialog.cpp @@ -293,8 +293,8 @@ void TrackSelectionDialog::accept() { // Save tags in the background QFuture future = QtConcurrent::run(&TrackSelectionDialog::SaveData, data_); QFutureWatcher *watcher = new QFutureWatcher(this); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, &TrackSelectionDialog::AcceptFinished); + watcher->setFuture(future); return; } diff --git a/src/engine/gstengine.cpp b/src/engine/gstengine.cpp index 3595cc24..0eb89d6e 100644 --- a/src/engine/gstengine.cpp +++ b/src/engine/gstengine.cpp @@ -237,12 +237,12 @@ bool GstEngine::Play(const quint64 offset_nanosec) { QFuture future = current_pipeline_->SetState(GST_STATE_PLAYING); QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); int pipeline_id = current_pipeline_->id(); QObject::connect(watcher, &QFutureWatcher::finished, this, [this, watcher, offset_nanosec, pipeline_id]() { PlayDone(watcher->result(), offset_nanosec, pipeline_id); watcher->deleteLater(); }); + watcher->setFuture(future); if (is_fading_out_to_pause_) { current_pipeline_->SetState(GST_STATE_PAUSED); diff --git a/src/moodbar/moodbaritemdelegate.cpp b/src/moodbar/moodbaritemdelegate.cpp index a6492187..f5dc6dad 100644 --- a/src/moodbar/moodbaritemdelegate.cpp +++ b/src/moodbar/moodbaritemdelegate.cpp @@ -216,11 +216,11 @@ void MoodbarItemDelegate::StartLoadingColors(const QUrl &url, const QByteArray & QFuture future = QtConcurrent::run(MoodbarRenderer::Colors, bytes, style_, qApp->palette()); QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, [this, watcher, url]() { ColorsLoaded(url, watcher->result()); watcher->deleteLater(); }); + watcher->setFuture(future); } @@ -247,11 +247,11 @@ void MoodbarItemDelegate::StartLoadingImage(const QUrl &url, Data *data) { QFuture future = QtConcurrent::run(MoodbarRenderer::RenderToImage, data->colors_, data->desired_size_); QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, [this, watcher, url]() { ImageLoaded(url, watcher->result()); watcher->deleteLater(); }); + watcher->setFuture(future); } diff --git a/src/musicbrainz/tagfetcher.cpp b/src/musicbrainz/tagfetcher.cpp index cfa79693..c4f85401 100644 --- a/src/musicbrainz/tagfetcher.cpp +++ b/src/musicbrainz/tagfetcher.cpp @@ -74,8 +74,8 @@ void TagFetcher::StartFetch(const SongList &songs) { else { QFuture future = QtConcurrent::mapped(songs_, GetFingerprint); fingerprint_watcher_ = new QFutureWatcher(this); - fingerprint_watcher_->setFuture(future); QObject::connect(fingerprint_watcher_, &QFutureWatcher::resultReadyAt, this, &TagFetcher::FingerprintFound); + fingerprint_watcher_->setFuture(future); for (const Song &song : songs_) { emit Progress(song, tr("Fingerprinting song")); } diff --git a/src/organize/organizedialog.cpp b/src/organize/organizedialog.cpp index 19d051c8..51520ef6 100644 --- a/src/organize/organizedialog.cpp +++ b/src/organize/organizedialog.cpp @@ -396,11 +396,11 @@ bool OrganizeDialog::SetFilenames(const QStringList &filenames) { songs_future_ = QtConcurrent::run(this, &OrganizeDialog::LoadSongsBlocking, filenames); #endif QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(songs_future_); QObject::connect(watcher, &QFutureWatcher::finished, this, [this, watcher]() { SetSongs(watcher->result()); watcher->deleteLater(); }); + watcher->setFuture(songs_future_); SetLoadingSongs(true); return true; diff --git a/src/playlist/playlist.cpp b/src/playlist/playlist.cpp index 855ba90f..6bee6830 100644 --- a/src/playlist/playlist.cpp +++ b/src/playlist/playlist.cpp @@ -431,11 +431,11 @@ void Playlist::ItemReload(const QPersistentModelIndex &idx, const Song &old_meta if (item) { QFuture future = item->BackgroundReload(); QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, [this, watcher, idx, old_metadata, metadata_edit]() { ItemReloadComplete(idx, old_metadata, metadata_edit); watcher->deleteLater(); }); + watcher->setFuture(future); } } @@ -1471,8 +1471,8 @@ void Playlist::Restore() { QFuture future = QtConcurrent::run(backend_, &PlaylistBackend::GetPlaylistItems, id_); #endif QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, &Playlist::ItemsLoaded); + watcher->setFuture(future); } diff --git a/src/playlist/playlistdelegates.cpp b/src/playlist/playlistdelegates.cpp index e67160f4..a896f53a 100644 --- a/src/playlist/playlistdelegates.cpp +++ b/src/playlist/playlistdelegates.cpp @@ -404,8 +404,8 @@ TagCompleter::TagCompleter(CollectionBackend *backend, Playlist::Column column, QFuture future = QtConcurrent::run(&InitCompletionModel, backend, column); QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, &TagCompleter::ModelReady); + watcher->setFuture(future); } diff --git a/src/playlist/playlistmanager.cpp b/src/playlist/playlistmanager.cpp index dc143d8f..a47b3891 100644 --- a/src/playlist/playlistmanager.cpp +++ b/src/playlist/playlistmanager.cpp @@ -226,11 +226,11 @@ void PlaylistManager::Save(const int id, const QString &filename, const Playlist QFuture future = QtConcurrent::run(playlist_backend_, &PlaylistBackend::GetPlaylistSongs, id); #endif QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, [this, watcher, filename, path_type]() { ItemsLoadedForSavePlaylist(watcher->result(), filename, path_type); watcher->deleteLater(); }); + watcher->setFuture(future); } } diff --git a/src/smartplaylists/playlistgeneratorinserter.cpp b/src/smartplaylists/playlistgeneratorinserter.cpp index e3397bc4..2046b798 100644 --- a/src/smartplaylists/playlistgeneratorinserter.cpp +++ b/src/smartplaylists/playlistgeneratorinserter.cpp @@ -69,8 +69,8 @@ void PlaylistGeneratorInserter::Load(Playlist *destination, const int row, const QFuture future = QtConcurrent::run(PlaylistGeneratorInserter::Generate, generator, dynamic_count); QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, &PlaylistGeneratorInserter::Finished); + watcher->setFuture(future); } diff --git a/src/smartplaylists/smartplaylistsearchpreview.cpp b/src/smartplaylists/smartplaylistsearchpreview.cpp index 3cc51451..3e87b06f 100644 --- a/src/smartplaylists/smartplaylistsearchpreview.cpp +++ b/src/smartplaylists/smartplaylistsearchpreview.cpp @@ -117,8 +117,8 @@ void SmartPlaylistSearchPreview::RunSearch(const SmartPlaylistSearch &search) { ui_->count_label->hide(); QFuture future = QtConcurrent::run(DoRunSearch, generator_); QFutureWatcher *watcher = new QFutureWatcher(); - watcher->setFuture(future); QObject::connect(watcher, &QFutureWatcher::finished, this, &SmartPlaylistSearchPreview::SearchFinished); + watcher->setFuture(future); } diff --git a/tests/src/concurrentrun_test.cpp b/tests/src/concurrentrun_test.cpp index d28fb0c1..08a8828d 100644 --- a/tests/src/concurrentrun_test.cpp +++ b/tests/src/concurrentrun_test.cpp @@ -19,9 +19,9 @@ TEST(ConcurrentRunTest, ConcurrentRun0StartAndWait) { QThreadPool threadpool; QFuture future = QtConcurrent::run(&threadpool, &f); QFutureWatcher watcher; - watcher.setFuture(future); QEventLoop loop; QObject::connect(&watcher, &QFutureWatcher::finished, &loop, &QEventLoop::quit); + watcher.setFuture(future); loop.exec(); EXPECT_EQ(1337, watcher.result()); @@ -38,9 +38,9 @@ TEST(ConcurrentRunTest, ConcurrentRun1StartAndWait) { int i = 1336; QFuture future = QtConcurrent::run(&threadpool, &g, i); QFutureWatcher watcher; - watcher.setFuture(future); QEventLoop loop; QObject::connect(&watcher, &QFutureWatcher::finished, &loop, &QEventLoop::quit); + watcher.setFuture(future); loop.exec(); EXPECT_EQ(1337, watcher.result()); @@ -58,9 +58,9 @@ TEST(ConcurrentRunTest, ConcurrentRun2StartAndWait) { QThreadPool threadpool; QFuture future = QtConcurrent::run(&threadpool, &max, i, j); QFutureWatcher watcher; - watcher.setFuture(future); QEventLoop loop; QObject::connect(&watcher, &QFutureWatcher::finished, &loop, &QEventLoop::quit); + watcher.setFuture(future); loop.exec(); EXPECT_EQ(42, watcher.result()); @@ -79,9 +79,9 @@ TEST(ConcurrentRunTest, ConcurrentRun3StartAndWait) { QThreadPool threadpool; QFuture future = QtConcurrent::run(&threadpool, &sum, i, j, k); QFutureWatcher watcher; - watcher.setFuture(future); QEventLoop loop; QObject::connect(&watcher, &QFutureWatcher::finished, &loop, &QEventLoop::quit); + watcher.setFuture(future); loop.exec(); EXPECT_EQ(102, watcher.result()); @@ -111,9 +111,9 @@ TEST(ConcurrentRunTest, ConcurrentRunVoidFunction1Start) { int n = 10; QFuture future = QtConcurrent::run(&threadpool, &aFunction, &n); QFutureWatcher watcher; - watcher.setFuture(future); QEventLoop loop; QObject::connect(&watcher, &QFutureWatcher::finished, &loop, &QEventLoop::quit); + watcher.setFuture(future); loop.exec(); EXPECT_EQ(1337, n); @@ -126,9 +126,9 @@ TEST(ConcurrentRunTest, ConcurrentRunVoidFunction2Start) { int n = 10, m = 11; QFuture future = QtConcurrent::run(&threadpool, &bFunction, &n, &m); QFutureWatcher watcher; - watcher.setFuture(future); QEventLoop loop; QObject::connect(&watcher, &QFutureWatcher::finished, &loop, &QEventLoop::quit); + watcher.setFuture(future); loop.exec(); EXPECT_EQ(1337, n); EXPECT_EQ(1338, m); @@ -142,9 +142,9 @@ TEST(ConcurrentRunTest, ConcurrentRunVoidFunction3Start) { int n = 10, m = 11, o = 12; QFuture future = QtConcurrent::run(&threadpool, &cFunction, &n, &m, &o); QFutureWatcher watcher; - watcher.setFuture(future); QEventLoop loop; QObject::connect(&watcher, &QFutureWatcher::finished, &loop, &QEventLoop::quit); + watcher.setFuture(future); loop.exec(); EXPECT_EQ(1337, n); EXPECT_EQ(1338, m); @@ -167,9 +167,9 @@ TEST(ConcurrentRunTest, ConcurrentRunVoidBindFunctionStart) { int nb = 10; QFuture future = QtConcurrent::run(&threadpool, std::bind(&A::f, &a, &nb)); QFutureWatcher watcher; - watcher.setFuture(future); QEventLoop loop; QObject::connect(&watcher, &QFutureWatcher::finished, &loop, &QEventLoop::quit); + watcher.setFuture(future); loop.exec(); EXPECT_EQ(11, nb);