import json from datetime import datetime import pandas as pd from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLineEdit, QLabel, QHBoxLayout, QPushButton, QFileDialog, \ QPlainTextEdit, QGridLayout from core.context import AppContext from widget.task_run import TaskRunDialog class TaskEditWidget(QWidget): def __init__(self, parent=None): super(TaskEditWidget, self).__init__(parent) self.ratio = AppContext.get_ratio() self.run_dialog = None # 导入数据按钮 file_button = QPushButton('导入任务数据') file_button.setObjectName('fileButton') file_button.setFixedHeight(self.ratio * 42) file_button.setStyleSheet("background-color: #3A36DB;") file_button.clicked.connect(self.import_excel) # 任务名称 item_layout = QHBoxLayout() item_layout.setContentsMargins(0, 0, 0, 0) name_label = QLabel('任务名称') self.name_text = QLineEdit() self.name_text.setObjectName('name_text') self.name_text.setReadOnly(True) self.name_text.textChanged.connect(self.text_changed) item_layout.addWidget(name_label) item_layout.addSpacing(10) item_layout.addWidget(self.name_text) name_item = QWidget() name_item.setLayout(item_layout) # 数据内容 json_widget = QWidget() json_widget.setObjectName('jsonWidget') json_layout = QGridLayout() json_layout.setContentsMargins(10, 10, 1, 10) self.file_content = QPlainTextEdit() self.file_content.setReadOnly(True) self.file_content.textChanged.connect(self.text_changed) json_layout.addWidget(self.file_content) json_widget.setLayout(json_layout) # 任务开始按钮 self.start_task_button = QPushButton('开始任务') self.start_task_button.setObjectName('startTaskButton') self.start_task_button.setEnabled(False) self.start_task_button.setFixedWidth(self.ratio * 200) self.start_task_button.setFixedHeight(self.ratio * 42) self.start_task_button.clicked.connect(self.start_task) layout = QVBoxLayout() layout.setSpacing(20) layout.setContentsMargins(100,30,100,30) layout.addWidget(file_button) layout.addWidget(name_item) layout.addWidget(json_widget) layout.addWidget(self.start_task_button, alignment=Qt.AlignRight) self.setLayout(layout) def import_excel(self): options = QFileDialog.Options() file_path, _ = QFileDialog.getOpenFileName(self, "选择 Excel 文件", "", "Excel Files (*.xlsx *.xls)", options=options) if file_path: try: # Read Excel file into a pandas DataFrame df = pd.read_excel(file_path) # Convert DataFrame to JSON json_data = json.loads(df.to_json(orient='records')) for data in json_data: if data["name"] is not None and data["name"] != '' : self.name_text.setText(data["name"]) del data["name"] json_data_str = json.dumps(json_data, indent=4, ensure_ascii=False) self.file_content.setPlainText(json_data_str) except Exception as e: self.file_content.setPlainText(f'Error: {e}') def start_task(self): task_table = AppContext.get_edge_context().get_component("dat_task") task_table.insert_task({ "name": self.name_text.text(), "param_json": self.file_content.toPlainText(), "start_time": datetime.now() }) self.run_dialog = TaskRunDialog(json.loads(self.file_content.toPlainText())) dialog_result = self.run_dialog.exec_() if dialog_result == 0: self.name_text.setText(None) self.file_content.setPlainText(None) def text_changed(self): self.start_task_button.setEnabled(self.file_content.toPlainText() !='' and self.name_text.text() != '')