220 lines
7.9 KiB
Python
220 lines
7.9 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
|
|||
|
|
"""
|
|||
|
|
DUT管理视图(Device Under Test - 试验样品管理)
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QLabel,
|
|||
|
|
QTableWidget, QTableWidgetItem, QGroupBox,
|
|||
|
|
QPushButton, QLineEdit, QComboBox, QMessageBox)
|
|||
|
|
from PyQt5.QtCore import Qt
|
|||
|
|
|
|||
|
|
|
|||
|
|
class DUTManagementView(QWidget):
|
|||
|
|
def __init__(self):
|
|||
|
|
super().__init__()
|
|||
|
|
self.current_dut = None
|
|||
|
|
self.init_ui()
|
|||
|
|
self.load_data()
|
|||
|
|
|
|||
|
|
def init_ui(self):
|
|||
|
|
"""初始化界面"""
|
|||
|
|
layout = QVBoxLayout(self)
|
|||
|
|
layout.setSpacing(15)
|
|||
|
|
|
|||
|
|
# 标题
|
|||
|
|
title_label = QLabel("DUT管理")
|
|||
|
|
title_label.setObjectName("title")
|
|||
|
|
layout.addWidget(title_label)
|
|||
|
|
|
|||
|
|
# 数据统计
|
|||
|
|
stats_group = QGroupBox("数据统计")
|
|||
|
|
stats_layout = QHBoxLayout(stats_group)
|
|||
|
|
|
|||
|
|
self.dut_count_label = QLabel("DUT总数: 0")
|
|||
|
|
stats_layout.addWidget(self.dut_count_label)
|
|||
|
|
stats_layout.addStretch()
|
|||
|
|
|
|||
|
|
layout.addWidget(stats_group)
|
|||
|
|
|
|||
|
|
# 主体区域
|
|||
|
|
main_layout = QHBoxLayout()
|
|||
|
|
|
|||
|
|
# DUT列表
|
|||
|
|
list_group = QGroupBox("DUT列表")
|
|||
|
|
list_layout = QVBoxLayout(list_group)
|
|||
|
|
|
|||
|
|
self.dut_table = QTableWidget()
|
|||
|
|
self.dut_table.setColumnCount(4)
|
|||
|
|
self.dut_table.setHorizontalHeaderLabels(["ID", "名称", "状态", "操作"])
|
|||
|
|
self.dut_table.setAlternatingRowColors(True)
|
|||
|
|
self.dut_table.setSelectionBehavior(self.dut_table.SelectRows)
|
|||
|
|
self.dut_table.cellClicked.connect(self.on_dut_selected)
|
|||
|
|
|
|||
|
|
list_layout.addWidget(self.dut_table)
|
|||
|
|
|
|||
|
|
main_layout.addWidget(list_group, 1)
|
|||
|
|
|
|||
|
|
# DUT详情
|
|||
|
|
detail_group = QGroupBox("DUT详情")
|
|||
|
|
detail_layout = QVBoxLayout(detail_group)
|
|||
|
|
|
|||
|
|
# 名称输入
|
|||
|
|
name_layout = QHBoxLayout()
|
|||
|
|
name_layout.addWidget(QLabel("名称:"))
|
|||
|
|
self.name_input = QLineEdit()
|
|||
|
|
name_layout.addWidget(self.name_input)
|
|||
|
|
detail_layout.addLayout(name_layout)
|
|||
|
|
|
|||
|
|
# 描述输入
|
|||
|
|
desc_layout = QHBoxLayout()
|
|||
|
|
desc_layout.addWidget(QLabel("描述:"))
|
|||
|
|
self.desc_input = QLineEdit()
|
|||
|
|
desc_layout.addWidget(self.desc_input)
|
|||
|
|
detail_layout.addLayout(desc_layout)
|
|||
|
|
|
|||
|
|
# 状态选择
|
|||
|
|
status_layout = QHBoxLayout()
|
|||
|
|
status_layout.addWidget(QLabel("状态:"))
|
|||
|
|
self.status_combo = QComboBox()
|
|||
|
|
self.status_combo.addItems(["Active", "Inactive"])
|
|||
|
|
status_layout.addWidget(self.status_combo)
|
|||
|
|
detail_layout.addLayout(status_layout)
|
|||
|
|
|
|||
|
|
# 操作按钮
|
|||
|
|
btn_layout = QHBoxLayout()
|
|||
|
|
self.save_btn = QPushButton("保存")
|
|||
|
|
self.save_btn.clicked.connect(self.save_dut)
|
|||
|
|
self.new_btn = QPushButton("新建")
|
|||
|
|
self.new_btn.clicked.connect(self.new_dut)
|
|||
|
|
self.delete_btn = QPushButton("删除")
|
|||
|
|
self.delete_btn.clicked.connect(self.delete_dut)
|
|||
|
|
|
|||
|
|
btn_layout.addWidget(self.save_btn)
|
|||
|
|
btn_layout.addWidget(self.new_btn)
|
|||
|
|
btn_layout.addWidget(self.delete_btn)
|
|||
|
|
detail_layout.addLayout(btn_layout)
|
|||
|
|
|
|||
|
|
main_layout.addWidget(detail_group, 1)
|
|||
|
|
|
|||
|
|
layout.addLayout(main_layout)
|
|||
|
|
|
|||
|
|
# 初始化按钮状态
|
|||
|
|
self.update_button_states()
|
|||
|
|
|
|||
|
|
def load_data(self):
|
|||
|
|
"""加载数据"""
|
|||
|
|
# 这里应该从控制器获取数据,现在使用示例数据
|
|||
|
|
self.dut_data = [
|
|||
|
|
{"id": 1, "name": "DUT-A", "description": "DUT-A描述", "status": "Active"},
|
|||
|
|
{"id": 2, "name": "DUT-B", "description": "DUT-B描述", "status": "Inactive"},
|
|||
|
|
{"id": 3, "name": "DUT-C", "description": "DUT-C描述", "status": "Active"},
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
# 更新统计数据
|
|||
|
|
self.dut_count_label.setText(f"DUT总数: {len(self.dut_data)}")
|
|||
|
|
|
|||
|
|
# 填充表格
|
|||
|
|
self.dut_table.setRowCount(len(self.dut_data))
|
|||
|
|
|
|||
|
|
for row, dut in enumerate(self.dut_data):
|
|||
|
|
# ID列
|
|||
|
|
item_id = QTableWidgetItem(str(dut["id"]))
|
|||
|
|
item_id.setFlags(item_id.flags() & ~Qt.ItemIsEditable)
|
|||
|
|
self.dut_table.setItem(row, 0, item_id)
|
|||
|
|
|
|||
|
|
# 名称列
|
|||
|
|
item_name = QTableWidgetItem(dut["name"])
|
|||
|
|
item_name.setFlags(item_name.flags() & ~Qt.ItemIsEditable)
|
|||
|
|
self.dut_table.setItem(row, 1, item_name)
|
|||
|
|
|
|||
|
|
# 状态列
|
|||
|
|
item_status = QTableWidgetItem(dut["status"])
|
|||
|
|
item_status.setFlags(item_status.flags() & ~Qt.ItemIsEditable)
|
|||
|
|
if dut["status"] == "Active":
|
|||
|
|
item_status.setForeground(Qt.darkGreen)
|
|||
|
|
else:
|
|||
|
|
item_status.setForeground(Qt.red)
|
|||
|
|
self.dut_table.setItem(row, 2, item_status)
|
|||
|
|
|
|||
|
|
# 操作列
|
|||
|
|
btn_edit = QPushButton("编辑")
|
|||
|
|
btn_edit.clicked.connect(lambda checked, did=dut["id"]: self.edit_dut(did))
|
|||
|
|
self.dut_table.setCellWidget(row, 3, btn_edit)
|
|||
|
|
|
|||
|
|
# 调整列宽
|
|||
|
|
self.dut_table.resizeColumnsToContents()
|
|||
|
|
|
|||
|
|
def on_dut_selected(self, row, column):
|
|||
|
|
"""DUT被选中"""
|
|||
|
|
if row >= 0 and row < len(self.dut_data):
|
|||
|
|
self.current_dut = self.dut_data[row]
|
|||
|
|
self.populate_form()
|
|||
|
|
self.update_button_states()
|
|||
|
|
|
|||
|
|
def populate_form(self):
|
|||
|
|
"""填充表单"""
|
|||
|
|
if self.current_dut:
|
|||
|
|
self.name_input.setText(self.current_dut["name"])
|
|||
|
|
self.desc_input.setText(self.current_dut["description"])
|
|||
|
|
self.status_combo.setCurrentText(self.current_dut["status"])
|
|||
|
|
|
|||
|
|
def update_button_states(self):
|
|||
|
|
"""更新按钮状态"""
|
|||
|
|
has_selection = self.current_dut is not None
|
|||
|
|
self.save_btn.setEnabled(has_selection)
|
|||
|
|
self.delete_btn.setEnabled(has_selection)
|
|||
|
|
|
|||
|
|
def new_dut(self):
|
|||
|
|
"""新建DUT"""
|
|||
|
|
self.current_dut = None
|
|||
|
|
self.name_input.clear()
|
|||
|
|
self.desc_input.clear()
|
|||
|
|
self.status_combo.setCurrentIndex(0)
|
|||
|
|
self.update_button_states()
|
|||
|
|
|
|||
|
|
def edit_dut(self, dut_id):
|
|||
|
|
"""编辑DUT"""
|
|||
|
|
for dut in self.dut_data:
|
|||
|
|
if dut["id"] == dut_id:
|
|||
|
|
self.current_dut = dut
|
|||
|
|
self.populate_form()
|
|||
|
|
self.update_button_states()
|
|||
|
|
break
|
|||
|
|
|
|||
|
|
def save_dut(self):
|
|||
|
|
"""保存DUT"""
|
|||
|
|
if not self.current_dut:
|
|||
|
|
# 新建DUT
|
|||
|
|
new_id = max([d["id"] for d in self.dut_data], default=0) + 1
|
|||
|
|
self.current_dut = {
|
|||
|
|
"id": new_id,
|
|||
|
|
"name": self.name_input.text(),
|
|||
|
|
"description": self.desc_input.text(),
|
|||
|
|
"status": self.status_combo.currentText()
|
|||
|
|
}
|
|||
|
|
self.dut_data.append(self.current_dut)
|
|||
|
|
else:
|
|||
|
|
# 更新现有DUT
|
|||
|
|
self.current_dut["name"] = self.name_input.text()
|
|||
|
|
self.current_dut["description"] = self.desc_input.text()
|
|||
|
|
self.current_dut["status"] = self.status_combo.currentText()
|
|||
|
|
|
|||
|
|
# 重新加载数据
|
|||
|
|
self.load_data()
|
|||
|
|
QMessageBox.information(self, "保存成功", "DUT信息已保存")
|
|||
|
|
|
|||
|
|
def delete_dut(self):
|
|||
|
|
"""删除DUT"""
|
|||
|
|
if self.current_dut:
|
|||
|
|
reply = QMessageBox.question(self, "确认删除",
|
|||
|
|
f"确定要删除DUT '{self.current_dut['name']}' 吗?",
|
|||
|
|
QMessageBox.Yes | QMessageBox.No)
|
|||
|
|
if reply == QMessageBox.Yes:
|
|||
|
|
self.dut_data.remove(self.current_dut)
|
|||
|
|
self.current_dut = None
|
|||
|
|
self.load_data()
|
|||
|
|
self.new_dut() # 清空表单
|
|||
|
|
QMessageBox.information(self, "删除成功", "DUT已删除")
|