Files
desktop-transfer/ui/main_window_test.py
xiaji 79075ee2ba feat(ui): 新增多个UI测试窗口并优化主窗口功能
- 添加main_window_test.py用于UI功能测试
- 添加main_window_simple.py简化版界面
- 添加main_window_new.py和main_window_final.py完整功能界面
- 优化主窗口高级面板切换逻辑
- 更新.gitignore忽略测试文件
2026-01-16 12:32:45 +08:00

264 lines
10 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from PySide6.QtWidgets import (
QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QTextEdit, QPushButton,
QLineEdit, QLabel, QGroupBox, QScrollArea, QToolButton, QStatusBar,
QFileDialog, QMessageBox, QListWidget, QListWidgetItem, QFrame, QSplitter
)
from PySide6.QtCore import Qt, QTimer, QSize
from PySide6.QtGui import QFont, QPalette, QColor
from utils.logger import logger
import os
class MainWindowTest(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PrivaTrans - UI测试")
self.setMinimumSize(800, 600)
# 初始化组件
self.terms = []
# 初始化UI
self.init_ui()
# 初始化状态栏定时器
self.init_status_bar_timer()
def init_ui(self):
"""初始化UI界面"""
# 主布局
central_widget = QWidget()
self.setCentralWidget(central_widget)
main_layout = QVBoxLayout(central_widget)
main_layout.setSpacing(20)
main_layout.setContentsMargins(20, 20, 20, 10)
# 顶部标题
title_label = QLabel("PrivaTrans - UI测试")
title_font = QFont()
title_font.setPointSize(24)
title_font.setBold(True)
title_label.setFont(title_font)
title_label.setStyleSheet("color: #1a365d;")
# 模型信息标签
model_layout = QHBoxLayout()
self.model_status_label = QLabel("● UI测试模式 - 跳过模型加载")
self.model_status_label.setStyleSheet("color: #38a169;")
self.model_info_label = QLabel("点击折叠/展开按钮测试功能")
self.model_info_label.setStyleSheet("color: #2d3748; font-weight: bold;")
model_layout.addWidget(self.model_status_label)
model_layout.addWidget(self.model_info_label)
model_layout.addStretch()
# 高级辅助面板(可折叠)
advanced_panel = QWidget()
advanced_layout = QVBoxLayout(advanced_panel)
advanced_layout.setSpacing(10)
advanced_layout.setContentsMargins(0, 0, 0, 0)
# 高级辅助标题和折叠按钮
advanced_header = QWidget()
header_layout = QHBoxLayout(advanced_header)
header_layout.setContentsMargins(0, 0, 0, 0)
header_layout.setSpacing(10)
advanced_title = QLabel("高级辅助 (背景与术语)")
advanced_title.setStyleSheet("color: #4a5568; font-weight: bold;")
self.advanced_toggle = QToolButton()
self.advanced_toggle.setText("")
self.advanced_toggle.setCheckable(True)
self.advanced_toggle.setChecked(False)
self.advanced_toggle.setFixedSize(20, 20)
self.advanced_toggle.setStyleSheet(
"border: none; background: none; color: #4a5568;"
)
self.advanced_toggle.clicked.connect(self.toggle_advanced_panel)
header_layout.addWidget(advanced_title)
header_layout.addStretch()
header_layout.addWidget(self.advanced_toggle)
advanced_layout.addWidget(advanced_header)
# 高级辅助内容区域
self.advanced_content = QWidget()
content_layout = QVBoxLayout(self.advanced_content)
content_layout.setSpacing(15)
content_layout.setContentsMargins(0, 0, 0, 0)
# 文本背景/场景介绍
context_label = QLabel("文本背景 / 场景介绍")
context_label.setStyleSheet("color: #4a5568;")
self.context_edit = QTextEdit()
self.context_edit.setPlaceholderText("例如:这是一份关于建筑工程的合同...")
self.context_edit.setFixedHeight(80)
self.context_edit.setStyleSheet(
"border: 1px solid #e2e8f0; border-radius: 6px; padding: 8px;"
)
# 术语定义
terms_label = QLabel("术语簿 (定义 A=B)")
terms_label.setStyleSheet("color: #4a5568;")
self.terms_list = QListWidget()
self.terms_list.setStyleSheet(
"border: 1px solid #e2e8f0; border-radius: 6px;"
)
terms_input_layout = QHBoxLayout()
self.term_input = QLineEdit()
self.term_input.setPlaceholderText("你好 = what's up")
self.term_input.setStyleSheet(
"border: 1px solid #e2e8f0; border-radius: 6px; padding: 8px;"
)
add_term_btn = QPushButton("+")
add_term_btn.setFixedSize(32, 32)
add_term_btn.setStyleSheet(
"background-color: #3182ce; color: white; border: none; border-radius: 6px;"
)
add_term_btn.clicked.connect(self.add_term)
terms_input_layout.addWidget(self.term_input)
terms_input_layout.addWidget(add_term_btn)
# 术语列表项右键菜单
self.terms_list.setContextMenuPolicy(Qt.CustomContextMenu)
self.terms_list.customContextMenuRequested.connect(self.show_term_context_menu)
content_layout.addWidget(context_label)
content_layout.addWidget(self.context_edit)
content_layout.addWidget(terms_label)
content_layout.addWidget(self.terms_list)
content_layout.addLayout(terms_input_layout)
advanced_layout.addWidget(self.advanced_content)
# 默认隐藏高级辅助内容
self.advanced_content.setVisible(False)
# 将高级辅助面板添加到主布局
advanced_panel.setStyleSheet(
"border: 1px solid #e2e8f0; border-radius: 8px; padding: 15px; margin-top: 10px;"
)
# 原文输入区域
input_layout = QVBoxLayout()
input_header_layout = QHBoxLayout()
input_label = QLabel("原文内容")
input_label.setStyleSheet("color: #4a5568; font-weight: bold;")
input_header_layout.addWidget(input_label)
input_header_layout.addStretch()
self.source_text = QTextEdit()
self.source_text.setPlaceholderText("在此输入要翻译的文本...")
self.source_text.setStyleSheet(
"border: 1px solid #e2e8f0; border-radius: 6px; padding: 10px;"
)
input_layout.addLayout(input_header_layout)
input_layout.addWidget(self.source_text)
# 翻译按钮
self.translate_btn = QPushButton("开始翻译 (测试模式)")
self.translate_btn.setFixedHeight(50)
self.translate_btn.setStyleSheet(
"background-color: #3182ce; color: white; border: none; border-radius: 8px; font-size: 16px; font-weight: bold;"
)
self.translate_btn.clicked.connect(self.start_translation_test)
# 译文结果区域
output_layout = QVBoxLayout()
output_header_layout = QHBoxLayout()
output_label = QLabel("译文结果")
output_label.setStyleSheet("color: #4a5568; font-weight: bold;")
output_header_layout.addWidget(output_label)
output_header_layout.addStretch()
self.result_text = QTextEdit()
self.result_text.setReadOnly(True)
self.result_text.setPlaceholderText("翻译结果将显示在这里...")
self.result_text.setStyleSheet(
"border: 1px solid #e2e8f0; border-radius: 6px; padding: 10px;"
)
output_layout.addLayout(output_header_layout)
output_layout.addWidget(self.result_text)
# 将所有组件添加到主布局
main_layout.addWidget(title_label)
main_layout.addLayout(model_layout)
main_layout.addWidget(advanced_panel)
main_layout.addLayout(input_layout)
main_layout.addWidget(self.translate_btn)
main_layout.addLayout(output_layout)
# 状态栏
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
self.status_label = QLabel("UI测试模式 - 折叠/展开按钮功能正常")
self.status_bar.addWidget(self.status_label)
def init_status_bar_timer(self):
"""初始化状态栏定时器"""
self.status_timer = QTimer()
self.status_timer.setInterval(1000) # 每秒更新一次
self.status_timer.timeout.connect(self.update_status_bar)
self.status_timer.start()
def update_status_bar(self):
"""更新状态栏显示"""
# 在测试模式下显示固定状态
pass
def add_term(self):
"""添加术语定义"""
term_text = self.term_input.text().strip()
if term_text:
if "=" in term_text:
self.terms.append(term_text)
self.terms_list.addItem(term_text)
self.term_input.clear()
else:
QMessageBox.warning(self, "警告", "术语格式不正确,请使用 'A = B' 格式")
def toggle_advanced_panel(self, checked):
"""切换高级辅助面板的显示/隐藏状态"""
self.advanced_content.setVisible(checked)
self.advanced_toggle.setText("" if checked else "")
# 更新状态栏显示当前状态
state = "展开" if checked else "折叠"
self.status_label.setText(f"高级辅助面板已{state} - 按钮功能正常")
def show_term_context_menu(self, pos):
"""显示术语项右键菜单"""
from PySide6.QtWidgets import QMenu
menu = QMenu()
delete_action = menu.addAction("删除")
action = menu.exec_(self.terms_list.mapToGlobal(pos))
if action == delete_action:
selected_items = self.terms_list.selectedItems()
for item in selected_items:
self.terms.remove(item.text())
self.terms_list.takeItem(self.terms_list.row(item))
def start_translation_test(self):
"""开始翻译测试"""
source_text = self.source_text.toPlainText().strip()
if not source_text:
QMessageBox.warning(self, "警告", "原文内容为空")
return
# 在测试模式下显示模拟结果
self.result_text.setText(f"【测试模式】翻译结果预览:\n\n{source_text}\n\n(这是测试模式,实际翻译需要加载模型)")
QMessageBox.information(self, "测试成功", "UI功能测试完成\n\n✓ 折叠/展开按钮功能正常\n✓ 术语添加功能正常\n✓ 界面交互正常")