init
This commit is contained in:
165
app/view/widgets/policy_messageBox.py
Normal file
165
app/view/widgets/policy_messageBox.py
Normal file
@@ -0,0 +1,165 @@
|
||||
# coding: utf-8
|
||||
import logging
|
||||
|
||||
from PyQt6.QtCore import Qt, QTimer
|
||||
from PyQt6.QtWidgets import QListWidgetItem
|
||||
from qfluentwidgets import (
|
||||
InfoBar,
|
||||
InfoBarPosition,
|
||||
ListWidget,
|
||||
MessageBoxBase,
|
||||
SubtitleLabel,
|
||||
)
|
||||
|
||||
from app.core import ChangePolicyThread, GetPoliciesThread, policyConfig, signalBus
|
||||
|
||||
|
||||
class PolicyChooseMessageBox(MessageBoxBase):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent=parent)
|
||||
self.currentPath = "/"
|
||||
self.policyDict = {}
|
||||
self.isLoading = True
|
||||
self.originalTitle = "选择存储策略"
|
||||
self.setClosableOnMaskClicked(True)
|
||||
|
||||
self.setupUI()
|
||||
|
||||
# 开始获取策略列表
|
||||
self.getPoliciesThread = GetPoliciesThread()
|
||||
self.getPoliciesThread.successGetSignal.connect(self.refreshPolicyList)
|
||||
self.getPoliciesThread.errorSignal.connect(self._handleGetPoliciesError)
|
||||
self.getPoliciesThread.start()
|
||||
|
||||
# 初始化更改策略线程(但不启动)
|
||||
self.changePolicyThread = None
|
||||
|
||||
def setupUI(self):
|
||||
"""设置UI界面"""
|
||||
self.titleLabel = SubtitleLabel(self.originalTitle, self)
|
||||
self.policyListWidget = ListWidget(self)
|
||||
|
||||
# 添加加载提示
|
||||
self.loadingItem = QListWidgetItem("正在加载策略列表...")
|
||||
self.policyListWidget.addItem(self.loadingItem)
|
||||
|
||||
self.viewLayout.addWidget(self.titleLabel)
|
||||
self.viewLayout.addWidget(self.policyListWidget)
|
||||
|
||||
# 隐藏确定取消按钮组
|
||||
self.buttonGroup.hide()
|
||||
|
||||
def connectSignals(self):
|
||||
"""连接信号与槽"""
|
||||
self.policyListWidget.currentTextChanged.connect(self.onPolicyChanged)
|
||||
self.policyListWidget.itemClicked.connect(self.selfClicked)
|
||||
|
||||
def selfClicked(self, listWidget: QListWidgetItem):
|
||||
if listWidget.text() == policyConfig.returnPolicy()["name"]:
|
||||
QTimer.singleShot(100, self.accept)
|
||||
|
||||
def onPolicyChanged(self, text):
|
||||
"""处理策略更改"""
|
||||
if not text or self.isLoading or text == "正在加载策略列表...":
|
||||
return
|
||||
|
||||
policy_id = self.policyDict.get(text)
|
||||
if not policy_id:
|
||||
return
|
||||
|
||||
# 如果已经有更改线程在运行,先停止它
|
||||
if self.changePolicyThread and self.changePolicyThread.isRunning():
|
||||
self.changePolicyThread.quit()
|
||||
self.changePolicyThread.wait()
|
||||
|
||||
# 创建并启动新的更改线程
|
||||
self.changePolicyThread = ChangePolicyThread(self.currentPath, policy_id)
|
||||
self.changePolicyThread.successChangedSignal.connect(
|
||||
self._handlePolicyChangeSuccess
|
||||
)
|
||||
self.changePolicyThread.errorSignal.connect(self._handlePolicyChangeError)
|
||||
self.changePolicyThread.start()
|
||||
|
||||
# 更新UI状态 - 只更改标题
|
||||
self._setLoadingState(True, f"正在切换到策略: {text}")
|
||||
|
||||
def refreshPolicyList(self, policiesList):
|
||||
"""刷新策略列表"""
|
||||
self.isLoading = False
|
||||
self.policyListWidget.clear()
|
||||
self.policyDict.clear()
|
||||
|
||||
currentPolicy = policyConfig.returnPolicy()
|
||||
currentIndex = 0
|
||||
|
||||
for i, policy in enumerate(policiesList):
|
||||
self.policyListWidget.addItem(QListWidgetItem(policy["name"]))
|
||||
self.policyDict[policy["name"]] = policy["id"]
|
||||
|
||||
if policy["id"] == currentPolicy["id"]:
|
||||
currentIndex = i
|
||||
|
||||
# 设置当前选中项
|
||||
if self.policyListWidget.count() > 0:
|
||||
self.policyListWidget.setCurrentRow(currentIndex)
|
||||
|
||||
self.currentPath = policyConfig.returnCurrentPath()
|
||||
self.connectSignals()
|
||||
|
||||
# 恢复原始标题
|
||||
self.titleLabel.setText(self.originalTitle)
|
||||
|
||||
def _handleGetPoliciesError(self, error_msg):
|
||||
"""处理获取策略列表错误"""
|
||||
self.policyListWidget.clear()
|
||||
errorItem = QListWidgetItem(f"加载失败: {error_msg}")
|
||||
self.policyListWidget.addItem(errorItem)
|
||||
self.isLoading = False
|
||||
# 恢复原始标题
|
||||
self.titleLabel.setText(self.originalTitle)
|
||||
|
||||
def _handlePolicyChangeSuccess(self):
|
||||
"""处理策略更改成功"""
|
||||
self._setLoadingState(False)
|
||||
|
||||
# 显示成功提示
|
||||
if self.parent():
|
||||
InfoBar.success(
|
||||
title="操作成功",
|
||||
content="存储策略已成功更改",
|
||||
orient=Qt.Orientation.Horizontal,
|
||||
isClosable=True,
|
||||
position=InfoBarPosition.TOP_RIGHT,
|
||||
duration=2000,
|
||||
parent=self.window(),
|
||||
)
|
||||
signalBus.refreshFolderListSignal.emit()
|
||||
QTimer.singleShot(1000, self.accept)
|
||||
|
||||
def _handlePolicyChangeError(self, error_msg):
|
||||
"""处理策略更改错误"""
|
||||
self._setLoadingState(False)
|
||||
|
||||
# 显示错误提示
|
||||
if self.parent():
|
||||
InfoBar.error(
|
||||
title="操作失败",
|
||||
content=f"更改策略时出错: {error_msg}",
|
||||
orient=Qt.Orientation.Horizontal,
|
||||
isClosable=True,
|
||||
position=InfoBarPosition.TOP_RIGHT,
|
||||
duration=3000,
|
||||
parent=self.window(),
|
||||
)
|
||||
QTimer.singleShot(1000, self.reject)
|
||||
|
||||
def _setLoadingState(self, loading, message=None):
|
||||
"""设置加载状态"""
|
||||
if loading:
|
||||
self.policyListWidget.setEnabled(False)
|
||||
if message:
|
||||
logging.info(message)
|
||||
else:
|
||||
self.policyListWidget.setEnabled(True)
|
||||
# 恢复原始标题
|
||||
self.titleLabel.setText(self.originalTitle)
|
||||
Reference in New Issue
Block a user