Files
safe-marks-client/components/main_window.py
2025-10-18 21:31:06 -04:00

104 lines
4.0 KiB
Python

from PySide6.QtWidgets import QApplication
from PySide6.QtCore import Qt, QThread, QModelIndex
from PySide6.QtGui import QIcon
from qfluentwidgets import MSFluentWindow, setTheme, Theme, InfoBar, FluentIcon as Fi
from common.utils import show_quick_tip, accept_warning
from common.api_worker import ApiWorker
from components.ext_dialog import ExtensionDialog
from components.main_interface import MainInterface
class MainWindow(MSFluentWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("插件安全标记")
self.setWindowIcon(QIcon(":/logo.png"))
# self.navigationInterface.hide()
self.resize(1000, 760)
desktop = QApplication.screens()[0].availableGeometry()
w, h = desktop.width(), desktop.height()
self.move(w // 2 - self.width() // 2, h // 2 - self.height() // 2)
setTheme(Theme.LIGHT)
# --- UI ---
self.main_interface = MainInterface(parent=self)
self.addSubInterface(self.main_interface, Fi.HOME, "主页", Fi.HOME_FILL)
# --- 初始化后台工作线程和 QObject ---
self.thread = QThread()
self.worker = ApiWorker()
self.worker.moveToThread(self.thread)
# 连接 Worker 的新信号
self.worker.queryAllFinished.connect(self.main_interface.ext_model.update_data)
self.worker.addOneFinished.connect(self.handle_operation_finished)
self.worker.updateOneFinished.connect(self.handle_operation_finished)
self.worker.deleteOneFinished.connect(self.handle_operation_finished)
self.worker.error.connect(self.handle_error)
self.thread.start()
# 连接信号和槽
self.main_interface.pbn_refresh.clicked.connect(self.worker.do_query_all)
self.main_interface.pbn_add.clicked.connect(self.on_add)
self.main_interface.pbn_delete.clicked.connect(self.on_delete)
self.main_interface.tbv_m.doubleClicked.connect(self.on_edit)
# 初始加载数据
self.worker.do_query_all()
def closeEvent(self, event):
"""关闭窗口时,确保线程也退出"""
self.thread.quit()
self.thread.wait()
super().closeEvent(event)
# --- GUI 槽函数 (在主线程中运行) ---
def handle_operation_finished(self, result: dict):
"""添加/更新/删除成功后的通用处理器"""
InfoBar.success("", "操作已成功完成。", duration=2000, parent=self.window())
self.worker.do_query_all() # 重新加载数据
def handle_error(self, error_message: str):
"""显示来自工作线程的错误消息"""
show_quick_tip(self, "API 错误", f"与服务器通信时发生错误:\n{error_message}")
# --- 按钮点击处理 ---
def on_add(self):
"""打开“添加”对话框 (add_one)"""
dialog = ExtensionDialog(parent=self)
if dialog.exec():
data = dialog.get_data()
self.worker.do_add_one(data)
def on_edit(self, index: QModelIndex):
"""打开“编辑”对话框(通过双击)(update_one)"""
item_data = index.data(Qt.ItemDataRole.UserRole)
item_id = item_data.get("ID")
dialog = ExtensionDialog(extension_data=item_data, parent=self)
if dialog.exec():
update_payload = dialog.get_update_payload()
self.worker.do_update_one(item_id, update_payload)
def on_delete(self):
"""删除当前选定的行 (delete_one)"""
# 前面设定了只能选中一行
ext_data: list[dict] = [index.data(Qt.ItemDataRole.UserRole)
for index in self.main_interface.tbv_m.selectedIndexes()
if index.column() == 0]
if len(ext_data) == 0:
show_quick_tip(self, "警告", "你没有选中任何插件。")
return
ext_d: dict = ext_data[0]
if accept_warning(self, True, "警告",
f"你确定要删除插件【{ext_d.get('NAME')}】吗?"):
return
self.worker.do_delete_one(ext_d.get("ID"))