185 lines
6.4 KiB
Python
185 lines
6.4 KiB
Python
|
|
"""
|
||
|
|
代码生成控件
|
||
|
|
集成在主窗口的标签页中
|
||
|
|
"""
|
||
|
|
from PyQt5.QtWidgets import (
|
||
|
|
QWidget, QVBoxLayout, QHBoxLayout, QPushButton,
|
||
|
|
QGroupBox, QFormLayout, QComboBox, QLineEdit,
|
||
|
|
QLabel, QMessageBox, QFileDialog, QTextEdit
|
||
|
|
)
|
||
|
|
from PyQt5.QtCore import Qt
|
||
|
|
|
||
|
|
from controllers.codegen_controller import CodeGenController
|
||
|
|
from controllers.message_controller import MessageController
|
||
|
|
from config import ProtocolType, SerializationType, LanguageStandard
|
||
|
|
from utils.logger import get_logger
|
||
|
|
|
||
|
|
logger = get_logger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
class CodeGenWidget(QWidget):
|
||
|
|
"""代码生成控件"""
|
||
|
|
|
||
|
|
def __init__(self, codegen_controller: CodeGenController,
|
||
|
|
message_controller: MessageController):
|
||
|
|
super().__init__()
|
||
|
|
self.codegen_controller = codegen_controller
|
||
|
|
self.message_controller = message_controller
|
||
|
|
|
||
|
|
self.source_message = None
|
||
|
|
self.target_message = None
|
||
|
|
|
||
|
|
self.init_ui()
|
||
|
|
|
||
|
|
def init_ui(self):
|
||
|
|
"""初始化UI"""
|
||
|
|
main_layout = QVBoxLayout(self)
|
||
|
|
|
||
|
|
# 配置区域
|
||
|
|
config_group = QGroupBox("生成配置")
|
||
|
|
config_layout = QFormLayout(config_group)
|
||
|
|
|
||
|
|
# 源消息选择
|
||
|
|
source_layout = QHBoxLayout()
|
||
|
|
self.source_message_combo = QComboBox()
|
||
|
|
self.load_messages_to_combo(self.source_message_combo)
|
||
|
|
self.source_message_combo.currentIndexChanged.connect(self.on_source_message_changed)
|
||
|
|
source_layout.addWidget(self.source_message_combo)
|
||
|
|
config_layout.addRow("源消息*:", source_layout)
|
||
|
|
|
||
|
|
# 目标消息选择
|
||
|
|
target_layout = QHBoxLayout()
|
||
|
|
self.target_message_combo = QComboBox()
|
||
|
|
self.load_messages_to_combo(self.target_message_combo)
|
||
|
|
self.target_message_combo.currentIndexChanged.connect(self.on_target_message_changed)
|
||
|
|
target_layout.addWidget(self.target_message_combo)
|
||
|
|
config_layout.addRow("目标消息*:", target_layout)
|
||
|
|
|
||
|
|
# 协议选择
|
||
|
|
self.protocol_combo = QComboBox()
|
||
|
|
for protocol in ProtocolType:
|
||
|
|
self.protocol_combo.addItem(protocol.value, protocol)
|
||
|
|
config_layout.addRow("传输协议*:", self.protocol_combo)
|
||
|
|
|
||
|
|
# 序列化格式
|
||
|
|
self.serialization_combo = QComboBox()
|
||
|
|
for serialization in SerializationType:
|
||
|
|
self.serialization_combo.addItem(serialization.value, serialization)
|
||
|
|
config_layout.addRow("序列化格式*:", self.serialization_combo)
|
||
|
|
|
||
|
|
# 语言标准
|
||
|
|
self.language_standard_combo = QComboBox()
|
||
|
|
for standard in LanguageStandard:
|
||
|
|
self.language_standard_combo.addItem(standard.value, standard)
|
||
|
|
config_layout.addRow("语言标准:", self.language_standard_combo)
|
||
|
|
|
||
|
|
# 输出目录
|
||
|
|
output_layout = QHBoxLayout()
|
||
|
|
self.output_dir_edit = QLineEdit()
|
||
|
|
self.browse_btn = QPushButton("浏览...")
|
||
|
|
self.browse_btn.clicked.connect(self.on_browse_output_dir)
|
||
|
|
output_layout.addWidget(self.output_dir_edit)
|
||
|
|
output_layout.addWidget(self.browse_btn)
|
||
|
|
config_layout.addRow("输出目录:", output_layout)
|
||
|
|
|
||
|
|
main_layout.addWidget(config_group)
|
||
|
|
|
||
|
|
# 操作按钮
|
||
|
|
button_layout = QHBoxLayout()
|
||
|
|
|
||
|
|
self.generate_btn = QPushButton("生成代码")
|
||
|
|
self.generate_btn.clicked.connect(self.on_generate_code)
|
||
|
|
|
||
|
|
self.preview_btn = QPushButton("预览代码")
|
||
|
|
self.preview_btn.clicked.connect(self.on_preview_code)
|
||
|
|
|
||
|
|
button_layout.addWidget(self.generate_btn)
|
||
|
|
button_layout.addWidget(self.preview_btn)
|
||
|
|
button_layout.addStretch()
|
||
|
|
|
||
|
|
main_layout.addLayout(button_layout)
|
||
|
|
|
||
|
|
# 日志区域
|
||
|
|
log_group = QGroupBox("生成日志")
|
||
|
|
log_layout = QVBoxLayout(log_group)
|
||
|
|
|
||
|
|
self.log_text = QTextEdit()
|
||
|
|
self.log_text.setReadOnly(True)
|
||
|
|
self.log_text.setMaximumHeight(200)
|
||
|
|
log_layout.addWidget(self.log_text)
|
||
|
|
|
||
|
|
main_layout.addWidget(log_group)
|
||
|
|
main_layout.addStretch()
|
||
|
|
|
||
|
|
def load_messages_to_combo(self, combo: QComboBox):
|
||
|
|
"""加载消息到下拉框"""
|
||
|
|
messages, _ = self.message_controller.get_all_messages(page=1, page_size=1000)
|
||
|
|
|
||
|
|
combo.clear()
|
||
|
|
combo.addItem("请选择...", None)
|
||
|
|
for message in messages:
|
||
|
|
combo.addItem(message.full_name, message)
|
||
|
|
|
||
|
|
def on_source_message_changed(self, index):
|
||
|
|
"""源消息改变"""
|
||
|
|
self.source_message = self.source_message_combo.currentData()
|
||
|
|
|
||
|
|
def on_target_message_changed(self, index):
|
||
|
|
"""目标消息改变"""
|
||
|
|
self.target_message = self.target_message_combo.currentData()
|
||
|
|
|
||
|
|
def on_browse_output_dir(self):
|
||
|
|
"""浏览输出目录"""
|
||
|
|
dir_path = QFileDialog.getExistingDirectory(
|
||
|
|
self,
|
||
|
|
"选择输出目录",
|
||
|
|
"",
|
||
|
|
QFileDialog.ShowDirsOnly
|
||
|
|
)
|
||
|
|
|
||
|
|
if dir_path:
|
||
|
|
self.output_dir_edit.setText(dir_path)
|
||
|
|
|
||
|
|
def on_generate_code(self):
|
||
|
|
"""生成代码"""
|
||
|
|
# 验证
|
||
|
|
if not self.source_message or not self.target_message:
|
||
|
|
QMessageBox.warning(self, "警告", "请选择源消息和目标消息")
|
||
|
|
return
|
||
|
|
|
||
|
|
if not self.output_dir_edit.text().strip():
|
||
|
|
QMessageBox.warning(self, "警告", "请选择输出目录")
|
||
|
|
return
|
||
|
|
|
||
|
|
# 构建配置
|
||
|
|
config = {
|
||
|
|
'source_message': self.source_message,
|
||
|
|
'target_message': self.target_message,
|
||
|
|
'protocol': self.protocol_combo.currentData().value,
|
||
|
|
'serialization': self.serialization_combo.currentData().value,
|
||
|
|
'language_standard': self.language_standard_combo.currentData().value,
|
||
|
|
'output_dir': self.output_dir_edit.text().strip()
|
||
|
|
}
|
||
|
|
|
||
|
|
# 生成代码
|
||
|
|
self.log_text.append("开始生成代码...")
|
||
|
|
success, msg, files = self.codegen_controller.generate_code(config)
|
||
|
|
|
||
|
|
if success:
|
||
|
|
self.log_text.append(f"✓ {msg}")
|
||
|
|
self.log_text.append(f"生成文件列表:")
|
||
|
|
for filename in files.keys():
|
||
|
|
self.log_text.append(f" - {filename}")
|
||
|
|
|
||
|
|
QMessageBox.information(self, "成功", msg)
|
||
|
|
else:
|
||
|
|
self.log_text.append(f"✗ {msg}")
|
||
|
|
QMessageBox.warning(self, "失败", msg)
|
||
|
|
|
||
|
|
def on_preview_code(self):
|
||
|
|
"""预览代码"""
|
||
|
|
from ui.dialogs.codegen_dialog import CodeGenDialog
|
||
|
|
|
||
|
|
dialog = CodeGenDialog(self, self.source_message, self.target_message)
|
||
|
|
dialog.exec_()
|