|
|
|
|
@ -1,8 +1,10 @@
|
|
|
|
|
import sys
|
|
|
|
|
from PyQt5.QtWidgets import (QApplication, QMainWindow, QTextEdit, QAction,
|
|
|
|
|
QFileDialog, QVBoxLayout, QWidget, QLabel, QStatusBar)
|
|
|
|
|
from PyQt5.QtGui import QFont, QTextCharFormat, QColor
|
|
|
|
|
QFileDialog, QVBoxLayout, QWidget, QLabel, QStatusBar, QMessageBox)
|
|
|
|
|
from PyQt5.QtGui import QFont, QTextCharFormat, QColor, QTextCursor
|
|
|
|
|
from PyQt5.QtCore import Qt
|
|
|
|
|
from src.file_parser import FileParser
|
|
|
|
|
from src.typing_logic import TypingLogic
|
|
|
|
|
|
|
|
|
|
class MainWindow(QMainWindow):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
@ -14,8 +16,13 @@ class MainWindow(QMainWindow):
|
|
|
|
|
- 初始化当前输入位置
|
|
|
|
|
- 调用initUI()方法
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现构造函数逻辑
|
|
|
|
|
pass
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.learning_content = ""
|
|
|
|
|
self.current_position = 0
|
|
|
|
|
self.typing_logic = None
|
|
|
|
|
self.text_edit = None
|
|
|
|
|
self.status_bar = None
|
|
|
|
|
self.initUI()
|
|
|
|
|
|
|
|
|
|
def initUI(self):
|
|
|
|
|
"""
|
|
|
|
|
@ -25,8 +32,24 @@ class MainWindow(QMainWindow):
|
|
|
|
|
- 创建状态栏并显示"就绪"
|
|
|
|
|
- 连接文本变化信号到onTextChanged
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现UI初始化逻辑
|
|
|
|
|
pass
|
|
|
|
|
# 设置窗口属性
|
|
|
|
|
self.setWindowTitle("隐私学习软件 - 仿Word")
|
|
|
|
|
self.setGeometry(100, 100, 800, 600)
|
|
|
|
|
|
|
|
|
|
# 创建中央文本编辑区域
|
|
|
|
|
self.text_edit = QTextEdit()
|
|
|
|
|
self.text_edit.setFont(QFont("Arial", 12))
|
|
|
|
|
self.setCentralWidget(self.text_edit)
|
|
|
|
|
|
|
|
|
|
# 创建菜单栏
|
|
|
|
|
self.createMenuBar()
|
|
|
|
|
|
|
|
|
|
# 创建状态栏
|
|
|
|
|
self.status_bar = self.statusBar()
|
|
|
|
|
self.status_bar.showMessage("就绪")
|
|
|
|
|
|
|
|
|
|
# 连接文本变化信号
|
|
|
|
|
self.text_edit.textChanged.connect(self.onTextChanged)
|
|
|
|
|
|
|
|
|
|
def createMenuBar(self):
|
|
|
|
|
"""
|
|
|
|
|
@ -35,8 +58,39 @@ class MainWindow(QMainWindow):
|
|
|
|
|
- 帮助菜单:关于
|
|
|
|
|
- 为每个菜单项连接对应的槽函数
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现菜单栏创建逻辑
|
|
|
|
|
pass
|
|
|
|
|
menu_bar = self.menuBar()
|
|
|
|
|
|
|
|
|
|
# 文件菜单
|
|
|
|
|
file_menu = menu_bar.addMenu('文件')
|
|
|
|
|
|
|
|
|
|
# 打开动作
|
|
|
|
|
open_action = QAction('打开', self)
|
|
|
|
|
open_action.setShortcut('Ctrl+O')
|
|
|
|
|
open_action.triggered.connect(self.openFile)
|
|
|
|
|
file_menu.addAction(open_action)
|
|
|
|
|
|
|
|
|
|
# 保存动作
|
|
|
|
|
save_action = QAction('保存', self)
|
|
|
|
|
save_action.setShortcut('Ctrl+S')
|
|
|
|
|
save_action.triggered.connect(self.saveFile)
|
|
|
|
|
file_menu.addAction(save_action)
|
|
|
|
|
|
|
|
|
|
# 分隔线
|
|
|
|
|
file_menu.addSeparator()
|
|
|
|
|
|
|
|
|
|
# 退出动作
|
|
|
|
|
exit_action = QAction('退出', self)
|
|
|
|
|
exit_action.setShortcut('Ctrl+Q')
|
|
|
|
|
exit_action.triggered.connect(self.close)
|
|
|
|
|
file_menu.addAction(exit_action)
|
|
|
|
|
|
|
|
|
|
# 帮助菜单
|
|
|
|
|
help_menu = menu_bar.addMenu('帮助')
|
|
|
|
|
|
|
|
|
|
# 关于动作
|
|
|
|
|
about_action = QAction('关于', self)
|
|
|
|
|
about_action.triggered.connect(self.showAbout)
|
|
|
|
|
help_menu.addAction(about_action)
|
|
|
|
|
|
|
|
|
|
def openFile(self):
|
|
|
|
|
"""
|
|
|
|
|
@ -46,8 +100,33 @@ class MainWindow(QMainWindow):
|
|
|
|
|
- 成功时:将内容显示在文本区域,重置打字状态
|
|
|
|
|
- 失败时:显示错误消息框
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现打开文件逻辑
|
|
|
|
|
pass
|
|
|
|
|
options = QFileDialog.Options()
|
|
|
|
|
file_path, _ = QFileDialog.getOpenFileName(
|
|
|
|
|
self,
|
|
|
|
|
"打开文件",
|
|
|
|
|
"",
|
|
|
|
|
"文本文件 (*.txt);;Word文档 (*.docx);;所有文件 (*)",
|
|
|
|
|
options=options
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if file_path:
|
|
|
|
|
try:
|
|
|
|
|
# 解析文件内容
|
|
|
|
|
content = FileParser.parse_file(file_path)
|
|
|
|
|
self.learning_content = content
|
|
|
|
|
|
|
|
|
|
# 显示内容到文本编辑区域
|
|
|
|
|
self.text_edit.setPlainText(content)
|
|
|
|
|
|
|
|
|
|
# 重置打字状态
|
|
|
|
|
self.typing_logic = TypingLogic(content)
|
|
|
|
|
self.current_position = 0
|
|
|
|
|
|
|
|
|
|
# 更新状态栏
|
|
|
|
|
self.status_bar.showMessage(f"已打开文件: {file_path}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# 显示错误消息框
|
|
|
|
|
QMessageBox.critical(self, "错误", f"无法打开文件:\n{str(e)}")
|
|
|
|
|
|
|
|
|
|
def saveFile(self):
|
|
|
|
|
"""
|
|
|
|
|
@ -56,16 +135,49 @@ class MainWindow(QMainWindow):
|
|
|
|
|
- 将文本区域内容写入选定文件
|
|
|
|
|
- 返回操作结果
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现保存文件逻辑
|
|
|
|
|
pass
|
|
|
|
|
options = QFileDialog.Options()
|
|
|
|
|
file_path, _ = QFileDialog.getSaveFileName(
|
|
|
|
|
self,
|
|
|
|
|
"保存文件",
|
|
|
|
|
"",
|
|
|
|
|
"文本文件 (*.txt);;所有文件 (*)",
|
|
|
|
|
options=options
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if file_path:
|
|
|
|
|
try:
|
|
|
|
|
# 获取文本编辑区域的内容
|
|
|
|
|
content = self.text_edit.toPlainText()
|
|
|
|
|
|
|
|
|
|
# 写入文件
|
|
|
|
|
with open(file_path, 'w', encoding='utf-8') as f:
|
|
|
|
|
f.write(content)
|
|
|
|
|
|
|
|
|
|
# 更新状态栏
|
|
|
|
|
self.status_bar.showMessage(f"文件已保存: {file_path}")
|
|
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# 显示错误消息框
|
|
|
|
|
QMessageBox.critical(self, "错误", f"无法保存文件:\n{str(e)}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def showAbout(self):
|
|
|
|
|
"""
|
|
|
|
|
显示关于对话框
|
|
|
|
|
- 显示消息框,包含软件名称、版本、描述
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现关于对话框逻辑
|
|
|
|
|
pass
|
|
|
|
|
QMessageBox.about(
|
|
|
|
|
self,
|
|
|
|
|
"关于",
|
|
|
|
|
"隐私学习软件 - 仿Word\n\n"
|
|
|
|
|
"版本: 1.0\n\n"
|
|
|
|
|
"这是一个用于隐私学习的打字练习软件,\n"
|
|
|
|
|
"可以加载文档并进行打字练习,\n"
|
|
|
|
|
"帮助提高打字速度和准确性。"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def onTextChanged(self):
|
|
|
|
|
"""
|
|
|
|
|
@ -74,8 +186,30 @@ class MainWindow(QMainWindow):
|
|
|
|
|
- 调用打字逻辑检查输入正确性
|
|
|
|
|
- 更新高亮显示和状态栏
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现文本变化处理逻辑
|
|
|
|
|
pass
|
|
|
|
|
if self.typing_logic is None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 获取当前文本内容
|
|
|
|
|
current_text = self.text_edit.toPlainText()
|
|
|
|
|
|
|
|
|
|
# 调用打字逻辑检查输入正确性
|
|
|
|
|
result = self.typing_logic.check_input(current_text)
|
|
|
|
|
|
|
|
|
|
# 更新高亮显示
|
|
|
|
|
if result['correct']:
|
|
|
|
|
self.highlightText(len(current_text), QColor('lightgreen'))
|
|
|
|
|
else:
|
|
|
|
|
# 高亮显示错误部分
|
|
|
|
|
self.highlightText(len(current_text), QColor('lightcoral'))
|
|
|
|
|
|
|
|
|
|
# 更新状态栏
|
|
|
|
|
progress = self.typing_logic.get_progress()
|
|
|
|
|
accuracy = result.get('accuracy', 0) * 100
|
|
|
|
|
self.status_bar.showMessage(
|
|
|
|
|
f"进度: {progress['percentage']:.1f}% | "
|
|
|
|
|
f"准确率: {accuracy:.1f}% | "
|
|
|
|
|
f"位置: {result['position']}/{progress['total']}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def highlightText(self, position, color):
|
|
|
|
|
"""
|
|
|
|
|
@ -84,5 +218,24 @@ class MainWindow(QMainWindow):
|
|
|
|
|
- 应用背景颜色格式
|
|
|
|
|
- 恢复光标位置
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现文本高亮逻辑
|
|
|
|
|
pass
|
|
|
|
|
# 创建文本格式
|
|
|
|
|
format = QTextCharFormat()
|
|
|
|
|
format.setBackground(color)
|
|
|
|
|
|
|
|
|
|
# 获取文本游标
|
|
|
|
|
cursor = self.text_edit.textCursor()
|
|
|
|
|
|
|
|
|
|
# 保存当前光标位置
|
|
|
|
|
current_pos = cursor.position()
|
|
|
|
|
|
|
|
|
|
# 选择从开始到指定位置的文本
|
|
|
|
|
cursor.select(QTextCursor.Document)
|
|
|
|
|
cursor.setPosition(0, QTextCursor.MoveAnchor)
|
|
|
|
|
cursor.setPosition(position, QTextCursor.KeepAnchor)
|
|
|
|
|
|
|
|
|
|
# 应用格式
|
|
|
|
|
cursor.mergeCharFormat(format)
|
|
|
|
|
|
|
|
|
|
# 恢复光标位置
|
|
|
|
|
cursor.setPosition(current_pos)
|
|
|
|
|
self.text_edit.setTextCursor(cursor)
|