diff options
author | RJ Ryan <rryan@mixxx.org> | 2014-10-27 23:47:34 -0400 |
---|---|---|
committer | RJ Ryan <rryan@mixxx.org> | 2014-10-27 23:50:51 -0400 |
commit | 8b77640653c01b5b5df6c29149fc32265848c482 (patch) | |
tree | dc18c467363a9031b39e2938163acbe37d074d55 | |
parent | da949ae812c600290330395081d7f5ce65617306 (diff) |
Allow segregation of multiple users of CoverArtCache.
* Provide a QObject* requestor pointer when calling requestCover. In
practice this can be any number you want it to but it is particularly
useful for the requestor class to pass a pointer to itself so it can
compare the signal it gets to itself.
* Provide the original CoverInfo request.
-rw-r--r-- | src/dlgtrackinfo.cpp | 21 | ||||
-rw-r--r-- | src/dlgtrackinfo.h | 3 | ||||
-rw-r--r-- | src/library/coverartcache.cpp | 13 | ||||
-rw-r--r-- | src/library/coverartcache.h | 17 | ||||
-rw-r--r-- | src/library/coverartdelegate.cpp | 2 | ||||
-rw-r--r-- | src/test/coverartcache_test.cpp | 4 | ||||
-rw-r--r-- | src/widget/wcoverart.cpp | 13 | ||||
-rw-r--r-- | src/widget/wcoverart.h | 3 |
8 files changed, 49 insertions, 27 deletions
diff --git a/src/dlgtrackinfo.cpp b/src/dlgtrackinfo.cpp index 161c0b75a7..bc203e4689 100644 --- a/src/dlgtrackinfo.cpp +++ b/src/dlgtrackinfo.cpp @@ -82,8 +82,8 @@ void DlgTrackInfo::init(){ CoverArtCache* pCache = CoverArtCache::instance(); if (pCache != NULL) { - connect(pCache, SIGNAL(pixmapFound(int, QPixmap)), - this, SLOT(slotPixmapFound(int, QPixmap))); + connect(pCache, SIGNAL(coverFound(const QObject*, const CoverInfo&, QPixmap)), + this, SLOT(slotCoverFound(const QObject*, const CoverInfo&, QPixmap))); } connect(m_pWCoverArtLabel, SIGNAL(coverArtSelected(const CoverArt&)), this, SLOT(slotCoverArtSelected(const CoverArt&))); @@ -177,7 +177,7 @@ void DlgTrackInfo::populateFields(TrackPointer pTrack) { m_pWCoverArtLabel->setCoverArt(pTrack, m_loadedCover, QPixmap()); CoverArtCache* pCache = CoverArtCache::instance(); if (pCache != NULL) { - pCache->requestCover(m_loadedCover); + pCache->requestCover(m_loadedCover, this); } } @@ -200,13 +200,12 @@ void DlgTrackInfo::loadTrack(TrackPointer pTrack) { this, SLOT(updateTrackMetadata())); } -void DlgTrackInfo::slotPixmapFound(int trackId, QPixmap pixmap) { - qDebug() << "DlgTrackInfo::slotPixmapFound" << trackId << pixmap.size(); - if (m_pLoadedTrack.isNull()) { - return; - } - - if (m_pLoadedTrack->getId() == trackId) { +void DlgTrackInfo::slotCoverFound(const QObject* pRequestor, + const CoverInfo& info, QPixmap pixmap) { + qDebug() << "DlgTrackInfo::slotPixmapFound" << pRequestor << info + << pixmap.size(); + if (pRequestor == this && m_pLoadedTrack && + m_pLoadedTrack->getId() == info.trackId) { m_pWCoverArtLabel->setCoverArt(m_pLoadedTrack, m_loadedCover, pixmap); } } @@ -220,7 +219,7 @@ void DlgTrackInfo::slotCoverArtSelected(const CoverArt& art) { } CoverArtCache* pCache = CoverArtCache::instance(); if (pCache != NULL) { - pCache->requestCover(m_loadedCover); + pCache->requestCover(m_loadedCover, this); } } diff --git a/src/dlgtrackinfo.h b/src/dlgtrackinfo.h index c0ea90fee8..9c98e64739 100644 --- a/src/dlgtrackinfo.h +++ b/src/dlgtrackinfo.h @@ -55,7 +55,8 @@ class DlgTrackInfo : public QDialog, public Ui::DlgTrackInfo { void updateTrackMetadata(); void slotOpenInFileBrowser(); - void slotPixmapFound(int trackId, QPixmap pixmap); + void slotCoverFound(const QObject* pRequestor, const CoverInfo& info, + QPixmap pixmap); void slotCoverArtSelected(const CoverArt& art); private: diff --git a/src/library/coverartcache.cpp b/src/library/coverartcache.cpp index 80098e5691..f3d845e627 100644 --- a/src/library/coverartcache.cpp +++ b/src/library/coverartcache.cpp @@ -30,6 +30,7 @@ CoverArtCache::~CoverArtCache() { } QPixmap CoverArtCache::requestCover(const CoverInfo& requestInfo, + const QObject* pRequestor, const int desiredWidth, const bool onlyCached, const bool signalWhenDone) { @@ -45,7 +46,7 @@ QPixmap CoverArtCache::requestCover(const CoverInfo& requestInfo, if (requestInfo.type == CoverInfo::NONE) { if (signalWhenDone) { - emit(pixmapFound(requestInfo.trackId, QPixmap())); + emit(coverFound(pRequestor, requestInfo, QPixmap())); } return QPixmap(); } @@ -68,7 +69,7 @@ QPixmap CoverArtCache::requestCover(const CoverInfo& requestInfo, QPixmap pixmap; if (QPixmapCache::find(cacheKey, &pixmap)) { if (signalWhenDone) { - emit(pixmapFound(requestInfo.trackId, pixmap)); + emit(coverFound(pRequestor, requestInfo, pixmap)); } return pixmap; } @@ -84,8 +85,8 @@ QPixmap CoverArtCache::requestCover(const CoverInfo& requestInfo, m_runningIds.insert(requestInfo.trackId); QFutureWatcher<FutureResult>* watcher = new QFutureWatcher<FutureResult>(this); QFuture<FutureResult> future = QtConcurrent::run( - this, &CoverArtCache::loadCover, requestInfo, desiredWidth, - signalWhenDone); + this, &CoverArtCache::loadCover, requestInfo, pRequestor, + desiredWidth, signalWhenDone); connect(watcher, SIGNAL(finished()), this, SLOT(coverLoaded())); watcher->setFuture(future); return QPixmap(); @@ -93,6 +94,7 @@ QPixmap CoverArtCache::requestCover(const CoverInfo& requestInfo, CoverArtCache::FutureResult CoverArtCache::loadCover( const CoverInfo& info, + const QObject* pRequestor, const int desiredWidth, const bool signalWhenDone) { if (sDebug) { @@ -101,6 +103,7 @@ CoverArtCache::FutureResult CoverArtCache::loadCover( } FutureResult res; + res.pRequestor = pRequestor; res.cover.info = info; res.desiredWidth = desiredWidth; res.signalWhenDone = signalWhenDone; @@ -147,7 +150,7 @@ void CoverArtCache::coverLoaded() { } if (res.signalWhenDone) { - emit(pixmapFound(res.cover.info.trackId, pixmap)); + emit(coverFound(res.pRequestor, res.cover.info, pixmap)); } m_runningIds.remove(res.cover.info.trackId); diff --git a/src/library/coverartcache.h b/src/library/coverartcache.h index 003320cbe1..6be8e693f8 100644 --- a/src/library/coverartcache.h +++ b/src/library/coverartcache.h @@ -12,19 +12,33 @@ class CoverArtCache : public QObject, public Singleton<CoverArtCache> { public: /* This method is used to request a cover art pixmap. * + * @param pRequestor : an arbitrary pointer (can be any number you'd like, + * really) that will be provided in the coverFound signal for this + * request. This allows you to match requests with their responses. + * * @param onlyCached : if it is 'true', the method will NOT try to load * covers from the given 'coverLocation' and it will also NOT run the * search algorithm. * In this way, the method will just look into CoverCache and return * a Pixmap if it is already loaded in the QPixmapCache. + * + * TODO(rryan): Provide a QObject* and a SLOT to invoke directly. Why make + * everyone filter the signals they receive? */ QPixmap requestCover(const CoverInfo& info, + const QObject* pRequestor, const int desiredWidth = 0, const bool onlyCached = false, const bool signalWhenDone = true); struct FutureResult { + FutureResult() : pRequestor(NULL), + desiredWidth(0), + signalWhenDone(false) { + } + CoverArt cover; + const QObject* pRequestor; int desiredWidth; bool signalWhenDone; }; @@ -34,7 +48,7 @@ class CoverArtCache : public QObject, public Singleton<CoverArtCache> { void coverLoaded(); signals: - void pixmapFound(int trackId, QPixmap pixmap); + void coverFound(const QObject* requestor, const CoverInfo& info, QPixmap pixmap); protected: CoverArtCache(); @@ -44,6 +58,7 @@ class CoverArtCache : public QObject, public Singleton<CoverArtCache> { // Load cover from path indicated in coverInfo. WARNING: This is run in a // worker thread. FutureResult loadCover(const CoverInfo& coverInfo, + const QObject* pRequestor, const int desiredWidth, const bool emitSignals); diff --git a/src/library/coverartdelegate.cpp b/src/library/coverartdelegate.cpp index f7fff3669c..0c13fb5b62 100644 --- a/src/library/coverartdelegate.cpp +++ b/src/library/coverartdelegate.cpp @@ -81,7 +81,7 @@ void CoverArtDelegate::paint(QPainter *painter, // Do not signal when done sine we don't listen to CoverArtCache for updates // and instead refresh on a timer in WTrackTableView. - QPixmap pixmap = pCache->requestCover(info, 100, m_bOnlyCachedCover, false); + QPixmap pixmap = pCache->requestCover(info, this, 100, m_bOnlyCachedCover, false); if (!pixmap.isNull()) { int width = math_min(pixmap.width(), option.rect.width()); int height = math_min(pixmap.height(), option.rect.height()); diff --git a/src/test/coverartcache_test.cpp b/src/test/coverartcache_test.cpp index 5be57a35ed..0ac3288172 100644 --- a/src/test/coverartcache_test.cpp +++ b/src/test/coverartcache_test.cpp @@ -71,7 +71,7 @@ TEST_F(CoverArtCacheTest, loadCover) { info.hash = "coverhash"; // fake cover hash CoverArtCache::FutureResult res; - res = CoverArtCache::loadCover(info, 0, false); + res = CoverArtCache::loadCover(info, NULL, 0, false); EXPECT_EQ(info.trackId, res.cover.info.trackId); EXPECT_QSTRING_EQ(info.coverLocation, res.cover.info.coverLocation); EXPECT_QSTRING_EQ(info.hash, res.cover.info.hash); @@ -82,7 +82,7 @@ TEST_F(CoverArtCacheTest, loadCover) { info.source = CoverInfo::GUESSED; info.coverLocation = QString(); info.trackLocation = kTrackLocationTest; - res = CoverArtCache::loadCover(info, 0, false); + res = CoverArtCache::loadCover(info, NULL, 0, false); EXPECT_EQ(info.trackId, res.cover.info.trackId); EXPECT_QSTRING_EQ(QString(), res.cover.info.coverLocation); EXPECT_QSTRING_EQ(info.hash, res.cover.info.hash); diff --git a/src/widget/wcoverart.cpp b/src/widget/wcoverart.cpp index b7e4b3ec7a..c476f4f3cd 100644 --- a/src/widget/wcoverart.cpp +++ b/src/widget/wcoverart.cpp @@ -28,8 +28,8 @@ WCoverArt::WCoverArt(QWidget* parent, CoverArtCache* pCache = CoverArtCache::instance(); if (pCache != NULL) { - connect(pCache, SIGNAL(pixmapFound(int, QPixmap)), - this, SLOT(slotPixmapFound(int, QPixmap))); + connect(pCache, SIGNAL(coverFound(const QObject*, const CoverInfo&, QPixmap)), + this, SLOT(slotCoverFound(const QObject*, const CoverInfo&, QPixmap))); } connect(m_pMenu, SIGNAL(coverArtSelected(const CoverArt&)), this, SLOT(slotCoverArtSelected(const CoverArt&))); @@ -105,11 +105,14 @@ void WCoverArt::slotReset() { update(); } -void WCoverArt::slotPixmapFound(int trackId, QPixmap pixmap) { +void WCoverArt::slotCoverFound(const QObject* pRequestor, const CoverInfo& info, + QPixmap pixmap) { if (!m_bEnable) { return; } - if (m_lastRequestedCover.trackId == trackId) { + + if (pRequestor == this && m_loadedTrack && + m_loadedTrack->getId() == info.trackId) { m_loadedCover = pixmap; m_loadedCoverScaled = scaledCoverArt(pixmap); update(); @@ -123,7 +126,7 @@ void WCoverArt::slotTrackCoverArtUpdated() { m_lastRequestedCover.trackLocation = m_loadedTrack->getLocation(); CoverArtCache* pCache = CoverArtCache::instance(); if (pCache != NULL) { - pCache->requestCover(m_lastRequestedCover); + pCache->requestCover(m_lastRequestedCover, this); } } } diff --git a/src/widget/wcoverart.h b/src/widget/wcoverart.h index fcd27f725c..eda0778cbe 100644 --- a/src/widget/wcoverart.h +++ b/src/widget/wcoverart.h @@ -32,7 +32,8 @@ class WCoverArt : public QWidget, public WBaseWidget { void trackDropped(QString filename, QString group); private slots: - void slotPixmapFound(int trackId, QPixmap pixmap); + void slotCoverFound(const QObject* pRequestor, const CoverInfo& info, + QPixmap pixmap); void slotCoverArtSelected(const CoverArt& art); void slotTrackCoverArtUpdated(); |