From 79a93f6fb6b5115faadfa1150a0b4d980bb0957d Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Tue, 10 Dec 2019 13:26:08 +0100 Subject: [PATCH] Send a single signal after modifying multiple playlists --- src/library/baseplaylistfeature.cpp | 58 ++++++++++++++++++----------- src/library/baseplaylistfeature.h | 2 +- src/library/dao/playlistdao.cpp | 43 ++++++++++++--------- src/library/dao/playlistdao.h | 7 ++-- src/library/playlistfeature.cpp | 13 ++++--- src/library/playlistfeature.h | 2 +- src/library/playlisttablemodel.cpp | 10 +++-- src/library/playlisttablemodel.h | 2 +- src/library/setlogfeature.cpp | 13 ++++--- src/library/setlogfeature.h | 2 +- 10 files changed, 91 insertions(+), 61 deletions(-) diff --git a/src/library/baseplaylistfeature.cpp b/src/library/baseplaylistfeature.cpp index c5e64c61767..62dcb61794f 100644 --- a/src/library/baseplaylistfeature.cpp +++ b/src/library/baseplaylistfeature.cpp @@ -74,29 +74,45 @@ BasePlaylistFeature::BasePlaylistFeature(QObject* parent, this, SLOT(slotExportTrackFiles())); m_pAnalyzePlaylistAction = new QAction(tr("Analyze entire Playlist"), this); - connect(m_pAnalyzePlaylistAction, SIGNAL(triggered()), - this, SLOT(slotAnalyzePlaylist())); - - connect(&m_playlistDao, SIGNAL(added(int)), - this, SLOT(slotPlaylistTableChanged(int))); - - connect(&m_playlistDao, SIGNAL(deleted(int)), - this, SLOT(slotPlaylistTableChanged(int))); - - connect(&m_playlistDao, SIGNAL(renamed(int,QString)), - this, SLOT(slotPlaylistTableRenamed(int,QString))); - - connect(&m_playlistDao, SIGNAL(changed(int)), - this, SLOT(slotPlaylistContentChanged(int))); - - connect(&m_playlistDao, SIGNAL(lockChanged(int)), - this, SLOT(slotPlaylistTableChanged(int))); + connect(m_pAnalyzePlaylistAction, + &QAction::triggered, + this, + &BasePlaylistFeature::slotAnalyzePlaylist); + + connect(&m_playlistDao, + &PlaylistDAO::added, + this, + &BasePlaylistFeature::slotPlaylistTableChanged); + + connect(&m_playlistDao, + &PlaylistDAO::deleted, + this, + &BasePlaylistFeature::slotPlaylistTableChanged); + + connect(&m_playlistDao, + &PlaylistDAO::renamed, + this, + &BasePlaylistFeature::slotPlaylistTableRenamed); + + connect(&m_playlistDao, + &PlaylistDAO::tracksChanged, + this, + &BasePlaylistFeature::slotPlaylistContentChanged); + + connect(&m_playlistDao, + &PlaylistDAO::lockChanged, + this, + &BasePlaylistFeature::slotPlaylistTableChanged); Library* pLibrary = static_cast(parent); - connect(pLibrary, SIGNAL(trackSelected(TrackPointer)), - this, SLOT(slotTrackSelected(TrackPointer))); - connect(pLibrary, SIGNAL(switchToView(const QString&)), - this, SLOT(slotResetSelectedTrack())); + connect(pLibrary, + &Library::trackSelected, + this, + &BasePlaylistFeature::slotTrackSelected); + connect(pLibrary, + &Library::switchToView, + this, + &BasePlaylistFeature::slotResetSelectedTrack); } BasePlaylistFeature::~BasePlaylistFeature() { diff --git a/src/library/baseplaylistfeature.h b/src/library/baseplaylistfeature.h index b78538c8a24..88e6d3c525c 100644 --- a/src/library/baseplaylistfeature.h +++ b/src/library/baseplaylistfeature.h @@ -47,7 +47,7 @@ class BasePlaylistFeature : public LibraryFeature { virtual void htmlLinkClicked(const QUrl& link); virtual void slotPlaylistTableChanged(int playlistId) = 0; - virtual void slotPlaylistContentChanged(int playlistId) = 0; + virtual void slotPlaylistContentChanged(QSet playlistIds) = 0; virtual void slotPlaylistTableRenamed(int playlistId, QString newName) = 0; void slotCreatePlaylist(); diff --git a/src/library/dao/playlistdao.cpp b/src/library/dao/playlistdao.cpp index 4cd1bcc9387..2734cc82b65 100644 --- a/src/library/dao/playlistdao.cpp +++ b/src/library/dao/playlistdao.cpp @@ -256,7 +256,7 @@ bool PlaylistDAO::removeTracksFromPlaylist(int playlistId, int startIndex) { return false; } transaction.commit(); - emit(changed(playlistId)); + emit tracksChanged(QSet{playlistId}); return true; } @@ -298,7 +298,7 @@ bool PlaylistDAO::appendTracksToPlaylist(const QList& trackIds, const i // TODO(XXX) don't emit if the track didn't add successfully. emit(trackAdded(playlistId, trackId, insertPosition++)); } - emit(changed(playlistId)); + emit tracksChanged(QSet{playlistId}); return true; } @@ -411,13 +411,18 @@ void PlaylistDAO::removeHiddenTracks(const int playlistId) { } transaction.commit(); - emit(changed(playlistId)); + emit tracksChanged(QSet{playlistId}); } -void PlaylistDAO::removeTrackFromPlaylist(int playlistId, TrackId trackId) { +void PlaylistDAO::removeTracksFromPlaylistById(int playlistId, TrackId trackId) { ScopedTransaction transaction(m_database); + removeTracksFromPlaylistByIdInner(playlistId, trackId); + transaction.commit(); + emit tracksChanged(QSet{playlistId}); +} +void PlaylistDAO::removeTracksFromPlaylistByIdInner(int playlistId, TrackId trackId) { QSqlQuery query(m_database); query.prepare("SELECT position FROM PlaylistTracks WHERE playlist_id=:id " "AND track_id=:track_id"); @@ -433,11 +438,7 @@ void PlaylistDAO::removeTrackFromPlaylist(int playlistId, TrackId trackId) { while (query.next()) { int position = query.value(query.record().indexOf("position")).toInt(); removeTracksFromPlaylistInner(playlistId, position); - } - - transaction.commit(); - emit(changed(playlistId)); } @@ -447,7 +448,7 @@ void PlaylistDAO::removeTrackFromPlaylist(int playlistId, int position) { ScopedTransaction transaction(m_database); removeTracksFromPlaylistInner(playlistId, position); transaction.commit(); - emit(changed(playlistId)); + emit tracksChanged(QSet{playlistId}); } void PlaylistDAO::removeTracksFromPlaylist(int playlistId, QList positions) { @@ -461,7 +462,7 @@ void PlaylistDAO::removeTracksFromPlaylist(int playlistId, QList positions) removeTracksFromPlaylistInner(playlistId, position); } transaction.commit(); - emit(changed(playlistId)); + emit tracksChanged(QSet{playlistId}); } void PlaylistDAO::removeTracksFromPlaylistInner(int playlistId, int position) { @@ -549,7 +550,7 @@ bool PlaylistDAO::insertTrackIntoPlaylist(TrackId trackId, const int playlistId, m_playlistsTrackIsIn.insert(trackId, playlistId); emit(trackAdded(playlistId, trackId, position)); - emit(changed(playlistId)); + emit tracksChanged(QSet{playlistId}); return true; } @@ -610,7 +611,7 @@ int PlaylistDAO::insertTracksIntoPlaylist(const QList& trackIds, // TODO(XXX) The position is wrong if any track failed to insert. emit(trackAdded(playlistId, trackId, insertPositon++)); } - emit(changed(playlistId)); + emit tracksChanged(QSet{playlistId}); return tracksAdded; } @@ -723,7 +724,7 @@ bool PlaylistDAO::copyPlaylistTracks(const int sourcePlaylistID, const int targe m_playlistsTrackIsIn.insert(copiedTrackId, targetPlaylistID); emit(trackAdded(targetPlaylistID, copiedTrackId, copiedPosition)); } - emit(changed(targetPlaylistID)); + emit tracksChanged(QSet{targetPlaylistID}); return true; } @@ -749,14 +750,22 @@ int PlaylistDAO::getMaxPosition(const int playlistId) const { void PlaylistDAO::removeTracksFromPlaylists(const QList& trackIds) { // copy the hash, because there is no guarantee that "it" is valid after remove QMultiHash playlistsTrackIsInCopy = m_playlistsTrackIsIn; - for (const auto& trackId: trackIds) { + QSet playlistIds; + + ScopedTransaction transaction(m_database); + for (const auto& trackId : trackIds) { for (auto it = playlistsTrackIsInCopy.constBegin(); it != playlistsTrackIsInCopy.constEnd(); ++it) { if (it.key() == trackId) { - removeTrackFromPlaylist(it.value(), trackId); + const auto playlistId = it.value(); + removeTracksFromPlaylistByIdInner(playlistId, trackId); + playlistIds.insert(playlistId); } } } + transaction.commit(); + + emit tracksChanged(playlistIds); } int PlaylistDAO::tracksInPlaylist(const int playlistId) const { @@ -829,7 +838,7 @@ void PlaylistDAO::moveTrack(const int playlistId, const int oldPosition, const i qDebug() << query.lastError(); } - emit(changed(playlistId)); + emit tracksChanged(QSet{playlistId}); } void PlaylistDAO::searchForDuplicateTrack(const int fromPosition, @@ -993,7 +1002,7 @@ void PlaylistDAO::shuffleTracks(const int playlistId, const QList& position } transaction.commit(); - emit(changed(playlistId)); + emit tracksChanged(QSet{playlistId}); } bool PlaylistDAO::isTrackInPlaylist(TrackId trackId, const int playlistId) const { diff --git a/src/library/dao/playlistdao.h b/src/library/dao/playlistdao.h index aae654eec94..941525a440a 100644 --- a/src/library/dao/playlistdao.h +++ b/src/library/dao/playlistdao.h @@ -91,9 +91,9 @@ class PlaylistDAO : public QObject, public virtual DAO { // removes all hidden and purged Tracks from the playlist void removeHiddenTracks(const int playlistId); // Remove a track from a playlist - void removeTrackFromPlaylist(int playlistId, TrackId trackId); void removeTrackFromPlaylist(int playlistId, int position); void removeTracksFromPlaylist(int playlistId, QList positions); + void removeTracksFromPlaylistById(int playlistId, TrackId trackId); // Insert a track into a specific position in a playlist bool insertTrackIntoPlaylist(TrackId trackId, int playlistId, int position); // Inserts a list of tracks into playlist @@ -125,14 +125,15 @@ class PlaylistDAO : public QObject, public virtual DAO { void added(int playlistId); void deleted(int playlistId); void renamed(int playlistId, QString newName); - void changed(int playlistId); + void lockChanged(int playlistId); void trackAdded(int playlistId, TrackId trackId, int position); void trackRemoved(int playlistId, TrackId trackId, int position); - void lockChanged(int playlistId); + void tracksChanged(QSet playlistIds); // added/removed/reordered private: bool removeTracksFromPlaylist(int playlistId, int startIndex); void removeTracksFromPlaylistInner(int playlistId, int position); + void removeTracksFromPlaylistByIdInner(int playlistId, TrackId trackId); void searchForDuplicateTrack(const int fromPosition, const int toPosition, TrackId trackID, diff --git a/src/library/playlistfeature.cpp b/src/library/playlistfeature.cpp index 269f5a0fabf..c199cd670b4 100644 --- a/src/library/playlistfeature.cpp +++ b/src/library/playlistfeature.cpp @@ -232,16 +232,17 @@ void PlaylistFeature::slotPlaylistTableChanged(int playlistId) { } } -void PlaylistFeature::slotPlaylistContentChanged(int playlistId) { +void PlaylistFeature::slotPlaylistContentChanged(QSet playlistIds) { if (!m_pPlaylistTableModel) { return; } - //qDebug() << "slotPlaylistContentChanged() playlistId:" << playlistId; - enum PlaylistDAO::HiddenType type = m_playlistDao.getHiddenType(playlistId); - if (type == PlaylistDAO::PLHT_NOT_HIDDEN || - type == PlaylistDAO::PLHT_UNKNOWN) { // In case of a deleted Playlist - updateChildModel(playlistId); + for (const auto playlistId : playlistIds) { + enum PlaylistDAO::HiddenType type = m_playlistDao.getHiddenType(playlistId); + if (type == PlaylistDAO::PLHT_NOT_HIDDEN || + type == PlaylistDAO::PLHT_UNKNOWN) { // In case of a deleted Playlist + updateChildModel(playlistId); + } } } diff --git a/src/library/playlistfeature.h b/src/library/playlistfeature.h index 9900c9b5287..545f4907ddd 100644 --- a/src/library/playlistfeature.h +++ b/src/library/playlistfeature.h @@ -36,7 +36,7 @@ class PlaylistFeature : public BasePlaylistFeature { private slots: void slotPlaylistTableChanged(int playlistId) override; - void slotPlaylistContentChanged(int playlistId) override; + void slotPlaylistContentChanged(QSet playlistIds) override; void slotPlaylistTableRenamed(int playlistId, QString newName) override; protected: diff --git a/src/library/playlisttablemodel.cpp b/src/library/playlisttablemodel.cpp index d6ac44e729d..fc71087da51 100644 --- a/src/library/playlisttablemodel.cpp +++ b/src/library/playlisttablemodel.cpp @@ -68,8 +68,10 @@ void PlaylistTableModel::setTableModel(int playlistId) { setDefaultSort(fieldIndex(ColumnCache::COLUMN_PLAYLISTTRACKSTABLE_POSITION), Qt::AscendingOrder); setSort(defaultSortColumn(), defaultSortOrder()); - connect(&m_pTrackCollection->getPlaylistDAO(), SIGNAL(changed(int)), - this, SLOT(playlistChanged(int))); + connect(&m_pTrackCollection->getPlaylistDAO(), + &PlaylistDAO::tracksChanged, + this, + &PlaylistTableModel::playlistsChanged); } int PlaylistTableModel::addTracks(const QModelIndex& index, @@ -259,8 +261,8 @@ TrackModel::CapabilitiesFlags PlaylistTableModel::getCapabilities() const { return caps; } -void PlaylistTableModel::playlistChanged(int playlistId) { - if (playlistId == m_iPlaylistId) { +void PlaylistTableModel::playlistsChanged(QSet playlistIds) { + if (playlistIds.contains(m_iPlaylistId)) { select(); // Repopulate the data model. } } diff --git a/src/library/playlisttablemodel.h b/src/library/playlisttablemodel.h index 070a187f269..e282612059c 100644 --- a/src/library/playlisttablemodel.h +++ b/src/library/playlisttablemodel.h @@ -33,7 +33,7 @@ class PlaylistTableModel : public BaseSqlTableModel { CapabilitiesFlags getCapabilities() const final; private slots: - void playlistChanged(int playlistId); + void playlistsChanged(QSet playlistIds); private: int m_iPlaylistId; diff --git a/src/library/setlogfeature.cpp b/src/library/setlogfeature.cpp index aced064c683..5ccddcca3d1 100644 --- a/src/library/setlogfeature.cpp +++ b/src/library/setlogfeature.cpp @@ -332,16 +332,17 @@ void SetlogFeature::slotPlaylistTableChanged(int playlistId) { } } -void SetlogFeature::slotPlaylistContentChanged(int playlistId) { +void SetlogFeature::slotPlaylistContentChanged(QSet playlistIds) { if (!m_pPlaylistTableModel) { return; } - //qDebug() << "slotPlaylistContentChanged() playlistId:" << playlistId; - enum PlaylistDAO::HiddenType type = m_playlistDao.getHiddenType(playlistId); - if (type == PlaylistDAO::PLHT_SET_LOG || - type == PlaylistDAO::PLHT_UNKNOWN) { // In case of a deleted Playlist - updateChildModel(playlistId); + for (const auto playlistId : playlistIds) { + enum PlaylistDAO::HiddenType type = m_playlistDao.getHiddenType(playlistId); + if (type == PlaylistDAO::PLHT_SET_LOG || + type == PlaylistDAO::PLHT_UNKNOWN) { // In case of a deleted Playlist + updateChildModel(playlistId); + } } } diff --git a/src/library/setlogfeature.h b/src/library/setlogfeature.h index ba2869e85c0..c7a3b2a56d8 100644 --- a/src/library/setlogfeature.h +++ b/src/library/setlogfeature.h @@ -40,7 +40,7 @@ class SetlogFeature : public BasePlaylistFeature { private slots: void slotPlayingTrackChanged(TrackPointer currentPlayingTrack); void slotPlaylistTableChanged(int playlistId) override; - void slotPlaylistContentChanged(int playlistId) override; + void slotPlaylistContentChanged(QSet playlistIds) override; void slotPlaylistTableRenamed(int playlistId, QString newName) override; private: