From 6d7b08f921ba0f77f548c536272d6a35aa0a56b9 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Thu, 21 Oct 2010 08:47:10 -0400 Subject: [PATCH] Random - Made a random check button to toggle the random preference - Playlists pick a random song from what is visible --- libsaria/collection/__init__.py | 14 ++++++++++++-- libsaria/collection/collection.py | 7 +++++++ libsaria/collection/lens.py | 14 ++++++++++++++ ocarina/button.py | 17 +++++++++++++++++ ocarina/info.py | 8 ++++++-- 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/libsaria/collection/__init__.py b/libsaria/collection/__init__.py index 30a87ed7..7d9b9d6f 100644 --- a/libsaria/collection/__init__.py +++ b/libsaria/collection/__init__.py @@ -17,6 +17,13 @@ import lens library = lens.Library() playlist = lens.Playlist() +def init(): + libsaria.init_pref("random", False) +libsaria.event.invite("POSTINIT", init) + +def toggle_rand(): + libsaria.prefs["random"] = not libsaria.prefs["random"] + cur_lib_id = -1 def new_source(path, bg=True): @@ -94,10 +101,13 @@ def plist_filter(text): def plist_refresh(): return call("PLISTREFRESH") -def plist_next(arg): +def plist_next(arg=None): global playlist global cur_lib_id - next = playlist.next_id(cur_lib_id) + if libsaria.prefs["random"] == True: + next = playlist.random() + else: + next = playlist.next_id(cur_lib_id) if next != None: lib_play_id(next) libsaria.event.invite("POSTEOS", plist_next) diff --git a/libsaria/collection/collection.py b/libsaria/collection/collection.py index 396de61c..de1e1970 100644 --- a/libsaria/collection/collection.py +++ b/libsaria/collection/collection.py @@ -77,6 +77,12 @@ class Collection: return True return id in self.visible + def num_visible(self): + if self.filtered == True: + return len(self.visible) + else: + return self.size + def walk_tags(self): for tag in self.tag_tree.walk_forwards(): rec = self.records[tag[3]] @@ -118,6 +124,7 @@ class Collection: def insert_tags(self, artist, album, title, id): tags = self.tag_tree.insert([artist, album, title, id]) self.index.insert([artist, album, title], id) + self.size += 1 def insert_allocate(self, components, ref): t = ref.tag() diff --git a/libsaria/collection/lens.py b/libsaria/collection/lens.py index 17526ba1..516cc68d 100644 --- a/libsaria/collection/lens.py +++ b/libsaria/collection/lens.py @@ -3,6 +3,8 @@ import libsaria import collection +import random as rand + from libsaria import data save = data.save @@ -78,5 +80,17 @@ class Playlist(collection.Collection): return first return None + def random(self): + num = self.num_visible() + next_idx = rand.randint(0, num-1) + func = self.visible + if self.filtered == False: + func = self.walk_ids + for n in func(): + if next_idx == 0: + return n + next_idx -= 1 + + diff --git a/ocarina/button.py b/ocarina/button.py index 24f6ae0e..e5fe8b8f 100644 --- a/ocarina/button.py +++ b/ocarina/button.py @@ -55,6 +55,13 @@ class StopButton(Button): def clicked(self, button): LS.music.stop() + +class NextButton(Button): + def __init__(self): + Button.__init__(self, gtk.STOCK_MEDIA_NEXT) + def clicked(self, button): + LS.collection.plist_next() + class OpenButton(Button): def __init__(self): Button.__init__(self, gtk.STOCK_OPEN) @@ -62,6 +69,16 @@ class OpenButton(Button): from ocarina import fsselect fsselect.run_chooser2(LS.data.universal_open) +class RandomButton(gtk.CheckButton): + def __init__(self): + gtk.CheckButton.__init__(self, "Random") + self.set_active(prefs["random"]) + self.connect("toggled", self.toggle) + self.show() + + def toggle(self, button): + LS.collection.toggle_rand() + class UpButton(Button): def __init__(self, callback): diff --git a/ocarina/info.py b/ocarina/info.py index d0042376..fbb0d598 100644 --- a/ocarina/info.py +++ b/ocarina/info.py @@ -41,6 +41,8 @@ class FilterBar(Bar): self.pack(entry.FilterEntry(), True, True) self.pack(button.OpenButton()) + self.pack(button.RandomButton()) + self.pack(button.VolumeButton()) class InfoBar(Bar): @@ -57,7 +59,8 @@ class InfoBar(Bar): self.pack(button.PlayButton()) self.pack(button.PauseButton()) self.pack(button.StopButton()) - self.pack(button.VolumeButton()) + self.pack(button.NextButton()) + #self.pack(button.VolumeButton()) self.pack(button.UpButton(up_button)) libsaria.event.invite("POSTLOAD", self.change_title) @@ -82,7 +85,8 @@ class InfoTab(gtk.Notebook): hbox.pack_start(button.PlayButton()) hbox.pack_start(button.PauseButton()) hbox.pack_start(button.StopButton()) - hbox.pack_start(button.VolumeButton()) + hbox.pack_start(button.NextButton()) + #hbox.pack_start(button.VolumeButton()) hbox.pack_start(button.DownButton(down_button)) self.set_action_widget(hbox, gtk.PACK_END)