-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathscreen_library.py
executable file
·426 lines (378 loc) · 18.2 KB
/
screen_library.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# This file is part of pi-jukebox.
#
# pi-jukebox is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pi-jukebox 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with pi-jukebox. If not, see < http://www.gnu.org/licenses/ >.
#
# (C) 2015- by Mark Zwart, <[email protected]>
"""
=======================================================
**screen_library.py**: MPD Library browsing screen
=======================================================
"""
__author__ = 'Mark Zwart'
import sys, pygame
from pygame.locals import *
import time
import subprocess
import os
import glob
from gui_widgets import *
from pij_screen_navigation import *
from mpd_client import *
from settings import *
from screen_keyboard import *
from screen_settings import *
class LetterBrowser(ItemList):
""" The graphical control for selecting artists/albums/songs starting with a letter.
:param screen_rect: The screen rect where the library browser is drawn on.
"""
def __init__(self, surface):
ItemList.__init__(self, 'list_letters', surface, SCREEN_WIDTH - 52, 40, 52, SCREEN_HEIGHT - 45)
self.item_outline_visible = True
self.outline_visible = False
self.font_color = FIFTIES_GREEN
self.set_item_alignment(HOR_MID, VERT_MID)
self.list = []
class LibraryBrowser(ItemList):
""" The component that displays mpd library entries.
:param screen_rect: The screen rect where the library browser is drawn on.
"""
def __init__(self, surface):
ItemList.__init__(self, 'list_library', surface, 55, 42, SCREEN_WIDTH - 110, SCREEN_HEIGHT - 46)
self.outline_visible = False
self.item_outline_visible = True
self.font_color = FIFTIES_YELLOW
self.set_item_alignment(HOR_LEFT, VERT_MID)
def show_artists(self, search=None, only_start=True):
""" Displays all artists or based on the first letter or partial string match.
:param search: Search string, default = None
:param only_start: Boolean indicating whether the search string only matches the first letters, default = True
"""
updated = False
if self.list != mpd.artists_get(search, only_start):
self.list = mpd.artists_get(search, only_start)
updated = True
if updated:
self.page_showing_index = 0
self.draw()
def show_albums(self, search=None, only_start=True):
""" Displays all albums or based on the first letter or partial string match.
:param search: Search string, default = None
:param only_start: Boolean indicating whether the search string only matches the first letters, default = True
"""
updated = False
if self.list != mpd.albums_get(search, only_start):
self.list = mpd.albums_get(search, only_start)
updated = True
if updated:
self.page_showing_index = 0
self.draw()
def show_songs(self, search=None, only_start=True):
""" Displays all songs or based on the first letter or partial string match.
:param search: Search string, default = None
:param only_start: Boolean indicating whether the search string only matches the first letters, default = True
"""
updated = False
if self.list != mpd.songs_get(search, only_start):
self.list = mpd.songs_get(search, only_start)
updated = True
if updated:
self.page_showing_index = 0
self.draw()
def show_playlists(self, first_letter=None):
""" Displays all playlists or based on the first letter.
:param first_letter: Search string, default = None
"""
updated = False
if self.list != mpd.playlists_get(first_letter):
self.list = mpd.playlists_get(first_letter)
updated = True
if updated:
self.page_showing_index = 0
self.draw()
def first_letters_in_result_get(self):
""" Get's the symbols that are first letters of the items in the result list.
:return: List of letters
"""
output_set = set()
for item in self.list:
first_letter = item[:1].upper()
output_set.add(first_letter)
letter_list = list(output_set)
letter_list.sort(key=lambda item: (
[str, int].index(type(item)), item)) # Sorting, making sure letters are put before numbers
return letter_list
class ScreenLibrary(Screen):
""" The screen where the user can browse in the MPD database and playlist_add items to playlists.
:param screen_rect: The display's rect where the library browser is drawn on.
"""
def __init__(self, screen_rect):
Screen.__init__(self, screen_rect)
self.first_time_showing = True
# Screen navigation buttons
self.add_component(ScreenNavigation('screen_nav', self.surface, 'btn_library'))
# Library buttons
self.add_component(ButtonIcon('btn_artists', self.surface, ICO_SEARCH_ARTIST, 55, 5))
self.add_component(ButtonIcon('btn_albums', self.surface, ICO_SEARCH_ALBUM, 107, 5))
self.add_component(ButtonIcon('btn_songs', self.surface, ICO_SEARCH_SONG, 159, 5))
self.add_component(ButtonIcon('btn_playlists', self.surface, ICO_PLAYLISTS, 211, 5))
self.add_component(ButtonIcon('btn_search', self.surface, ICO_SEARCH, 263, 5))
# Lists
self.add_component(LibraryBrowser(self.surface))
self.add_component(LetterBrowser(self.surface))
self.currently_showing = 'artists'
def show(self):
self.components['screen_nav'].radio_mode_set(mpd.radio_mode_get())
if self.first_time_showing:
self.set_currently_showing('artists')
self.components['list_library'].show_artists()
self.letter_list_update()
self.first_time_showing = False
return super(ScreenLibrary, self).show()
def update(self):
self.components['screen_nav'].radio_mode_set(mpd.radio_mode_get())
def set_currently_showing(self, type_showing):
""" Switch icons to active dependent on which kind of searching is active.
:param type_showing: The type of search results showing [artists, albums, songs, playlists].
"""
self.currently_showing = type_showing
if type_showing == 'artists':
self.components['btn_artists'].set_image_file(ICO_SEARCH_ARTIST_ACTIVE)
self.components['btn_albums'].set_image_file(ICO_SEARCH_ALBUM)
self.components['btn_songs'].set_image_file(ICO_SEARCH_SONG)
self.components['btn_playlists'].set_image_file(ICO_PLAYLISTS)
elif type_showing == 'albums':
self.components['btn_artists'].set_image_file(ICO_SEARCH_ARTIST)
self.components['btn_albums'].set_image_file(ICO_SEARCH_ALBUM_ACTIVE)
self.components['btn_songs'].set_image_file(ICO_SEARCH_SONG)
self.components['btn_playlists'].set_image_file(ICO_PLAYLISTS)
elif type_showing == 'songs':
self.components['btn_artists'].set_image_file(ICO_SEARCH_ARTIST)
self.components['btn_albums'].set_image_file(ICO_SEARCH_ALBUM)
self.components['btn_songs'].set_image_file(ICO_SEARCH_SONG_ACTIVE)
self.components['btn_playlists'].set_image_file(ICO_PLAYLISTS)
elif type_showing == 'playlists':
self.components['btn_artists'].set_image_file(ICO_SEARCH_ARTIST)
self.components['btn_albums'].set_image_file(ICO_SEARCH_ALBUM)
self.components['btn_songs'].set_image_file(ICO_SEARCH_SONG)
self.components['btn_playlists'].set_image_file(ICO_PLAYLISTS_ACTIVE)
def letter_list_update(self):
self.components['list_letters'].list = self.components['list_library'].first_letters_in_result_get()
self.components['list_letters'].draw()
def find_first_letter(self):
""" Adjust current search type according to the letter clicked in the letter list. """
letter = self.components['list_letters'].item_selected_get()
if self.currently_showing == 'artists':
self.components['list_library'].show_artists(letter)
elif self.currently_showing == 'albums':
self.components['list_library'].show_albums(letter)
elif self.currently_showing == 'songs':
self.components['list_library'].show_songs(letter)
elif self.currently_showing == 'playlists':
self.components['list_library'].show_playlists(letter)
self.letter_list_update()
def find_text(self):
""" Find results according to part of the text.
Launching a keyboard so that the user can specify the search string.
"""
screen_search = ScreenSearch(self) # The search screen
screen_search.show()
search_text = screen_search.search_text # The text the user searches for
search_type = screen_search.search_type # The type of tag the user searches for (artist, album, song)
if search_type == 'artist':
self.components['list_library'].show_artists(search_text, False)
self.set_currently_showing('artists')
elif search_type == 'album':
self.components['list_library'].show_albums(search_text, False)
self.set_currently_showing('albums')
elif search_type == 'song':
self.components['list_library'].show_songs(search_text, False)
self.set_currently_showing('songs')
self.letter_list_update()
self.show()
def playlist_action(self):
""" Displays screen for follow-up actions when an item was selected from the library. """
selected = self.components['list_library'].item_selected_get()
select_screen = ScreenSelected(self, self.currently_showing, selected)
select_screen.outline_color = FIFTIES_YELLOW
select_screen.show()
if isinstance(select_screen.return_object, list):
self.components['list_library'].list = select_screen.return_object
self.components['list_library'].draw()
self.set_currently_showing(select_screen.return_type)
self.letter_list_update()
self.show()
def on_click(self, x, y):
""" Handles click event. """
tag_name = super(ScreenLibrary, self).on_click(x, y)
if tag_name == 'btn_player':
self.return_object = 0
self.close()
elif tag_name == 'btn_playlist':
self.return_object = 1
self.close()
elif tag_name == 'btn_library':
self.return_object = 2
self.close()
elif tag_name == 'btn_directory':
self.return_object = 3
self.close()
elif tag_name == 'btn_radio':
self.return_object = 4
self.close()
elif tag_name == 'btn_settings':
setting_screen = ScreenSettings(self)
setting_screen.show()
self.show()
elif tag_name == 'btn_artists':
self.set_currently_showing('artists')
self.components['list_library'].show_artists()
self.letter_list_update()
elif tag_name == 'btn_albums':
self.set_currently_showing('albums')
self.components['list_library'].show_albums()
self.letter_list_update()
elif tag_name == 'btn_songs':
self.set_currently_showing('songs')
self.components['list_library'].show_songs()
self.letter_list_update()
elif tag_name == 'btn_playlists':
self.set_currently_showing('playlists')
self.components['list_library'].show_playlists()
self.letter_list_update()
elif tag_name == 'btn_search':
self.find_text()
elif tag_name == 'list_letters':
self.find_first_letter()
elif tag_name == 'list_library':
self.playlist_action()
class ScreenSearch(ScreenModal):
""" Screen used further searching based on an item selected from the library
:param screen_rect: The display's rect where the library browser is drawn on.
:ivar search_type: Searching for... [artist, album, song].
:ivar search_text: Partial text which should be searched for
"""
def __init__(self, screen):
ScreenModal.__init__(self, screen, "Search library for...")
self.outline_color = FIFTIES_YELLOW
self.title_color = FIFTIES_YELLOW
self.search_type = ""
self.search_text = ""
self.initialize()
def initialize(self):
""" Set-up screen controls. """
button_left = self.window_x + 10
button_width = self.window_width - 2 * button_left
label = "Artists"
self.add_component(ButtonText('btn_artists', self.surface, button_left, 50, button_width, 32, label))
label = "Albums"
self.add_component(ButtonText('btn_albums', self.surface, button_left, 92, button_width, 32, label))
label = "Songs"
self.add_component(ButtonText('btn_songs', self.surface, button_left, 134, button_width, 32, label))
label = "Cancel"
self.add_component(ButtonText('btn_cancel', self.surface, button_left, 176, button_width, 32, label))
def on_click(self, x, y):
""" Action that should be performed on a click.
:param tag_name: The identifying tag_name of the clicked widget.
"""
tag_name = super(ScreenModal, self).on_click(x, y)
search_label = tag_name
if tag_name == 'btn_cancel':
self.close()
return
elif tag_name == 'btn_artists':
self.search_type = 'artist'
search_label = "Search artists"
elif tag_name == 'btn_albums':
self.search_type = 'album'
search_label = "Search albums"
elif tag_name == 'btn_songs':
self.search_type = 'song'
search_label = "Search songs"
# Open on-screen keyboard for entering search string
keyboard = Keyboard(self, search_label)
keyboard.title_color = FIFTIES_YELLOW
self.search_text = keyboard.show() # Get entered search text
self.close()
class ScreenSelected(ScreenModal):
""" Screen for selecting playback actions with an item selected from the library.
:param screen_rect: The display's rect where the library browser is drawn on.
:param selected_type: The selected library item [artists, albums, songs].
:param selected_title: The title of the selected library item.
"""
def __init__(self, screen, selected_type, selected_title):
ScreenModal.__init__(self, screen, selected_title)
self.type = selected_type
self.selected = selected_title
self.title_color = FIFTIES_YELLOW
self.initialize()
self.return_type = ""
def initialize(self):
""" Set-up screen controls. """
button_left = self.window_x + 10
button_width = self.window_width - 2 * button_left
label = "Add to playlist"
self.add_component(ButtonText('btn_add', self.surface, button_left, 30, button_width, 32, label))
self.components['btn_add'].button_color = FIFTIES_TEAL
label = "Add to playlist and play"
self.add_component(ButtonText('btn_add_play', self.surface, button_left, 72, button_width, 32, label))
self.components['btn_add_play'].button_color = FIFTIES_TEAL
label = "Replace playlist and play"
self.add_component(ButtonText('btn_replace', self.surface, button_left, 114, button_width, 32, label))
self.components['btn_replace'].button_color = FIFTIES_TEAL
if self.type == 'artists':
label = "Albums of " + self.title
self.add_component(
ButtonText('btn_artist_get_albums', self.surface, button_left, 156, button_width, 32, label))
label = "Songs of " + self.title
self.add_component(
ButtonText('btn_artist_get_songs', self.surface, button_left, 198, button_width, 32, label))
elif self.type == 'albums':
label = "Songs of " + self.title
self.add_component(
ButtonText('btn_album_get_songs', self.surface, button_left, 156, button_width, 32, label))
#label = "Cancel"
# self.add_component(ButtonText("btn_cancel", self.surface, button_left, 134, button_width, label))
def on_click(self, x, y):
""" Action that should be performed on a click. """
play = False
clear_playlist = False
tag_name = super(ScreenModal, self).on_click(x, y)
if tag_name == 'btn_add_play':
play = True
elif tag_name == 'btn_replace':
play = True
clear_playlist = True
if tag_name == 'btn_add' or tag_name == 'btn_add_play' or tag_name == 'btn_replace':
if self.type == 'artists':
mpd.playlist_add_artist(self.selected, play, clear_playlist)
elif self.type == 'albums':
mpd.playlist_add_album(self.selected, play, clear_playlist)
elif self.type == 'songs':
mpd.playlist_add_song(self.selected, play, clear_playlist)
elif self.type == 'playlists':
mpd.playlist_add_playlist(self.selected, play, clear_playlist)
self.return_object = None
elif tag_name == 'btn_artist_get_albums':
self.return_object = mpd.artist_albums_get(self.selected)
self.return_type = 'albums'
self.close()
elif tag_name == 'btn_artist_get_songs':
self.return_object = mpd.artist_songs_get(self.selected)
self.return_type = 'songs'
self.close()
elif tag_name == 'btn_album_get_songs':
self.return_object = mpd.album_songs_get(self.selected)
self.return_type = 'songs'
self.close()
self.close()