forked from Vocab-Apps/anki-hyper-tts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent_batch_preview.py
278 lines (226 loc) · 11.4 KB
/
component_batch_preview.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import sys
import aqt.qt
import time
import html
import aqt.operations
constants = __import__('constants', globals(), locals(), [], sys._addon_import_level_base)
component_common = __import__('component_common', globals(), locals(), [], sys._addon_import_level_base)
batch_status = __import__('batch_status', globals(), locals(), [], sys._addon_import_level_base)
logging_utils = __import__('logging_utils', globals(), locals(), [], sys._addon_import_level_base)
logger = logging_utils.get_child_logger(__name__)
class TableRepaintTimer():
def __init__(self, delay_ms):
self.delay_ms = delay_ms
self.timer_obj = None
class BatchPreviewTableModel(aqt.qt.QAbstractTableModel):
def __init__(self, batch_status):
aqt.qt.QAbstractTableModel.__init__(self, None)
self.batch_status = batch_status
self.note_id_header = 'Note Id'
self.source_text_header = 'Source Text'
self.processed_text_header = 'Processed Text'
self.status_header = 'Status'
def flags(self, index):
return aqt.qt.Qt.ItemFlag.ItemIsSelectable | aqt.qt.Qt.ItemFlag.ItemIsEnabled
def rowCount(self, parent):
# logger.debug('SourceTextPreviewTableModel.rowCount')
return len(self.batch_status.note_id_list)
def columnCount(self, parent):
# logger.debug('SourceTextPreviewTableModel.columnCount')
return 4
def notifyChange(self, row):
# logger.info(f'notifyChange, row: {row}')
start_index = self.createIndex(row, 0)
end_index = self.createIndex(row, 2)
self.dataChanged.emit(start_index, end_index)
def data(self, index, role):
if role != aqt.qt.Qt.ItemDataRole.DisplayRole:
return None
# logger.debug('SourceTextPreviewTableModel.data')
if not index.isValid():
return aqt.qt.QVariant()
data = None
note_status = self.batch_status[index.row()]
if index.column() == 0:
data = note_status.note_id
elif index.column() == 1:
data = note_status.source_text
elif index.column() == 2:
data = note_status.processed_text
elif index.column() == 3:
if note_status.status != None:
data = note_status.status.name
if data != None:
return aqt.qt.QVariant(data)
return aqt.qt.QVariant()
def headerData(self, col, orientation, role):
# logger.debug('SourceTextPreviewTableModel.headerData')
if orientation == aqt.qt.Qt.Orientation.Horizontal and role == aqt.qt.Qt.ItemDataRole.DisplayRole:
if col == 0:
return aqt.qt.QVariant(self.note_id_header)
elif col == 1:
return aqt.qt.QVariant(self.source_text_header)
elif col == 2:
return aqt.qt.QVariant(self.processed_text_header)
elif col == 3:
return aqt.qt.QVariant(self.status_header)
return aqt.qt.QVariant()
class BatchPreview(component_common.ComponentBase):
def __init__(self, hypertts, dialog, note_id_list, sample_selection_fn, batch_start_fn, batch_end_fn):
self.hypertts = hypertts
self.dialog = dialog
self.note_id_list = note_id_list
self.sample_selection_fn = sample_selection_fn
self.batch_start_fn = batch_start_fn
self.batch_end_fn = batch_end_fn
self.batch_status = batch_status.BatchStatus(hypertts.anki_utils, note_id_list, self)
self.batch_preview_table_model = BatchPreviewTableModel(self.batch_status)
self.table_view = None
# create certain widgets right away
self.stack = aqt.qt.QStackedWidget()
self.progress_bar = aqt.qt.QProgressBar()
self.progress_bar.setMaximum(len(self.note_id_list))
self.progress_details = aqt.qt.QLabel()
self.selected_row = None
self.apply_to_notes_batch_started = False
self.table_repaint_timer = TableRepaintTimer(500)
def load_model(self, model):
self.batch_model = model
self.hypertts.anki_utils.run_in_background(self.update_batch_status_task, self.update_batch_status_task_done)
def update_batch_status_task(self):
if self.batch_status.is_running():
# stop current batch
self.batch_status.stop()
while self.batch_status.is_running():
time.sleep(0.1)
logger.info('update_batch_status_task')
if self.batch_model.text_processing != None:
self.hypertts.populate_batch_status_processed_text(self.note_id_list, self.batch_model.source, self.batch_model.text_processing, self.batch_status)
def update_batch_status_task_done(self, result):
logger.info('update_batch_status_task_done')
def draw(self):
# populate processed text
self.batch_preview_layout = aqt.qt.QVBoxLayout()
self.table_view = aqt.qt.QTableView()
self.table_view.setModel(self.batch_preview_table_model)
self.table_view.setSelectionMode(aqt.qt.QTableView.SelectionMode.SingleSelection)
self.table_view.setSelectionBehavior(aqt.qt.QTableView.SelectionBehavior.SelectRows)
self.table_view.selectionModel().selectionChanged.connect(self.selection_changed)
self.batch_preview_layout.addWidget(self.table_view, stretch=1)
self.error_label = aqt.qt.QLabel()
self.error_label.setWordWrap(True)
self.batch_preview_layout.addWidget(self.error_label)
# create stack of widgets which will be toggled when we're running the batch
self.batchNotRunningStack = aqt.qt.QWidget()
self.batchRunningStack = aqt.qt.QWidget()
self.batchCompletedStack = aqt.qt.QWidget()
# populate the "notRunning" stack
notRunningLayout = aqt.qt.QVBoxLayout()
self.batchNotRunningStack.setLayout(notRunningLayout)
# poulate the "running" stack
runningLayout = aqt.qt.QVBoxLayout()
self.stop_button = aqt.qt.QPushButton('Stop')
stop_and_status = aqt.qt.QHBoxLayout()
stop_and_status.addWidget(self.progress_details, stretch=1)
stop_and_status.addWidget(self.stop_button)
runningLayout.addLayout(stop_and_status)
runningLayout.addWidget(self.progress_bar)
self.batchRunningStack.setLayout(runningLayout)
# populate the completed stack
completedLayout = aqt.qt.QVBoxLayout()
label = aqt.qt.QLabel(constants.GUI_TEXT_BATCH_COMPLETED)
label.setWordWrap(True)
completedLayout.addWidget(label)
self.batchCompletedStack.setLayout(completedLayout)
self.stack.addWidget(self.batchNotRunningStack)
self.stack.addWidget(self.batchRunningStack)
self.stack.addWidget(self.batchCompletedStack)
self.show_not_running_stack()
self.batch_preview_layout.addWidget(self.stack)
# wire events
self.stop_button.pressed.connect(self.stop_button_pressed)
return self.batch_preview_layout
def show_not_running_stack(self):
self.stack.setCurrentIndex(0)
self.progress_bar.setValue(0)
def show_running_stack(self):
self.stack.setCurrentIndex(1)
def show_completed_stack(self):
self.stack.setCurrentIndex(2)
def selection_changed(self):
logger.info('selection_changed')
self.report_sample_text()
self.update_error_label_for_selected()
def report_sample_text(self):
note_status = self.get_selected_note_status()
if note_status != None:
text = note_status.processed_text
self.sample_selection_fn(note_status.note_id, text)
def update_error_label_for_selected(self):
note_status = self.get_selected_note_status()
if note_status != None:
if note_status.status == constants.BatchNoteStatus.Error:
# show error label
self.error_label.setText('<b>Error:</b> ' + html.escape(str(note_status.error)))
else:
self.error_label.setText('')
def get_selected_note_status(self):
row_indices = self.table_view.selectionModel().selectedIndexes()
if len(row_indices) >= 1:
self.selected_row = row_indices[0].row()
return self.batch_status[self.selected_row]
return None
def apply_audio_to_notes(self):
self.apply_to_notes_batch_started = True
self.hypertts.anki_utils.run_in_background_collection_op(self.dialog, self.apply_audio_fn, self.finished_apply_audio_fn)
def stop_button_pressed(self):
self.batch_status.stop()
def apply_audio_fn(self, anki_collection):
self.hypertts.process_batch_audio(self.note_id_list, self.batch_model, self.batch_status, anki_collection)
def finished_apply_audio_fn(self, result):
logger.debug(f'finished_apply_audio_fn, result: {result}')
def batch_start(self):
self.hypertts.anki_utils.run_on_main(self.show_running_stack)
self.hypertts.anki_utils.run_on_main(self.batch_start_fn)
def batch_end(self, completed):
if completed and self.apply_to_notes_batch_started == True:
self.hypertts.anki_utils.run_on_main(self.show_completed_stack)
else:
self.hypertts.anki_utils.run_on_main(self.show_not_running_stack)
if self.apply_to_notes_batch_started:
self.batch_end_fn(completed)
def update_progress_bar(self, row, total_count, start_time, current_time):
self.progress_bar.setValue(row + 1)
completed_note_count = row + 1
elapsed_time = current_time - start_time
elapsed_time_seconds = elapsed_time.seconds
time_per_note = elapsed_time_seconds / completed_note_count
remaining_count = total_count - completed_note_count
completed_text = f'Completed {row} / {total_count}'
# time remaining computation
time_remaining_s = time_per_note * remaining_count
time_remaining_m = time_remaining_s / 60
time_remaining_s = time_remaining_s % 60
if time_remaining_m >= 5:
time_remaining_text = f', {time_remaining_m:.0f} minutes remaining'
else:
time_remaining_text = f', {time_remaining_m:.0f} minutes, {time_remaining_s:.0f} seconds remaining'
status_text = completed_text
if completed_note_count >= 2:
status_text = f'{completed_text}{time_remaining_text}'
self.progress_details.setText(status_text)
def table_viewport_repaint_refresh_timer(self):
# needs to be called on main thread
self.hypertts.anki_utils.call_on_timer_expire(self.table_repaint_timer, self.table_viewport_repaint)
def table_viewport_repaint(self):
if self.table_view != None:
# logger.info('table_viewport_repaint')
self.table_view.viewport().repaint()
def batch_change(self, note_id, row, total_count, start_time, current_time):
# logger.info(f'change_listener row {row}')
self.hypertts.anki_utils.run_on_main(lambda: self.batch_preview_table_model.notifyChange(row))
self.hypertts.anki_utils.run_on_main(lambda: self.update_progress_bar(row, total_count, start_time, current_time))
self.hypertts.anki_utils.run_on_main(lambda: self.table_viewport_repaint_refresh_timer())
if row == self.selected_row:
self.hypertts.anki_utils.run_on_main(self.update_error_label_for_selected)
self.hypertts.anki_utils.run_on_main(self.report_sample_text)