-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer_elements.py
391 lines (322 loc) · 16 KB
/
player_elements.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import sys,os
from curses_elements import *
from database import *
from config import *
class Tab:
def __init__(self, config_type, config_attr, stdscr, database, status_bar):
self.panes = []
self.pane_titles = []
self.stdscr = stdscr
self.isActive = False
self.attr_keys = [] #GENRE, ALBUM, PERFORMER, TRACKS (Given)
self.attr_values = [] #List of genres, List of albums, List of performers, "EMPTY" (Generated)
self.filtered_tracks = []
self.main_filter = [] #Main filter for the tab
self.exclude_filter = []
self.current_pane = 0
self.database = database
self.scrll_start = ui_config['SCRLL-TH']
self.window_dims = [None, None]
self.status_bar = status_bar
self.do_not_overwrite = False
self.selected_tracks = []
self.in_add_to_playlist_mode = False
self.height_offset = 2
if config_type == "SINGLE":
self.create_single_pane(config_attr)
elif config_type == "4-PANE":
self.create_4_pane(config_attr)
elif config_type == "3-PANE":
self.create_3_pane(config_attr)
elif config_type == "2-PANE":
self.create_2_pane(config_attr)
elif config_type == "PLAYLIST":
self.create_playlist_tab(config_attr)
else:
raise ValueError("Invalid config type")
def add_to_playlist_pane(self):
self.temp_vars = [self.attr_keys, self.attr_values, self.main_filter, self.exclude_filter, self.pane_titles]
self.temp_panes = self.panes
self.attr_keys = ["PLAYLIST"]
self.attr_values = ["Empty"]
self.main_filter = ["PLAYLISTS", None]
self.exclude_filter = [None, None]
self.pane_titles = ["Playlist"]
self.filtered_tracks = self.database.get_all_tracks_with("PLAYLISTS", None, None, None)
pane = List_Pane(self.window_dims[0], self.window_dims[1], #main window dims
self.window_dims[0] - self.height_offset, 30, #height and width of pane
0, 0, #y and x position of top left corner of pane
self.scrll_start, self.stdscr, self.pane_titles[0])
self.in_add_to_playlist_mode = True
pane.selectedpos = 0
pane.activate()
self.panes = [pane]
self.refresh_panes()
self.populate_lists()
#self.reset_tab()
def add_to_playlist(self):
if self.in_add_to_playlist_mode:
selected_file = self.attr_values[0][self.panes[0].selectedpos] + ".m3u"
with open(playlists_dir + "/" + selected_file, 'a') as f:
for path in self.selected_tracks:
f.write(path)
f.write('\n')
self.attr_keys, self.attr_values, self.main_filter, self.exclude_filter, self.pane_titles = self.temp_vars
self.panes = self.temp_panes
self.refresh_panes()
new_tracks = self.database.get_all_tracks_with("PATH", self.selected_tracks, None, None).copy()
for track in new_tracks:
track["PLAYLIST"] = selected_file[:-4]
self.database.playlists.playlist_dict_list.append(track.copy())
del new_tracks
self.in_add_to_playlist_mode = False
def create_2_pane(self, config_attr):
self.window_dims[0], self.window_dims[1], self.main_filter, self.exclude_filter, self.attr_keys, self.pane_titles = config_attr
self.filtered_tracks = self.database.get_all_tracks_with(self.main_filter[0], self.main_filter[1],
self.exclude_filter[0], self.exclude_filter[1])
pane1 = List_Pane(self.window_dims[0], self.window_dims[1], #main window dims
self.window_dims[0] - self.height_offset, 60, #height and width of pane
0, 0, #y and x position of top left corner of pane
self.scrll_start, self.stdscr, self.pane_titles[0])
pane2 = Track_Pane(self.window_dims[0], self.window_dims[1],
self.window_dims[0] - 2, self.window_dims[1] - 59,
0, 59,
self.scrll_start, self.stdscr)
pane1.activate()
self.panes = [pane1, pane2]
self.refresh_panes()
def create_playlist_tab(self, config_attr):
self.window_dims[0], self.window_dims[1], x, y, z, k = config_attr
self.attr_keys.append("PLAYLIST")
self.attr_values.append("Empty")
self.main_filter = ["PLAYLISTS", None]
self.exclude_filter = [None, None]
self.pane_titles = ["Playlist", "TRACK"]
self.filtered_tracks = self.database.get_all_tracks_with("PLAYLISTS", None, None, None)
pane1 = List_Pane(self.window_dims[0], self.window_dims[1], #main window dims
self.window_dims[0] - self.height_offset, 30, #height and width of pane
0, 0, #y and x position of top left corner of pane
self.scrll_start, self.stdscr, self.pane_titles[0])
pane2 = Track_Pane(self.window_dims[0], self.window_dims[1],
self.window_dims[0] - self.height_offset, self.window_dims[1] - 29,
0, 29,
self.scrll_start, self.stdscr)
pane1.activate()
self.panes = [pane1, pane2]
self.refresh_panes()
def create_4_pane(self, config_attr):
self.window_dims[0], self.window_dims[1], self.main_filter, self.exclude_filter, self.attr_keys, self.pane_titles = config_attr
self.filtered_tracks = self.database.get_all_tracks_with(self.main_filter[0], self.main_filter[1],
self.exclude_filter[0], self.exclude_filter[1])
pane1 = List_Pane(self.window_dims[0], self.window_dims[1], #main window dims
self.window_dims[0] - self.height_offset, 30, #height and width of pane
0, 0, #y and x position of top left corner of pane
self.scrll_start, self.stdscr, self.pane_titles[0])
pane2 = List_Pane(self.window_dims[0], self.window_dims[1],
self.window_dims[0] - self.height_offset, 60,
0, 29,
self.scrll_start, self.stdscr, self.pane_titles[1])
pane3 = List_Pane(self.window_dims[0], self.window_dims[1],
9, self.window_dims[1] - 88,
0, 88,
self.scrll_start, self.stdscr, self.pane_titles[2])
pane4 = Track_Pane(self.window_dims[0], self.window_dims[1],
self.window_dims[0] - 8 - self.height_offset, self.window_dims[1] - 88,
8, 88,
self.scrll_start, self.stdscr)
pane1.activate()
self.panes = [pane1, pane2, pane3, pane4]
self.refresh_panes()
def create_3_pane(self, config_attr):
self.window_dims[0], self.window_dims[1], self.main_filter, self.exclude_filter, self.attr_keys, self.pane_titles = config_attr
self.filtered_tracks = self.database.get_all_tracks_with(self.main_filter[0], self.main_filter[1],
self.exclude_filter[0], self.exclude_filter[1])
pane1 = List_Pane(self.window_dims[0], self.window_dims[1], #main window dims
self.window_dims[0] - self.height_offset, 30, #height and width of pane
0, 0, #y and x position of top left corner of pane
self.scrll_start, self.stdscr, self.pane_titles[0])
pane2 = List_Pane(self.window_dims[0], self.window_dims[1],
self.window_dims[0] - self.height_offset, 60,
0, 29,
self.scrll_start, self.stdscr, self.pane_titles[1])
pane3 = Track_Pane(self.window_dims[0], self.window_dims[1],
self.window_dims[0] - self.height_offset, self.window_dims[1] - 88,
0, 88,
self.scrll_start, self.stdscr)
pane1.activate()
self.panes = [pane1, pane2, pane3]
self.refresh_panes()
def create_single_pane(self, config_attr):
self.window_dims[0], self.window_dims[1], self.main_filter, self.exclude_filter = [x for x in config_attr if x is not None]
self.filtered_tracks = self.database.get_all_tracks_with(self.main_filter[0], self.main_filter[1],
self.exclude_filter[0], self.exclude_filter[1])
self.attr_keys.append("TRACK")
self.attr_values.append("Empty")
pane = Track_Pane(self.window_dims[0], self.window_dims[1], self.window_dims[0] - self.height_offset, self.window_dims[1], 0, 0, self.scrll_start, self.stdscr)
pane.refresh()
pane.activate()
self.panes.append(pane)
def populate_lists(self):
#this func must be run at least once in the beginning
self.load_all_tracks()
attr_values_tmp = []
for key, pane in zip(self.attr_keys, self.panes):
if not key == "TRACK" and not pane.selectedpos == None:
tmp_list = get_unique_attributes(self.filtered_tracks, key)
attr_values_tmp.append(tmp_list)
self.filtered_tracks = get_tracks_with(self.filtered_tracks, key, [tmp_list[pane.selectedpos]])
if not key == "TRACK" and pane.selectedpos == None:
tmp_list = get_unique_attributes(self.filtered_tracks, key)
attr_values_tmp.append(tmp_list)
attr_values_tmp.append("Empty") #insert empty string in last entry for tracks in next loop
self.attr_values = attr_values_tmp
def render_all_panes(self, full_render=False):
if self.isActive:
if full_render:
self.populate_lists()
for pane, lst in zip(self.panes, self.attr_values):
if pane.is_track_pane():
pane.render(self.filtered_tracks, self.selected_tracks)
else:
pane.render(lst)
def activate_tab(self):
self.filtered_tracks = self.database.get_all_tracks_with(self.main_filter[0], self.main_filter[1],
self.exclude_filter[0], self.exclude_filter[1])
self.isActive = True
def deactivate_tab(self):
self.isActive = False
del self.filtered_tracks
def load_all_tracks(self):
if self.isActive and not self.do_not_overwrite:
self.filtered_tracks = self.database.get_all_tracks_with(self.main_filter[0], self.main_filter[1], self.exclude_filter[0], self.exclude_filter[1])
def move_up(self):
if self.isActive:
for pane in self.panes:
pane.move_up_one()
def move_down(self):
if self.isActive:
for pane, l in zip(self.panes, self.attr_values):
if not pane.is_track_pane():
pane.move_down_one(len(l))
else:
pane.move_down_one(len(self.filtered_tracks))
def skip_down_pane(self):
if self.isActive:
for pane, l in zip(self.panes, self.attr_values):
if not pane.is_track_pane():
for i in range(0, 10):
self.move_down()
self.render_all_panes(False)
else:
for i in range(0, 10):
self.move_down()
self.render_all_panes(False)
def skip_up_pane(self):
if self.isActive:
for pane in self.panes:
for i in range(0, 10):
self.move_up()
self.render_all_panes(False)
def move_left(self):
if not self.current_pane == 0 and self.isActive and not self.do_not_overwrite:
self.panes[self.current_pane].deactivate()
self.panes[self.current_pane].stop_showing_position()
self.panes[self.current_pane].clear()
self.current_pane -= 1
self.panes[self.current_pane].activate()
def move_right(self):
if not self.current_pane == len(self.panes) - 1 and self.isActive:
self.panes[self.current_pane].deactivate()
self.current_pane += 1
self.panes[self.current_pane].activate()
def jump_to_track_pane(self):
if self.isActive:
self.panes[self.current_pane].deactivate()
self.current_pane = len(self.panes) - 1
self.panes[len(self.panes) - 1].activate()
def refresh_panes(self):
if self.isActive:
for pane in self.panes:
pane.refresh()
def clear_panes(self):
for pane in self.panes:
pane.clear()
def reset_tab(self):
if self.isActive:
self.do_not_overwrite = False
self.load_all_tracks()
self.selected_tracks = []
self.render_all_panes(True)
self.refresh_panes()
def select_track(self):
if self.isActive:
if self.panes[self.current_pane].is_track_pane():
path = self.filtered_tracks[self.panes[self.current_pane].selectedpos]["PATH"]
if path in self.selected_tracks:
self.selected_tracks.remove(path)
self.move_down()
else:
self.selected_tracks.append(path)
self.move_down()
else:
self.selected_tracks += [track["PATH"] for track in self.filtered_tracks]
def remove_tracks(self):
if self.isActive:
curses.cbreak()
k = 100
self.status_bar.set_bar_string("Are you sure? (y/n)")
self.status_bar.render_bar()
k = self.stdscr.getch()
if k == ord('y'):
self.database.delete_tracks_from_database(self.selected_tracks)
curses.halfdelay(15)
self.status_bar.set_bar_string("DEFAULT")
self.status_bar.render_bar()
def search_mode(self):
if self.isActive:
curses.cbreak()
k = 100
self.status_bar.set_bar_string("find: ")
self.status_bar.render_bar()
self.jump_to_track_pane()
while (k != 27):
self.render_all_panes(False)
self.refresh_panes()
k = self.stdscr.getch()
if k == ord('\n'):
self.do_not_overwrite = True
#Remember at this point, current pane is the track pane
self.panes[self.current_pane].clear() #to ensure the selected track is within the bounds of the found tracks
for pane in self.panes:
if not pane.is_track_pane() and pane.can_show_position:
pane.reset_pos()
break
elif k != ord('ć'):
self.status_bar.set_bar_string(self.status_bar.get_bar_string() + str(chr(k)))
elif len(self.status_bar.get_bar_string()) > 6:
self.status_bar.set_bar_string(self.status_bar.get_bar_string()[:-1])
self.load_all_tracks()
self.status_bar.render_bar()
self.filtered_tracks = get_tracks_with(self.filtered_tracks, "TITLE", [self.status_bar.get_bar_string()[6:]])
curses.halfdelay(15)
self.status_bar.render_bar()
def play_track(self):
if self.isActive:
client = start_mpd_client()
subprocess.call(["mpc", "clear"], stdout=subprocess.PIPE)
try:
for track in self.filtered_tracks:
#subprocess.call(["mpc", "add", track["PATH"]], stdout=subprocess.PIPE)
client.add(track["PATH"])
#subprocess.call(["mpc", "play",
# str(self.panes[len(self.panes) - 1].selectedpos + 1)], stdout=subprocess.PIPE)
client.play(self.panes[len(self.panes) - 1].selectedpos)
except CommandError as e:
height, width = self.stdscr.getmaxyx()
self.status_bar.set_bar_string("MPD Error: " + str(e))
stop_mpd_client(client)
def toggle_random(self):
if self.isActive:
client = start_mpd_client()
client.random(int(not bool(int(client.status()["random"]))))
stop_mpd_client(client)