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.5KB

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