Commit f9dac3d7 authored by Abdel-Rahman Abdel-Rahman's avatar Abdel-Rahman Abdel-Rahman Committed by Michael Pyne

Replace observer pattern of PlaylistInterface

parent 3cc8f50f
......@@ -344,12 +344,6 @@ CollectionList::~CollectionList()
config.writeEntry("CollectionListSortColumn", header()->sortIndicatorSection());
config.writeEntry("CollectionListSortAscending", header()->sortIndicatorOrder() == Qt::AscendingOrder);
// In some situations the dataChanged signal from clearItems will cause observers to
// subsequently try to access a deleted item. Since we're going away just remove all
// observers.
clearObservers();
// The CollectionListItems will try to remove themselves from the
// m_columnTags member, so we must make sure they're gone before we
// are.
......
......@@ -20,25 +20,14 @@
#include "tracksequencemanager.h"
#include <QTimer>
#include <QObject>
class PlaylistDirtyObserver : public PlaylistObserver
PlaylistDirtyObserver::PlaylistDirtyObserver(DynamicPlaylist* parent, Playlist* playlist) :
m_parent(parent)
{
public:
PlaylistDirtyObserver(DynamicPlaylist *parent, Playlist *playlist) :
PlaylistObserver(playlist),
m_parent(parent)
{
}
virtual void playlistItemDataHasChanged() Q_DECL_FINAL override
{
m_parent->slotSetDirty();
}
QObject::connect(&(playlist->signaller), &PlaylistInterfaceSignaller::playingItemDataChanged, m_parent, &DynamicPlaylist::slotSetDirty);
}
private:
DynamicPlaylist *m_parent;
};
////////////////////////////////////////////////////////////////////////////////
// public methods
......@@ -72,7 +61,7 @@ DynamicPlaylist::~DynamicPlaylist()
{
lower();
foreach(PlaylistObserver *observer, m_observers)
foreach(PlaylistDirtyObserver *observer, m_observers)
delete observer;
}
......
......@@ -21,6 +21,8 @@
#include <QVector>
class PlaylistDirtyObserver;
/**
* A Playlist that is a union of other playlists that is created dynamically.
*/
......@@ -100,13 +102,22 @@ private:
void checkUpdateItems();
private:
QVector<PlaylistObserver *> m_observers;
QVector<PlaylistDirtyObserver *> m_observers;
PlaylistItemList m_siblings;
PlaylistList m_playlists;
bool m_dirty;
bool m_synchronizePlaying;
};
class PlaylistDirtyObserver
{
public:
PlaylistDirtyObserver(DynamicPlaylist *parent, Playlist *playlist);
private:
DynamicPlaylist *m_parent;
};
#endif
// vim: set et sw=4 tw=0 sta:
......@@ -54,18 +54,13 @@ signals:
void nowPlayingHidden();
private:
struct Observer final : public PlaylistObserver
struct Observer final
{
Observer(NowPlaying *parent, PlaylistInterface *playlist) :
PlaylistObserver(playlist),
m_parent(parent) {}
virtual void playlistItemDataHasChanged() override
Observer(NowPlaying *parent, PlaylistInterface *playlist)
{
m_parent->slotReloadCurrentItem();
connect(&playlist->signaller, &PlaylistInterfaceSignaller::playingItemDataChanged, parent, &NowPlaying::slotReloadCurrentItem);
}
NowPlaying *m_parent;
};
friend struct Observer;
Observer m_observer;
Observer m_collectionListObserver;
......
......@@ -168,12 +168,6 @@ Playlist::Playlist(PlaylistCollection *collection, bool delaySetup, int extraCol
Playlist::~Playlist()
{
// In some situations the dataChanged signal from clearItems will cause observers to
// subsequently try to access a deleted item. Since we're going away just remove all
// observers.
clearObservers();
// clearItem() will take care of removing the items from the history,
// so call clearItems() to make sure it happens.
......
......@@ -319,6 +319,8 @@ public:
static void setShuttingDown() { m_shuttingDown = true; }
void playlistItemsChanged() override;
public slots:
/**
* Remove the currently selected items from the playlist and disk.
......@@ -402,8 +404,6 @@ public slots:
void slotColumnResizeModeChanged();
virtual void playlistItemsChanged() override;
protected:
/**
* Remove \a items from the playlist and disk. This will ignore items that
......
......@@ -709,7 +709,6 @@ PlaylistBox::Item *PlaylistBox::Item::m_collectionItem = 0;
PlaylistBox::Item::Item(PlaylistBox *listBox, const QString &icon, const QString &text, Playlist *l)
: QObject(listBox), QTreeWidgetItem(listBox, QStringList(text)),
PlaylistObserver(l),
m_playlist(l), m_iconName(icon), m_sortedFirst(false)
{
init();
......@@ -717,7 +716,6 @@ PlaylistBox::Item::Item(PlaylistBox *listBox, const QString &icon, const QString
PlaylistBox::Item::Item(Item *parent, const QString &icon, const QString &text, Playlist *l)
: QObject(parent->listView()), QTreeWidgetItem(parent, QStringList(text)),
PlaylistObserver(l),
m_playlist(l), m_iconName(icon), m_sortedFirst(false)
{
init();
......@@ -755,11 +753,7 @@ void PlaylistBox::Item::slotSetName(const QString &name)
//listView()->viewMode()->queueRefresh();
}
void PlaylistBox::Item::playingItemHasChanged()
{
}
void PlaylistBox::Item::playlistItemDataHasChanged()
void PlaylistBox::Item::playlistItemDataChanged()
{
// This avoids spuriously re-saving all playlists just because play queue
// changes.
......@@ -798,6 +792,8 @@ void PlaylistBox::Item::init()
m_sortedFirst = true;
setText(1, sortTextFor(itemText));
connect(&(m_playlist->signaller), &PlaylistInterfaceSignaller::playingItemDataChanged, this, &PlaylistBox::Item::playlistItemDataChanged);
}
QString PlaylistBox::Item::sortTextFor(const QString &name) const
......
......@@ -144,7 +144,7 @@ private:
QTimer *m_savePlaylistTimer;
};
class PlaylistBox::Item final : public QObject, public QTreeWidgetItem, public PlaylistObserver
class PlaylistBox::Item final : public QObject, public QTreeWidgetItem
{
friend class PlaylistBox;
friend class PlaylistSplitter;
......@@ -181,14 +181,8 @@ protected:
static Item *collectionItem() { return m_collectionItem; }
static void setCollectionItem(Item *item) { m_collectionItem = item; }
//
// Reimplemented from PlaylistObserver
//
virtual void playingItemHasChanged() override;
// Used to post a timer in PlaylistBox to save playlists.
virtual void playlistItemDataHasChanged() override;
void playlistItemDataChanged();
protected slots:
......
......@@ -17,67 +17,20 @@
#include "playlistinterface.h"
////////////////////////////////////////////////////////////////////////////////
// Watched implementation
// PlaylistInterface implementation
////////////////////////////////////////////////////////////////////////////////
void Watched::currentPlayingItemChanged()
{
foreach(PlaylistObserver *observer, m_observers)
observer->playingItemHasChanged();
}
void Watched::playlistItemsChanged()
{
foreach(PlaylistObserver *observer, m_observers)
observer->playlistItemDataHasChanged();
}
void Watched::addObserver(PlaylistObserver *observer)
{
m_observers.append(observer);
}
void Watched::removeObserver(PlaylistObserver *observer)
{
if(observer)
observer->clearWatched();
m_observers.removeAll(observer);
}
void Watched::clearObservers()
{
foreach(PlaylistObserver *observer, m_observers)
observer->clearWatched();
m_observers.clear();
}
Watched::~Watched()
{
clearObservers();
void PlaylistInterface::currentPlayingItemChanged(){
emit signaller.playingItemChanged();
}
////////////////////////////////////////////////////////////////////////////////
// PlaylistObserver implementation
////////////////////////////////////////////////////////////////////////////////
PlaylistObserver::~PlaylistObserver()
{
if(m_playlist)
m_playlist->removeObserver(this);
void PlaylistInterface::playlistItemsChanged(){
emit signaller.playingItemDataChanged();
}
PlaylistObserver::PlaylistObserver(PlaylistInterface *playlist) :
m_playlist(playlist)
PlaylistInterface::~PlaylistInterface()
{
if(m_playlist)
playlist->addObserver(this);
}
const PlaylistInterface *PlaylistObserver::playlist() const
{
return m_playlist;
}
// vim: set et sw=4 tw=0 sta:
......@@ -18,52 +18,18 @@
#define PLAYLISTINTERFACE_H
#include <QVector>
#include <QObject>
class FileHandle;
class PlaylistObserver;
/**
* An interface implemented by PlaylistInterface to make it possible to watch
* for changes in the PlaylistInterface. This is a semi-standard observer
* pattern from i.e. Design Patterns.
*/
class Watched
{
public:
void addObserver(PlaylistObserver *observer);
void removeObserver(PlaylistObserver *observer);
/**
* Call this to remove all objects observing this class unconditionally (for example, when
* you're being destructed).
*/
void clearObservers();
/**
* This is triggered when the currently playing item has been changed.
*/
virtual void currentPlayingItemChanged();
/**
* This is triggered when the data in the playlist -- i.e. the tag content
* changes.
*/
virtual void playlistItemsChanged();
protected:
virtual ~Watched();
private:
QVector<PlaylistObserver *> m_observers;
class PlaylistInterfaceSignaller: public QObject{
Q_OBJECT
signals:
void playingItemChanged();
void playingItemDataChanged();
};
/**
* This is a simple interface that should be used by things that implement a
* playlist-like API.
*/
class PlaylistInterface : public Watched
class PlaylistInterface
{
public:
virtual QString name() const = 0;
......@@ -76,36 +42,12 @@ public:
virtual void stop() = 0;
virtual bool playing() const = 0;
};
class PlaylistObserver
{
public:
virtual ~PlaylistObserver();
/**
* This method is called when the item which is currently playing
* in the given playlist changes.
* @todo TODO: Move to PlayerManager...
*/
virtual void playingItemHasChanged()
{ ; }
/**
* This method is called when the data of a PlaylistItem in the playlist
* being watched changes.
*/
virtual void playlistItemDataHasChanged()
{ ; }
void clearWatched() { m_playlist = nullptr; }
void currentPlayingItemChanged();
virtual void playlistItemsChanged();
protected:
PlaylistObserver(PlaylistInterface *playlist);
const PlaylistInterface *playlist() const;
PlaylistInterfaceSignaller signaller;
private:
PlaylistInterface *m_playlist;
virtual ~PlaylistInterface();
};
#endif
......
......@@ -106,23 +106,16 @@ private:
int m_width;
};
class CollectionObserver final : public PlaylistObserver
class CollectionObserver final
{
public:
CollectionObserver(TagEditor *parent) :
PlaylistObserver(CollectionList::instance()),
m_parent(parent)
CollectionObserver(TagEditor *parent)
{
QObject::connect(&CollectionList::instance()->signaller, &PlaylistInterfaceSignaller::playingItemDataChanged, parent, [parent]{
if(parent && parent->m_currentPlaylist && parent->isVisible())
parent->slotSetItems(parent->m_currentPlaylist->selectedItems());
});
}
virtual void playlistItemDataHasChanged() override
{
if(m_parent && m_parent->m_currentPlaylist && m_parent->isVisible())
m_parent->slotSetItems(m_parent->m_currentPlaylist->selectedItems());
}
private:
TagEditor *m_parent;
};
////////////////////////////////////////////////////////////////////////////////
......
......@@ -159,7 +159,7 @@ void UpcomingPlaylist::removeIteratorOverride()
setPlaying(manager()->currentItem(), true);
Watched::currentPlayingItemChanged();
PlaylistInterface::currentPlayingItemChanged();
}
TrackSequenceManager *UpcomingPlaylist::manager() const
......
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