Commit 516a69f9 authored by Michael Pyne's avatar Michael Pyne

Refactor and extract SearchPlaylist's playlist item sync code.

And use it for DynamicPlaylist now as well.
parent 39523ee3
...@@ -151,7 +151,11 @@ void DynamicPlaylist::updateItems() ...@@ -151,7 +151,11 @@ void DynamicPlaylist::updateItems()
if(m_siblings != siblings) { if(m_siblings != siblings) {
m_siblings = siblings; m_siblings = siblings;
slotUpdateItems(); this->synchronizeItemsTo(siblings);
if(m_synchronizePlaying) {
synchronizePlayingItems(m_playlists, true);
}
} }
} }
...@@ -174,19 +178,4 @@ void DynamicPlaylist::checkUpdateItems() ...@@ -174,19 +178,4 @@ void DynamicPlaylist::checkUpdateItems()
m_dirty = false; m_dirty = false;
} }
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
void DynamicPlaylist::slotUpdateItems()
{
// This should be optimized to check to see which items are already in the
// list and just adding those and removing the ones that aren't.
clear();
createItems(m_siblings);
if(m_synchronizePlaying)
synchronizePlayingItems(m_playlists, true);
}
// vim: set et sw=4 tw=0 sta: // vim: set et sw=4 tw=0 sta:
...@@ -99,9 +99,6 @@ private: ...@@ -99,9 +99,6 @@ private:
*/ */
void checkUpdateItems(); void checkUpdateItems();
private slots:
void slotUpdateItems();
private: private:
QList<PlaylistObserver *> m_observers; QList<PlaylistObserver *> m_observers;
PlaylistItemList m_siblings; PlaylistItemList m_siblings;
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include <QCursor> #include <QCursor>
#include <QDir> #include <QDir>
#include <QDirIterator> #include <QDirIterator>
#include <QHash>
#include <QToolTip> #include <QToolTip>
#include <QFile> #include <QFile>
#include <QFileDialog> #include <QFileDialog>
...@@ -51,6 +52,7 @@ ...@@ -51,6 +52,7 @@
#include <QPixmap> #include <QPixmap>
#include <QStackedWidget> #include <QStackedWidget>
#include <QScrollBar> #include <QScrollBar>
#include <id3v1genres.h> #include <id3v1genres.h>
#include <time.h> #include <time.h>
...@@ -937,6 +939,39 @@ void Playlist::removeFromDisk(const PlaylistItemList &items) ...@@ -937,6 +939,39 @@ void Playlist::removeFromDisk(const PlaylistItemList &items)
} }
} }
void Playlist::synchronizeItemsTo(const PlaylistItemList &itemList)
{
const auto &existingItems = items();
if(qAbs(itemList.count() - existingItems.count()) >
qMax(itemList.count(), existingItems.count()) / 2)
{
// Large imbalance in list sizes, just clear all and add without
// figuring out the diff also
clearItems(existingItems);
createItems(itemList);
return;
}
// Determine differences between existing playlist items and patch up
QHash<CollectionListItem *, PlaylistItem *> oldItems;
oldItems.reserve(qMax(existingItems.count(), itemList.count()));
for(const auto &item : existingItems) {
oldItems.insert(item->collectionItem(), item);
}
PlaylistItemList newItems;
for(const auto &item : itemList) {
if(oldItems.remove(item->collectionItem()) == 0) {
newItems.append(item->collectionItem());
}
}
clearItems(PlaylistItemList(oldItems.values()));
createItems(newItems);
}
void Playlist::dragEnterEvent(QDragEnterEvent *e) void Playlist::dragEnterEvent(QDragEnterEvent *e)
{ {
if(CoverDrag::isCover(e->mimeData())) { if(CoverDrag::isCover(e->mimeData())) {
......
...@@ -414,6 +414,15 @@ protected: ...@@ -414,6 +414,15 @@ protected:
*/ */
void removeFromDisk(const PlaylistItemList &items); void removeFromDisk(const PlaylistItemList &items);
/**
* Adds and removes items from this Playlist as necessary to ensure that
* the same items are present in this Playlist as in @p itemList.
*
* No ordering guarantees are imposed, just that the playlist will have the
* same items as in the given list afterwards.
*/
void synchronizeItemsTo(const PlaylistItemList &itemList);
// the following are all reimplemented from base classes // the following are all reimplemented from base classes
virtual bool eventFilter(QObject *watched, QEvent *e); virtual bool eventFilter(QObject *watched, QEvent *e);
...@@ -437,7 +446,7 @@ protected: ...@@ -437,7 +446,7 @@ protected:
virtual void addColumn(const QString &label, int width = -1); virtual void addColumn(const QString &label, int width = -1);
/** /**
* Do some finial initialization of created items. Notably ensure that they * Do some final initialization of created items. Notably ensure that they
* are shown or hidden based on the contents of the current PlaylistSearch. * are shown or hidden based on the contents of the current PlaylistSearch.
* *
* This is called by the PlaylistItem constructor. * This is called by the PlaylistItem constructor.
......
...@@ -55,25 +55,8 @@ void SearchPlaylist::updateItems() ...@@ -55,25 +55,8 @@ void SearchPlaylist::updateItems()
// Here we don't simply use "clear" since that would involve a call to // Here we don't simply use "clear" since that would involve a call to
// items() which would in turn call this method... // items() which would in turn call this method...
PlaylistItemList l = Playlist::items();
QHash<CollectionListItem *, PlaylistItem *> oldItems;
oldItems.reserve(503);
foreach(PlaylistItem *item, l)
oldItems.insert(item->collectionItem(), item);
m_search.search(); m_search.search();
PlaylistItemList matched = m_search.matchedItems(); synchronizeItemsTo(m_search.matchedItems());
PlaylistItemList newItems;
foreach(PlaylistItem *item, matched) {
if(oldItems.remove(item->collectionItem()) == 0)
newItems.append(item->collectionItem());
}
clearItems(PlaylistItemList(oldItems.values()));
createItems(newItems);
if(synchronizePlaying()) { if(synchronizePlaying()) {
qCDebug(JUK_LOG) << "synchronizing playing"; qCDebug(JUK_LOG) << "synchronizing playing";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment