Couple more high DPI fixes for the editor

This commit is contained in:
Kovid Goyal 2016-08-24 20:11:20 +05:30
parent 6be52464ae
commit 04d8f2cda5
2 changed files with 17 additions and 14 deletions

View File

@ -108,11 +108,13 @@ class ImageDelegate(QStyledItemDelegate):
except: except:
pass pass
else: else:
dpr = painter.device().devicePixelRatio()
cover.loadFromData(raw) cover.loadFromData(raw)
cover.setDevicePixelRatio(dpr)
if not cover.isNull(): if not cover.isNull():
scaled, width, height = fit_image(cover.width(), cover.height(), self.cover_size.width(), self.cover_size.height()) scaled, width, height = fit_image(cover.width(), cover.height(), self.cover_size.width(), self.cover_size.height())
if scaled: if scaled:
cover = self.cover_cache[name] = cover.scaled(width, height, transformMode=Qt.SmoothTransformation) cover = self.cover_cache[name] = cover.scaled(int(dpr*width), int(dpr*height), transformMode=Qt.SmoothTransformation)
painter.save() painter.save()
try: try:
@ -121,8 +123,8 @@ class ImageDelegate(QStyledItemDelegate):
trect = QRect(rect) trect = QRect(rect)
rect.setBottom(rect.bottom() - self.title_height) rect.setBottom(rect.bottom() - self.title_height)
if not cover.isNull(): if not cover.isNull():
dx = max(0, int((rect.width() - cover.width())/2.0)) dx = max(0, int((rect.width() - int(cover.width()/cover.devicePixelRatio()))/2.0))
dy = max(0, rect.height() - cover.height()) dy = max(0, rect.height() - int(cover.height()/cover.devicePixelRatio()))
rect.adjust(dx, dy, -dx, 0) rect.adjust(dx, dy, -dx, 0)
painter.drawPixmap(rect, cover) painter.drawPixmap(rect, cover)
rect = trect rect = trect
@ -455,7 +457,5 @@ if __name__ == '__main__':
from calibre.gui2.tweak_book.boss import get_container from calibre.gui2.tweak_book.boss import get_container
set_current_container(get_container(sys.argv[-1])) set_current_container(get_container(sys.argv[-1]))
d = ChooseFolder() d = InsertImage(for_browsing=True)
if d.exec_() == d.Accepted: d.exec_()
print (repr(d.chosen_folder))

View File

@ -373,25 +373,28 @@ class ImagesDelegate(QStyledItemDelegate):
k = (th, entry.name) k = (th, entry.name)
pmap = self.cache.get(k) pmap = self.cache.get(k)
if pmap is None: if pmap is None:
pmap = self.cache[k] = self.pixmap(th, entry) pmap = self.cache[k] = self.pixmap(th, entry, painter.device().devicePixelRatio())
if pmap.isNull(): if pmap.isNull():
bottom = option.rect.top() bottom = option.rect.top()
else: else:
m = 2 * self.MARGIN m = 2 * self.MARGIN
x = option.rect.left() + (option.rect.width() - m - pmap.width()) // 2 x = option.rect.left() + (option.rect.width() - m - int(pmap.width()/pmap.devicePixelRatio())) // 2
painter.drawPixmap(x, option.rect.top() + self.MARGIN, pmap) painter.drawPixmap(x, option.rect.top() + self.MARGIN, pmap)
bottom = m + pmap.height() + option.rect.top() bottom = m + int(pmap.height() / pmap.devicePixelRatio()) + option.rect.top()
rect = QRect(option.rect.left(), bottom, option.rect.width(), option.rect.bottom() - bottom) rect = QRect(option.rect.left(), bottom, option.rect.width(), option.rect.bottom() - bottom)
if option.state & QStyle.State_Selected: if option.state & QStyle.State_Selected:
painter.setPen(self.parent().palette().color(QPalette.HighlightedText)) painter.setPen(self.parent().palette().color(QPalette.HighlightedText))
painter.drawText(rect, Qt.AlignHCenter | Qt.AlignVCenter, entry.basename) painter.drawText(rect, Qt.AlignHCenter | Qt.AlignVCenter, entry.basename)
painter.restore() painter.restore()
def pixmap(self, thumbnail_height, entry): def pixmap(self, thumbnail_height, entry, dpr):
pmap = QPixmap(current_container().name_to_abspath(entry.name)) if entry.width > 0 and entry.height > 0 else QPixmap() pmap = QPixmap(current_container().name_to_abspath(entry.name)) if entry.width > 0 and entry.height > 0 else QPixmap()
if not pmap.isNull():
pmap.setDevicePixelRatio(dpr)
scaled, width, height = fit_image(entry.width, entry.height, thumbnail_height, thumbnail_height) scaled, width, height = fit_image(entry.width, entry.height, thumbnail_height, thumbnail_height)
if scaled and not pmap.isNull(): if scaled:
pmap = pmap.scaled(width, height, transformMode=Qt.SmoothTransformation) pmap = pmap.scaled(int(dpr * width), int(dpr * height), transformMode=Qt.SmoothTransformation)
pmap.setDevicePixelRatio(dpr)
return pmap return pmap