Skip to content

Commit

Permalink
show high quality pixmap
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven committed Nov 30, 2024
1 parent be339c7 commit 22b5b7e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 26 deletions.
6 changes: 3 additions & 3 deletions feeluown/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def create_config() -> Config:
type_=dict,
default={
'contents': [
{'name': 'RecACollectionOfVideos', 'provider': 'bilibili'},
# {'name': 'RecListDailySongs', 'provider': 'netease'},
{'name': 'RecListDailyPlaylists', 'provider': 'qqmusic'},
{'name': 'RecListDailySongs', 'provider': 'netease'},
# {'name': 'RecACollectionOfSongs', 'provider': 'qqmusic'},
{'name': 'RecListDailyPlaylists', 'provider': 'qqmusic'},
{'name': 'RecACollectionOfVideos', 'provider': 'bilibili'},
]
},
desc='主页配置'
Expand Down
6 changes: 4 additions & 2 deletions feeluown/gui/pages/homepage.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,13 @@ def __init__(self, app: 'GuiApp', provider: SupportsRecACollectionOfVideos):
video_list_view.play_video_needed.connect(self._app.playlist.play_model)

async def render(self):
videos = await run_fn(self._provider.rec_a_collection_of_videos)
coll = await run_fn(self._provider.rec_a_collection_of_videos)
videos = coll.models
if videos:
# TODO: maybe show all videos
model = VideoCardListModel.create(videos[:8], self._app)
self.video_list_view.setModel(model)
self.header.setText(coll.name)
else:
self.header.setText('暂无推荐视频')
self.video_list_view.hide()
Expand All @@ -214,7 +216,7 @@ def __init__(self, app: 'GuiApp'):

self._layout = QVBoxLayout(self)
self._layout.setContentsMargins(20, 10, 20, 0)
self._layout.setSpacing(0)
self._layout.setSpacing(10)

