From ec40a0d59c82afdbab6f446836ddec084692ce76 Mon Sep 17 00:00:00 2001 From: Shaowen Yin Date: Wed, 31 Jul 2024 21:10:54 +0800 Subject: [PATCH] gui: draw pixmap whose width >> height (#806) --- feeluown/gui/uimain/page_view.py | 38 +++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/feeluown/gui/uimain/page_view.py b/feeluown/gui/uimain/page_view.py index 12d44ea110..624e0081d6 100644 --- a/feeluown/gui/uimain/page_view.py +++ b/feeluown/gui/uimain/page_view.py @@ -280,22 +280,34 @@ def _draw_overlay(self, painter, draw_width, draw_height, scrolled): def _draw_pixmap(self, painter, draw_width, draw_height, scrolled): # scale pixmap assert self._pixmap is not None - scaled_pixmap = self._pixmap.scaledToWidth( - draw_width, - mode=Qt.SmoothTransformation) - pixmap_size = scaled_pixmap.size() + pixmap_size = self._pixmap.size() # draw the center part of the pixmap on available rect painter.save() - brush = QBrush(scaled_pixmap) - painter.setBrush(brush) - # note: in practice, most of the time, we can't show the - # whole artist pixmap, as a result, the artist head will be cut, - # which causes bad visual effect. So we render the top-center part - # of the pixmap here. - y = (pixmap_size.height() - draw_height) // 3 - painter.translate(0, - y - scrolled) - rect = QRect(0, y, draw_width, draw_height) + if pixmap_size.width() / draw_width * draw_height >= pixmap_size.height(): + scaled_pixmap = self._pixmap.scaledToHeight( + draw_height, + mode=Qt.SmoothTransformation) + brush = QBrush(scaled_pixmap) + painter.setBrush(brush) + pixmap_size = scaled_pixmap.size() + x = (pixmap_size.width() - draw_width) // 2 + painter.translate(-x, -scrolled) + rect = QRect(0, 0, pixmap_size.width(), draw_height) + else: + scaled_pixmap = self._pixmap.scaledToWidth( + draw_width, + mode=Qt.SmoothTransformation) + pixmap_size = scaled_pixmap.size() + brush = QBrush(scaled_pixmap) + painter.setBrush(brush) + # note: in practice, most of the time, we can't show the + # whole artist pixmap, as a result, the artist head will be cut, + # which causes bad visual effect. So we render the top-center part + # of the pixmap here. + y = (pixmap_size.height() - draw_height) // 3 + painter.translate(0, - y - scrolled) + rect = QRect(0, y, draw_width, draw_height) painter.drawRect(rect) painter.restore()