Fix a regression in 6.0 that caused the default cover to be rendered small in some contexts

This commit is contained in:
Kovid Goyal 2022-07-20 08:22:47 +05:30
parent ef6c2b439f
commit 4f68d2e7c5
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 22 additions and 14 deletions

View File

@ -13,10 +13,10 @@ from functools import lru_cache
from qt.core import (
QApplication, QBuffer, QByteArray, QColor, QDateTime, QDesktopServices, QDialog,
QDialogButtonBox, QEvent, QFile, QFileDialog, QFileIconProvider, QFileInfo,
QFont, QFontDatabase, QFontInfo, QFontMetrics, QGuiApplication, QIcon, QIODevice,
QLocale, QNetworkProxyFactory, QObject, QPalette, QResource, QSettings,
QSocketNotifier, QStringListModel, Qt, QThread, QTimer, QTranslator,
QUrl, pyqtSignal, pyqtSlot
QFont, QFontDatabase, QFontInfo, QFontMetrics, QGuiApplication, QIcon,
QImageReader, QImageWriter, QIODevice, QLocale, QNetworkProxyFactory, QObject,
QPalette, QResource, QSettings, QSocketNotifier, QStringListModel, Qt, QThread,
QTimer, QTranslator, QUrl, pyqtSignal, pyqtSlot
)
from threading import Lock, RLock
@ -201,7 +201,6 @@ class IconResourceManager:
ans = self(name)
ba = QByteArray()
if ans.availableSizes():
from qt.core import QImageWriter
pmap = ans.pixmap(ans.availableSizes()[0])
buf = QBuffer(ba)
buf.open(QIODevice.OpenModeFlag.WriteOnly)
@ -1247,13 +1246,19 @@ class Application(QApplication):
load_builtin_fonts()
@lru_cache(maxsize=256)
def cached_qimage(self, name):
return self.cached_qpixmap(name).toImage()
def cached_qimage(self, name, device_pixel_ratio=0):
return self.cached_qpixmap(name, device_pixel_ratio).toImage()
@lru_cache(maxsize=256)
def cached_qpixmap(self, name):
def cached_qpixmap(self, name, device_pixel_ratio=0):
# get the actual size of the image since QIcon does not tell us this for
# icons loaded from a theme
path = I(name, allow_user_override=False)
r = QImageReader(path)
ic = QIcon.ic(name)
return ic.pixmap((ic.availableSizes() or (256,))[0])
if not device_pixel_ratio:
device_pixel_ratio = self.devicePixelRatio()
return ic.pixmap(r.size(), device_pixel_ratio)
def stylesheet_for_line_edit(self, is_error=False):
return 'QLineEdit { border: 2px solid %s; border-radius: 3px }' % (

View File

@ -530,7 +530,7 @@ class CoverView(QWidget): # {{{
QSizePolicy.Policy.Expanding if vertical else QSizePolicy.Policy.Minimum,
QSizePolicy.Policy.Expanding)
self.default_pixmap = QApplication.instance().cached_qpixmap('default_cover.png')
self.default_pixmap = QApplication.instance().cached_qpixmap('default_cover.png', device_pixel_ratio=self.devicePixelRatio())
self.pixmap = self.default_pixmap
self.pwidth = self.pheight = None
self.data = {}

View File

@ -106,12 +106,12 @@ class MetadataWidget(Widget, Ui_Form):
pm = QPixmap()
pm.loadFromData(cover)
if not pm.isNull():
pm.setDevicePixelRatio(getattr(self, 'devicePixelRatioF', self.devicePixelRatio)())
pm.setDevicePixelRatio(self.devicePixelRatio())
self.cover.setPixmap(pm)
self.cover_data = cover
self.set_cover_tooltip(pm)
else:
pm = QApplication.instance().cached_qpixmap('default_cover.png')
pm = QApplication.instance().cached_qpixmap('default_cover.png', device_pixel_ratio=self.devicePixelRatio())
self.cover.setPixmap(pm)
self.cover.setToolTip(_('This book has no cover'))
for x in ('author', 'series', 'publisher'):

View File

@ -268,7 +268,10 @@ class BooksModel(QAbstractTableModel): # {{{
@property
def default_image(self):
return QApplication.instance().cached_qimage('default_cover.png')
from calibre.gui2.ui import get_gui
gui = get_gui()
dpr = gui.devicePixelRatio() if gui else 0
return QApplication.instance().cached_qimage('default_cover.png', device_pixel_ratio=dpr)
def _clear_caches(self):
self.color_cache = defaultdict(dict)

View File

@ -1285,7 +1285,7 @@ class Cover(ImageView): # {{{
if cdata:
pm.loadFromData(cdata)
if pm.isNull():
pm = QApplication.instance().cached_qpixmap('default_cover.png')
pm = QApplication.instance().cached_qpixmap('default_cover.png', device_pixel_ratio=self.devicePixelRatio())
else:
self._cdata = cdata
pm.setDevicePixelRatio(getattr(self, 'devicePixelRatioF', self.devicePixelRatio)())