poc/project/llmclipboard/llmclipboard/gui.py
zhukang a57ef160d6 feat: 更新版本号和程序信息
1. 更新版本号至 0.1.4
- 更新程序标题显示版本号
- 添加版本信息配置文件

2. 修复配置保存和加载问题
- 修复保存路径设置不生效的问题
- 优化配置加载时机,确保使用最新设置
2025-01-16 00:01:35 +08:00

161 lines
5.9 KiB
Python

import sys
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, QTimer
from PyQt6.QtGui import QIcon, QAction
from qt_material import apply_stylesheet
import darkdetect
from configparser import ConfigParser
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("LLMClipboard v0.1.4")
self.setMinimumWidth(500)
# 初始化服务
from .app import TextCaptureService
self.service = TextCaptureService()
# 加载配置
self.config = ConfigParser()
self.config_file = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'config.ini')
self.load_config()
# 创建主窗口部件
main_widget = QWidget()
self.setCentralWidget(main_widget)
layout = QVBoxLayout(main_widget)
# 保存路径设置
save_path_layout = QHBoxLayout()
save_path_label = QLabel("保存路径:")
self.save_path_edit = QLineEdit(self.config.get('Settings', 'save_location'))
browse_button = QPushButton("浏览...")
browse_button.clicked.connect(self.browse_save_path)
save_path_layout.addWidget(save_path_label)
save_path_layout.addWidget(self.save_path_edit)
save_path_layout.addWidget(browse_button)
layout.addLayout(save_path_layout)
# 双击阈值设置
threshold_layout = QHBoxLayout()
threshold_label = QLabel("双击阈值 (秒):")
self.threshold_spin = QSpinBox()
self.threshold_spin.setRange(1, 1000)
self.threshold_spin.setValue(int(float(self.config.get('Settings', 'double_click_threshold')) * 1000))
self.threshold_spin.setSingleStep(100)
threshold_layout.addWidget(threshold_label)
threshold_layout.addWidget(self.threshold_spin)
layout.addLayout(threshold_layout)
# 状态显示
self.status_label = QLabel("状态: 已停止")
layout.addWidget(self.status_label)
# 控制按钮
button_layout = QHBoxLayout()
self.start_button = QPushButton("启动监听")
self.start_button.clicked.connect(self.toggle_monitoring)
self.save_button = QPushButton("保存设置")
self.save_button.clicked.connect(self.save_config)
button_layout.addWidget(self.start_button)
button_layout.addWidget(self.save_button)
layout.addLayout(button_layout)
# 系统托盘
self.setup_system_tray()
# 设置样式
self.setup_style()
def load_config(self):
if os.path.exists(self.config_file):
self.config.read(self.config_file)
else:
self.config['Settings'] = {
'save_location': os.path.expanduser('~/Documents'),
'double_click_threshold': '0.3'
}
self.save_config()
def save_config(self):
self.config['Settings']['save_location'] = self.save_path_edit.text()
self.config['Settings']['double_click_threshold'] = str(self.threshold_spin.value() / 1000)
with open(self.config_file, 'w') as f:
self.config.write(f)
def browse_save_path(self):
folder = QFileDialog.getExistingDirectory(self, "选择保存路径", self.save_path_edit.text())
if folder:
self.save_path_edit.setText(folder)
def toggle_monitoring(self):
if self.start_button.text() == "启动监听":
self.start_button.setText("停止监听")
self.status_label.setText("状态: 正在运行")
self.save_config()
# 重新加载配置
self.service.load_config()
self.service.start()
else:
self.start_button.setText("启动监听")
self.status_label.setText("状态: 已停止")
self.service.stop()
def setup_system_tray(self):
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(self.style().standardIcon(QStyle.StandardPixmap.SP_ComputerIcon))
# 创建托盘菜单
tray_menu = QMenu()
show_action = QAction("显示", self)
quit_action = QAction("退出", self)
show_action.triggered.connect(self.show)
quit_action.triggered.connect(self.close)
tray_menu.addAction(show_action)
tray_menu.addAction(quit_action)
self.tray_icon.setContextMenu(tray_menu)
self.tray_icon.show()
# 双击托盘图标显示主窗口
self.tray_icon.activated.connect(self.tray_icon_activated)
def tray_icon_activated(self, reason):
if reason == QSystemTrayIcon.ActivationReason.DoubleClick:
self.show()
def setup_style(self):
# 根据系统主题设置深色/浅色模式
if darkdetect.isDark():
apply_stylesheet(self, theme='dark_blue.xml')
else:
apply_stylesheet(self, theme='light_blue.xml')
def closeEvent(self, event):
if self.start_button.text() == "停止监听":
self.service.stop()
event.ignore()
self.hide()
self.tray_icon.showMessage(
"LLMClipboard",
"应用程序已最小化到系统托盘",
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("就绪"))