-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortcuts_widget.py
78 lines (63 loc) · 2.62 KB
/
shortcuts_widget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QGridLayout
from PyQt5.QtCore import Qt
from calibre_plugins.ask_grok.i18n import get_translation
from calibre_plugins.ask_grok.config import get_prefs
import sys
class ShortcutsWidget(QWidget):
"""快捷键展示组件"""
def __init__(self, parent=None):
super().__init__(parent)
self.labels = [] # 保存所有标签的引用
self.init_ui()
def init_ui(self):
"""初始化界面"""
# 创建主布局
main_layout = QVBoxLayout()
main_layout.setContentsMargins(20, 20, 20, 20)
self.setLayout(main_layout)
# 创建网格布局用于对齐快捷键
self.grid_layout = QGridLayout()
self.grid_layout.setSpacing(10)
self.grid_layout.setColumnStretch(1, 1) # 让第二列(快捷键)可以拉伸
main_layout.addLayout(self.grid_layout)
# 添加弹性空间
main_layout.addStretch()
self.update_shortcuts()
def update_shortcuts(self):
"""更新快捷键显示"""
# 清除现有标签
for label in self.labels:
self.grid_layout.removeWidget(label)
label.deleteLater()
self.labels.clear()
# 判断当前系统
is_mac = sys.platform == 'darwin'
modifier = 'Command' if is_mac else 'Ctrl'
# 获取当前语言的翻译
i18n = get_translation(get_prefs()['language'])
# 定义快捷键列表
shortcuts = [
(i18n['menu_ask_grok'], f'{modifier}+L'), # 打开询问窗口
]
# 创建快捷键标签
label_style = """
QLabel {
font-size: 1em;
color: palette(text);
}
"""
for row, (action, key) in enumerate(shortcuts):
# 创建动作标签(左侧)
action_label = QLabel(f"{action}:")
action_label.setStyleSheet(label_style)
self.grid_layout.addWidget(action_label, row, 0, Qt.AlignLeft | Qt.AlignVCenter)
# 创建快捷键标签(右侧)
key_label = QLabel(f"<b>{key}</b>")
key_label.setTextFormat(Qt.RichText)
key_label.setStyleSheet(label_style)
self.grid_layout.addWidget(key_label, row, 1, Qt.AlignLeft | Qt.AlignVCenter)
self.labels.extend([action_label, key_label])
def showEvent(self, event):
"""当组件显示时更新快捷键"""
super().showEvent(event)
self.update_shortcuts()