Somewhat fancy voice command recognition software
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

GtkUI.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # This is part of Blather
  2. # -- this code is licensed GPLv3
  3. # Copyright 2013 Jezra
  4. import sys
  5. from gi.repository import GObject
  6. # Gtk
  7. from gi.repository import Gtk, Gdk
  8. class UI(GObject.GObject):
  9. __gsignals__ = {
  10. 'command' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
  11. }
  12. def __init__(self,args, continuous):
  13. GObject.GObject.__init__(self)
  14. self.continuous = continuous
  15. # Make a window
  16. self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
  17. self.window.connect("delete_event", self.delete_event)
  18. # Give the window a name
  19. self.window.set_title("BlatherGtk")
  20. self.window.set_resizable(False)
  21. layout = Gtk.VBox()
  22. self.window.add(layout)
  23. # Make a listen/stop button
  24. self.lsbutton = Gtk.Button("Listen")
  25. layout.add(self.lsbutton)
  26. # Make a continuous button
  27. self.ccheckbox = Gtk.CheckButton("Continuous Listen")
  28. layout.add(self.ccheckbox)
  29. # Connect the buttons
  30. self.lsbutton.connect("clicked",self.lsbutton_clicked)
  31. self.ccheckbox.connect("clicked",self.ccheckbox_clicked)
  32. # Add a label to the UI to display the last command
  33. self.label = Gtk.Label()
  34. layout.add(self.label)
  35. # Create an accellerator group for this window
  36. accel = Gtk.AccelGroup()
  37. # Add the ctrl+q to quit
  38. accel.connect(Gdk.keyval_from_name('q'), Gdk.ModifierType.CONTROL_MASK,
  39. Gtk.AccelFlags.VISIBLE, self.accel_quit)
  40. # Lock the group
  41. accel.lock()
  42. # Add the group to the window
  43. self.window.add_accel_group(accel)
  44. def ccheckbox_clicked(self, widget):
  45. checked = self.ccheckbox.get_active()
  46. self.lsbutton.set_sensitive(not checked)
  47. if checked:
  48. self.lsbutton_stopped()
  49. self.emit('command', "continuous_listen")
  50. self.set_icon_active()
  51. else:
  52. self.emit('command', "continuous_stop")
  53. self.set_icon_inactive()
  54. def lsbutton_stopped(self):
  55. self.lsbutton.set_label("Listen")
  56. def lsbutton_clicked(self, button):
  57. val = self.lsbutton.get_label()
  58. if val == "Listen":
  59. self.emit("command", "listen")
  60. self.lsbutton.set_label("Stop")
  61. # Clear the label
  62. self.label.set_text("")
  63. self.set_icon_active()
  64. else:
  65. self.lsbutton_stopped()
  66. self.emit("command", "stop")
  67. self.set_icon_inactive()
  68. def run(self):
  69. # Set the default icon
  70. self.set_icon_inactive()
  71. self.window.show_all()
  72. if self.continuous:
  73. self.set_icon_active()
  74. self.ccheckbox.set_active(True)
  75. def accel_quit(self, accel_group, acceleratable, keyval, modifier):
  76. self.emit("command", "quit")
  77. def delete_event(self, x, y):
  78. self.emit("command", "quit")
  79. def finished(self, text):
  80. # If the continuous isn't pressed
  81. if not self.ccheckbox.get_active():
  82. self.lsbutton_stopped()
  83. self.set_icon_inactive()
  84. self.label.set_text(text)
  85. def set_icon_active_asset(self, i):
  86. self.icon_active = i
  87. def set_icon_inactive_asset(self, i):
  88. self.icon_inactive = i
  89. def set_icon_active(self):
  90. Gtk.Window.set_default_icon_from_file(self.icon_active)
  91. def set_icon_inactive(self):
  92. Gtk.Window.set_default_icon_from_file(self.icon_inactive)