Files
desktop-transfer/ui/main_window_simple.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

475 lines
17 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,
QDialog, QProgressBar, QGridLayout
)
from PySide6.QtCore import Qt, QTimer, QSize, QPropertyAnimation, QEasingCurve
from PySide6.QtGui import QFont, QPalette, QColor
from utils.logger import logger
import os
class SettingsDialog(QDialog):
"""设置对话框,包含高级辅助功能"""
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("设置 - 高级辅助")
self.setMinimumSize(500, 600)
self.setModal(True)
# 从父窗口获取数据
self.parent_window = parent
self.terms = parent.terms if parent else []
# 初始化UI
self.init_ui()
# 加载现有数据
self.load_existing_data()
def init_ui(self):
"""初始化设置对话框UI"""
main_layout = QVBoxLayout(self)
main_layout.setSpacing(20)
main_layout.setContentsMargins(20, 20, 20, 20)
# 标题
title_label = QLabel("高级辅助设置")
title_font = QFont()
title_font.setPointSize(18)
title_font.setBold(True)
title_label.setFont(title_font)
title_label.setStyleSheet("color: #1a365d; margin-bottom: 10px;")
# 文本背景/场景介绍
context_group = QGroupBox("文本背景 / 场景介绍")
context_group.setStyleSheet("""
QGroupBox {
font-weight: bold;
color: #4a5568;
border: 1px solid #e2e8f0;
border-radius: 8px;
margin-top: 10px;
padding-top: 10px;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 10px;
padding: 0 5px 0 5px;
}
""")
context_layout = QVBoxLayout(context_group)
self.context_edit = QTextEdit()
self.context_edit.setPlaceholderText("例如:这是一份关于建筑工程的合同...")
self.context_edit.setFixedHeight(100)
self.context_edit.setStyleSheet(
"border: 1px solid #e2e8f0; border-radius: 6px; padding: 8px;"
)
context_layout.addWidget(self.context_edit)
# 术语定义
terms_group = QGroupBox("术语簿 (定义 A=B)")
terms_group.setStyleSheet("""
QGroupBox {
font-weight: bold;
color: #4a5568;
border: 1px solid #e2e8f0;
border-radius: 8px;
margin-top: 10px;
padding-top: 10px;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 10px;
padding: 0 5px 0 5px;
}
""")
terms_layout = QVBoxLayout(terms_group)
# 术语列表
self.terms_list = QListWidget()
self.terms_list.setStyleSheet(
"border: 1px solid #e2e8f0; border-radius: 6px; min-height: 150px;"
)
# 术语输入区域
terms_input_layout = QHBoxLayout()
self.term_input = QLineEdit()
self.term_input.setPlaceholderText("输入术语格式:原文 = 译文")
self.term_input.setStyleSheet(
"border: 1px solid #e2e8f0; border-radius: 6px; padding: 8px;"
)
add_term_btn = QPushButton("添加")
add_term_btn.setFixedSize(80, 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)
# 术语操作按钮
terms_buttons_layout = QHBoxLayout()
delete_term_btn = QPushButton("删除选中")
delete_term_btn.setFixedSize(100, 32)
delete_term_btn.setStyleSheet(
"background-color: #e53e3e; color: white; border: none; border-radius: 6px;"
)
delete_term_btn.clicked.connect(self.delete_selected_term)
clear_terms_btn = QPushButton("清空所有")
clear_terms_btn.setFixedSize(100, 32)
clear_terms_btn.setStyleSheet(
"background-color: #a0aec0; color: white; border: none; border-radius: 6px;"
)
clear_terms_btn.clicked.connect(self.clear_all_terms)
terms_buttons_layout.addWidget(delete_term_btn)
terms_buttons_layout.addWidget(clear_terms_btn)
terms_buttons_layout.addStretch()
terms_layout.addWidget(self.terms_list)
terms_layout.addLayout(terms_input_layout)
terms_layout.addLayout(terms_buttons_layout)
# 按钮区域
buttons_layout = QHBoxLayout()
save_btn = QPushButton("保存并应用")
save_btn.setFixedSize(120, 40)
save_btn.setStyleSheet(
"background-color: #38a169; color: white; border: none; border-radius: 6px; font-weight: bold;"
)
save_btn.clicked.connect(self.save_and_apply)
cancel_btn = QPushButton("取消")
cancel_btn.setFixedSize(120, 40)
cancel_btn.setStyleSheet(
"background-color: #e2e8f0; color: #2d3748; border: none; border-radius: 6px;"
)
cancel_btn.clicked.connect(self.reject)
buttons_layout.addStretch()
buttons_layout.addWidget(cancel_btn)
buttons_layout.addWidget(save_btn)
# 添加到主布局
main_layout.addWidget(title_label)
main_layout.addWidget(context_group)
main_layout.addWidget(terms_group)
main_layout.addStretch()
main_layout.addLayout(buttons_layout)
def load_existing_data(self):
"""加载现有数据"""
if self.parent_window:
# 加载上下文
if hasattr(self.parent_window, 'context_edit'):
self.context_edit.setText(self.parent_window.context_edit.toPlainText())
# 加载术语
for term in self.terms:
self.terms_list.addItem(term)
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, "警告", "术语格式不正确,请使用 '原文 = 译文' 格式")
def delete_selected_term(self):
"""删除选中的术语"""
selected_items = self.terms_list.selectedItems()
if not selected_items:
QMessageBox.warning(self, "警告", "请先选择要删除的术语")
return
for item in selected_items:
term_text = item.text()
if term_text in self.terms:
self.terms.remove(term_text)
self.terms_list.takeItem(self.terms_list.row(item))
def clear_all_terms(self):
"""清空所有术语"""
if self.terms_list.count() == 0:
return
reply = QMessageBox.question(
self, "确认", "确定要清空所有术语吗?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No
)
if reply == QMessageBox.Yes:
self.terms.clear()
self.terms_list.clear()
def save_and_apply(self):
"""保存并应用设置"""
# 保存到父窗口
if self.parent_window:
# 保存上下文
if hasattr(self.parent_window, 'context_edit'):
self.parent_window.context_edit.setText(self.context_edit.toPlainText())
# 保存术语
self.parent_window.terms = self.terms.copy()
# 更新术语列表显示(如果父窗口有显示的话)
if hasattr(self.parent_window, 'terms_list'):
self.parent_window.terms_list.clear()
for term in self.terms:
self.parent_window.terms_list.addItem(term)
QMessageBox.information(self, "成功", "设置已保存并应用")
self.accept()
class MainWindowSimple(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PrivaTrans - 简化测试版")
self.setMinimumSize(800, 600)
# 术语列表
self.terms = []
# 倒计时相关
self.countdown_timer = None
self.countdown_seconds = 60
self.countdown_label = None
# 初始化UI
self.init_ui()
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_bar = QWidget()
title_bar_layout = QHBoxLayout(title_bar)
title_bar_layout.setContentsMargins(0, 0, 0, 0)
title_bar_layout.setSpacing(0)
# 应用标题
title_label = QLabel("PrivaTrans")
title_font = QFont()
title_font.setPointSize(24)
title_font.setBold(True)
title_label.setFont(title_font)
title_label.setStyleSheet("color: #1a365d;")
# 右侧设置按钮
self.settings_btn = QPushButton("⚙ 设置")
self.settings_btn.setFixedSize(80, 36)
self.settings_btn.setStyleSheet("""
QPushButton {
background-color: #e2e8f0;
color: #2d3748;
border: none;
border-radius: 6px;
font-weight: bold;
}
QPushButton:hover {
background-color: #cbd5e0;
}
QPushButton:pressed {
background-color: #a0aec0;
}
""")
self.settings_btn.clicked.connect(self.open_settings)
title_bar_layout.addWidget(title_label)
title_bar_layout.addStretch()
title_bar_layout.addWidget(self.settings_btn)
# 模型信息(简化版)
model_layout = QHBoxLayout()
self.model_status_label = QLabel("● 测试模式 - 跳过模型加载")
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()
# 原文输入区域
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; min-height: 150px;"
)
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)
# 倒计时提示区域(初始隐藏)
self.countdown_container = QWidget()
self.countdown_container.setVisible(False)
countdown_layout = QHBoxLayout(self.countdown_container)
countdown_layout.setContentsMargins(0, 5, 0, 5)
countdown_icon = QLabel("")
countdown_icon.setStyleSheet("font-size: 16px; color: #d69e2e;")
self.countdown_label = QLabel("大模型正在翻译中预计剩余时间60秒")
self.countdown_label.setStyleSheet("color: #d69e2e; font-weight: bold;")
countdown_layout.addWidget(countdown_icon)
countdown_layout.addWidget(self.countdown_label)
countdown_layout.addStretch()
# 译文结果区域
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; min-height: 150px;"
)
output_layout.addLayout(output_header_layout)
output_layout.addWidget(self.result_text)
# 将所有组件添加到主布局
main_layout.addWidget(title_bar)
main_layout.addLayout(model_layout)
main_layout.addLayout(input_layout)
main_layout.addWidget(self.translate_btn)
main_layout.addWidget(self.countdown_container)
main_layout.addLayout(output_layout)
# 状态栏
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
self.status_label = QLabel("新界面设计测试 - 设置按钮在右上角,倒计时功能已实现")
self.status_bar.addWidget(self.status_label)
# 初始化上下文编辑框(用于存储设置)
self.context_edit = QTextEdit()
self.context_edit.setVisible(False)
def open_settings(self):
"""打开设置对话框"""
dialog = SettingsDialog(self)
dialog.exec()
def start_countdown(self):
"""开始60秒倒计时"""
self.countdown_seconds = 60
self.countdown_container.setVisible(True)
self.update_countdown_display()
# 创建倒计时定时器
self.countdown_timer = QTimer()
self.countdown_timer.setInterval(1000) # 每秒触发一次
self.countdown_timer.timeout.connect(self.update_countdown)
self.countdown_timer.start()
def update_countdown(self):
"""更新倒计时"""
self.countdown_seconds -= 1
self.update_countdown_display()
if self.countdown_seconds <= 0:
self.stop_countdown()
def update_countdown_display(self):
"""更新倒计时显示"""
if self.countdown_label:
self.countdown_label.setText(f"大模型正在翻译中,预计剩余时间:{self.countdown_seconds}")
def stop_countdown(self):
"""停止倒计时"""
if self.countdown_timer:
self.countdown_timer.stop()
self.countdown_timer = None
self.countdown_container.setVisible(False)
def start_translation_test(self):
"""开始翻译测试"""
source_text = self.source_text.toPlainText().strip()
if not source_text:
QMessageBox.warning(self, "警告", "原文内容为空")
return
# 禁用翻译按钮
self.translate_btn.setEnabled(False)
self.translate_btn.setText("翻译中...")
# 开始倒计时
self.start_countdown()
# 获取上下文和术语
context = self.context_edit.toPlainText().strip()
terms = self.terms if self.terms else None
# 模拟翻译过程5秒后完成
from PySide6.QtCore import QTimer
self.simulation_timer = QTimer()
self.simulation_timer.setSingleShot(True)
self.simulation_timer.setInterval(5000) # 5秒后完成
self.simulation_timer.timeout.connect(lambda: self.on_translation_finished(f"【测试翻译结果】\n\n原文:{source_text}\n\n译文:这是模拟的翻译结果\n\n上下文:{context if context else ''}\n术语:{', '.join(terms) if terms else ''}"))
self.simulation_timer.start()
def on_translation_finished(self, result):
"""翻译完成回调"""
# 停止倒计时
self.stop_countdown()
# 启用翻译按钮
self.translate_btn.setEnabled(True)
self.translate_btn.setText("开始翻译 (测试倒计时)")
if result:
self.result_text.setText(result)
QMessageBox.information(self, "测试完成", "翻译测试完成!\n\n✓ 设置按钮功能正常\n✓ 倒计时功能正常\n✓ 界面布局优化完成")
# 修复导入问题
from PySide6.QtWidgets import QApplication