feat: now-playing album art scales to full dock width
Stores the raw downloaded pixmap and rescales it (smooth, aspect-ratio preserved) whenever the dock is resized, so the image always fills the available width. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,8 +7,6 @@
|
|||||||
namespace Context
|
namespace Context
|
||||||
{
|
{
|
||||||
|
|
||||||
static constexpr int ArtSize = 96;
|
|
||||||
|
|
||||||
View::View(QobuzBackend *backend, QWidget *parent)
|
View::View(QobuzBackend *backend, QWidget *parent)
|
||||||
: QDockWidget(tr("Now Playing"), parent)
|
: QDockWidget(tr("Now Playing"), parent)
|
||||||
, m_backend(backend)
|
, m_backend(backend)
|
||||||
@@ -25,12 +23,11 @@ View::View(QobuzBackend *backend, QWidget *parent)
|
|||||||
layout->setSpacing(6);
|
layout->setSpacing(6);
|
||||||
|
|
||||||
m_albumArt = new QLabel(container);
|
m_albumArt = new QLabel(container);
|
||||||
m_albumArt->setFixedSize(ArtSize, ArtSize);
|
|
||||||
m_albumArt->setScaledContents(true);
|
|
||||||
m_albumArt->setAlignment(Qt::AlignCenter);
|
m_albumArt->setAlignment(Qt::AlignCenter);
|
||||||
m_albumArt->setStyleSheet(QStringLiteral(
|
m_albumArt->setStyleSheet(QStringLiteral(
|
||||||
"background: #1a1a1a; border-radius: 4px;"));
|
"background: #1a1a1a; border-radius: 4px;"));
|
||||||
layout->addWidget(m_albumArt, 0, Qt::AlignCenter);
|
m_albumArt->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||||
|
layout->addWidget(m_albumArt);
|
||||||
|
|
||||||
m_title = new QLabel(tr("Not playing"), container);
|
m_title = new QLabel(tr("Not playing"), container);
|
||||||
m_title->setAlignment(Qt::AlignCenter);
|
m_title->setAlignment(Qt::AlignCenter);
|
||||||
@@ -80,9 +77,23 @@ void View::onArtReady(QNetworkReply *reply)
|
|||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
if (reply->error() != QNetworkReply::NoError)
|
if (reply->error() != QNetworkReply::NoError)
|
||||||
return;
|
return;
|
||||||
QPixmap pix;
|
if (m_artPixmap.loadFromData(reply->readAll()))
|
||||||
if (pix.loadFromData(reply->readAll()))
|
scaleArtToWidth();
|
||||||
m_albumArt->setPixmap(pix);
|
}
|
||||||
|
|
||||||
|
void View::resizeEvent(QResizeEvent *event)
|
||||||
|
{
|
||||||
|
QDockWidget::resizeEvent(event);
|
||||||
|
scaleArtToWidth();
|
||||||
|
}
|
||||||
|
|
||||||
|
void View::scaleArtToWidth()
|
||||||
|
{
|
||||||
|
if (m_artPixmap.isNull()) return;
|
||||||
|
// Available width = dock width minus the 8px margins on each side
|
||||||
|
const int side = qMax(32, widget() ? widget()->width() - 16 : width() - 16);
|
||||||
|
m_albumArt->setFixedSize(side, side);
|
||||||
|
m_albumArt->setPixmap(m_artPixmap.scaled(side, side, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Context
|
} // namespace Context
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
#include <QDockWidget>
|
#include <QDockWidget>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
#include <QPixmap>
|
||||||
|
#include <QResizeEvent>
|
||||||
#include <QNetworkAccessManager>
|
#include <QNetworkAccessManager>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
@@ -21,12 +23,18 @@ namespace Context
|
|||||||
void onTrackChanged(const QJsonObject &track);
|
void onTrackChanged(const QJsonObject &track);
|
||||||
void onArtReady(QNetworkReply *reply);
|
void onArtReady(QNetworkReply *reply);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void resizeEvent(QResizeEvent *event) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void scaleArtToWidth();
|
||||||
|
|
||||||
QobuzBackend *m_backend = nullptr;
|
QobuzBackend *m_backend = nullptr;
|
||||||
QLabel *m_albumArt = nullptr;
|
QLabel *m_albumArt = nullptr;
|
||||||
QLabel *m_title = nullptr;
|
QLabel *m_title = nullptr;
|
||||||
QLabel *m_artist = nullptr;
|
QLabel *m_artist = nullptr;
|
||||||
QNetworkAccessManager *m_nam = nullptr;
|
QNetworkAccessManager *m_nam = nullptr;
|
||||||
QString m_currentArtUrl;
|
QString m_currentArtUrl;
|
||||||
|
QPixmap m_artPixmap;
|
||||||
};
|
};
|
||||||
} // namespace Context
|
} // namespace Context
|
||||||
|
|||||||
Reference in New Issue
Block a user