forked from Vocab-Apps/anki-hyper-tts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent_realtime.py
148 lines (110 loc) · 5.12 KB
/
component_realtime.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
import sys
import aqt.qt
component_common = __import__('component_common', globals(), locals(), [], sys._addon_import_level_base)
component_realtime_side = __import__('component_realtime_side', globals(), locals(), [], sys._addon_import_level_base)
config_models = __import__('config_models', globals(), locals(), [], sys._addon_import_level_base)
constants = __import__('constants', globals(), locals(), [], sys._addon_import_level_base)
errors = __import__('errors', globals(), locals(), [], sys._addon_import_level_base)
gui_utils = __import__('gui_utils', 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 ComponentRealtime(component_common.ConfigComponentBase):
MIN_WIDTH_COMPONENT = 600
MIN_HEIGHT = 400
def __init__(self, hypertts, dialog, card_ord):
self.hypertts = hypertts
self.dialog = dialog
self.card_ord = card_ord
self.model = config_models.RealtimeConfig()
self.apply_button = aqt.qt.QPushButton('Apply To Note')
self.cancel_button = aqt.qt.QPushButton('Cancel')
self.existing_preset_name = None
self.manage_apply_button = True
def configure_note(self, note):
self.manage_apply_button = False
self.note = note
self.front = component_realtime_side.ComponentRealtimeSide(self.hypertts,
self.dialog, constants.AnkiCardSide.Front, self.card_ord, self.front_model_updated, self.existing_preset_found)
self.back = component_realtime_side.ComponentRealtimeSide(self.hypertts,
self.dialog, constants.AnkiCardSide.Back, self.card_ord, self.back_model_updated, self.existing_preset_found)
self.front.configure_note(note)
self.back.configure_note(note)
self.manage_apply_button = True
def load_existing_preset(self):
self.manage_apply_button = False
self.front.load_existing_preset()
self.back.load_existing_preset()
self.manage_apply_button = True
def existing_preset_found(self, preset_name):
self.existing_preset_name = preset_name
def load_model(self, model):
self.manage_apply_button = False
self.model = model
# disseminate to all components
self.front.load_model(model.front)
self.back.load_model(model.back)
self.manage_apply_button = True
def get_model(self):
return self.model
def front_model_updated(self, model):
logger.info('front_model_updated')
self.model.front = model
if self.manage_apply_button:
self.enable_apply_button()
def back_model_updated(self, model):
logger.info('back_model_update')
self.model.back = model
if self.manage_apply_button:
self.enable_apply_button()
def draw(self, layout):
self.vlayout = aqt.qt.QVBoxLayout()
# header
# ======
hlayout = aqt.qt.QHBoxLayout()
# logo header
hlayout.addLayout(gui_utils.get_hypertts_label_header(self.hypertts.hypertts_pro_enabled()))
self.vlayout.addLayout(hlayout)
# sides tabs
# ==========
self.tabs = aqt.qt.QTabWidget()
self.tabs.setTabPosition(aqt.qt.QTabWidget.TabPosition.West)
self.tab_front = aqt.qt.QWidget()
self.tab_back = aqt.qt.QWidget()
self.tab_front.setLayout(self.front.draw())
self.tab_back.setLayout(self.back.draw())
self.tabs.addTab(self.tab_front, 'Front Side')
self.tabs.addTab(self.tab_back, 'Back Side')
# self.tabs.setEnabled(False)
self.vlayout.addWidget(self.tabs)
# setup bottom buttons
# ====================
hlayout = aqt.qt.QHBoxLayout()
hlayout.addStretch()
# apply button
hlayout.addWidget(self.apply_button)
# cancel button
self.cancel_button.setStyleSheet(self.hypertts.anki_utils.get_red_stylesheet())
hlayout.addWidget(self.cancel_button)
self.vlayout.addLayout(hlayout)
# wire events
self.apply_button.pressed.connect(self.apply_button_pressed)
self.cancel_button.pressed.connect(self.cancel_button_pressed)
# defaults
self.cancel_button.setFocus()
self.disable_apply_button()
layout.addLayout(self.vlayout)
def disable_apply_button(self):
self.apply_button.setEnabled(False)
self.apply_button.setStyleSheet(None)
def enable_apply_button(self):
logger.info('enable_apply_button')
self.apply_button.setEnabled(True)
self.apply_button.setStyleSheet(self.hypertts.anki_utils.get_green_stylesheet())
def apply_button_pressed(self):
with self.hypertts.error_manager.get_single_action_context('Applying Realtime Audio to Card'):
self.get_model().validate()
self.hypertts.persist_realtime_config_update_note_type(self.get_model(),
self.note, self.card_ord, self.existing_preset_name)
self.dialog.close()
def cancel_button_pressed(self):
self.dialog.close()