352 lines
8.8 KiB
Python
352 lines
8.8 KiB
Python
"""
|
|
代码查看对话框
|
|
"""
|
|
from PyQt5.QtWidgets import (
|
|
QDialog, QVBoxLayout, QHBoxLayout, QLabel,
|
|
QTextEdit, QPushButton, QMessageBox, QFileDialog, QApplication
|
|
)
|
|
from PyQt5.QtCore import Qt
|
|
|
|
|
|
class CodeViewDialog(QDialog):
|
|
"""代码查看对话框"""
|
|
|
|
def __init__(self, file_name: str, parent=None):
|
|
"""
|
|
初始化对话框
|
|
|
|
Args:
|
|
file_name: 文件名
|
|
parent: 父窗口
|
|
"""
|
|
super().__init__(parent)
|
|
self.file_name = file_name
|
|
self._setup_ui()
|
|
|
|
def _setup_ui(self):
|
|
"""设置UI"""
|
|
self.setWindowTitle(f"代码查看 - {self.file_name}")
|
|
self.resize(900, 600)
|
|
|
|
layout = QVBoxLayout(self)
|
|
layout.setSpacing(10)
|
|
|
|
# 工具栏
|
|
toolbar = QHBoxLayout()
|
|
|
|
file_label = QLabel(f"📄 {self.file_name}")
|
|
file_label.setStyleSheet("""
|
|
font-weight: bold;
|
|
color: #60a5fa;
|
|
font-size: 14px;
|
|
""")
|
|
toolbar.addWidget(file_label)
|
|
|
|
toolbar.addStretch()
|
|
|
|
copy_btn = QPushButton("📋 复制代码")
|
|
copy_btn.clicked.connect(self._copy_code)
|
|
toolbar.addWidget(copy_btn)
|
|
|
|
download_btn = QPushButton("⬇ 下载")
|
|
download_btn.clicked.connect(self._download_code)
|
|
toolbar.addWidget(download_btn)
|
|
|
|
layout.addLayout(toolbar)
|
|
|
|
# 代码编辑器
|
|
self.code_editor = QTextEdit()
|
|
self.code_editor.setReadOnly(True)
|
|
self.code_editor.setStyleSheet("""
|
|
QTextEdit {
|
|
background: #1e293b;
|
|
color: #e0e6ed;
|
|
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
|
|
font-size: 13px;
|
|
border: 1px solid rgba(59, 130, 246, 0.2);
|
|
border-radius: 8px;
|
|
padding: 15px;
|
|
line-height: 1.5;
|
|
}
|
|
""")
|
|
|
|
# 生成示例代码
|
|
sample_code = self._generate_sample_code()
|
|
self.code_editor.setPlainText(sample_code)
|
|
|
|
layout.addWidget(self.code_editor)
|
|
|
|
# 关闭按钮
|
|
close_btn = QPushButton("关闭")
|
|
close_btn.clicked.connect(self.accept)
|
|
layout.addWidget(close_btn)
|
|
|
|
def _generate_sample_code(self) -> str:
|
|
"""生成示例代码"""
|
|
if self.file_name.endswith('.java'):
|
|
return self._get_java_sample()
|
|
elif self.file_name.endswith('.py'):
|
|
return self._get_python_sample()
|
|
elif self.file_name.endswith('.xml'):
|
|
return self._get_xml_sample()
|
|
else:
|
|
return f"# {self.file_name}\n\n这是一个示例文件的内容。"
|
|
|
|
def _get_java_sample(self) -> str:
|
|
"""获取Java示例代码"""
|
|
return '''package com.example.project;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
/**
|
|
* 示例控制器
|
|
*
|
|
* @author AI Generator
|
|
* @version 1.0
|
|
* @date 2024-01-15
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/api/v1")
|
|
public class ExampleController {
|
|
|
|
@Autowired
|
|
private ExampleService exampleService;
|
|
|
|
/**
|
|
* 获取数据
|
|
*
|
|
* @param id 数据ID
|
|
* @return 响应结果
|
|
*/
|
|
@GetMapping("/data/{id}")
|
|
public ResponseEntity<DataResponse> getData(@PathVariable Long id) {
|
|
try {
|
|
DataResponse data = exampleService.getDataById(id);
|
|
return ResponseEntity.ok(data);
|
|
} catch (Exception e) {
|
|
return ResponseEntity.internalServerError().build();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建数据
|
|
*
|
|
* @param request 请求参数
|
|
* @return 响应结果
|
|
*/
|
|
@PostMapping("/data")
|
|
public ResponseEntity<DataResponse> createData(@RequestBody DataRequest request) {
|
|
DataResponse data = exampleService.createData(request);
|
|
return ResponseEntity.ok(data);
|
|
}
|
|
|
|
/**
|
|
* 更新数据
|
|
*
|
|
* @param id 数据ID
|
|
* @param request 请求参数
|
|
* @return 响应结果
|
|
*/
|
|
@PutMapping("/data/{id}")
|
|
public ResponseEntity<DataResponse> updateData(
|
|
@PathVariable Long id,
|
|
@RequestBody DataRequest request) {
|
|
DataResponse data = exampleService.updateData(id, request);
|
|
return ResponseEntity.ok(data);
|
|
}
|
|
|
|
/**
|
|
* 删除数据
|
|
*
|
|
* @param id 数据ID
|
|
* @return 响应结果
|
|
*/
|
|
@DeleteMapping("/data/{id}")
|
|
public ResponseEntity<Void> deleteData(@PathVariable Long id) {
|
|
exampleService.deleteData(id);
|
|
return ResponseEntity.ok().build();
|
|
}
|
|
}'''
|
|
|
|
def _get_python_sample(self) -> str:
|
|
"""获取Python示例代码"""
|
|
return '''#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
示例模块
|
|
|
|
Author: AI Generator
|
|
Version: 1.0
|
|
Date: 2024-01-15
|
|
"""
|
|
|
|
from flask import Flask, jsonify, request
|
|
from typing import Dict, Any
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
@app.route('/api/v1/data/<int:data_id>', methods=['GET'])
|
|
def get_data(data_id: int) -> Dict[str, Any]:
|
|
"""
|
|
获取数据
|
|
|
|
Args:
|
|
data_id: 数据ID
|
|
|
|
Returns:
|
|
响应数据
|
|
"""
|
|
try:
|
|
# 实现逻辑
|
|
return jsonify({
|
|
'id': data_id,
|
|
'status': 'success',
|
|
'data': {}
|
|
})
|
|
except Exception as e:
|
|
return jsonify({
|
|
'status': 'error',
|
|
'message': str(e)
|
|
}), 500
|
|
|
|
|
|
@app.route('/api/v1/data', methods=['POST'])
|
|
def create_data() -> Dict[str, Any]:
|
|
"""
|
|
创建数据
|
|
|
|
Returns:
|
|
响应数据
|
|
"""
|
|
data = request.get_json()
|
|
|
|
# 实现逻辑
|
|
return jsonify({
|
|
'status': 'success',
|
|
'data': data
|
|
})
|
|
|
|
|
|
@app.route('/api/v1/data/<int:data_id>', methods=['PUT'])
|
|
def update_data(data_id: int) -> Dict[str, Any]:
|
|
"""
|
|
更新数据
|
|
|
|
Args:
|
|
data_id: 数据ID
|
|
|
|
Returns:
|
|
响应数据
|
|
"""
|
|
data = request.get_json()
|
|
|
|
# 实现逻辑
|
|
return jsonify({
|
|
'id': data_id,
|
|
'status': 'success',
|
|
'data': data
|
|
})
|
|
|
|
|
|
@app.route('/api/v1/data/<int:data_id>', methods=['DELETE'])
|
|
def delete_data(data_id: int) -> Dict[str, Any]:
|
|
"""
|
|
删除数据
|
|
|
|
Args:
|
|
data_id: 数据ID
|
|
|
|
Returns:
|
|
响应数据
|
|
"""
|
|
# 实现逻辑
|
|
return jsonify({
|
|
'status': 'success',
|
|
'message': 'Data deleted'
|
|
})
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True, host='0.0.0.0', port=5000)'''
|
|
|
|
def _get_xml_sample(self) -> str:
|
|
"""获取XML示例代码"""
|
|
return '''<?xml version="1.0" encoding="UTF-8"?>
|
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
|
http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
<modelVersion>4.0.0</modelVersion>
|
|
|
|
<groupId>com.example</groupId>
|
|
<artifactId>lowcode-project</artifactId>
|
|
<version>1.0.0</version>
|
|
<packaging>jar</packaging>
|
|
|
|
<name>LowCode Project</name>
|
|
<description>AI Generated Project</description>
|
|
|
|
<parent>
|
|
<groupId>org.springframework.boot</groupId>
|
|
<artifactId>spring-boot-starter-parent</artifactId>
|
|
<version>2.7.0</version>
|
|
</parent>
|
|
|
|
<properties>
|
|
<java.version>17</java.version>
|
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
</properties>
|
|
|
|
<dependencies>
|
|
<dependency>
|
|
<groupId>org.springframework.boot</groupId>
|
|
<artifactId>spring-boot-starter-web</artifactId>
|
|
</dependency>
|
|
|
|
<dependency>
|
|
<groupId>org.springframework.boot</groupId>
|
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
|
</dependency>
|
|
|
|
<dependency>
|
|
<groupId>mysql</groupId>
|
|
<artifactId>mysql-connector-java</artifactId>
|
|
</dependency>
|
|
</dependencies>
|
|
|
|
<build>
|
|
<plugins>
|
|
<plugin>
|
|
<groupId>org.springframework.boot</groupId>
|
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
|
</plugin>
|
|
</plugins>
|
|
</build>
|
|
</project>'''
|
|
|
|
def _copy_code(self):
|
|
"""复制代码"""
|
|
clipboard = QApplication.clipboard()
|
|
clipboard.setText(self.code_editor.toPlainText())
|
|
QMessageBox.information(self, "复制", "代码已复制到剪贴板")
|
|
|
|
def _download_code(self):
|
|
"""下载代码"""
|
|
file_path, _ = QFileDialog.getSaveFileName(
|
|
self,
|
|
"保存代码",
|
|
self.file_name
|
|
)
|
|
|
|
if file_path:
|
|
try:
|
|
with open(file_path, 'w', encoding='utf-8') as f:
|
|
f.write(self.code_editor.toPlainText())
|
|
QMessageBox.information(self, "下载", f"代码已保存到:\n{file_path}")
|
|
except Exception as e:
|
|
QMessageBox.critical(self, "错误", f"保存失败:\n{str(e)}")
|