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

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