使用 ProgressInfoBar 替代原有方案

This commit is contained in:
2025-05-26 19:42:20 +08:00
parent e3fde36390
commit 50a360aca2
5 changed files with 66 additions and 95 deletions

View File

@@ -215,10 +215,10 @@ class ExcelReader:
except Exception as e: except Exception as e:
error_message = traceback.format_exc() error_message = traceback.format_exc()
raise f""" raise Exception(f"""
原始错误: 原始错误:
{error_message} {error_message}
""" """)
def set_file_path(self, file_path: str): def set_file_path(self, file_path: str):
self.file_path = file_path self.file_path = file_path

View File

@@ -71,6 +71,7 @@ class DTGWorker(QObject):
class ARGWorker(QObject): class ARGWorker(QObject):
finished = Signal() finished = Signal()
error = Signal(str, str) error = Signal(str, str)
info = Signal(str, str)
def __init__(self, input_filepath: str, output_filepath: str, output_filename: str, disable_cc: bool = False): def __init__(self, input_filepath: str, output_filepath: str, output_filename: str, disable_cc: bool = False):
super().__init__() super().__init__()
@@ -81,10 +82,10 @@ class ARGWorker(QObject):
def run(self): def run(self):
try: try:
excel = ExcelReader(self.input_filepath, self.disable_compatibility_check) excel = ExcelReader(self.input_filepath, self.disable_compatibility_check, self.info.emit)
excel.run() excel.run()
doc = DocxWriter(self.output_filepath, self.output_filename, excel) doc = DocxWriter(self.output_filepath, self.output_filename, excel, self.info.emit)
doc.write() doc.write()
except Exception as e: except Exception as e:
self.error.emit("😢 不好出错了", str(e)) self.error.emit("😢 不好出错了", str(e))

View File

@@ -5,9 +5,10 @@ from qfluentwidgets import InfoBar, InfoBarPosition, InfoBarIcon, FluentIconBase
class ProgressInfoBar(InfoBar): class ProgressInfoBar(InfoBar):
def __init__(self, icon: InfoBarIcon | FluentIconBase | QIcon | str, title: str, content: str, orient=Qt.Horizontal, def __init__(self, icon: InfoBarIcon | FluentIconBase | QIcon | str = InfoBarIcon.INFORMATION, title: str = "1",
content: str = "", orient=Qt.Horizontal,
isClosable=True, duration=1000, isClosable=True, duration=1000,
position=InfoBarPosition.TOP_RIGHT, parent=None): position=InfoBarPosition.BOTTOM, parent=None):
super().__init__(icon, title, content, orient, isClosable, duration, position, parent) super().__init__(icon, title, content, orient, isClosable, duration, position, parent)
self.pb = ProgressBar() self.pb = ProgressBar()
@@ -30,8 +31,7 @@ class ProgressInfoBar(InfoBar):
self.pb.setVisible(False) self.pb.setVisible(False)
self.adjustSize() self.adjustSize()
manager.add(self) manager.add(self)
else:
return
else: else:
if not self.pb.isVisible(): if not self.pb.isVisible():
manager.remove(self) manager.remove(self)
@@ -72,3 +72,18 @@ class ProgressInfoBar(InfoBar):
duration=duration, duration=duration,
parent=self.parent() parent=self.parent()
) )
def show_error(self, title: str = '错误!', content: str = "操作失败", isCloseable: bool = True,
duration: int = 5000):
self.setVisible(False)
InfoBarManager.make(self.position).remove(self)
InfoBar.error(
title=title,
content=content,
orient=Qt.Horizontal,
isClosable=isCloseable,
position=InfoBarPosition.BOTTOM,
duration=duration,
parent=self.parent()
)

View File

