87 lines
3.1 KiB
Python
87 lines
3.1 KiB
Python
from PySide6.QtWidgets import (QWidget, QListWidget, QStyledItemDelegate,
|
|
QApplication, QVBoxLayout, QMenu, QListWidgetItem,
|
|
QStyle)
|
|
from PySide6.QtCore import Qt, QSize, QRect, QPoint
|
|
from PySide6.QtGui import (QPainter, QPainterPath, QColor, QLinearGradient,
|
|
QPen, QFont, QIcon)
|
|
|
|
|
|
class CardItemDelegate(QStyledItemDelegate):
|
|
def __init__(self, parent=None):
|
|
super().__init__(parent)
|
|
|
|
def paint(self, painter: QPainter, option, index):
|
|
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
|
|
painter.save()
|
|
|
|
# 获取数据
|
|
data = index.data(Qt.ItemDataRole.UserRole)
|
|
|
|
# 绘制卡片背景
|
|
rect = option.rect
|
|
rect.adjust(8, 4, -8, -4)
|
|
|
|
# 创建圆角路径
|
|
path = QPainterPath()
|
|
path.addRoundedRect(rect, 8, 8)
|
|
|
|
# 绘制阴影
|
|
shadow_color = QColor(0, 0, 0, 30)
|
|
for i in range(5):
|
|
shadow_rect = rect.adjusted(0, i, 0, i)
|
|
shadow_path = QPainterPath()
|
|
shadow_path.addRoundedRect(shadow_rect, 8, 8)
|
|
painter.fillPath(shadow_path, shadow_color)
|
|
|
|
# 绘制卡片背景
|
|
gradient = QLinearGradient(rect.topLeft(), rect.bottomLeft())
|
|
if data.activated:
|
|
gradient.setColorAt(0, QColor(40, 70, 45))
|
|
gradient.setColorAt(1, QColor(45, 80, 50))
|
|
painter.fillPath(path, gradient)
|
|
painter.setPen(QPen(QColor(60, 180, 90), 2))
|
|
elif option.state & QStyle.State_Selected:
|
|
gradient.setColorAt(0, QColor(45, 45, 55))
|
|
gradient.setColorAt(1, QColor(55, 55, 65))
|
|
painter.fillPath(path, gradient)
|
|
painter.setPen(QPen(QColor(70, 130, 180), 2))
|
|
else:
|
|
painter.fillPath(path, QColor(35, 35, 40))
|
|
painter.setPen(QPen(QColor(60, 60, 65)))
|
|
|
|
painter.drawPath(path)
|
|
|
|
# 设置字体
|
|
name_font = QFont("Microsoft YaHei", 10)
|
|
name_font.setBold(True)
|
|
normal_font = QFont("Microsoft YaHei", 9)
|
|
|
|
# 计算文本区域
|
|
padding = 15
|
|
name_rect = rect.adjusted(padding, padding, -padding, 0)
|
|
name_rect.setHeight(25)
|
|
|
|
date_rect = QRect(name_rect)
|
|
date_rect.translate(0, name_rect.height())
|
|
|
|
desc_rect = QRect(date_rect)
|
|
desc_rect.translate(0, date_rect.height())
|
|
desc_rect.setHeight(40)
|
|
|
|
# 绘制文本
|
|
painter.setFont(name_font)
|
|
painter.setPen(Qt.GlobalColor.white)
|
|
painter.drawText(name_rect, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, data.name)
|
|
|
|
painter.setFont(normal_font)
|
|
painter.setPen(QColor(180, 180, 180))
|
|
painter.drawText(date_rect, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, data.date)
|
|
|
|
painter.setPen(QColor(160, 160, 160))
|
|
painter.drawText(desc_rect, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop | Qt.TextFlag.TextWordWrap,
|
|
data.description)
|
|
|
|
painter.restore()
|
|
|
|
def sizeHint(self, option, index):
|
|
return QSize(380, 130) |