Use std::shared_ptrfor AlbumCoverLoaderResult

Reduces memory fragmentation with Qt 6
This commit is contained in:
Jonas Kvinge
2023-04-06 23:18:10 +02:00
parent 962536bc83
commit b660287779
38 changed files with 363 additions and 294 deletions

View File

@@ -1296,7 +1296,7 @@ void MainWindow::MediaStopped() {
song_playing_ = Song();
song_ = Song();
album_cover_ = AlbumCoverImageResult();
album_cover_.reset();
app_->scrobbler()->ClearPlaying();
@@ -3034,7 +3034,11 @@ void MainWindow::SearchForCover() {
}
void MainWindow::SaveCoverToFile() {
album_cover_choice_controller_->SaveCoverToFileManual(song_, album_cover_);
if (album_cover_) {
album_cover_choice_controller_->SaveCoverToFileManual(song_, album_cover_);
}
}
void MainWindow::UnsetCover() {
@@ -3050,7 +3054,11 @@ void MainWindow::DeleteCover() {
}
void MainWindow::ShowCover() {
album_cover_choice_controller_->ShowCover(song_, album_cover_.image);
if (album_cover_) {
album_cover_choice_controller_->ShowCover(song_, album_cover_->image);
}
}
void MainWindow::SearchCoverAutomatically() {
@@ -3059,24 +3067,29 @@ void MainWindow::SearchCoverAutomatically() {
}
void MainWindow::AlbumCoverLoaded(const Song &song, const AlbumCoverLoaderResult &result) {
void MainWindow::AlbumCoverLoaded(const Song &song, AlbumCoverLoaderResultPtr result) {
if (song != song_playing_) return;
song_ = song;
album_cover_ = result.album_cover;
emit AlbumCoverReady(song, result.album_cover.image);
if (result) {
album_cover_ = result->album_cover;
emit AlbumCoverReady(song, result->album_cover->image);
}
else {
album_cover_.reset();
emit AlbumCoverReady(song, QImage());
}
const bool enable_change_art = song.is_collection_song() && !song.effective_albumartist().isEmpty() && !song.album().isEmpty();
album_cover_choice_controller_->show_cover_action()->setEnabled(result.success && result.type != AlbumCoverLoaderResult::Type::ManuallyUnset);
album_cover_choice_controller_->cover_to_file_action()->setEnabled(result.success && result.type != AlbumCoverLoaderResult::Type::ManuallyUnset);
album_cover_choice_controller_->show_cover_action()->setEnabled(result && result->success && result->type != AlbumCoverLoaderResult::Type::ManuallyUnset);
album_cover_choice_controller_->cover_to_file_action()->setEnabled(result && result->success && result->type != AlbumCoverLoaderResult::Type::ManuallyUnset);
album_cover_choice_controller_->cover_from_file_action()->setEnabled(enable_change_art);
album_cover_choice_controller_->cover_from_url_action()->setEnabled(enable_change_art);
album_cover_choice_controller_->search_for_cover_action()->setEnabled(app_->cover_providers()->HasAnyProviders() && enable_change_art);
album_cover_choice_controller_->unset_cover_action()->setEnabled(enable_change_art && !song.has_manually_unset_cover());
album_cover_choice_controller_->clear_cover_action()->setEnabled(enable_change_art && !song.art_manual().isEmpty());
album_cover_choice_controller_->delete_cover_action()->setEnabled(enable_change_art && result.success && result.type != AlbumCoverLoaderResult::Type::ManuallyUnset);
album_cover_choice_controller_->delete_cover_action()->setEnabled(enable_change_art && result && result->success && result->type != AlbumCoverLoaderResult::Type::ManuallyUnset);
GetCoverAutomatically();
@@ -3085,13 +3098,12 @@ void MainWindow::AlbumCoverLoaded(const Song &song, const AlbumCoverLoaderResult
void MainWindow::GetCoverAutomatically() {
// Search for cover automatically?
bool search =
album_cover_choice_controller_->search_cover_auto_action()->isChecked() &&
!song_.has_manually_unset_cover() &&
!song_.art_automatic_is_valid() &&
!song_.art_manual_is_valid() &&
!song_.effective_albumartist().isEmpty() &&
!song_.effective_album().isEmpty();
const bool search = album_cover_choice_controller_->search_cover_auto_action()->isChecked() &&
!song_.has_manually_unset_cover() &&
!song_.art_automatic_is_valid() &&
!song_.art_manual_is_valid() &&
!song_.effective_albumartist().isEmpty() &&
!song_.effective_album().isEmpty();
if (search) {
emit SearchCoverInProgress();

View File

@@ -258,7 +258,7 @@ class MainWindow : public QMainWindow, public PlatformInterface {
void DeleteCover();
void ShowCover();
void SearchCoverAutomatically();
void AlbumCoverLoaded(const Song &song, const AlbumCoverLoaderResult &result);
void AlbumCoverLoaded(const Song &song, AlbumCoverLoaderResultPtr result);
void ScrobblingEnabledChanged(const bool value);
void ScrobbleButtonVisibilityChanged(const bool value);
@@ -391,7 +391,7 @@ class MainWindow : public QMainWindow, public PlatformInterface {
Song song_;
Song song_playing_;
AlbumCoverImageResult album_cover_;
AlbumCoverImageResultPtr album_cover_;
bool exit_;
int exit_count_;
bool delete_files_;

View File

@@ -123,6 +123,7 @@ void RegisterMetaTypes() {
qRegisterMetaType<PlaylistSequence::RepeatMode>("PlaylistSequence::RepeatMode");
qRegisterMetaType<PlaylistSequence::ShuffleMode>("PlaylistSequence::ShuffleMode");
qRegisterMetaType<AlbumCoverLoaderResult>("AlbumCoverLoaderResult");
qRegisterMetaType<AlbumCoverLoaderResultPtr>("AlbumCoverLoaderResultPtr");
qRegisterMetaType<AlbumCoverLoaderResult::Type>("AlbumCoverLoaderResult::Type");
qRegisterMetaType<CoverProviderSearchResult>("CoverProviderSearchResult");
qRegisterMetaType<CoverProviderSearchResults>("CoverProviderSearchResults");

View File

@@ -386,7 +386,9 @@ void Mpris2::CurrentSongChanged(const Song &song) {
}
// ... and we add the cover information later, when it's available.
void Mpris2::AlbumCoverLoaded(const Song &song, const AlbumCoverLoaderResult &result) {
void Mpris2::AlbumCoverLoaded(const Song &song, AlbumCoverLoaderResultPtr result) {
if (!result) return;
last_metadata_ = QVariantMap();
song.ToXesam(&last_metadata_);
@@ -395,11 +397,11 @@ void Mpris2::AlbumCoverLoaded(const Song &song, const AlbumCoverLoaderResult &re
AddMetadata("mpris:trackid", current_track_id(), &last_metadata_);
QUrl cover_url;
if (result.album_cover.cover_url.isValid() && result.album_cover.cover_url.isLocalFile() && QFile(result.album_cover.cover_url.toLocalFile()).exists()) {
cover_url = result.album_cover.cover_url;
if (result->album_cover->cover_url.isValid() && result->album_cover->cover_url.isLocalFile() && QFile(result->album_cover->cover_url.toLocalFile()).exists()) {
cover_url = result->album_cover->cover_url;
}
else if (result.temp_cover_url.isValid() && result.temp_cover_url.isLocalFile()) {
cover_url = result.temp_cover_url;
else if (result->temp_cover_url.isValid() && result->temp_cover_url.isLocalFile()) {
cover_url = result->temp_cover_url;
}
else if (song.art_manual().isValid() && song.art_manual().isLocalFile() && song.art_manual().path() != Song::kManuallyUnsetCover && song.art_manual().path() != Song::kEmbeddedCover) {
cover_url = song.art_manual();

View File

@@ -202,7 +202,7 @@ class Mpris2 : public QObject {
void PlaylistChanged(MprisPlaylist playlist);
private slots:
void AlbumCoverLoaded(const Song &song, const AlbumCoverLoaderResult &result = AlbumCoverLoaderResult());
void AlbumCoverLoaded(const Song &song, AlbumCoverLoaderResultPtr result = AlbumCoverLoaderResultPtr());
void EngineStateChanged(Engine::State newState);
void VolumeChanged();

View File

@@ -96,13 +96,15 @@ void StandardItemIconLoader::ModelReset() {
}
void StandardItemIconLoader::AlbumCoverLoaded(const quint64 id, const AlbumCoverLoaderResult &result) {
void StandardItemIconLoader::AlbumCoverLoaded(const quint64 id, AlbumCoverLoaderResultPtr result) {
if (!pending_covers_.contains(id)) return;
QStandardItem *item = pending_covers_.take(id);
if (!item) return;
if (result.success && !result.image_scaled.isNull() && result.type != AlbumCoverLoaderResult::Type::ManuallyUnset) {
item->setIcon(QIcon(QPixmap::fromImage(result.image_scaled)));
if (result && result->success && !result->image_scaled.isNull() && result->type != AlbumCoverLoaderResult::Type::ManuallyUnset) {
item->setIcon(QIcon(QPixmap::fromImage(result->image_scaled)));
}
}

View File

@@ -55,7 +55,7 @@ class StandardItemIconLoader : public QObject {
void LoadIcon(const Song &song, QStandardItem *for_item);
private slots:
void AlbumCoverLoaded(const quint64 id, const AlbumCoverLoaderResult &result);
void AlbumCoverLoaded(const quint64 id, AlbumCoverLoaderResultPtr result);
void RowsAboutToBeRemoved(const QModelIndex &parent, int begin, int end);
void ModelReset();