reorganize file structure
This commit is contained in:
119
components/ext_dialog.py
Normal file
119
components/ext_dialog.py
Normal file
@@ -0,0 +1,119 @@
|
||||
from PySide6.QtCore import QAbstractListModel, QModelIndex, Qt
|
||||
from PySide6.QtWidgets import QWidget, QFormLayout, QVBoxLayout
|
||||
from qfluentwidgets import (
|
||||
MessageBoxBase, LineEdit, ModelComboBox, TextEdit, TeachingTip,
|
||||
InfoBarIcon, TeachingTipTailPosition
|
||||
)
|
||||
|
||||
from common.utils import SAFE, SAFE_MAP, SAFE_MAP_ICON
|
||||
|
||||
|
||||
class SafeStatusListModel(QAbstractListModel):
|
||||
|
||||
def __init__(self, parent: QWidget = None):
|
||||
super().__init__(parent)
|
||||
self.safe_status = SAFE
|
||||
|
||||
def rowCount(self, /, parent = ...):
|
||||
return len(self.safe_status)
|
||||
|
||||
def columnCount(self, parent, /):
|
||||
return 1
|
||||
|
||||
def data(self, index: QModelIndex, /, role: int = ...):
|
||||
row = index.row()
|
||||
if role == Qt.ItemDataRole.EditRole:
|
||||
return SAFE_MAP[self.safe_status[row]]
|
||||
elif role == Qt.ItemDataRole.DecorationRole:
|
||||
return SAFE_MAP_ICON[self.safe_status[row]]
|
||||
elif role == Qt.ItemDataRole.UserRole:
|
||||
return self.safe_status[row]
|
||||
return None
|
||||
|
||||
# --- 添加/编辑 对话框 ---
|
||||
class ExtensionDialog(MessageBoxBase):
|
||||
"""用于添加或编辑插件条目的对话框。"""
|
||||
|
||||
def __init__(self, extension_data: dict[str, str | int] = None, parent=None):
|
||||
super().__init__(parent)
|
||||
self.is_edit_mode = extension_data is not None
|
||||
|
||||
self.setMinimumWidth(400)
|
||||
|
||||
# 创建控件
|
||||
self.lne_id = LineEdit(self)
|
||||
self.lne_name = LineEdit(self)
|
||||
self.safe_combo = ModelComboBox(self)
|
||||
self.notes_edit = TextEdit(self)
|
||||
|
||||
# 填充 SAFE 下拉框 (使用新的 MAP)
|
||||
safe_list_model = SafeStatusListModel(self)
|
||||
self.safe_combo.setModel(safe_list_model)
|
||||
|
||||
# 布局
|
||||
form_layout = QFormLayout()
|
||||
form_layout.addRow("ID:", self.lne_id)
|
||||
form_layout.addRow("名称:", self.lne_name)
|
||||
form_layout.addRow("安全状态:", self.safe_combo)
|
||||
form_layout.addRow("备注:", self.notes_edit)
|
||||
|
||||
self.cw = QWidget(self)
|
||||
main_layout = QVBoxLayout()
|
||||
main_layout.addLayout(form_layout)
|
||||
self.cw.setLayout(main_layout)
|
||||
|
||||
self.viewLayout.addWidget(self.cw)
|
||||
|
||||
if self.is_edit_mode:
|
||||
self.lne_id.setText(extension_data.get("ID", ""))
|
||||
self.lne_id.setReadOnly(True) # ID 不允许修改
|
||||
self.lne_name.setText(extension_data.get("NAME", ""))
|
||||
safe_status: int = extension_data.get("SAFE", -2)
|
||||
self.safe_combo.setCurrentIndex(SAFE.index(safe_status))
|
||||
self.notes_edit.setText(extension_data.get("NOTES", ""))
|
||||
else:
|
||||
self.safe_combo.setCurrentIndex(SAFE.index(-2))
|
||||
|
||||
def validate(self) -> bool:
|
||||
if len(self.lne_id.text()) == 0:
|
||||
TeachingTip.create(
|
||||
target=self.lne_id,
|
||||
title="错误",
|
||||
content="ID 不能为空!",
|
||||
icon=InfoBarIcon.ERROR,
|
||||
isClosable=True,
|
||||
duration=2000,
|
||||
tailPosition=TeachingTipTailPosition.BOTTOM,
|
||||
parent=self
|
||||
)
|
||||
return False
|
||||
if len(self.lne_name.text()) == 0:
|
||||
TeachingTip.create(
|
||||
target=self.lne_name,
|
||||
title="错误",
|
||||
content="名称不能为空!",
|
||||
icon=InfoBarIcon.ERROR,
|
||||
isClosable=True,
|
||||
duration=2000,
|
||||
tailPosition=TeachingTipTailPosition.BOTTOM,
|
||||
parent=self,
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
def get_data(self) -> dict[str, str | int]:
|
||||
"""获取对话框中用于 'add_one' 的数据"""
|
||||
return {
|
||||
"ID": self.lne_id.text(),
|
||||
"NAME": self.lne_name.text(),
|
||||
"SAFE": self.safe_combo.currentData(),
|
||||
"NOTES": self.notes_edit.toPlainText()
|
||||
}
|
||||
|
||||
def get_update_payload(self) -> dict[str, str | int]:
|
||||
"""获取对话框中用于 'update_one' 的数据 (不含ID)"""
|
||||
return {
|
||||
"NAME": self.lne_name.text(),
|
||||
"SAFE": self.safe_combo.currentData(),
|
||||
"NOTES": self.notes_edit.toPlainText()
|
||||
}
|
||||
Reference in New Issue
Block a user