Hack the Play button to have the right cursor
[reinteract/rox.git] / lib / replay.py
blobf8f43b4931b2414c9c8bdaf58a517f56b0289b49
1 import gtk
2 import os
4 from numpy import float32, float64
6 from reinteract.custom_result import CustomResult
8 class PlayResult(CustomResult):
9 def __init__(self, data):
10 self.__data = data
12 def create_widget(self):
13 widget = gtk.Button("Play")
14 widget.connect('clicked', self.play)
16 widget.connect('button_press_event', self.on_button_press)
17 widget.connect('realize', self.on_realize)
19 return widget
21 def play(self, *args):
22 if self.__data.dtype == float32:
23 command = "play -t raw -r 44100 -f -4 -L -q -"
24 else:
25 command = "play -t raw -r 44100 -f -8 -L -q -"
27 f = os.popen(command, 'w')
28 self.__data.tofile(f)
29 f.close()
31 def __save(self, filename):
32 escaped = filename.replace("'", r"'\''")
33 print repr(escaped)
35 if self.__data.dtype == float32:
36 command = "sox -t raw -r 44100 -f -4 -L -q - '%s'" % escaped
37 else:
38 command = "sox -t raw -r 44100 -f -8 -L -q - '%s'" % escaped
40 f = os.popen(command, 'w')
41 self.__data.tofile(f)
42 f.close()
44 def on_button_press(self, button, event):
45 if event.button == 3:
46 toplevel = button.get_toplevel()
48 menu = gtk.Menu()
49 menu_item = gtk.ImageMenuItem(stock_id=gtk.STOCK_SAVE_AS)
50 menu_item.show()
51 menu.add(menu_item)
53 def on_selection_done(menu):
54 menu.destroy()
55 menu.connect('selection-done', on_selection_done)
57 def on_activate(menu):
58 chooser = gtk.FileChooserDialog("Save As...", toplevel, gtk.FILE_CHOOSER_ACTION_SAVE,
59 (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
60 gtk.STOCK_SAVE, gtk.RESPONSE_OK))
61 chooser.set_default_response(gtk.RESPONSE_OK)
62 response = chooser.run()
63 filename = None
64 if response == gtk.RESPONSE_OK:
65 filename = chooser.get_filename()
67 chooser.destroy()
69 if filename != None:
70 self.__save(filename)
72 menu_item.connect('activate', on_activate)
74 menu.popup(None, None, None, event.button, event.time)
76 return True
77 return False
79 def on_realize(self, button):
80 # Hack to get the right cursor over the button, since the button
81 # doesn't set a cursor itself. button.window is the text view's
82 # window, we have to search to find button.event_window, since
83 # its not bound
84 for c in button.window.get_children():
85 if c.get_user_data() == button:
86 cursor = gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)
87 c.set_cursor(cursor)
89 def play(data):
90 if data.dtype != float32 and data.dtype != float64:
91 raise TypeError("Data must be float32 or float64, not %s", data.dtype)
93 return PlayResult(data)