Skip to content

Commit

Permalink
AsyncImageProvider: Colocate coverart encode/decode methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Holzhaus committed Jun 5, 2021
1 parent 50ee247 commit 054cb0f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
26 changes: 23 additions & 3 deletions src/skin/qml/asyncimageprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include "library/coverartcache.h"

namespace {
const QString kCoverArtPrefix = QStringLiteral("coverart/");
}

namespace mixxx {
namespace skin {
namespace qml {
Expand All @@ -16,9 +20,8 @@ QQuickTextureFactory* AsyncImageResponse::textureFactory() const {
}

void AsyncImageResponse::run() {
if (m_id.startsWith(QStringLiteral("coverart/"))) {
QString trackLocation = QString::fromUtf8(QByteArray::fromBase64(
m_id.mid(9).toLatin1(), QByteArray::Base64UrlEncoding));
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
Expand Down Expand Up @@ -50,6 +53,23 @@ QQuickImageResponse* AsyncImageProvider::requestImageResponse(
return response;
}

// static
const QString AsyncImageProvider::kProviderName = QStringLiteral("mixxx");

// static
QUrl AsyncImageProvider::trackLocationToCoverArtUrl(const QString& trackLocation) {
QUrl url("image://" + kProviderName + "/" + kCoverArtPrefix);
return url.resolved(
QString::fromLatin1(trackLocation.toUtf8().toBase64(
QByteArray::Base64UrlEncoding)));
}

//static
QString AsyncImageProvider::coverArtUrlIdToTrackLocation(const QString& coverArtUrlId) {
return QString::fromUtf8(QByteArray::fromBase64(
coverArtUrlId.mid(kCoverArtPrefix.size()).toLatin1(), QByteArray::Base64UrlEncoding));
}

} // namespace qml
} // namespace skin
} // namespace mixxx
4 changes: 4 additions & 0 deletions src/skin/qml/asyncimageprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class AsyncImageProvider : public QQuickAsyncImageProvider {
QQuickImageResponse* requestImageResponse(
const QString& id, const QSize& requestedSize) override;

static const QString kProviderName;
static QUrl trackLocationToCoverArtUrl(const QString& trackLocation);
static QString coverArtUrlIdToTrackLocation(const QString& coverArtUrlId);

private:
QThreadPool pool;
};
Expand Down
6 changes: 2 additions & 4 deletions src/skin/qml/qmlplayerproxy.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "skin/qml/qmlplayerproxy.h"

#include "mixer/basetrackplayer.h"
#include "skin/qml/asyncimageprovider.h"

#define PROPERTY_IMPL(TYPE, NAME, GETTER, SETTER) \
TYPE QmlPlayerProxy::GETTER() const { \
Expand Down Expand Up @@ -164,10 +165,7 @@ QUrl QmlPlayerProxy::getCoverArtUrl() const {
}

const CoverInfo coverInfo = pTrack->getCoverInfoWithLocation();
QUrl url("image://mixxx/coverart/");
return url.resolved(
QString::fromLatin1(coverInfo.trackLocation.toUtf8().toBase64(
QByteArray::Base64UrlEncoding)));
return AsyncImageProvider::trackLocationToCoverArtUrl(coverInfo.trackLocation);
}

} // namespace qml
Expand Down
2 changes: 1 addition & 1 deletion src/skin/qml/qmlskin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ QWidget* QmlSkin::loadSkin(QWidget* pParent,

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

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

0 comments on commit 054cb0f

Please sign in to comment.