1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
| import os import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QWidget, QVBoxLayout, QHBoxLayout from PyQt5.QtGui import QPixmap, QPainterPath, QPainter, QMouseEvent, QIcon from PyQt5.QtCore import Qt, QPropertyAnimation, QEasingCurve, pyqtSignal, QSize, QAbstractAnimation, QPoint
from PyQt5.QtWidgets import QGraphicsOpacityEffect
from qfluentwidgets import FluentIcon, ToolButton
from NetEase.common.ThemeColor import Theme_Color
class RoundedLabel(QLabel): """ QLabel subclass that rounds the top-left and top-right corners of the pixmap. """
def __init__(self, parent=None, radius=10): super().__init__(parent) self._radius = radius self._original_pixmap = None
@property def radius(self): return self._radius
@radius.setter def radius(self, value): self._radius = value if self._original_pixmap: self.setPixmap(self._original_pixmap)
def setPixmap(self, pixmap): """ Overrides QLabel.setPixmap to apply rounding on the top corners. """ if not isinstance(pixmap, QPixmap): super().setPixmap(pixmap) return
self._original_pixmap = pixmap rounded = self._rounded_top_corners(pixmap, self._radius) super().setPixmap(rounded)
def _rounded_top_corners(self, pixmap, radius): """ Returns a new QPixmap with only the top-left and top-right corners rounded by the given radius. """ size = pixmap.size() result = QPixmap(size) result.fill(Qt.transparent)
painter = QPainter(result) painter.setRenderHint(QPainter.Antialiasing)
path = QPainterPath() w, h = size.width(), size.height() r = radius
path.moveTo(0, h) path.lineTo(0, r) path.arcTo(0, 0, 2 * r, 2 * r, 180, -90) path.lineTo(w - r, 0) path.arcTo(w - 2 * r, 0, 2 * r, 2 * r, 90, -90) path.lineTo(w, h) path.closeSubpath()
painter.setClipPath(path) painter.drawPixmap(0, 0, pixmap) painter.end()
return result
class RoundedToolButton(ToolButton):
def __init__(self, parent=None): super().__init__(parent) self.setStyleSheet(""" ToolButton { color: black; border-radius: 30px; background: transparent; outline: none; }
ToolButton:disabled { color: rgba(0, 0, 0, 0.36); background: rgba(249, 249, 249, 0.3); border: 1px solid rgba(0, 0, 0, 0.06); border-bottom: 1px solid rgba(0, 0, 0, 0.06); } """) self.normal_icon = QIcon() self.hover_icon = QIcon()
def setIcon(self, icon): if isinstance(icon, str): self.normal_icon = QIcon(icon) base, ext = os.path.splitext(icon) hover_path = f"{base}-hover{ext}" if os.path.exists(hover_path): self.hover_icon = QIcon(hover_path) else: self.hover_icon = self.normal_icon else: self.normal_icon = icon self.hover_icon = icon
super().setIcon(self.normal_icon)
def setHoverIcon(self, icon): self.hover_icon = icon
def enterEvent(self, event): super().enterEvent(event) if not self.hover_icon.isNull(): super().setIcon(self.hover_icon)
def leaveEvent(self, event): super().leaveEvent(event) super().setIcon(self.normal_icon)
class playlistCover(QWidget): played = pyqtSignal() clicked = pyqtSignal()
def __init__(self, image_path, parent=None): super().__init__(parent=parent)
self._imagepath = image_path self.setAttribute(Qt.WA_TranslucentBackground) self.setWindowFlag(Qt.FramelessWindowHint) self.setCursor(Qt.PointingHandCursor)
self.layout = QVBoxLayout(self)
self.coverLabel = RoundedLabel(self) self.coverLabel.setObjectName("coverLabel") self.coverLabel.setMinimumSize(180, 180) self.coverLabel.setScaledContents(True) self.coverLabel.setPixmap( QPixmap(self._imagepath).scaled(180, 180, Qt.IgnoreAspectRatio, Qt.SmoothTransformation))
self.despLabel = QLabel(self) self.despLabel.setObjectName("despLabel") self.despLabel.setText("你是我的单曲循环 我是你的随机播放") self.despLabel.setWordWrap(True) self.despLabel.setAlignment(Qt.AlignTop | Qt.AlignLeft) self.despLabel.setFixedHeight(60)
self.layout.addWidget(self.coverLabel) self.layout.addWidget(self.despLabel) self.layout.setContentsMargins(0, 0, 0, 0) self.layout.setSpacing(0) self.setLayout(self.layout)
self.mask_layer = QWidget(self) self.mask_layer.setObjectName("maskLayer") self.mask_layer.resize(self.coverLabel.width(), self.coverLabel.height() + self.despLabel.height()) self.mask_layer.setVisible(False)
self.listenCountLabel = QLabel(self) self.listenCountLabel.setObjectName("listenCountLabel") self.listenCountLabel.setText("🎧️ 167.6万") self.listenCountLabel.move(self.coverLabel.width() - 90, self.coverLabel.y() + 10)
self.mask_layout = QVBoxLayout(self.mask_layer) self.mask_layout.setAlignment(Qt.AlignCenter) self.playButton = RoundedToolButton(self.mask_layer) self.playButton.setIcon("./resource/play.svg") self.playButton.setMinimumSize(60, 60) self.playButton.setIconSize(QSize(60, 60)) self.mask_layout.addWidget(self.playButton)
self.opacity_effect = QGraphicsOpacityEffect(self.mask_layer) self.mask_layer.setGraphicsEffect(self.opacity_effect) self.mask_layer.move(self.x(), self.y())
self._themecolor = Theme_Color.getThemeColor(self._imagepath) r, g, b = self._themecolor self.setStyleSheet(f""" QWidget#maskLayer {{ background-color: rgba(0, 0, 0, 0.4); border-radius: 15px; }} QLabel#despLabel {{ background-color: rgb({r},{g},{b}); font-family: Microsoft YaHei; font-size: 15px; color: #ffffff; padding-top: 5px; padding-left: 5px; padding-right: 5px; border-bottom-left-radius: 15px; /* 左下角圆角 */ border-bottom-right-radius: 15px; /* 右下角圆角 */ }} QLabel#listenCountLabel {{ background-color: transparent; font-family: Microsoft YaHei; font-size: 15px; font-weight: bold; color: #ffffff; }} """)
self.enter_animation = QPropertyAnimation(self.opacity_effect, b"opacity") self.enter_animation.setDuration(300) self.enter_animation.setStartValue(0.0) self.enter_animation.setEndValue(1) self.enter_animation.setEasingCurve(QEasingCurve.OutQuad)
self.leave_animation = QPropertyAnimation(self.opacity_effect, b"opacity") self.leave_animation.setDuration(300) self.leave_animation.setStartValue(1) self.leave_animation.setEndValue(0.0) self.leave_animation.setEasingCurve(QEasingCurve.InQuad)
self.animation_offset = 20 self.original_pos = self.pos()
self.pos_animation = QPropertyAnimation(self, b"pos") self.pos_animation.setDuration(300) self.pos_animation.setEasingCurve(QEasingCurve.OutBounce) self.__connectSignalToSlot()
def __connectSignalToSlot(self): self.playButton.clicked.connect(lambda: self.played.emit())
def resizeEvent(self, event): super().resizeEvent(event) self.mask_layer.setGeometry( self.coverLabel.x(), self.coverLabel.y(), self.coverLabel.width(), self.coverLabel.height() + self.despLabel.height() ) self.listenCountLabel.move(self.coverLabel.width() - 90, self.coverLabel.y() + 10)
def mousePressEvent(self, event: QMouseEvent): self.clicked.emit() super().mousePressEvent(event)
def enterEvent(self, event): self.original_pos = self.pos()
self.pos_animation.stop() start = self.original_pos end = start - QPoint(0, self.animation_offset) self.pos_animation.setStartValue(start) self.pos_animation.setEndValue(end) self.pos_animation.start()
self.enter_animation.start() self.mask_layer.show() self.listenCountLabel.hide() super().enterEvent(event)
def leaveEvent(self, event): current = self.pos() self.pos_animation.stop() self.pos_animation.setStartValue(current) self.pos_animation.setEndValue(self.original_pos) self.pos_animation.start()
self.leave_animation.start() self.listenCountLabel.show() super().leaveEvent(event)
def moveEvent(self, event): if self.pos_animation.state() == QAbstractAnimation.Stopped: self.original_pos = self.pos() super().moveEvent(event)
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.init_ui()
def init_ui(self): central_widget = QWidget() self.setCentralWidget(central_widget) layout = QHBoxLayout(central_widget) layout.setContentsMargins(10, 10, 10, 10) layout.setSpacing(20)
image_label = playlistCover("./resource/test.jpg") image_label_2 = playlistCover("./resource/test2.jpg") image_label_3 = playlistCover("./resource/test3.jpg") image_label_4 = playlistCover("./resource/test4.jpg")
layout.addWidget(image_label) layout.addWidget(image_label_2) layout.addWidget(image_label_3) layout.addWidget(image_label_4) layout.setAlignment(Qt.AlignCenter)
if __name__ == "__main__": QApplication.setHighDpiScaleFactorRoundingPolicy( Qt.HighDpiScaleFactorRoundingPolicy.PassThrough) QApplication.setAttribute(Qt.AA_EnableHighDpiScaling) QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps) app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())
|