|
| 1 | +from typing import TYPE_CHECKING |
| 2 | + |
| 3 | +from PyQt6.QtCore import Qt |
| 4 | +from PyQt6.QtGui import QPixmap, QGuiApplication |
| 5 | +from PyQt6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel |
| 6 | + |
| 7 | +from feeluown.i18n import t |
| 8 | +from feeluown.library import SupportsRecListDailyPlaylists |
| 9 | +from feeluown.utils.reader import create_reader |
| 10 | +from feeluown.utils.aio import run_fn |
| 11 | +from feeluown.gui.widgets.header import LargeHeader |
| 12 | +from feeluown.gui.widgets.img_card_list import ( |
| 13 | + PlaylistCardListView, |
| 14 | + PlaylistCardListModel, |
| 15 | + PlaylistFilterProxyModel, |
| 16 | + PlaylistCardListDelegate, |
| 17 | +) |
| 18 | +from feeluown.gui.widgets.selfpaint_btn import TriagleButton |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + from feeluown.app.gui_app import GuiApp |
| 22 | + |
| 23 | + |
| 24 | +PlaylistCardMinWidth = 140 |
| 25 | +PlaylistCardSpacing = 25 |
| 26 | + |
| 27 | + |
| 28 | +class FoldButton(TriagleButton): |
| 29 | + def __init__(self, *args, **kwargs): |
| 30 | + super().__init__(*args, **kwargs) |
| 31 | + self.setToolTip(t("fold-tooltip")) |
| 32 | + self.setCheckable(True) |
| 33 | + self.toggled.connect(self.on_toggled) |
| 34 | + # Checked means folded, and show down direction. Click to unfold. |
| 35 | + self.setChecked(True) |
| 36 | + |
| 37 | + def on_toggled(self, checked): |
| 38 | + self.setToolTip( |
| 39 | + t("fold-expand") if checked else t("fold-collapse"), |
| 40 | + ) |
| 41 | + self.set_direction("down" if checked else "up") |
| 42 | + |
| 43 | + |
| 44 | +class Panel(QWidget): |
| 45 | + # Shared panel chrome for homepage/discovery recommendation blocks. |
| 46 | + _id_pixmap_cache = {} |
| 47 | + |
| 48 | + def __init__(self, title, body, pixmap, *, show_icon: bool = True): |
| 49 | + super().__init__(parent=None) |
| 50 | + |
| 51 | + self.icon_label = QLabel() |
| 52 | + self.header = LargeHeader(title) |
| 53 | + self.body = body |
| 54 | + |
| 55 | + self.icon_label.setFixedSize(20, 20) |
| 56 | + self.icon_label.setPixmap(pixmap) |
| 57 | + |
| 58 | + self._layout = QVBoxLayout(self) |
| 59 | + self._layout.setContentsMargins(0, 0, 0, 0) |
| 60 | + self._layout.setSpacing(10) |
| 61 | + |
| 62 | + self._h_layout = QHBoxLayout() |
| 63 | + self._h_layout.setSpacing(5) |
| 64 | + self._layout.addLayout(self._h_layout) |
| 65 | + # Homepage may aggregate panels from different providers, while |
| 66 | + # provider-scoped pages can hide this icon for a cleaner header. |
| 67 | + if show_icon: |
| 68 | + self._h_layout.addWidget(self.icon_label) |
| 69 | + else: |
| 70 | + self.icon_label.hide() |
| 71 | + self._h_layout.addWidget(self.header) |
| 72 | + self._h_layout.addStretch(1) |
| 73 | + |
| 74 | + self.fold_unfold_btn = FoldButton(length=16) |
| 75 | + self._h_layout.addWidget(self.fold_unfold_btn) |
| 76 | + self._h_layout.addSpacing(20) |
| 77 | + |
| 78 | + self._layout.addWidget(self.body) |
| 79 | + |
| 80 | + @classmethod |
| 81 | + def get_provider_pixmap(cls, app: "GuiApp", provider_id, width=20): |
| 82 | + device_pixel_ratio = QGuiApplication.instance().devicePixelRatio() |
| 83 | + if provider_id in cls._id_pixmap_cache: |
| 84 | + return cls._id_pixmap_cache[provider_id] |
| 85 | + pvd_ui = app.pvd_ui_mgr.get(provider_id) |
| 86 | + if pvd_ui is None: |
| 87 | + svg = "icons:feeluown.png" |
| 88 | + else: |
| 89 | + svg = pvd_ui.get_colorful_svg() |
| 90 | + pixmap = QPixmap(svg).scaledToWidth( |
| 91 | + int(width * device_pixel_ratio), Qt.TransformationMode.SmoothTransformation |
| 92 | + ) |
| 93 | + pixmap.setDevicePixelRatio(device_pixel_ratio) |
| 94 | + cls._id_pixmap_cache[provider_id] = pixmap |
| 95 | + return pixmap |
| 96 | + |
| 97 | + async def render(self): |
| 98 | + pass |
| 99 | + |
| 100 | + |
| 101 | +class RecPlaylistsPanel(Panel): |
| 102 | + def __init__( |
| 103 | + self, |
| 104 | + app: "GuiApp", |
| 105 | + provider: SupportsRecListDailyPlaylists, |
| 106 | + *, |
| 107 | + initial_row_count: int = 2, |
| 108 | + show_icon: bool = True, |
| 109 | + ): |
| 110 | + self._provider = provider |
| 111 | + self._app = app |
| 112 | + self._initial_row_count = initial_row_count |
| 113 | + self.playlist_list_view = PlaylistCardListView( |
| 114 | + no_scroll_v=True, fixed_row_count=self._initial_row_count |
| 115 | + ) |
| 116 | + # Bind once at init to avoid duplicate signal connections after re-render. |
| 117 | + self.playlist_list_view.show_playlist_needed.connect( |
| 118 | + lambda model: self._app.browser.goto(model=model) |
| 119 | + ) |
| 120 | + # Keep card visual style consistent between homepage and discovery page. |
| 121 | + self.playlist_list_view.setItemDelegate( |
| 122 | + PlaylistCardListDelegate( |
| 123 | + self.playlist_list_view, |
| 124 | + card_min_width=PlaylistCardMinWidth, |
| 125 | + card_spacing=PlaylistCardSpacing, |
| 126 | + ) |
| 127 | + ) |
| 128 | + pixmap = Panel.get_provider_pixmap(app, provider.identifier) |
| 129 | + super().__init__( |
| 130 | + t("recommended-playlist"), |
| 131 | + self.playlist_list_view, |
| 132 | + pixmap, |
| 133 | + show_icon=show_icon, |
| 134 | + ) |
| 135 | + |
| 136 | + self.fold_unfold_btn.clicked.connect(self._show_more_or_less) |
| 137 | + |
| 138 | + def _show_more_or_less(self, checked): |
| 139 | + # Folded keeps a predictable compact height; unfolded shows all rows. |
| 140 | + if checked: |
| 141 | + self.playlist_list_view.set_fixed_row_count(self._initial_row_count) |
| 142 | + else: |
| 143 | + self.playlist_list_view.set_fixed_row_count(-1) |
| 144 | + |
| 145 | + async def render(self): |
| 146 | + playlists = await run_fn(self._provider.rec_list_daily_playlists) |
| 147 | + if not playlists: |
| 148 | + return |
| 149 | + model = PlaylistCardListModel.create(create_reader(playlists), self._app) |
| 150 | + filter_model = PlaylistFilterProxyModel() |
| 151 | + filter_model.setSourceModel(model) |
| 152 | + self.playlist_list_view.setModel(filter_model) |
0 commit comments