From 87d09f6bc7cd3df395a4799a52143d83534d820a Mon Sep 17 00:00:00 2001 From: cosven Date: Thu, 29 Feb 2024 19:55:39 +0800 Subject: [PATCH 1/2] gui: draw pixmap whose width >> height --- feeluown/gui/uimain/page_view.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/feeluown/gui/uimain/page_view.py b/feeluown/gui/uimain/page_view.py index 50f0339616..eecdbc3cf4 100644 --- a/feeluown/gui/uimain/page_view.py +++ b/feeluown/gui/uimain/page_view.py @@ -273,19 +273,31 @@ def _draw_pixmap(self, painter, draw_width, draw_height, scrolled): scaled_pixmap = self._pixmap.scaledToWidth( draw_width, mode=Qt.SmoothTransformation) - pixmap_size = scaled_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 scaled_pixmap.height() < draw_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: + 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() From 7e965e842450e52226d08db13f8ce8dba737db07 Mon Sep 17 00:00:00 2001 From: cosven Date: Wed, 31 Jul 2024 21:08:11 +0800 Subject: [PATCH 2/2] refine code --- feeluown/gui/uimain/page_view.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/feeluown/gui/uimain/page_view.py b/feeluown/gui/uimain/page_view.py index eecdbc3cf4..50eb030670 100644 --- a/feeluown/gui/uimain/page_view.py +++ b/feeluown/gui/uimain/page_view.py @@ -270,17 +270,14 @@ 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 = self._pixmap.size() # draw the center part of the pixmap on available rect painter.save() - if scaled_pixmap.height() < draw_height: + if pixmap_size.width() / draw_width * draw_height >= pixmap_size.height(): scaled_pixmap = self._pixmap.scaledToHeight( draw_height, - mode=Qt.SmoothTransformation - ) + mode=Qt.SmoothTransformation) brush = QBrush(scaled_pixmap) painter.setBrush(brush) pixmap_size = scaled_pixmap.size() @@ -288,6 +285,9 @@ def _draw_pixmap(self, painter, draw_width, draw_height, scrolled): 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)