-
Notifications
You must be signed in to change notification settings - Fork 7
/
__init__.py
197 lines (156 loc) · 6.12 KB
/
__init__.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
# This file is part of Japanese Furigana <https://github.com/obynio/anki-japanese-furigana>.
#
# Japanese Furigana is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Japanese Furigana is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Japanese Furigana. If not, see <http://www.gnu.org/licenses/>.
import os
from aqt.utils import tooltip
from aqt.operations import QueryOp
from aqt.qt import *
from aqt import mw
from anki.hooks import addHook
from . import reading
from .config import Config
from .selection import Selection
from .utils import removeFurigana
from .bulk import bulkGenerate
mecab = reading.MecabController()
config = Config()
def setupGuiMenu():
useRubyTags = QAction(
"Use ruby tags", mw, checkable=True, checked=config.getUseRubyTags()
)
useRubyTags.toggled.connect(config.setUseRubyTags)
ignoreNumbers = QAction(
"Ignore numbers", mw, checkable=True, checked=config.getIgnoreNumbers()
)
ignoreNumbers.toggled.connect(config.setIgnoreNumbers)
mw.form.menuTools.addSeparator()
mw.form.menuTools.addAction(useRubyTags)
mw.form.menuTools.addAction(ignoreNumbers)
def tooltip_with_shortcut(tip, shortcut_name):
shortcut = config.getKeyboardShortcut(shortcut_name)
if shortcut:
key_str = QKeySequence(shortcut).toString(
QKeySequence.SequenceFormat.NativeText
)
tip += " ({})".format(key_str)
return tip
def addButtons(buttons, editor):
return buttons + [
editor.addButton(
icon=os.path.join(os.path.dirname(__file__), "icons", "add_furigana.svg"),
cmd="generateFurigana",
tip=tooltip_with_shortcut(
"Automatically generate furigana", "add_furigana"
),
func=lambda ed=editor: doIt(ed, generateFurigana),
keys=config.getKeyboardShortcut("add_furigana"),
),
editor.addButton(
icon=os.path.join(os.path.dirname(__file__), "icons", "del_furigana.svg"),
cmd="deleteFurigana",
tip=tooltip_with_shortcut("Delete all furigana", "del_furigana"),
func=lambda ed=editor: doIt(ed, deleteFurigana),
keys=config.getKeyboardShortcut("del_furigana"),
),
]
def addBrowserButtons(browser):
menu = browser.form.menuEdit
menu.addSeparator()
a = menu.addAction('Bulk Generate Furigana')
a.triggered.connect(lambda _, b=browser: onBulkUpdate(b))
def onBulkUpdate(browser):
nids = browser.selectedNotes()
if not nids:
tooltip("No notes selected.")
return
bulkUpdate(browser, nids)
def bulkUpdate(browser, nids):
config = mw.addonManager.getConfig(__name__)
lastSourceField = config.get('lastSourceField', None)
lastDestinationField = config.get('lastDestinationField', None)
# Extract fields from the first note
note = mw.col.get_note(nids[0])
fields = note.keys()
# Set up dialog
dialog = QDialog(browser)
dialog.setWindowTitle('Bulk Generate Furigana for ' + str(len(nids)) + ' note(s)')
layout = QVBoxLayout(dialog)
sourceFieldLayout = QHBoxLayout()
layout.addLayout(sourceFieldLayout)
sourceFieldLabel = QLabel('Source Field')
sourceFieldLayout.addWidget(sourceFieldLabel)
sourceField = QComboBox()
sourceFieldLayout.addWidget(sourceField)
destinationFieldLayout = QHBoxLayout()
layout.addLayout(destinationFieldLayout)
destinationFieldLabel = QLabel('Destination Field')
destinationFieldLayout.addWidget(destinationFieldLabel)
destinationField = QComboBox()
destinationFieldLayout.addWidget(destinationField)
for field in fields:
sourceField.addItem(field)
destinationField.addItem(field)
if field == lastSourceField:
sourceField.setCurrentText(field)
if field == lastDestinationField:
destinationField.setCurrentText(field)
buttonsLayout = QHBoxLayout()
layout.addLayout(buttonsLayout)
generateButton = QPushButton('Generate', parent=dialog)
buttonsLayout.addWidget(generateButton)
generateButton.clicked.connect(dialog.accept)
dialog.resize(320, 150)
if dialog.exec():
# Get the values *before* QueryOp otherwise the QT objects will be disposed of
sourceField = sourceField.currentText()
destinationField = destinationField.currentText()
# Save the values to be remembered later
configs = {
'lastSourceField': sourceField,
'lastDestinationField': destinationField,
}
mw.addonManager.writeConfig(__name__, configs)
def progressCallback(i, total):
mw.taskman.run_on_main(
lambda: mw.progress.update(
label=f"{i}/{total}",
value=i,
max=total,
)
)
QueryOp(
parent=mw,
op=lambda col: bulkGenerate(col, nids, sourceField, destinationField, progressCallback),
success=lambda col: tooltip('Furigana generated successfully'),
).with_progress().run_in_background()
def doIt(editor, action):
Selection(editor, lambda s: action(editor, s))
def generateFurigana(editor, s):
html = s.selected
html = removeFurigana(html)
html = mecab.reading(html, config.getIgnoreNumbers(), config.getUseRubyTags())
if html == s.selected:
tooltip("Nothing to generate!")
else:
s.modify(html)
def deleteFurigana(editor, s):
stripped = removeFurigana(s.selected)
if stripped == s.selected:
tooltip("No furigana found to delete")
else:
s.modify(stripped)
setupGuiMenu()
addHook("setupEditorButtons", addButtons)
addHook("browser.setupMenus", addBrowserButtons)