-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairsubs_gui.py
More file actions
298 lines (245 loc) · 10.9 KB
/
Copy pathpairsubs_gui.py
File metadata and controls
298 lines (245 loc) · 10.9 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import urwid
import io
SUBS_CNT_FOR_ALIGN = 12
class SubsLogStream(io.StringIO):
"""Stream for logging into a Text box.
Attributes:
`box` (`urwid.Text`): text widget to print into
`loop` (`urwid.MainLoop`): main loop to redraw
"""
def __init__(self, box, loop):
self.box = box
self.loop = loop
def write(self, message):
"""Writes message into Text box."""
self.box.set_text(self.box.text+message)
self.loop.draw_screen()
class AppBox(urwid.Frame):
"""Frame to show subtitles text."""
def __init__(self, db, sub_id=None):
# import ipdb; ipdb.set_trace()
self.db = db
self.sub_id = sub_id
self.random = False if sub_id else True
self.state = 'show'
self.title = urwid.Text('Title', align='left')
self.left_text = urwid.Text('', align='left')
self.right_text = urwid.Text('', align='left')
c = urwid.Columns((self.left_text, self.right_text))
pile = urwid.Pile([self.title, urwid.Divider(' '), c])
self.app_box = urwid.LineBox(urwid.Filler(pile, 'top'))
self.app_but = urwid.Padding(urwid.Button('Show'), 'center', 8)
super().__init__(self.app_box, footer=self.app_but, focus_part='footer')
urwid.connect_signal(self.app_but.original_widget, 'click', self.button_on_click)
self.subs = []
self.get_subs()
def get_subs(self):
sub_id = None if self.random else self.sub_id
self.sub_id, self.subs = self.db.get_subs(sub_id)
if self.subs:
text = '\n'.join([s.content for s in self.subs[0]])
self.left_text.set_text(text)
self.right_text.set_text('')
sub_title = '{} ({}, {})'.format(
self.db.data[self.sub_id]['subs'][0]['MovieName'],
self.db.data[self.sub_id]['subs'][0]['SubLanguageID'],
self.db.data[self.sub_id]['subs'][1]['SubLanguageID']
)
self.title.set_text(sub_title)
def button_on_click(self, button):
if self.state == 'show':
text = '\n'.join([s.content for s in self.subs[1]])
self.right_text.set_text(text)
self.app_but.original_widget.set_label('Next')
self.state = 'next'
else:
self.get_subs()
self.app_but.original_widget.set_label('Show')
self.state = 'show'
def get_sub_id(self):
return self.sub_id
class SearchBox(urwid.Frame):
"""Frame to search subtitles."""
def __init__(self, db):
self.db = db
self.url = urwid.Edit('URL: ')
self.lang1 = urwid.Edit('Lang #1: ')
self.lang2 = urwid.Edit('Lang #2: ')
self.log = urwid.Text('')
s = [
urwid.LineBox(self.url),
urwid.LineBox(self.lang1),
urwid.LineBox(self.lang2),
self.log
]
self.subs = urwid.ListBox(urwid.SimpleFocusListWalker(s))
self.app_box = urwid.LineBox(self.subs)
self.app_but = urwid.Padding(urwid.Button('Search'), 'center', 10)
super().__init__(self.app_box, footer=self.app_but, focus_part='body')
def keypress(self, size, key):
if key == 'down' and self.get_focus_path() == ['body', 2]:
self.focus_position = 'footer'
elif key == 'up' and self.focus_position == 'footer':
self.set_focus_path(['body', 2])
elif key == 'enter' and self.focus_position == 'footer':
self.log.set_text('')
url = self.url.get_edit_text()
lang1 = self.lang1.get_edit_text()
lang2 = self.lang2.get_edit_text()
if url and lang1 and lang2:
self.db.download(url, lang1, lang2)
else:
return self.focus.keypress(size, key)
def get_sub_id(self):
return None
class SubsListBox(urwid.Frame):
"""Frame to show a list of subtitles."""
def __init__(self, db, top_frame):
self.db = db
self.top_frame = top_frame
self.subs_list = list(self.db.data.items())
s = [urwid.CheckBox(self.sub_format(x[1])) for x in self.subs_list]
self.subs = urwid.ListBox(urwid.SimpleFocusListWalker(s))
self.app_box = urwid.LineBox(self.subs)
self.app_but = urwid.Padding(urwid.Button('Delete'), 'center', 10)
super().__init__(self.app_box, footer=self.app_but, focus_part='footer')
def sub_format(self, sub):
return '{} ({}, {})'.format(
sub['subs'][0]['MovieName'],
sub['subs'][0]['SubLanguageID'],
sub['subs'][1]['SubLanguageID'],
)
def keypress(self, size, key):
if key == 'down' and self.get_focus_path() == ['body', len(self.subs.body)-1]:
self.focus_position = 'footer'
elif key == 'up' and self.focus_position == 'footer' and self.subs.body:
self.set_focus_path(['body', 0])
elif key == 'enter' and self.focus_position == 'footer':
self.delete_subs()
self.__init__(self.db, self.top_frame)
self.focus_position = 'body'
self.focus_position = 'footer'
elif key == 'enter' and self.focus_position == 'body':
idx = self.get_focus_path()[1] # ['body', 0]
sub_id = self.subs_list[idx][0]
self.top_frame.set_show_mode(None, sub_id)
else:
return self.focus.keypress(size, key)
def delete_subs(self):
# import ipdb; ipdb.set_trace()
for i, e in enumerate(self.subs.body):
if e.get_state():
self.db.delete(self.subs_list[i][0])
def get_sub_id(self):
return None
class SubsAlignBox(urwid.Frame):
"""Frame to align subtitles."""
def __init__(self, db, top_frame, sub_id):
self.db = db
self.top_frame = top_frame
self.subs_id = sub_id
self.subs = self.db.get_subs_to_align(sub_id, SUBS_CNT_FOR_ALIGN)
bg_lt = []
bg_rt = []
bg_lb = []
bg_rb = []
self.left_top = [urwid.RadioButton(bg_lt, x.content) for x in self.subs[0]]
self.right_top = [urwid.RadioButton(bg_rt, x.content) for x in self.subs[1]]
self.left_bot = [urwid.RadioButton(bg_lb, x.content) for x in self.subs[2]]
self.right_bot = [urwid.RadioButton(bg_rb, x.content) for x in self.subs[3]]
left_top_box = urwid.ListBox(urwid.SimpleFocusListWalker(self.left_top))
right_top_box = urwid.ListBox(urwid.SimpleFocusListWalker(self.right_top))
left_bot_box = urwid.ListBox(urwid.SimpleFocusListWalker(self.left_bot))
right_bot_box = urwid.ListBox(urwid.SimpleFocusListWalker(self.right_bot))
c_top = urwid.Columns([left_top_box, right_top_box])
c_bot = urwid.Columns([left_bot_box, right_bot_box])
p = urwid.Pile([c_top, urwid.Filler(urwid.Divider('-'), 'middle'), c_bot])
self.app_box = urwid.LineBox(p)
self.app_but = urwid.Padding(urwid.Button('Align'), 'center', 10)
super().__init__(self.app_box, footer=self.app_but, focus_part='body')
def sub_format(self, sub):
return '{} ({}, {})'.format(
sub['subs'][0]['MovieName'],
sub['subs'][0]['SubLanguageID'],
sub['subs'][1]['SubLanguageID'],
)
def keypress(self, size, key):
if key == 'down' and self.get_focus_path() == ['body', 2, 0, len(self.subs[0])-1]:
self.focus_position = 'footer'
elif key == 'down' and self.get_focus_path() == ['body', 2, 1, len(self.subs[1])-1]:
self.focus_position = 'footer'
elif key == 'up' and self.focus_position == 'footer':
self.focus_position = 'body'
elif key == 'enter' and self.focus_position == 'footer':
self.db.align_subs(self.subs_id,
self.subs[0][self._find_rbutton(self.left_top)].index,
self.subs[1][self._find_rbutton(self.right_top)].index,
self.subs[2][self._find_rbutton(self.left_bot)].index,
self.subs[3][self._find_rbutton(self.right_bot)].index)
self.top_frame.set_show_mode(None, self.subs_id)
else:
return self.focus.keypress(size, key)
def _find_rbutton(self, a):
for e in enumerate(a):
if e[1].state is True:
return e[0]
def get_sub_id(self):
return None
class CtrlButtons(urwid.Columns):
def __init__(self):
self.home_but = urwid.Button('Home')
self.align_but = urwid.Button('Align')
self.list_but = urwid.Button('List')
self.search_but = urwid.Button('Search')
super().__init__((
urwid.Padding(self.home_but, 'center', 8),
urwid.Padding(self.align_but, 'center', 9),
urwid.Padding(self.list_but, 'center', 8),
urwid.Padding(self.search_but, 'center', 10),
))
class TopFrame(urwid.Frame):
"""Top application frame."""
def __init__(self, db, *args, **kwargs):
super().__init__(*args, **kwargs)
self.db = db
self.search_box = SearchBox(self.db)
# self.app_box = AppBox()
urwid.connect_signal(self.contents['footer'][0].search_but, 'click', self.set_search_mode)
urwid.connect_signal(self.contents['footer'][0].home_but, 'click', self.set_show_mode)
urwid.connect_signal(self.contents['footer'][0].list_but, 'click', self.set_list_mode)
urwid.connect_signal(self.contents['footer'][0].align_but, 'click', self.set_align_mode)
def keypress(self, size, key):
if key == 'up' and self.focus_position == 'footer':
self.focus_position = 'body'
elif key == 'down' and self.get_focus_path() == ['body', 'footer']:
self.focus_position = 'footer'
else:
return self.focus.keypress(size, key)
def set_search_mode(self, button):
body = self.search_box
body.log.set_text('')
self.contents['body'] = (body, body.options())
def set_show_mode(self, button, sub_id=None):
body = AppBox(self.db, sub_id)
self.contents['body'] = (body, body.options())
self.focus_position = 'body'
def set_list_mode(self, button):
body = SubsListBox(self.db, self)
self.contents['body'] = (body, body.options())
def set_align_mode(self, button):
sub_id = self.contents['body'][0].get_sub_id()
if sub_id:
body = SubsAlignBox(self.db, self, sub_id)
self.contents['body'] = (body, body.options())
class App:
"""Main application."""
def __init__(self, db):
self.db = db
self.top = TopFrame(self.db, AppBox(self.db), footer=CtrlButtons(), focus_part='footer')
self.loop = urwid.MainLoop(self.top)
def get_search_box(self):
return self.top.search_box.log
def get_loop(self):
return self.loop
def run(self):
self.loop.run()