Refactor SubProcessDlg class

重构子进程对话框类,将多功能按钮的槽函数移至类方法中;
修正各对话框类__init__方法的类型注解;
注释等其他微小优化;
This commit is contained in:
muzing 2022-11-26 17:09:11 +08:00
parent 775529805d
commit 7bb8306524
2 changed files with 42 additions and 44 deletions

View File

@ -1,4 +1,7 @@
from PySide6.QtCore import Qt
import subprocess
from typing import Optional
from PySide6 import QtCore
from PySide6.QtGui import QPixmap
from PySide6.QtWidgets import (
QDialog,
@ -19,7 +22,7 @@ class ScriptFileDlg(QFileDialog):
用于获取入口脚本文件的对话框
"""
def __init__(self, parent: QWidget = None) -> None:
def __init__(self, parent: Optional[QWidget] = None) -> None:
"""
:param parent: 父控件对象
"""
@ -48,7 +51,7 @@ class IconFileDlg(QFileDialog):
用于获取应用图标文件的对话框
"""
def __init__(self, parent: QWidget = None) -> None:
def __init__(self, parent: Optional[QWidget] = None) -> None:
"""
:param parent: 父控件对象
"""
@ -77,7 +80,7 @@ class AddDataDlg(QFileDialog):
用于添加附加数据的对话框
"""
def __init__(self, parent: QWidget = None) -> None:
def __init__(self, parent: Optional[QWidget] = None) -> None:
"""
:param parent: 父控件对象
"""
@ -99,7 +102,7 @@ class AboutDlg(QMessageBox):
用于显示关于信息的对话框
"""
def __init__(self, parent: QWidget = None) -> None:
def __init__(self, parent: Optional[QWidget] = None) -> None:
"""
:param parent: 父控件对象
"""
@ -116,7 +119,7 @@ class AboutDlg(QMessageBox):
self.setWindowTitle("关于")
self.setStandardButtons(QMessageBox.Ok)
self.setTextFormat(Qt.MarkdownText)
self.setTextFormat(QtCore.Qt.MarkdownText)
self.setText(self.about_text)
self.setIconPixmap(
QPixmap("py2exe_gui/Resources/Icons/Py2exe-GUI_icon_72px.png")
@ -145,11 +148,9 @@ class SubProcessDlg(QDialog):
用于显示子进程信息的对话框
"""
# TODO 重构SubProcessDlg
def __init__(self, parent: QWidget = None) -> None:
def __init__(self, parent: QWidget) -> None:
"""
:param parent: 父控件对象
:param parent: 父控件对象必须为 MainApp
"""
super(SubProcessDlg, self).__init__(parent)
@ -177,6 +178,10 @@ class SubProcessDlg(QDialog):
main_layout.addWidget(self.multifunction_btn)
self.setLayout(main_layout)
# 连接信号与槽
self.multifunction_btn.clicked.connect(self.handle_multifunction) # type:ignore
@QtCore.Slot(tuple)
def handle_output(self, subprocess_output: tuple[int, str]) -> None:
"""
处理子进程的输出 \n
@ -196,17 +201,21 @@ class SubProcessDlg(QDialog):
if output_text == "正在运行中……":
self.multifunction_btn.setText("取消")
@QtCore.Slot()
def handle_multifunction(self) -> None:
"""
处理多功能按钮点击信号的槽 \n
"""
if __name__ == "__main__":
import sys
from PySide6.QtWidgets import QApplication
app = QApplication(sys.argv)
# window = ScriptFileDlg()
# window = IconFileDlg()
# window.fileSelected.connect(lambda f: print(f)) # type: ignore
# window = AboutDlg()
window = SubProcessDlg()
window.open()
sys.exit(app.exec())
btn_text = self.multifunction_btn.text()
if btn_text == "取消":
self.parent().packager.subprocess.abort_process()
self.close()
elif btn_text == "打开输出位置":
dist_path = self.parent().packaging_task.script_path.parent / "dist"
if self.parent().running_platform == "Windows":
os.startfile(dist_path) # type: ignore
elif self.parent().running_platform == "Linux":
subprocess.call(["xdg-open", dist_path])
elif self.parent().running_platform == "macOS":
subprocess.call(["open", dist_path])

View File

@ -1,22 +1,26 @@
import os
import subprocess
import sys
from PySide6 import QtCore, QtGui
from PySide6.QtWidgets import QApplication
# from .Constants import *
from .Core import Packaging, PackagingTask
from .Widgets import MainWindow, SubProcessDlg
def get_platform() -> str:
"""
辅助函数用于获取当前运行的平台 \n
:return: platform
"""
if sys.platform.startswith("win32"):
return "Windows"
elif sys.platform.startswith("linux"):
return "Linux"
elif sys.platform.startswith("darwin"):
return "macOS"
else:
return "others"
class MainApp(MainWindow):
@ -57,28 +61,13 @@ class MainApp(MainWindow):
@QtCore.Slot()
def run_packaging() -> None:
"""
运行打包按钮的槽函数 \n
"""
self.packager.run_packaging_process()
self.subprocess_dlg.show()
self.center_widget.run_packaging_btn.clicked.connect(run_packaging)
@QtCore.Slot()
def handle_multifunction() -> None:
"""处理子进程对话框多功能按钮点击信号的槽 \n"""
btn_text = self.subprocess_dlg.multifunction_btn.text()
if btn_text == "取消":
self.packager.subprocess.abort_process()
self.subprocess_dlg.close()
elif btn_text == "打开输出位置":
dist_path = self.packaging_task.script_path.parent / "dist"
if self.running_platform == "Windows":
os.startfile(dist_path) # type: ignore
elif self.running_platform == "Linux":
subprocess.call(["xdg-open", dist_path])
elif self.running_platform == "macOS":
subprocess.call(["open", dist_path])
self.subprocess_dlg.multifunction_btn.clicked.connect(handle_multifunction)
self.packager.subprocess.output.connect(self.subprocess_dlg.handle_output)
def closeEvent(self, event: QtGui.QCloseEvent) -> None: