Files
Signal_Label_Reborn/func/utils/PublicFunc.py
marques adb90c258c 修改粗对齐文件读取后时间显示为秒数
修改计算对齐按钮位置到对齐板块
添加数据文件相关错误提示
添加粗对齐文件读取自动识别文件名称中的采样率
添加粗对齐选择对其位置后逐epoch拟合视图接口
2025-05-19 09:42:17 +08:00

175 lines
6.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from datetime import datetime
from logging import error, info
from PySide6.QtWidgets import QMessageBox, QWidget, QPushButton, QProgressBar, QApplication, QRadioButton
from func.utils.Constants import Constants
from func.utils.CustomException import TipsTypeValueNotExistError, MsgBoxTypeValueNotExistError
class PublicFunc:
@staticmethod
def get_current_localtime() -> str:
"""
获取当前本地时间
Parameters:
Returns:
str格式化为"%H:%M:%S"的当前本地时间
Raises:
"""
return str(datetime.now().strftime("%H:%M:%S"))
@staticmethod
def format_status_msg(msg) -> str:
"""
格式化状态栏信息
Parameters:
msg - str需要被格式化的字符串
Returns:
str格式化为"%H:%M:%S"的当前本地时间 + 信息
Raises:
"""
return str(datetime.now().strftime("%H:%M:%S")) + " " + msg
@staticmethod
def text_output(main_window: object, content: str, tips_type: str) -> None:
"""
更新textBrowser中的内容同时输出日志
Parameters:
main_window - objectQt的含有textBrowser属性的对象这里一般指的是mainWindow里面有唯一的textBrowser属性
content - str需要输出的内容
tips_type - 日志输出的类型
Returns:
Raises:
TipsTypeValueNotExistError
"""
if tips_type is Constants.TIPS_TYPE_INFO:
info(f"{tips_type}: {content}")
main_window.textBrowser_info.append(
f"<font color='black'>{PublicFunc.get_current_localtime()} {tips_type}: {content}</font>")
elif tips_type is Constants.TIPS_TYPE_ERROR:
error(f"{tips_type}: {content}")
main_window.textBrowser_info.append(
f"<font color='red'>{PublicFunc.get_current_localtime()} {tips_type}: {content}</font>")
else:
raise TipsTypeValueNotExistError()
main_window.textBrowser_info.verticalScrollBar().setValue(
main_window.textBrowser_info.verticalScrollBar().maximum())
@staticmethod
def msgbox_output(main_window: object, content: str, msg_box_type: str) -> None:
"""
更新messageBox中的内容并弹出
Parameters:
main_window - objectQt的含有messageBox属性的对象这里一般指的是mainWindow里面有唯一的messageBox属性
content - str需要输出的内容
msg_box_type - strmessageBox弹窗的类型
Returns:
Raises:
MsgBoxTypeValueNotExistError
"""
main_window.msgBox.setText(f"{msg_box_type}: {content}")
if msg_box_type is Constants.MSGBOX_TYPE_INFO:
main_window.msgBox.setIcon(QMessageBox.Information)
elif msg_box_type is Constants.MSGBOX_TYPE_WARNING:
main_window.msgBox.setIcon(QMessageBox.Warning)
elif msg_box_type is Constants.MSGBOX_TYPE_ERROR:
main_window.msgBox.setIcon(QMessageBox.Critical)
elif msg_box_type is Constants.MSGBOX_TYPE_QUESTION:
main_window.msgBox.setIcon(QMessageBox.Question)
else:
raise MsgBoxTypeValueNotExistError()
main_window.msgBox.exec()
@staticmethod
def __disableAllButton__(mainWindow, buttonState):
# 禁用所有按钮
all_widgets = mainWindow.centralWidget().findChildren(QWidget)
# 迭代所有部件,查找按钮并禁用它们
for widget in all_widgets:
if isinstance(widget, QPushButton):
if widget.objectName() in buttonState["Current"].keys():
widget.setEnabled(False)
if isinstance(widget, QRadioButton):
if widget.objectName() in buttonState["Current"].keys():
widget.setEnabled(False)
@staticmethod
def __enableAllButton__(mainWindow, buttonState):
# 启用按钮
all_widgets = mainWindow.centralWidget().findChildren(QWidget)
# 迭代所有部件,查找按钮并启用它们
for widget in all_widgets:
if isinstance(widget, QPushButton):
if widget.objectName() in buttonState["Current"].keys():
widget.setEnabled(buttonState["Current"][widget.objectName()])
if isinstance(widget, QRadioButton):
if widget.objectName() in buttonState["Current"].keys():
widget.setEnabled(buttonState["Current"][widget.objectName()])
@staticmethod
def __resetAllButton__(mainWindow, buttonState):
# 启用按钮
all_widgets = mainWindow.centralWidget().findChildren(QWidget)
# 迭代所有部件,查找按钮并启用它们
for widget in all_widgets:
if isinstance(widget, QPushButton):
if widget.objectName() in buttonState["Default"].keys():
widget.setEnabled(buttonState["Default"][widget.objectName()])
if isinstance(widget, QRadioButton):
if widget.objectName() in buttonState["Default"].keys():
widget.setEnabled(buttonState["Default"][widget.objectName()])
@staticmethod
def add_progressbar(mainWindow):
mainWindow.progressbar = QProgressBar()
mainWindow.progressbar.setRange(0, 100)
mainWindow.progressbar.setValue(0)
mainWindow.progressbar.setStyleSheet(Constants.PROGRESSBAR_STYLE)
mainWindow.ui.statusbar.addPermanentWidget(mainWindow.progressbar)
@staticmethod
def statusbar_show_msg(mainWindow, msg):
mainWindow.ui.statusbar.showMessage(msg)
@staticmethod
def statusbar_clear_msg(mainWindow):
mainWindow.ui.statusbar.clearMessage()
@staticmethod
def finish_operation(mainWindow, buttonState):
PublicFunc.statusbar_show_msg(mainWindow, PublicFunc.format_status_msg(Constants.OPERATION_FINISHED))
mainWindow.progressbar.setValue(100)
QApplication.processEvents()
PublicFunc.__enableAllButton__(mainWindow, buttonState)
@staticmethod
def progressbar_update(mainWindow, current: int, total: int, msg: str, progressbarState: int):
if current > total:
raise ValueError("当前进度值大于总进度值")
if progressbarState < 0 or progressbarState > 100:
raise ValueError("进度条值的范围应该在[0, 100]")
PublicFunc.statusbar_show_msg(mainWindow, PublicFunc.format_status_msg(f"({str(current)}/{str(total)}){msg}"))
mainWindow.progressbar.setValue(progressbarState)
QApplication.processEvents()