150 lines
5.4 KiB
Python
150 lines
5.4 KiB
Python
from PyQt5.QtCore import Qt
|
||
from PyQt5.QtWidgets import QFrame, QLabel, QHBoxLayout, QVBoxLayout, QWidget
|
||
import pyqtgraph as pg
|
||
|
||
|
||
class HrPlotGraph(QWidget): # 继承自 QWidget,减少一层嵌套
|
||
def __init__(self) -> None:
|
||
super().__init__()
|
||
|
||
self.setWindowTitle("心率监测")
|
||
|
||
# 初始化各个组件
|
||
self.title_widget = self._init_title_widget()
|
||
self.icon_widget = self._init_icon_widget()
|
||
self.value_label = self._init_value_label()
|
||
self.max_label = self._init_max_label()
|
||
|
||
# 设置标题布局
|
||
self.title_layout = QHBoxLayout()
|
||
self.title_layout.addWidget(self.icon_widget, 10) # 占据 10% 的屏幕宽度
|
||
self.title_layout.addWidget(self.title_widget, 90) # 占据 90% 的屏幕宽度
|
||
|
||
# 设置心率值布局
|
||
self.values_title_layout = QVBoxLayout()
|
||
self.values_title_layout.addWidget(self.value_label, 50) # 占据 50% 的垂直空间
|
||
self.values_title_layout.addWidget(self.max_label, 50) # 占据 50% 的垂直空间
|
||
|
||
# 初始化绘图组件
|
||
self.plot_widget_hr, self.plot_item_hr = self._init_plot_widget()
|
||
self.plot_layout = QHBoxLayout()
|
||
self.plot_layout.addWidget(self.plot_widget_hr)
|
||
|
||
# 设置显示布局
|
||
self.display_layout = QHBoxLayout()
|
||
self.display_layout.addLayout(self.values_title_layout, 40) # 占据 40% 的水平空间
|
||
self.display_layout.addLayout(self.plot_layout, 60) # 占据 60% 的水平空间
|
||
|
||
# 主布局
|
||
main_layout = QVBoxLayout()
|
||
main_layout.addLayout(self.title_layout, 20) # 占据 20% 的垂直空间
|
||
main_layout.addLayout(self.display_layout, 80) # 占据 80% 的垂直空间
|
||
|
||
self.setLayout(main_layout)
|
||
self.setStyleSheet(
|
||
"background-color: #ffffff;"
|
||
"border:1px solid #bbbbbb;"
|
||
"border-radius:4px;"
|
||
"margin-right: 20px;"
|
||
"margin-top:0px"
|
||
)
|
||
|
||
def set_data(self, data, max_data):
|
||
"""
|
||
设置心率数据和最大心率值
|
||
:param data: 心率数据列表
|
||
:param max_data: 最大心率值
|
||
"""
|
||
self.plot_item_hr.setData(data) # 更新绘图数据
|
||
self.max_label.setText(f"最大心率:{max_data}次/分钟") # 更新最大心率显示
|
||
if data:
|
||
self.value_label.setText(f'当前心率:{data[-1]}次/分钟') # 更新当前心率显示
|
||
|
||
@staticmethod
|
||
def _init_icon_widget():
|
||
"""
|
||
初始化图标组件
|
||
:return: 图标 QLabel 对象
|
||
"""
|
||
img = QLabel()
|
||
img.setStyleSheet(
|
||
"border-image: url(assets/heart.png);"
|
||
"background-size: contain;"
|
||
"background-repeat: no-repeat;"
|
||
"background-position: center;"
|
||
)
|
||
img.setAlignment(Qt.AlignCenter) # 图标居中对齐
|
||
img.setScaledContents(True) # 图标大小自动缩放
|
||
return img
|
||
|
||
@staticmethod
|
||
def _init_title_widget():
|
||
"""
|
||
初始化标题组件
|
||
:return: 标题 QLabel 对象
|
||
"""
|
||
title = QLabel("心率")
|
||
title.setStyleSheet(
|
||
"font: 30px \"Microsoft YaHei UI\";" # 设置字体和大小
|
||
"color: #2c2c2c;" # 设置字体颜色
|
||
"border:none;" # 无边框
|
||
)
|
||
title.setContentsMargins(20, 4, 20, 4) # 设置边距
|
||
return title
|
||
|
||
@staticmethod
|
||
def _init_value_label():
|
||
"""
|
||
初始化当前心率显示组件
|
||
:return: 当前心率 QLabel 对象
|
||
"""
|
||
value_label = QLabel('当前心率:次/分钟')
|
||
value_label.setFrameShape(QFrame.Box)
|
||
value_label.setLineWidth(0)
|
||
value_label.setStyleSheet(
|
||
"border:none;"
|
||
"background-color: #ececec;"
|
||
"margin-right: 10px;"
|
||
"margin:0px 0px 0px 0px;"
|
||
"border-radius: 5px;"
|
||
"font: 24px \"Microsoft YaHei UI\";"
|
||
)
|
||
value_label.setAlignment(Qt.AlignCenter) # 文本居中对齐
|
||
return value_label
|
||
|
||
@staticmethod
|
||
def _init_max_label():
|
||
"""
|
||
初始化最大心率显示组件
|
||
:return: 最大心率 QLabel 对象
|
||
"""
|
||
max_label = QLabel('最大心率:次/分钟')
|
||
max_label.setFrameShape(QFrame.Box)
|
||
max_label.setLineWidth(0)
|
||
max_label.setStyleSheet(
|
||
"border:none;"
|
||
"background-color: #ececec;"
|
||
"margin-right: 10px;"
|
||
"margin:0px 0px 0px 0px;"
|
||
"border-radius: 5px;"
|
||
"font: 24px \"Microsoft YaHei UI\";"
|
||
)
|
||
max_label.setAlignment(Qt.AlignCenter) # 文本居中对齐
|
||
return max_label
|
||
|
||
@staticmethod
|
||
def _init_plot_widget():
|
||
"""
|
||
初始化绘图组件
|
||
:return: 绘图组件 PlotWidget 和绘图项 PlotDataItem 对象
|
||
"""
|
||
plot_widget_hr = pg.PlotWidget()
|
||
plot_item_hr = plot_widget_hr.plot()
|
||
plot_widget_hr.getAxis('bottom').setTicks([]) # 不显示 x 轴坐标点
|
||
plot_widget_hr.setBackground('w') # 设置白色网格背景
|
||
plot_widget_hr.showGrid(x=True, y=True) # 显示网格
|
||
plot_widget_hr.setYRange(0, 100) # 设置 y 轴范围
|
||
pen = pg.mkPen(color='#5c7bd9', width=3) # 修改线条颜色和粗细
|
||
plot_item_hr.setPen(pen)
|
||
return plot_widget_hr, plot_item_hr
|