feat: 优化滚动截屏逻辑并改进UI交互

- 新增div签名机制用于内容去重
- 实现基于最后一个div位置的智能滚动计算
- 合并开始/停止按钮为单一操作按钮
- 增加处理进度和滚动距离的详细日志
- 优化UI状态显示和提示信息
This commit is contained in:
2026-03-06 17:24:27 +08:00
parent 41ff658e31
commit 4178bfed06
2 changed files with 211 additions and 41 deletions

77
gui.py
View File

@@ -43,6 +43,7 @@ class CaptureWorker(QThread):
status_signal = Signal(str)
finished_signal = Signal()
error_signal = Signal(str)
div_processed_signal = Signal(int, int) # (当前div序号, 总div数)
def __init__(self, scroll_capture: ScrollCaptureOCR):
super().__init__()
@@ -59,17 +60,24 @@ class CaptureWorker(QThread):
self.scroll_capture.previous_ocr_result = []
self.scroll_capture.scroll_count = 0
self.scroll_capture.all_results = []
self.scroll_capture.processed_divs = []
self.scroll_capture.last_div_signature = None
self.scroll_capture.total_scroll_distance = 0
self.scroll_capture.is_first_capture = True
# 循环处理
while self.is_running and self.scroll_capture.process_once():
progress = min(self.scroll_capture.scroll_count * 10, 90)
self.progress_signal.emit(progress)
self.log_signal.emit(f"{self.scroll_capture.scroll_count} 次截屏完成")
self.log_signal.emit(f"{self.scroll_capture.scroll_count} 次截屏完成"
f"累计滚动 {self.scroll_capture.total_scroll_distance} 像素")
# 保存最终结果
if self.scroll_capture.all_results:
self.scroll_capture.save_final_result()
self.log_signal.emit(f"✓ 共处理 {len(self.scroll_capture.all_results)} 次截屏")
total_divs = sum(len(result.get('texts', [])) for result in self.scroll_capture.all_results)
self.log_signal.emit(f"✓ 共处理 {len(self.scroll_capture.all_results)} 次截屏,"
f"识别 {total_divs} 个内容区域")
self.progress_signal.emit(100)
self.status_signal.emit("完成")
@@ -263,10 +271,11 @@ class MainWindow(QMainWindow):
# 使用说明
help_text = QLabel(
"<b>使用步骤:</b><br>"
"1. 点击「开始截屏」按钮<br>"
"1. 点击「开始」按钮<br>"
"2. 按住鼠标左键拖动选择区域<br>"
"3. 程序自动滚动截屏并OCR识别<br>"
"4. 检测到重复内容时自动停止"
"3. 程序自动分割div并逐个OCR<br>"
"4. 智能计算滚动距离,自动翻页<br>"
"5. 完成后点击「结束」按钮"
)
help_text.setFont(QFont("Microsoft YaHei", 10))
help_text.setStyleSheet("color: #555555; line-height: 1.6;")
@@ -320,16 +329,10 @@ class MainWindow(QMainWindow):
button_layout.addStretch()
# 停止按钮
self.stop_btn = ModernButton("停止", primary=False)
self.stop_btn.setEnabled(False)
self.stop_btn.clicked.connect(self.stop_capture)
button_layout.addWidget(self.stop_btn)
# 开始按钮
self.start_btn = ModernButton("开始截屏", primary=True)
self.start_btn.clicked.connect(self.start_capture)
button_layout.addWidget(self.start_btn)
# 操作按钮(开始/结束 二合一)
self.action_btn = ModernButton("开始", primary=True)
self.action_btn.clicked.connect(self.on_action_button_clicked)
button_layout.addWidget(self.action_btn)
# 清空日志按钮
self.clear_btn = ModernButton("清空日志", primary=False)
@@ -341,7 +344,7 @@ class MainWindow(QMainWindow):
main_layout.addLayout(button_layout)
# === 底部信息 ===
footer = QLabel("按 Ctrl+F9 也可以快速启动 | 输出目录: ./output/")
footer = QLabel("点击「开始」按钮启动 | 输出目录: ./output/")
footer.setFont(QFont("Microsoft YaHei", 9))
footer.setStyleSheet("color: #999999; margin-top: 10px;")
footer.setAlignment(Qt.AlignCenter)
@@ -359,10 +362,10 @@ class MainWindow(QMainWindow):
tray_menu.addSeparator()
start_action = tray_menu.addAction("开始截屏")
start_action = tray_menu.addAction("开始")
start_action.triggered.connect(self.start_capture)
stop_action = tray_menu.addAction("停止")
stop_action = tray_menu.addAction("结束")
stop_action.triggered.connect(self.stop_capture)
tray_menu.addSeparator()
@@ -407,6 +410,13 @@ class MainWindow(QMainWindow):
self.log_text.append_log(message, level)
def on_action_button_clicked(self):
"""操作按钮点击事件"""
if self.action_btn.text() == "开始":
self.start_capture()
else:
self.stop_capture()
def start_capture(self):
"""开始截屏"""
# 检查OCR服务
@@ -454,9 +464,9 @@ class MainWindow(QMainWindow):
self.worker.start()
# 更新UI状态
self.start_btn.setEnabled(False)
self.stop_btn.setEnabled(True)
# 更新UI状态 - 按钮变为"结束"
self.action_btn.setText("结束")
self.action_btn.update_style()
self.progress_bar.setValue(0)
def stop_capture(self):
@@ -464,15 +474,16 @@ class MainWindow(QMainWindow):
if self.worker and self.worker.isRunning():
self.worker.stop()
self.worker.wait(1000)
self.log_text.append_log("用户手动停止", "WARNING")
self.log_text.append_log("用户手动结束", "WARNING")
self.start_btn.setEnabled(True)
self.stop_btn.setEnabled(False)
self.status_label.setText("已停止")
# 更新UI状态 - 按钮恢复为"开始"
self.action_btn.setText("开始")
self.action_btn.update_style()
self.status_label.setText("就绪")
self.status_label.setStyleSheet("""
QLabel {
color: #F44336;
background-color: #ffebee;
color: #4CAF50;
background-color: #e8f5e9;
padding: 6px 16px;
border-radius: 16px;
}
@@ -516,15 +527,17 @@ class MainWindow(QMainWindow):
def on_finished(self):
"""任务完成回调"""
self.start_btn.setEnabled(True)
self.stop_btn.setEnabled(False)
# 按钮恢复为"开始"
self.action_btn.setText("开始")
self.action_btn.update_style()
self.log_text.append_log("✓ 截屏OCR任务已完成", "INFO")
def on_error(self, error_msg: str):
"""错误回调"""
self.log_text.append_log(f"✗ 错误: {error_msg}", "ERROR")
self.start_btn.setEnabled(True)
self.stop_btn.setEnabled(False)
# 按钮恢复为"开始"
self.action_btn.setText("开始")
self.action_btn.update_style()
def clear_logs(self):
"""清空日志"""
@@ -572,7 +585,7 @@ def main():
window.show()
# 显示启动提示
window.log_text.append_log("程序已启动,点击「开始截屏」按钮开始", "INFO")
window.log_text.append_log("程序已启动,点击「开始」按钮开始", "INFO")
window.log_text.append_log(f"OCR引擎: {Config.OCR_ENGINE}", "INFO")
sys.exit(app.exec())