@@ -3,11 +3,13 @@ from functools import wraps
from typing import Callable, Literal from typing import Callable, Literal
from PySide6.QtCore import Qt, Signal, QThread from PySide6.QtCore import Qt, Signal, QThread
from PySide6.QtWidgets import QVBoxLayout, QFileDialog, QHBoxLayout, QProgressBar from PySide6.QtWidgets import QVBoxLayout, QFileDialog, QHBoxLayout
from qfluentwidgets import GroupHeaderCardWidget, FluentIcon, PushButton, LineEdit, IconWidget, InfoBarIcon, BodyLabel, \ from qfluentwidgets import GroupHeaderCardWidget, FluentIcon, PushButton, LineEdit, IconWidget, InfoBarIcon, BodyLabel, \
PrimaryPushButton, SwitchButton, MessageBox, InfoBar, InfoBarPosition, IndeterminateProgressBar PrimaryPushButton, SwitchButton
from module import LOGLEVEL
from module.worker import ARGWorker from module.worker import ARGWorker
from ui.components.infobar import ProgressInfoBar
from ui.components.widget import Widget from ui.components.widget import Widget
@@ -120,12 +122,14 @@ class AchievementWidget(Widget):
# ================================= # =================================
self.infoBar = None self.pib = ProgressInfoBar(parent=self, isClosable=False, duration=-1)
self.pib.hide()
# ================================= # =================================
self.thread = None self.thread = None
self.worker = None self.worker = None
self.successFlag = True
# ================================== # ==================================
@@ -187,12 +191,13 @@ class AchievementWidget(Widget):
self.outputGroup.startButton.setEnabled(False) self.outputGroup.startButton.setEnabled(False)
# 线程启动与信号连接
self.thread.started.connect(self.worker.run) self.thread.started.connect(self.worker.run)
self.show_info_bar('') self.show_info_bar()
self.worker.error.connect(self.show_error) self.successFlag = True
self.worker.info.connect(self.show_info)
self.worker.error.connect(self.show_error)
self.worker.finished.connect(self.thread.quit) self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater) self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater) self.thread.finished.connect(self.thread.deleteLater)
@@ -207,37 +212,27 @@ class AchievementWidget(Widget):
self.worker = None self.worker = None
def after_generate(self): def after_generate(self):
self.outputGroup.startButton.setEnabled(True) if self.outputGroup.autoOpenSwitch.isChecked() and self.successFlag:
if self.outputGroup.autoOpenSwitch.isChecked():
try: try:
os.startfile(self.output_file_path + "/" + self.output_file_name + ".docx") os.startfile(self.output_file_path + "/" + self.output_file_name + ".docx")
except Exception as e: except Exception as e:
self.show_error("?? 不好出错了", str(e)) self.show_error("?? 不好出错了", str(e))
if self.infoBar: if self.successFlag:
self.infoBar.close() self.pib.show_success(
self.infoBar = InfoBar.success( content="正在打开文件" if self.outputGroup.autoOpenSwitch.isChecked() else "文件已保存")
title='成功!', else:
content="正在打开文件" if self.outputGroup.autoOpenSwitch.isChecked() else "文件已保存", self.pib.show_error()
orient=Qt.Horizontal, self.outputGroup.startButton.setEnabled(True)
isClosable=True,
position=InfoBarPosition.BOTTOM,
duration=5000,
parent=self
)
def show_error(self, title: str, content: str): def show_error(self, title: str, content: str):
self.successFlag = False
self.error.emit(title, content) self.error.emit(title, content)
def show_info_bar(self, info: str): def show_info_bar(self):
self.infoBar = InfoBar( self.pib.set_title('请稍后')
icon=InfoBarIcon.INFORMATION, self.pib.show()
title='请稍后', self.pib.set_progress(101)
content=info,
orient=Qt.Horizontal, def show_info(self, content: str, level: str):
isClosable=False, if level == LOGLEVEL.INFO:
position=InfoBarPosition.BOTTOM, self.pib.set_title(content)
duration=-1,
parent=self
)
self.infoBar.addWidget(IndeterminateProgressBar(start=True))
self.infoBar.show()

