Skip to content

Commit e2eba3a

Browse files
authored
Merge pull request #2247 from Be-ing/coverart_scaling
fix scaling of cover art on high pixel density screens
2 parents 3d1e817 + 71e7ce4 commit e2eba3a

7 files changed

+46
-27
lines changed

src/library/coverartcache.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mixxx::Logger kLogger("CoverArtCache");
1515

1616
QString pixmapCacheKey(quint16 hash, int width) {
1717
return QString("CoverArtCache_%1_%2")
18-
.arg(QString::number(hash)).arg(width);
18+
.arg(QString::number(hash), QString::number(width));
1919
}
2020

2121
// The transformation mode when scaling images
@@ -26,10 +26,10 @@ inline QImage resizeImageWidth(const QImage& image, int width) {
2626
return image.scaledToWidth(width, kTransformationMode);
2727
}
2828

29-
} // anonymous namespace
30-
3129
const bool sDebug = false;
3230

31+
} // anonymous namespace
32+
3333
CoverArtCache::CoverArtCache() {
3434
// The initial QPixmapCache limit is 10MB.
3535
// But it is not used just by the coverArt stuff,
@@ -80,6 +80,9 @@ QPixmap CoverArtCache::requestCover(const CoverInfo& requestInfo,
8080

8181
QPixmap pixmap;
8282
if (QPixmapCache::find(cacheKey, &pixmap)) {
83+
if (sDebug) {
84+
kLogger.debug() << "CoverArtCache::requestCover cover found in cache" << requestInfo << signalWhenDone;
85+
}
8386
if (signalWhenDone) {
8487
emit(coverFound(pRequestor, requestInfo, pixmap, true));
8588
}
@@ -93,6 +96,9 @@ QPixmap CoverArtCache::requestCover(const CoverInfo& requestInfo,
9396
return QPixmap();
9497
}
9598

99+
if (sDebug) {
100+
kLogger.debug() << "CoverArtCache::requestCover starting future for" << requestInfo;
101+
}
96102
m_runningRequests.insert(requestId);
97103
// The watcher will be deleted in coverLoaded()
98104
QFutureWatcher<FutureResult>* watcher = new QFutureWatcher<FutureResult>(this);

src/library/coverartdelegate.cpp

+7-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "library/coverartdelegate.h"
55
#include "library/coverartcache.h"
66
#include "library/dao/trackschema.h"
7+
#include "util/compatibility.h"
78
#include "util/math.h"
89

910
CoverArtDelegate::CoverArtDelegate(QTableView* parent)
@@ -23,10 +24,8 @@ CoverArtDelegate::CoverArtDelegate(QTableView* parent)
2324

2425
CoverArtCache* pCache = CoverArtCache::instance();
2526
if (pCache) {
26-
connect(pCache, SIGNAL(coverFound(const QObject*, const CoverInfoRelative&,
27-
QPixmap, bool)),
28-
this, SLOT(slotCoverFound(const QObject*, const CoverInfoRelative&,
29-
QPixmap, bool)));
27+
connect(pCache, &CoverArtCache::coverFound,
28+
this, &CoverArtDelegate::slotCoverFound);
3029
}
3130

3231
TrackModel* pTrackModel = NULL;
@@ -108,17 +107,14 @@ void CoverArtDelegate::paintItem(QPainter *painter,
108107
info.hash = index.sibling(index.row(), m_iCoverHashColumn).data().toUInt();
109108
info.trackLocation = index.sibling(index.row(), m_iTrackLocationColumn).data().toString();
110109

110+
double scaleFactor = getDevicePixelRatioF(static_cast<QWidget*>(parent()));
111111
// We listen for updates via slotCoverFound above and signal to
112112
// BaseSqlTableModel when a row's cover is ready.
113-
QPixmap pixmap = pCache->requestCover(info, this, option.rect.width(),
113+
QPixmap pixmap = pCache->requestCover(info, this, option.rect.width() * scaleFactor,
114114
m_bOnlyCachedCover, true);
115115
if (!pixmap.isNull()) {
116-
int width = math_min(pixmap.width(), option.rect.width());
117-
int height = math_min(pixmap.height(), option.rect.height());
118-
QRect target(option.rect.x(), option.rect.y(),
119-
width, height);
120-
QRect source(0, 0, target.width(), target.height());
121-
painter->drawPixmap(target, pixmap, source);
116+
pixmap.setDevicePixelRatio(scaleFactor);
117+
painter->drawPixmap(option.rect.topLeft(), pixmap);
122118
} else if (!m_bOnlyCachedCover) {
123119
// If we asked for a non-cache image and got a null pixmap, then our
124120
// request was queued.

src/library/dlgcoverartfullsize.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "library/dlgcoverartfullsize.h"
66
#include "library/coverartutils.h"
77
#include "library/coverartcache.h"
8+
#include "util/compatibility.h"
89

910
DlgCoverArtFullSize::DlgCoverArtFullSize(QWidget* parent, BaseTrackPlayer* pPlayer)
1011
: QDialog(parent),
@@ -131,8 +132,9 @@ void DlgCoverArtFullSize::slotCoverFound(const QObject* pRequestor,
131132
dialogSize.scale(availableScreenSpace.width(), dialogSize.height(),
132133
Qt::KeepAspectRatio);
133134
}
134-
QPixmap resizedPixmap = m_pixmap.scaled(size(),
135+
QPixmap resizedPixmap = m_pixmap.scaled(size() * getDevicePixelRatioF(this),
135136
Qt::KeepAspectRatio, Qt::SmoothTransformation);
137+
resizedPixmap.setDevicePixelRatio(getDevicePixelRatioF(this));
136138
coverArt->setPixmap(resizedPixmap);
137139
// center the window
138140
setGeometry(QStyle::alignedRect(
@@ -181,8 +183,9 @@ void DlgCoverArtFullSize::resizeEvent(QResizeEvent* event) {
181183
return;
182184
}
183185
// qDebug() << "DlgCoverArtFullSize::resizeEvent" << size();
184-
QPixmap resizedPixmap = m_pixmap.scaled(size(),
186+
QPixmap resizedPixmap = m_pixmap.scaled(size() * getDevicePixelRatioF(this),
185187
Qt::KeepAspectRatio, Qt::SmoothTransformation);
188+
resizedPixmap.setDevicePixelRatio(getDevicePixelRatioF(this));
186189
coverArt->setPixmap(resizedPixmap);
187190
}
188191

src/library/dlgtrackinfo.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "track/beatfactory.h"
1111
#include "track/keyfactory.h"
1212
#include "track/keyutils.h"
13+
#include "util/compatibility.h"
1314
#include "util/duration.h"
1415

1516
const int kFilterLength = 80;

src/widget/wcoverart.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "library/coverartcache.h"
1313
#include "library/coverartutils.h"
1414
#include "library/dlgcoverartfullsize.h"
15+
#include "util/compatibility.h"
1516
#include "util/dnd.h"
1617
#include "util/math.h"
1718

@@ -190,7 +191,11 @@ QPixmap WCoverArt::scaledCoverArt(const QPixmap& normal) {
190191
if (normal.isNull()) {
191192
return QPixmap();
192193
}
193-
return normal.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
194+
QPixmap scaled;
195+
scaled = normal.scaled(size() * getDevicePixelRatioF(this),
196+
Qt::KeepAspectRatio, Qt::SmoothTransformation);
197+
scaled.setDevicePixelRatio(getDevicePixelRatioF(this));
198+
return scaled;
194199
}
195200

196201
void WCoverArt::paintEvent(QPaintEvent* /*unused*/) {

src/widget/wcoverartlabel.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "library/dlgcoverartfullsize.h"
66
#include "library/coverartutils.h"
7+
#include "util/compatibility.h"
78

89
static const QSize s_labelDisplaySize = QSize(100, 100);
910

@@ -23,7 +24,8 @@ WCoverArtLabel::WCoverArtLabel(QWidget* parent)
2324
connect(m_pCoverMenu, SIGNAL(reloadCoverArt()),
2425
this, SIGNAL(reloadCoverArt()));
2526

26-
m_defaultCover = m_defaultCover.scaled(s_labelDisplaySize,
27+
m_defaultCover.setDevicePixelRatio(getDevicePixelRatioF(this));
28+
m_defaultCover = m_defaultCover.scaled(s_labelDisplaySize * getDevicePixelRatioF(this),
2729
Qt::KeepAspectRatio,
2830
Qt::SmoothTransformation);
2931
setPixmap(m_defaultCover);
@@ -38,18 +40,18 @@ void WCoverArtLabel::setCoverArt(const CoverInfo& coverInfo,
3840
QPixmap px) {
3941
qDebug() << "WCoverArtLabel::setCoverArt" << coverInfo << px.size();
4042

41-
m_loadedCover = px;
43+
m_loadedCover = px.scaled(s_labelDisplaySize * getDevicePixelRatioF(this),
44+
Qt::KeepAspectRatio, Qt::SmoothTransformation);
45+
m_loadedCover.setDevicePixelRatio(getDevicePixelRatioF(this));
4246
m_pCoverMenu->setCoverArt(coverInfo);
4347

44-
45-
if (px.isNull()) {
48+
if (m_loadedCover.isNull()) {
4649
setPixmap(m_defaultCover);
4750
} else {
48-
setPixmap(px.scaled(s_labelDisplaySize, Qt::KeepAspectRatio,
49-
Qt::SmoothTransformation));
51+
setPixmap(m_loadedCover);
5052
}
5153

52-
QSize frameSize = pixmap()->size();
54+
QSize frameSize = pixmap()->size() / getDevicePixelRatioF(this);
5355
frameSize += QSize(2,2); // margin
5456
setMinimumSize(frameSize);
5557
setMaximumSize(frameSize);

src/widget/wspinny.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "control/controlobject.h"
99
#include "control/controlproxy.h"
1010
#include "library/coverartcache.h"
11+
#include "util/compatibility.h"
1112
#include "util/dnd.h"
1213
#include "waveform/sharedglcontext.h"
1314
#include "util/math.h"
@@ -318,6 +319,8 @@ void WSpinny::render() {
318319
&m_dGhostAngleCurrentPlaypos);
319320
}
320321

322+
double scaleFactor = getDevicePixelRatioF(this);
323+
321324
QPainter p(this);
322325
p.setRenderHint(QPainter::Antialiasing);
323326
p.setRenderHint(QPainter::HighQualityAntialiasing);
@@ -329,8 +332,8 @@ void WSpinny::render() {
329332

330333
if (m_bShowCover && !m_loadedCoverScaled.isNull()) {
331334
// Some covers aren't square, so center them.
332-
int x = (width() - m_loadedCoverScaled.width()) / 2;
333-
int y = (height() - m_loadedCoverScaled.height()) / 2;
335+
int x = (width() - m_loadedCoverScaled.width() / scaleFactor) / 2;
336+
int y = (height() - m_loadedCoverScaled.height() / scaleFactor) / 2;
334337
p.drawPixmap(x, y, m_loadedCoverScaled);
335338
}
336339

@@ -403,7 +406,10 @@ QPixmap WSpinny::scaledCoverArt(const QPixmap& normal) {
403406
if (normal.isNull()) {
404407
return QPixmap();
405408
}
406-
return normal.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
409+
QPixmap scaled = normal.scaled(size() * getDevicePixelRatioF(this),
410+
Qt::KeepAspectRatio, Qt::SmoothTransformation);
411+
scaled.setDevicePixelRatio(getDevicePixelRatioF(this));
412+
return scaled;
407413
}
408414

409415
void WSpinny::resizeEvent(QResizeEvent* /*unused*/) {

0 commit comments

Comments
 (0)