async def render(self):
panels = []
Expand Down
2 changes: 1 addition & 1 deletion feeluown/gui/widgets/cover_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ async def show_cover(self, url, cover_uid):
label = CoverLabel()
layout.addWidget(label)
label.resize(100, 100)
label.show_img(QImage('/Users/cosven/Desktop/test.png'))
label.show_img(QImage('/path/to/test.png'))
13 changes: 9 additions & 4 deletions feeluown/gui/widgets/img_card_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
QRectF, QRect, QSize, QSortFilterProxyModel, pyqtSignal
)
from PyQt5.QtGui import (
QImage, QColor, QResizeEvent,
QImage, QColor, QResizeEvent, QGuiApplication,
QBrush, QPainter, QTextOption, QFontMetrics
)
from PyQt5.QtWidgets import (
Expand Down Expand Up @@ -162,6 +162,8 @@ def __init__(self, parent=None,
self.as_circle = True
self.w_h_ratio = 1.0

self._device_pixel_ratio = QGuiApplication.instance().devicePixelRatio()

self.card_min_width = card_min_width
self.card_spacing = card_spacing
self.card_text_height = card_text_height
Expand Down Expand Up @@ -227,10 +229,13 @@ def draw_img_or_color(self, painter, border_color, obj, draw_width, height):
brush = QBrush(color)
painter.setBrush(brush)
else:
if obj.height() < obj.width():
img = obj.scaledToHeight(height, Qt.SmoothTransformation)
if obj.width() / obj.height() > draw_width / height:
img = obj.scaledToHeight(int(height * self._device_pixel_ratio),
Qt.SmoothTransformation)
else:
img = obj.scaledToWidth(draw_width, Qt.SmoothTransformation)
img = obj.scaledToWidth(int(draw_width * self._device_pixel_ratio),
Qt.SmoothTransformation)
img.setDevicePixelRatio(self._device_pixel_ratio)
brush = QBrush(img)
painter.setBrush(brush)
border_radius = 3
Expand Down
45 changes: 29 additions & 16 deletions feeluown/gui/widgets/song_minicard_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)
from PyQt5.QtGui import (
QPainter, QPixmap, QImage, QColor, QPalette, QBrush,
QFontMetrics, QTextOption,
QFontMetrics, QTextOption, QGuiApplication,
)
from PyQt5.QtWidgets import (
QFrame, QListView, QStyle, QStyledItemDelegate
Expand Down Expand Up @@ -144,6 +144,8 @@ def __init__(
self.card_bottom_padding = card_padding[3]
self.card_left_padding = card_padding[0]

self._device_pixel_ratio = QGuiApplication.instance().devicePixelRatio()

def item_sizehint(self) -> tuple:
# HELP: listview needs about 20 spacing left on macOS
width = max(self.view.width() - 20, self.card_min_width)
Expand All @@ -158,8 +160,10 @@ def item_sizehint(self) -> tuple:
(self.card_min_width + self.card_right_spacing)
count = max(count, 1)
item_width = (width - ((count + 1) * self.card_right_spacing)) // count
return (item_width,
self.card_height + self.card_top_padding + self.card_bottom_padding)
return (
item_width,
self.card_height + self.card_top_padding + self.card_bottom_padding
)

def paint(self, painter, option, index):
card_top_padding = self.card_top_padding
Expand Down Expand Up @@ -194,7 +198,7 @@ def paint(self, painter, option, index):
painter.restore()

painter.save()
painter.translate(rect.x() + card_left_padding, rect.y() + card_top_padding)
painter.translate(rect.x() + card_left_padding, rect.y() + card_top_padding)

if selected:
text_color = option.palette.color(QPalette.HighlightedText)
Expand All @@ -211,8 +215,9 @@ def paint(self, painter, option, index):
# Draw image.
painter.save()
painter.translate(0, img_padding)
self.paint_pixmap(painter, non_text_color, obj,
cover_width, cover_height, border_radius)
self.paint_pixmap(
painter, non_text_color, obj, cover_width, cover_height, border_radius
)
painter.restore()

# Draw text.
Expand All @@ -225,16 +230,18 @@ def paint(self, painter, option, index):
# Note this is not a bool object.
is_enabled = option.state & QStyle.State_Enabled
self.paint_text(
painter, is_enabled, title, subtitle, text_color, non_text_color,
text_width, card_height
painter, is_enabled, title, subtitle, text_color, non_text_color, text_width,
card_height
)
painter.restore()

painter.restore()

def paint_text(self, painter, is_enabled, title, subtitle,
text_color, non_text_color, text_width, text_height):
each_height = text_height//2
def paint_text(
self, painter, is_enabled, title, subtitle, text_color, non_text_color,
text_width, text_height
):
each_height = text_height // 2
text_option = QTextOption()
text_option.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)

Expand All @@ -259,8 +266,9 @@ def paint_text(self, painter, is_enabled, title, subtitle,
painter.setPen(non_text_color)
painter.drawText(subtitle_rect, elided_title, text_option)

def paint_pixmap(self, painter, border_color, decoration,
width, height, border_radius):
def paint_pixmap(
self, painter, border_color, decoration, width, height, border_radius
):
painter.setRenderHint(QPainter.Antialiasing)
pen = painter.pen()
pen.setColor(border_color)
Expand All @@ -269,11 +277,16 @@ def paint_pixmap(self, painter, border_color, decoration,
color = decoration
brush = QBrush(color)
painter.setBrush(brush)
else:
else: # QImage
if decoration.height() < decoration.width():
pixmap = decoration.scaledToHeight(height, Qt.SmoothTransformation)
pixmap = decoration.scaledToHeight(
int(height * self._device_pixel_ratio), Qt.SmoothTransformation
)
else:
pixmap = decoration.scaledToWidth(width, Qt.SmoothTransformation)
pixmap = decoration.scaledToWidth(
int(width * self._device_pixel_ratio), Qt.SmoothTransformation
)
pixmap.setDevicePixelRatio(self._device_pixel_ratio)
brush = QBrush(pixmap)
painter.setBrush(brush)
cover_rect = QRect(0, 0, width, height)
Expand Down

0 comments on commit 22b5b7e

Please sign in to comment.