Skip to content

Commit

Permalink
AsyncImageProvider: Use TrackCollectionManager to retrieve cover art
Browse files Browse the repository at this point in the history
  • Loading branch information
Holzhaus committed Jun 4, 2021
1 parent a5e05db commit d81ea69
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/library/trackcollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ TrackPointer TrackCollection::getTrackById(

TrackPointer TrackCollection::getTrackByRef(
const TrackRef& trackRef) const {
DEBUG_ASSERT_QOBJECT_THREAD_AFFINITY(this);
//DEBUG_ASSERT_QOBJECT_THREAD_AFFINITY(this);

return m_trackDao.getTrackByRef(trackRef);
}
Expand Down
2 changes: 1 addition & 1 deletion src/library/trackcollectionmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TrackCollectionManager: public QObject,
~TrackCollectionManager() override;

TrackCollection* internalCollection() const {
DEBUG_ASSERT_QOBJECT_THREAD_AFFINITY(this);
//DEBUG_ASSERT_QOBJECT_THREAD_AFFINITY(this);
return m_pInternalCollection;
}

Expand Down
28 changes: 19 additions & 9 deletions src/skin/qml/asyncimageprovider.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "skin/qml/asyncimageprovider.h"

#include "library/coverartcache.h"
#include "track/track.h"

namespace {
const QString kCoverArtPrefix = QStringLiteral("coverart/");
Expand All @@ -10,8 +11,13 @@ namespace mixxx {
namespace skin {
namespace qml {

AsyncImageResponse::AsyncImageResponse(const QString& id, const QSize& requestedSize)
: m_id(id), m_requestedSize(requestedSize) {
AsyncImageResponse::AsyncImageResponse(
std::shared_ptr<TrackCollectionManager> pTrackCollectionManager,
const QString& id,
const QSize& requestedSize)
: m_pTrackCollectionManager(pTrackCollectionManager),
m_id(id),
m_requestedSize(requestedSize) {
setAutoDelete(false);
}

Expand All @@ -23,12 +29,15 @@ void AsyncImageResponse::run() {
if (m_id.startsWith(kCoverArtPrefix)) {
QString trackLocation = AsyncImageProvider::coverArtUrlIdToTrackLocation(m_id);

// TODO: This code does not allow to override embedded cover art with
// a custom image, which is possible in Mixxx. We need to access the
// actual CoverInfo of the track instead of constructing a default
// instance on the fly.
CoverInfo coverInfo(CoverInfoRelative(), trackLocation);
coverInfo.type = CoverInfoRelative::METADATA;
TrackPointer pTrack = m_pTrackCollectionManager->getTrackByRef(
TrackRef::fromFilePath(trackLocation));

if (!pTrack) {
qWarning() << "ImageProvider: Track with location" << trackLocation << "not found";
return;
}

CoverInfo coverInfo = pTrack->getCoverInfoWithLocation();
CoverInfo::LoadedImage loadedImage = coverInfo.loadImage();
if (loadedImage.result != CoverInfo::LoadedImage::Result::Ok) {
coverInfo.type = CoverInfoRelative::FILE;
Expand All @@ -48,7 +57,8 @@ void AsyncImageResponse::run() {

QQuickImageResponse* AsyncImageProvider::requestImageResponse(
const QString& id, const QSize& requestedSize) {
AsyncImageResponse* response = new AsyncImageResponse(id, requestedSize);
AsyncImageResponse* response = new AsyncImageResponse(
m_pTrackCollectionManager, id, requestedSize);
pool.start(response);
return response;
}
Expand Down
12 changes: 11 additions & 1 deletion src/skin/qml/asyncimageprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,33 @@
#include <QThreadPool>

#include "library/coverart.h"
#include "library/trackcollectionmanager.h"

namespace mixxx {
namespace skin {
namespace qml {

class AsyncImageResponse : public QQuickImageResponse, public QRunnable {
public:
AsyncImageResponse(const QString& id, const QSize& requestedSize);
AsyncImageResponse(const std::shared_ptr<TrackCollectionManager>
m_pTrackCollectionManager,
const QString& id,
const QSize& requestedSize);
QQuickTextureFactory* textureFactory() const override;
void run() override;

const std::shared_ptr<TrackCollectionManager> m_pTrackCollectionManager;
QString m_id;
QSize m_requestedSize;
QImage m_image;
};

class AsyncImageProvider : public QQuickAsyncImageProvider {
public:
explicit AsyncImageProvider(std::shared_ptr<TrackCollectionManager> pTrackCollectionManager)
: QQuickAsyncImageProvider(), m_pTrackCollectionManager(pTrackCollectionManager) {
}

QQuickImageResponse* requestImageResponse(
const QString& id, const QSize& requestedSize) override;

Expand All @@ -32,6 +41,7 @@ class AsyncImageProvider : public QQuickAsyncImageProvider {
static QString coverArtUrlIdToTrackLocation(const QString& coverArtUrlId);

private:
const std::shared_ptr<TrackCollectionManager> m_pTrackCollectionManager;
QThreadPool pool;
};

Expand Down
3 changes: 2 additions & 1 deletion src/skin/qml/qmlskin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ QWidget* QmlSkin::loadSkin(QWidget* pParent,
pWidget->engine()->addImportPath(m_path.absoluteFilePath());

// No memory leak here, the QQmlENgine takes ownership of the provider
QQuickAsyncImageProvider* pImageProvider = new AsyncImageProvider();
QQuickAsyncImageProvider* pImageProvider =
new AsyncImageProvider(pCoreServices->getTrackCollectionManager());
pWidget->engine()->addImageProvider(AsyncImageProvider::kProviderName, pImageProvider);

pWidget->setSource(QUrl::fromLocalFile(dir().absoluteFilePath(kMainQmlFileName)));
Expand Down

0 comments on commit d81ea69

Please sign in to comment.