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.

gui.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # This is part of Kaylee
  2. # -- this code is licensed GPLv3
  3. # Copyright 2015-2016 Clayton G. Hobbs
  4. # Portions Copyright 2013 Jezra
  5. import sys
  6. import gi
  7. from gi.repository import GObject
  8. # Gtk
  9. gi.require_version('Gtk', '3.0')
  10. from gi.repository import Gtk, Gdk
  11. class GTKTrayInterface(GObject.GObject):
  12. __gsignals__ = {
  13. 'command' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
  14. }
  15. idle_text = "Kaylee - Idle"
  16. listening_text = "Kaylee - Listening"
  17. def __init__(self, args, continuous):
  18. GObject.GObject.__init__(self)
  19. self.continuous = continuous
  20. self.statusicon = Gtk.StatusIcon()
  21. self.statusicon.set_title("Kaylee")
  22. self.statusicon.set_name("Kaylee")
  23. self.statusicon.set_tooltip_text(self.idle_text)
  24. self.statusicon.set_has_tooltip(True)
  25. self.statusicon.connect("activate", self.continuous_toggle)
  26. self.statusicon.connect("popup-menu", self.popup_menu)
  27. self.menu = Gtk.Menu()
  28. self.menu_listen = Gtk.MenuItem('Listen')
  29. self.menu_continuous = Gtk.CheckMenuItem('Continuous')
  30. self.menu_quit = Gtk.MenuItem('Quit')
  31. self.menu.append(self.menu_listen)
  32. self.menu.append(self.menu_continuous)
  33. self.menu.append(self.menu_quit)
  34. self.menu_listen.connect("activate", self.toggle_listen)
  35. self.menu_continuous.connect("toggled", self.toggle_continuous)
  36. self.menu_quit.connect("activate", self.quit)
  37. self.menu.show_all()
  38. def continuous_toggle(self, item):
  39. checked = self.menu_continuous.get_active()
  40. self.menu_continuous.set_active(not checked)
  41. def toggle_continuous(self, item):
  42. checked = self.menu_continuous.get_active()
  43. self.menu_listen.set_sensitive(not checked)
  44. if checked:
  45. self.menu_listen.set_label("Listen")
  46. self.emit('command', "continuous_listen")
  47. self.statusicon.set_tooltip_text(self.listening_text)
  48. self.set_icon_active()
  49. else:
  50. self.set_icon_inactive()
  51. self.statusicon.set_tooltip_text(self.idle_text)
  52. self.emit('command', "continuous_stop")
  53. def toggle_listen(self, item):
  54. val = self.menu_listen.get_label()
  55. if val == "Listen":
  56. self.set_icon_active()
  57. self.emit("command", "listen")
  58. self.menu_listen.set_label("Stop")
  59. self.statusicon.set_tooltip_text(self.listening_text)
  60. else:
  61. self.set_icon_inactive()
  62. self.menu_listen.set_label("Listen")
  63. self.emit("command", "stop")
  64. self.statusicon.set_tooltip_text(self.idle_text)
  65. def popup_menu(self, item, button, time):
  66. self.menu.popup(None, None, Gtk.StatusIcon.position_menu, item, button, time)
  67. def run(self):
  68. # Set the icon
  69. self.set_icon_inactive()
  70. if self.continuous:
  71. self.menu_continuous.set_active(True)
  72. self.set_icon_active()
  73. else:
  74. self.menu_continuous.set_active(False)
  75. self.statusicon.set_visible(True)
  76. def quit(self, item):
  77. self.statusicon.set_visible(False)
  78. self.emit("command", "quit")
  79. def finished(self, text):
  80. if not self.menu_continuous.get_active():
  81. self.menu_listen.set_label("Listen")
  82. self.set_icon_inactive()
  83. self.statusicon.set_tooltip_text(self.idle_text)
  84. def set_icon_active_asset(self, i):
  85. self.icon_active = i
  86. def set_icon_inactive_asset(self, i):
  87. self.icon_inactive = i
  88. def set_icon_active(self):
  89. self.statusicon.set_from_file(self.icon_active)
  90. def set_icon_inactive(self):
  91. self.statusicon.set_from_file(self.icon_inactive)
  92. class GTKInterface(GObject.GObject):
  93. __gsignals__ = {
  94. 'command' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
  95. }
  96. def __init__(self, args, continuous):
  97. GObject.GObject.__init__(self)
  98. self.continuous = continuous
  99. # Make a window
  100. self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
  101. self.window.connect("delete_event", self.delete_event)
  102. # Give the window a name
  103. self.window.set_title("Kaylee")
  104. self.window.set_resizable(False)
  105. layout = Gtk.VBox()
  106. self.window.add(layout)
  107. # Make a listen/stop button
  108. self.lsbutton = Gtk.Button("Listen")
  109. layout.add(self.lsbutton)
  110. # Make a continuous button
  111. self.ccheckbox = Gtk.CheckButton("Continuous Listen")
  112. layout.add(self.ccheckbox)
  113. # Connect the buttons
  114. self.lsbutton.connect("clicked", self.lsbutton_clicked)
  115. self.ccheckbox.connect("clicked", self.ccheckbox_clicked)
  116. # Add a label to the UI to display the last command
  117. self.label = Gtk.Label()
  118. layout.add(self.label)
  119. # Create an accellerator group for this window
  120. accel = Gtk.AccelGroup()
  121. # Add the ctrl+q to quit
  122. accel.connect(Gdk.keyval_from_name('q'), Gdk.ModifierType.CONTROL_MASK,
  123. Gtk.AccelFlags.VISIBLE, self.accel_quit)
  124. # Lock the group
  125. accel.lock()
  126. # Add the group to the window
  127. self.window.add_accel_group(accel)
  128. def ccheckbox_clicked(self, widget):
  129. checked = self.ccheckbox.get_active()
  130. self.lsbutton.set_sensitive(not checked)
  131. if checked:
  132. self.lsbutton_stopped()
  133. self.emit('command', "continuous_listen")
  134. self.set_icon_active()
  135. else:
  136. self.emit('command', "continuous_stop")
  137. self.set_icon_inactive()
  138. def lsbutton_stopped(self):
  139. self.lsbutton.set_label("Listen")
  140. def lsbutton_clicked(self, button):
  141. val = self.lsbutton.get_label()
  142. if val == "Listen":
  143. self.emit("command", "listen")
  144. self.lsbutton.set_label("Stop")
  145. # Clear the label
  146. self.label.set_text("")
  147. self.set_icon_active()
  148. else:
  149. self.lsbutton_stopped()
  150. self.emit("command", "stop")
  151. self.set_icon_inactive()
  152. def run(self):
  153. # Set the default icon
  154. self.set_icon_inactive()
  155. self.window.show_all()
  156. if self.continuous:
  157. self.set_icon_active()
  158. self.ccheckbox.set_active(True)
  159. def accel_quit(self, accel_group, acceleratable, keyval, modifier):
  160. self.emit("command", "quit")
  161. def delete_event(self, x, y):
  162. self.emit("command", "quit")
  163. def finished(self, text):
  164. # If the continuous isn't pressed
  165. if not self.ccheckbox.get_active():
  166. self.lsbutton_stopped()
  167. self.set_icon_inactive()
  168. self.label.set_text(text)
  169. def set_icon_active_asset(self, i):
  170. self.icon_active = i
  171. def set_icon_inactive_asset(self, i):
  172. self.icon_inactive = i
  173. def set_icon_active(self):
  174. Gtk.Window.set_default_icon_from_file(self.icon_active)
  175. def set_icon_inactive(self):
  176. Gtk.Window.set_default_icon_from_file(self.icon_inactive)