113 lines
5.0 KiB
Python
113 lines
5.0 KiB
Python
# StatusStyleDelegate.py
|
|
from PySide6.QtWidgets import QStyledItemDelegate
|
|
from PySide6.QtGui import QColor, QFont, QBrush
|
|
from PySide6.QtCore import Qt, QTimer
|
|
from PySide6.QtGui import QPalette
|
|
|
|
class StatusStyleDelegate(QStyledItemDelegate):
|
|
def __init__(self, parent=None, table_view=None):
|
|
super().__init__(parent)
|
|
self.flash_cycles = {} # 存储闪烁周期: {(row, col): (timer, cycle_count, original_brush)}
|
|
# self.flash_duration = 1000 # 闪烁持续时间(毫秒)
|
|
# self.updated_cells = set() # 存储需要高亮的单元格 (row, col)
|
|
self.table_view = table_view # 保存 QTableView 实例
|
|
|
|
def flash_cell(self, row, column):
|
|
# 如果单元格已经在闪烁,重置计时器
|
|
if (row, column) in self.flash_cycles:
|
|
timer, _, _ = self.flash_cycles[(row, column)]
|
|
timer.stop()
|
|
del self.flash_cycles[(row, column)]
|
|
|
|
# 获取原始背景色
|
|
index = self.table_view.model().index(row, column)
|
|
original_brush = self.get_original_background(index)
|
|
|
|
# 创建周期性计时器实现闪烁效果
|
|
cycle_count = 0
|
|
timer = QTimer()
|
|
timer.setInterval(200) # 200ms切换一次颜色
|
|
|
|
def update_flash():
|
|
nonlocal cycle_count
|
|
cycle_count += 1
|
|
# 闪烁3个周期(6次切换)后停止
|
|
if cycle_count > 6:
|
|
timer.stop()
|
|
if (row, column) in self.flash_cycles:
|
|
del self.flash_cycles[(row, column)]
|
|
self.table_view.viewport().update()
|
|
return
|
|
|
|
# 切换背景色 (黄色 <-> 原始色)
|
|
is_highlight = (cycle_count % 2 == 1)
|
|
self.flash_cycles[(row, column)] = (timer, cycle_count, original_brush)
|
|
self.table_view.viewport().update()
|
|
|
|
timer.timeout.connect(update_flash)
|
|
self.flash_cycles[(row, column)] = (timer, cycle_count, original_brush)
|
|
timer.start()
|
|
update_flash() # 立即开始第一个周期
|
|
|
|
|
|
def get_original_background(self, index):
|
|
# 获取单元格原始背景色
|
|
model = index.model()
|
|
status_index = model.index(index.row(), 7)
|
|
status = model.data(status_index, Qt.ItemDataRole.DisplayRole)
|
|
|
|
if status == "已触发":
|
|
return QBrush(QColor('lightcoral'))
|
|
elif status == "错买":
|
|
return QBrush(QColor('lightgreen'))
|
|
else:
|
|
return QBrush(QColor('white'))
|
|
|
|
def initStyleOption(self, option, index):
|
|
super().initStyleOption(option, index)
|
|
|
|
model = index.model()
|
|
status_index = model.index(index.row(), 7)
|
|
status = model.data(status_index, Qt.ItemDataRole.DisplayRole)
|
|
|
|
|
|
if status == "已触发":
|
|
option.font = QFont("Arial", 14, QFont.Weight.Bold)
|
|
option.palette.setColor(QPalette.ColorRole.Text, QColor('red'))
|
|
option.backgroundBrush = QBrush(QColor('lightcoral')) # 背景色
|
|
elif status == "错买":
|
|
option.font = QFont("Arial", 14, QFont.Weight.Bold)
|
|
option.palette.setColor(QPalette.ColorRole.Text, QColor('green')) # 文字颜色
|
|
option.backgroundBrush = QBrush(QColor('lightgreen')) # 背景色
|
|
elif status == "监控中":
|
|
option.font = QFont("Arial", 12, QFont.Weight.Normal)
|
|
option.palette.setColor(QPalette.ColorRole.Text, QColor('black'))
|
|
option.backgroundBrush = QBrush(QColor('white'))
|
|
else:
|
|
option.font = QFont("Arial", 12, QFont.Weight.Normal)
|
|
option.palette.setColor(QPalette.ColorRole.Text, QColor('black'))
|
|
option.backgroundBrush = QBrush(QColor('white'))
|
|
|
|
# 应用闪烁效果 (黄色 <-> 原始色交替)
|
|
row = index.row()
|
|
col = index.column()
|
|
if (row, col) in self.flash_cycles:
|
|
timer, cycle_count, original_brush = self.flash_cycles[(row, col)]
|
|
if cycle_count % 2 == 1:
|
|
# 使用高对比度黄色 (#FFFF99)
|
|
option.backgroundBrush = QBrush(QColor(255, 255, 153))
|
|
else:
|
|
option.backgroundBrush = original_brush
|
|
|
|
# 新增对获利列的处理
|
|
if index.column() == 6: # 检查是否为获利列
|
|
option.font = QFont(option.font().family(), option.font().pointSize(), QFont.Weight.Bold)
|
|
profit_text = model.data(index, Qt.ItemDataRole.DisplayRole)
|
|
try:
|
|
profit_value = float(profit_text.replace('%', '')) # 移除百分号再转换
|
|
if profit_value > 0:
|
|
option.palette.setColor(QPalette.ColorRole.Text, QColor('red'))
|
|
elif profit_value < 0:
|
|
option.palette.setColor(QPalette.ColorRole.Text, QColor('green'))
|
|
except ValueError:
|
|
pass # 如果转换失败,保持默认颜色 |