View File

@@ -1,12 +1,13 @@
from typing import Literal, Callable
from functools import wraps from functools import wraps
from typing import Literal, Callable
from PySide6.QtCore import Qt, Signal, QThread from PySide6.QtCore import Qt, Signal, QThread
from PySide6.QtWidgets import QHBoxLayout, QVBoxLayout, QFileDialog from PySide6.QtWidgets import QHBoxLayout, QVBoxLayout, QFileDialog
from qfluentwidgets import GroupHeaderCardWidget, PushButton, IconWidget, InfoBarIcon, \ from qfluentwidgets import GroupHeaderCardWidget, PushButton, IconWidget, InfoBarIcon, \
BodyLabel, PrimaryPushButton, FluentIcon, LineEdit, InfoBar, InfoBarPosition, ProgressBar, IndeterminateProgressBar BodyLabel, PrimaryPushButton, FluentIcon, LineEdit
from module.worker import DTGWorker from module.worker import DTGWorker
from ui.components.infobar import ProgressInfoBar
from ui.components.widget import Widget from ui.components.widget import Widget
@@ -121,8 +122,8 @@ class DefenseWidget(Widget):
self.vbox.addWidget(self.exportCard) self.vbox.addWidget(self.exportCard)
self.vbox.addStretch(1) self.vbox.addStretch(1)
self.infoBar = None self.pib = ProgressInfoBar(parent=self, isClosable=False, duration=-1)
self.pb = None self.pib.hide()
self.thread = None self.thread = None
self.worker = None self.worker = None
@@ -142,40 +143,16 @@ class DefenseWidget(Widget):
self.exportCard.startSignal.connect(self.start_generate) self.exportCard.startSignal.connect(self.start_generate)
def show_info_bar(self): def show_info_bar(self):
self.infoBar = InfoBar( self.pib.show()
icon=InfoBarIcon.INFORMATION, self.pib.set_title('请稍后')
title='请稍后',
content="",
orient=Qt.Horizontal,
isClosable=False,
position=InfoBarPosition.BOTTOM,
duration=-1,
parent=self
)
self.pb = ProgressBar(self.infoBar)
self.infoBar.addWidget(self.pb)
self.infoBar.show()
def set_pb_value(self, value: int) -> None: def set_pb_value(self, value: int) -> None:
if self.pb: self.pib.set_progress(value)
self.pb.setValue(value) if value == 100:
if value == 100: self.pib.set_progress(101)
self.infoBar.close() self.pib.set_title('正在转换文件')
self.infoBar = InfoBar( elif value == -1:
icon=InfoBarIcon.INFORMATION, self.successFlag = False
title='正在转换文件',
content="",
orient=Qt.Horizontal,
isClosable=False,
position=InfoBarPosition.BOTTOM,
duration=-1,
parent=self
)
self.infoBar.addWidget(IndeterminateProgressBar(start=True))
self.infoBar.show()
self.pb = None
elif value == -1:
self.successFlag = False
def enable_start_check(func: Callable): def enable_start_check(func: Callable):
@wraps(func) @wraps(func)
@@ -254,27 +231,10 @@ class DefenseWidget(Widget):
def after_generate(self): def after_generate(self):
self.exportCard.startButton.setEnabled(True) self.exportCard.startButton.setEnabled(True)
self.infoBar.close()
if self.successFlag: if self.successFlag:
self.infoBar = InfoBar.success( self.pib.show_success(content="正在打开文件...")
title='成功!',
content="正在打开文件...",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.BOTTOM,
duration=5000,
parent=self
)
else: else:
self.infoBar = InfoBar.error( self.pib.show_error()
title='失败!',
content="",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.BOTTOM,
duration=5000,
parent=self
)
def show_error(self, title: str, content: str): def show_error(self, title: str, content: str):
self.errorSignal.emit(title, content) self.errorSignal.emit(title, content)