diff --git a/func/Module_preprocess.py b/func/Module_preprocess.py index bb96559..7652406 100644 --- a/func/Module_preprocess.py +++ b/func/Module_preprocess.py @@ -20,6 +20,7 @@ from func.utils.PublicFunc import PublicFunc from func.utils.Constants import Constants from func.Filters.Preprocessing import Butterworth_for_BCG_PreProcess, Butterworth_for_ECG_PreProcess from func.utils.Result import Result +from func.utils.FloatingImagePanel import add_floating_image_panel from ui.MainWindow.MainWindow_preprocess import Ui_MainWindow_preprocess from ui.setting.preprocess_input_setting import Ui_MainWindow_preprocess_input_setting @@ -187,6 +188,12 @@ class MainWindow_preprocess(QMainWindow): self.progressbar = None PublicFunc.add_progressbar(self) + self.image_overlay = add_floating_image_panel( + self.centralWidget(), + title="参考图例", + img_path=r"image\legend_preprocess.png" + ) + self.plot_mode = plot_mode if self.plot_mode == "matplotlib": #初始化画框 @@ -492,6 +499,18 @@ class MainWindow_preprocess(QMainWindow): self.ax0.grid(True) self.ax0.xaxis.set_major_formatter(Params.FORMATTER) + def update_overlay_position(self): + """手动定位函数""" + margin = 20 + x = self.width() - self.image_overlay.width() - margin + y = margin + self.image_overlay.move(x, y) + + def resizeEvent(self, event): + """确保窗口变大小时,悬浮窗依然在右上角""" + super().resizeEvent(event) + self.update_overlay_position() + class Data: diff --git a/func/utils/FloatingImagePanel.py b/func/utils/FloatingImagePanel.py new file mode 100644 index 0000000..4a0862b --- /dev/null +++ b/func/utils/FloatingImagePanel.py @@ -0,0 +1,95 @@ +from PySide6 import QtWidgets, QtCore, QtGui + + +class FloatingImagePanel(QtWidgets.QWidget): + def __init__(self, parent, title="原始比例预览", image_path=None): + super().__init__(parent) + self.is_expanded = True + + # 1. 样式配置 (保持深色风格,方便看清信号) + self.setAttribute(QtCore.Qt.WA_StyledBackground, True) + self.setStyleSheet(""" + FloatingImagePanel { + background-color: rgba(30, 30, 30, 220); + border: 2px solid #444; + border-radius: 4px; + } + QLabel#title_label { color: #ffffff; font-weight: bold; font-family: 'Consolas'; } + QPushButton#toggle_btn { background: #444; color: white; border: none; } + """) + + # 2. 布局初始化 + self.main_layout = QtWidgets.QVBoxLayout(self) + self.main_layout.setContentsMargins(4, 4, 4, 4) + self.main_layout.setSpacing(2) + + # --- 标题栏 --- + self.header = QtWidgets.QWidget() + header_layout = QtWidgets.QHBoxLayout(self.header) + header_layout.setContentsMargins(2, 2, 2, 2) + + self.title_label = QtWidgets.QLabel(title) + self.title_label.setObjectName("title_label") + self.toggle_btn = QtWidgets.QPushButton("▲") + self.toggle_btn.setFixedSize(22, 22) + + header_layout.addWidget(self.title_label) + header_layout.addStretch() + header_layout.addWidget(self.toggle_btn) + self.main_layout.addWidget(self.header) + + # --- 图片展示区 --- + self.image_label = QtWidgets.QLabel() + # 关键点 1:关闭自动填充,确保我们手动控制尺寸 + self.image_label.setScaledContents(False) + # 关键点 2:设置对齐方式为中心 + self.image_label.setAlignment(QtCore.Qt.AlignCenter) + self.main_layout.addWidget(self.image_label) + + # 3. 信号绑定 + self.toggle_btn.clicked.connect(self.toggle_content) + + # 4. 加载图片 + if image_path: + self.update_image(image_path) + + def update_image(self, path): + """完全根据图片的物理尺寸调整窗口""" + pixmap = QtGui.QPixmap(path) + if not pixmap.isNull(): + # 拿到图片的原始宽高 + w = pixmap.width() + h = pixmap.height() + + # 强制 Label 等于图片大小 + self.image_label.setFixedSize(w, h) + self.image_label.setPixmap(pixmap) + + # 让整个窗口重新计算布局,贴合 Label + self.adjustSize() + + # 保持展开状态 + self.set_expanded(True) + + def set_expanded(self, expanded: bool): + self.is_expanded = expanded + self.image_label.setVisible(expanded) + self.toggle_btn.setText("▲" if expanded else "▼") + + # 关键点 3:重置窗口大小 + # 展开时它会变大包住图片,收起时它会缩成标题栏大小 + self.adjustSize() + + # 通知父窗口刷新位置,防止因为变大而超出右边界 + if hasattr(self.parent(), 'update_overlay_position'): + self.parent().update_overlay_position() + + def toggle_content(self): + self.set_expanded(not self.is_expanded) + + +# --- 封装的调用函数 --- +def add_floating_image_panel(parent_widget, title="图片原尺寸", img_path=None): + panel = FloatingImagePanel(parent_widget, title, img_path) + panel.raise_() + return panel \ No newline at end of file diff --git a/image/legend_preprocess.png b/image/legend_preprocess.png new file mode 100644 index 0000000..ee697f4 Binary files /dev/null and b/image/legend_preprocess.png differ