fix: improve clipboard handling and add status messages

- Enhance clipboard content detection and error handling
- Add detailed logging for debugging
- Add GUI status messages for user feedback
- Fix document saving issues
- Improve error reporting
This commit is contained in:
zhukang 2025-01-15 21:38:34 +08:00
parent b2494d439b
commit 595f22c929
3 changed files with 129 additions and 50 deletions

View File

@ -40,30 +40,16 @@ class TextCaptureService:
)
self.logger = logging.getLogger(__name__)
def save_to_markdown(self, content):
try:
# 使用文档处理器处理内容
result = self.doc_processor.process_document(content, self.save_location)
# 保存处理后的内容
with open(result['path'], 'w', encoding='utf-8') as f:
f.write(result['content'])
self.logger.info(f"Content saved to: {result['path']}")
self.logger.info(f"Title: {result['title']}")
self.logger.info(f"Category: {result['category']}")
self.logger.info(f"Tags: {', '.join(result['tags'])}")
except Exception as e:
self.logger.error(f"Error saving file: {e}")
def get_clipboard_content(self):
"""获取剪贴板内容"""
try:
win32clipboard.OpenClipboard()
content = None
# 尝试获取HTML格式
try:
content = win32clipboard.GetClipboardData(win32con.CF_HTML)
self.logger.info("Clipboard HTML content captured")
self.logger.info("获取HTML格式内容")
h = HTML2Text()
h.body_width = 0 # 禁用自动换行
h.single_line_break = True # 使用单行换行
@ -71,42 +57,43 @@ class TextCaptureService:
h.ignore_images = False
h.ignore_links = False
h.ignore_tables = False
return h.handle(content).strip()
except:
pass
content = h.handle(content).strip()
except Exception as e:
self.logger.debug(f"获取HTML格式失败: {e}")
try:
content = win32clipboard.GetClipboardData(win32con.CF_TEXT)
self.logger.info("Clipboard TEXT content captured")
text = content.decode('gbk')
# 处理纯文本的换行
return '\n'.join(line.strip() for line in text.splitlines() if line.strip())
except:
pass
# 如果HTML格式获取失败尝试获取Unicode文本
if not content:
try:
content = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
self.logger.info("获取Unicode文本格式内容")
except Exception as e:
self.logger.debug(f"获取Unicode文本失败: {e}")
try:
content = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
self.logger.info("Clipboard UNICODE TEXT content captured")
# 处理Unicode文本的换行
return '\n'.join(line.strip() for line in content.splitlines() if line.strip())
except:
# 如果Unicode获取失败尝试获取普通文本
if not content:
try:
content = win32clipboard.GetClipboardData(win32con.CF_TEXT)
content = content.decode('gbk')
self.logger.info("获取普通文本格式内容")
except Exception as e:
self.logger.debug(f"获取普通文本失败: {e}")
win32clipboard.CloseClipboard()
if content:
self.logger.info("成功获取剪贴板内容")
return content
else:
self.logger.warning("剪贴板内容为空")
return None
except Exception as e:
self.logger.error(f"Error getting clipboard content: {e}")
return None
finally:
self.logger.error(f"获取剪贴板内容失败: {e}")
try:
win32clipboard.CloseClipboard()
except Exception as e:
self.logger.error(f"Error closing clipboard: {e}")
def simulate_copy(self):
keyboard.press('ctrl')
keyboard.press('c')
keyboard.release('c')
keyboard.release('ctrl')
time.sleep(0.1) # 等待复制操作完成
except:
pass
return None
def on_click(self, x, y, button, pressed):
if not pressed or not self.running:
@ -115,15 +102,53 @@ class TextCaptureService:
if button == mouse.Button.right:
current_time = time.time()
if (current_time - self.last_right_click_time) < self.double_click_threshold:
self.simulate_copy()
self.logger.info("检测到双击右键")
content = self.get_clipboard_content()
if content:
self.logger.info("开始保存内容")
self.save_to_markdown(content)
else:
self.logger.warning("No content captured from clipboard")
self.logger.warning("没有可保存的内容")
self.last_right_click_time = current_time
return True
def save_to_markdown(self, content):
"""保存内容到Markdown文件"""
try:
# 确保保存目录存在
if not os.path.exists(self.save_location):
os.makedirs(self.save_location)
self.logger.info(f"创建保存目录: {self.save_location}")
# 使用文档处理器处理内容
result = self.doc_processor.process_document(content, self.save_location)
# 保存处理后的内容
with open(result['path'], 'w', encoding='utf-8') as f:
f.write(result['content'])
self.logger.info(f"文件保存成功: {result['path']}")
self.logger.info(f"标题: {result['title']}")
self.logger.info(f"分类: {result['category']}")
self.logger.info(f"标签: {', '.join(result['tags'])}")
# 在GUI中显示保存成功的消息
if hasattr(self, 'gui') and self.gui:
self.gui.show_message(f"已保存到: {result['path']}")
except Exception as e:
error_msg = f"保存文件失败: {str(e)}"
self.logger.error(error_msg)
if hasattr(self, 'gui') and self.gui:
self.gui.show_message(error_msg, error=True)
def simulate_copy(self):
keyboard.press('ctrl')
keyboard.press('c')
keyboard.release('c')
keyboard.release('ctrl')
time.sleep(0.1) # 等待复制操作完成
def start(self):
self.running = True
self._mouse_listener = mouse.Listener(on_click=self.on_click)

View File

@ -0,0 +1,43 @@
{
"技术": [
"编程",
"开发",
"代码",
"框架",
"算法",
"数据库",
"API"
],
"学习": [
"教程",
"课程",
"学习",
"笔记",
"知识",
"总结"
],
"工作": [
"会议",
"项目",
"计划",
"报告",
"任务",
"进度"
],
"想法": [
"想法",
"创意",
"思考",
"灵感",
"观点",
"建议"
],
"资源": [
"工具",
"资源",
"链接",
"参考",
"文档",
"书籍"
]
}

View File

@ -3,7 +3,7 @@ import os
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QPushButton, QLabel, QFileDialog,
QSystemTrayIcon, QMenu, QSpinBox, QStyle, QLineEdit)
from PyQt6.QtCore import Qt, QSettings
from PyQt6.QtCore import Qt, QSettings, QTimer
from PyQt6.QtGui import QIcon, QAction
from qt_material import apply_stylesheet
import darkdetect
@ -145,3 +145,14 @@ class MainWindow(QMainWindow):
QSystemTrayIcon.Icon.Information,
2000
)
def show_message(self, message, error=False):
"""显示消息通知"""
if error:
self.status_label.setStyleSheet("color: red;")
else:
self.status_label.setStyleSheet("color: green;")
self.status_label.setText(message)
# 5秒后清除消息
QTimer.singleShot(5000, lambda: self.status_label.setText("就绪"))