From f8c64e0f7863d8618887b6af4f61ca438b55b046 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Thu, 21 Apr 2011 08:14:21 -0400 Subject: [PATCH] ocarina: Added random button I also added code for creating toggle buttons easily. --- ocarina/body/button.py | 27 +++++++++++++++++++++++++++ ocarina/body/header/__init__.py | 6 ++++-- ocarina/components/button.py | 15 --------------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/ocarina/body/button.py b/ocarina/body/button.py index c7fed5a5..62e2568a 100644 --- a/ocarina/body/button.py +++ b/ocarina/body/button.py @@ -8,11 +8,19 @@ SIZE = gtk.ICON_SIZE_MENU def on_click(button, func): func() +def on_toggle(button, func): + func(button.get_active()) + def stock_image(stock_item): img = gtk.image_new_from_stock(stock_item, SIZE) img.show() return img +def file_image(file): + img = gtk.image_new_from_file(file) + img.show() + return img + def make_text(text): lbl = gtk.Label(text) lbl.show() @@ -33,6 +41,22 @@ def make_button(stock_item, func, tooltip, show): b.set_tooltip_text(tooltip) return b +def toggle_button(func, is_active, show): + b = gtk.ToggleButton() + b.set_relief(gtk.RELIEF_NONE) + b.set_active(is_active) + b.connect("toggled", on_toggle, func) + if show == True: + b.show() + return b + +def make_toggle(img_file, func, tooltip, is_active, show): + b = toggle_button(func, is_active, show) + img = file_image(img_file) + b.add(img) + b.set_tooltip_text(tooltip) + return b + def rewind_button(show): return make_button(gtk.STOCK_MEDIA_REWIND, controls.seek_backward, "Rewind", show) @@ -57,3 +81,6 @@ def goto_button(goto_func, show): def clear_button(clear_func, show): return make_button(gtk.STOCK_CLEAR, clear_func, "Clear Current Source", show) + +def random_button(is_active, show): + return make_toggle("images/random.png", controls.set_rand, "Random", is_active, show) diff --git a/ocarina/body/header/__init__.py b/ocarina/body/header/__init__.py index fc00e889..f1139d57 100644 --- a/ocarina/body/header/__init__.py +++ b/ocarina/body/header/__init__.py @@ -1,6 +1,7 @@ # Bryan Schumaker (11/25/2010) import gtk +import libsaria from ocarina.body import button import entry @@ -25,8 +26,9 @@ def add_button(name, button): header_body.pack_start(button, False, False) header_body.pack_start(entry.entry) -add_button( "GOTO", button.goto_button(goto_func, True)) -add_button("CLEAR", button.clear_button(clear_func, True)) +add_button( "GOTO", button.goto_button(goto_func, True)) +add_button( "CLEAR", button.clear_button(clear_func, True)) +add_button("RANDOM", button.random_button(libsaria.prefs.get_pref("libsaria.random"), True)) #import ocarina #from ocarina.components import button diff --git a/ocarina/components/button.py b/ocarina/components/button.py index 2a462e14..eaceb227 100644 --- a/ocarina/components/button.py +++ b/ocarina/components/button.py @@ -48,13 +48,6 @@ class SaveButton(Button): if loc != None: export.save_playlist(loc) -class ClearButton(Button): - def __init__(self): - Button.__init__(self, gtk.STOCK_CLEAR, "Clear Current Source") - def clicked(self, button): - from ocarina import body - body.cur_page_reset() - class ExportButton(Button): def __init__(self): Button.__init__(self, gtk.STOCK_CONVERT, "Export Library") @@ -162,11 +155,3 @@ class DislikeButton(ToggleButton): if status == True: status = None LS.controls.set_like(status) - -class RandomButton(ToggleButton): - def __init__(self): - ToggleButton.__init__(self, "images/random.png", "Random") - self.set_active(prefs.get_pref("libsaria.random")) - - def toggle(self, button): - LS.controls.set_rand(self.get_active())