summary refs log tree commit diff
diff options
context:
space:
mode:
authorJezra <jezra@jezra.net>2013-04-18 21:14:43 -0700
committerJezra <jezra@jezra.net>2013-04-18 21:14:43 -0700
commitc61a0c311fa2a795f9369cf94988b0c7c7ff3973 (patch)
treeb37000199921d6539013a53caa9e301575a30071
parent73c3ca50ddd83e49952b93b70c8bf0e660b77f87 (diff)
Cleaner quiting for Qt, added Gtk UI
-rwxr-xr-xBlather.py49
-rw-r--r--GtkUI.py76
-rw-r--r--QtUI.py20
3 files changed, 118 insertions, 27 deletions
diff --git a/Blather.py b/Blather.py
index c9e4800..af36edb 100755
--- a/Blather.py
+++ b/Blather.py
@@ -18,12 +18,25 @@ if not os.path.exists(lang_dir):
 	os.makedirs(lang_dir)
 
 class Blather:
-	def __init__(self):
+	def __init__(self, args):
+		self.ui = None
 		self.continuous_listen = False
 		self.commands = {}
 		self.read_commands()
 		self.recognizer = Recognizer(lang_file, dic_file)
 		self.recognizer.connect('finished',self.recognizer_finished)
+		#is there an arg?
+		if len(args) > 1:
+			if args[1] == "-qt":
+				#import the ui from qt
+				from QtUI import UI
+			elif args[1] == "-gtk":
+				from GtkUI import UI
+			else:
+				print "no GUI defined"
+				sys.exit()
+			self.ui = UI(args)
+			self.ui.connect("command", self.process_command)
 
 	def read_commands(self):
 		#read the.commands file
@@ -60,25 +73,19 @@ class Blather:
 			#let the UI know that there is a finish
 			self.ui.finished(text)
 	
-	def run(self, args):
-		#TODO check for UI request
-		#is there an arg?
-		if len(args) > 1:
-			if args[1] == "-qt":
-				#import the ui from qt
-				from QtUI import UI
-			elif args[1] == "-gtk":
-				from GtkUI import UI
-			else:
-				print "no GUI defined"
-				sys.exit()
-			self.ui = UI(args)
-			self.ui.connect("command", self.process_command)
+	def run(self):
+		if self.ui:
 			self.ui.run()
 		else:
 			blather.recognizer.listen()	
 
+	def quit(self):
+		if self.ui:
+			self.ui.quit()
+		sys.exit()
+
 	def process_command(self, UI, command):
+		print command
 		if command == "listen":
 			self.recognizer.listen()
 		elif command == "stop":
@@ -89,20 +96,26 @@ class Blather:
 		elif command == "continuous_stop":
 			self.continuous_listen = False
 			self.recognizer.pause()
+		elif command == "quit":
+			self.quit()
 		
 if __name__ == "__main__":
 	#make our blather object
-	blather = Blather()
+	blather = Blather(sys.argv)
 	#init gobject threads
 	gobject.threads_init()
 	#we want a main loop
 	main_loop = gobject.MainLoop()
+	#handle sigint
+	signal.signal(signal.SIGINT, signal.SIG_DFL)
 	#run the blather
-	blather.run(sys.argv)
+	blather.run()
 	#start the main loop
+		
 	try:
 		main_loop.run()
 	except:
+		print "time to quit"
 		main_loop.quit()
 		sys.exit()
-	
+		
diff --git a/GtkUI.py b/GtkUI.py
new file mode 100644
index 0000000..6b33dd7
--- /dev/null
+++ b/GtkUI.py
@@ -0,0 +1,76 @@
+#!/usr/bin/env python2
+import sys
+import gobject
+#Gtk
+import pygtk
+import gtk
+
+class UI(gobject.GObject):
+	__gsignals__ = {
+		'command' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,))
+	}
+	
+	def __init__(self,args):
+		gobject.GObject.__init__(self)
+		#make a window
+		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
+		self.window.connect("delete_event", self.delete_event)
+		#give the window a name
+		self.window.set_title("BlatherGtk")
+		
+		layout = gtk.VBox()
+		self.window.add(layout)
+		#make a listen/stop button
+		self.lsbutton = gtk.Button("Listen")
+		layout.add(self.lsbutton)
+		#make a continuous button
+		self.ccheckbox = gtk.CheckButton("Continuous Listen")
+		layout.add(self.ccheckbox)
+		
+		#connect the buttonsc
+		self.lsbutton.connect("clicked",self.lsbutton_clicked)
+		self.ccheckbox.connect("clicked",self.ccheckbox_clicked)
+		
+		#add a label to the UI to display the last command
+		self.label = gtk.Label()
+		layout.add(self.label)
+	
+	def ccheckbox_clicked(self, widget):
+		checked = self.ccheckbox.get_active()
+		self.lsbutton.set_sensitive(not checked)
+		if checked:
+			self.lsbutton_stopped()
+			self.emit('command', "continuous_listen")
+		else:
+			self.emit('command', "continuous_stop")
+	
+	def lsbutton_stopped(self):
+		self.lsbutton.set_label("Listen")
+		
+	def lsbutton_clicked(self, button):
+		val = self.lsbutton.get_label()
+		if val == "Listen":
+			self.emit("command", "listen")
+			self.lsbutton.set_label("Stop")
+			#clear the label
+			self.label.set_text("")
+		else:
+			self.lsbutton_stopped()
+			self.emit("command", "stop")
+			
+	def run(self):
+		self.window.show_all()
+	
+	def quit(self):
+		pass
+	
+	def delete_event(self, x, y ):
+		self.emit("command", "quit")
+		
+	def finished(self, text):
+		print text
+		#if the continuous isn't pressed
+		if not self.ccheckbox.get_active():
+			self.lsbutton_stopped()
+		self.label.set_text(text)
+		
diff --git a/QtUI.py b/QtUI.py
index 032e93b..35e2c38 100644
--- a/QtUI.py
+++ b/QtUI.py
@@ -1,6 +1,5 @@
 #!/usr/bin/env python2
 import sys
-import signal
 import gobject
 # Qt stuff
 from PySide.QtCore import Signal, Qt
@@ -35,14 +34,11 @@ class UI(gobject.GObject):
 		#connect the buttonsc
 		self.lsbutton.clicked.connect(self.lsbutton_clicked)
 		self.ccheckbox.clicked.connect(self.ccheckbox_clicked)
+		
+		#add a label to the UI to display the last command
+		self.label = QLabel()
+		layout.addWidget(self.label)
 	
-	def recognizer_finished(self, x, y):
-		if self.ccheckbox.isChecked():
-			pass
-		else:
-			self.lsbutton_stopped()
-			
-			
 	def ccheckbox_clicked(self):
 		checked = self.ccheckbox.isChecked()
 		if checked:
@@ -59,10 +55,11 @@ class UI(gobject.GObject):
 		
 	def lsbutton_clicked(self):
 		val = self.lsbutton.text()
-		print val
 		if val == "Listen":
 			self.emit("command", "listen")
 			self.lsbutton.setText("Stop")
+			#clear the label
+			self.label.setText("")
 		else:
 			self.lsbutton_stopped()
 			self.emit("command", "stop")
@@ -70,12 +67,17 @@ class UI(gobject.GObject):
 	def run(self):
 		self.window.show()
 		self.app.exec_()
+		self.emit("command", "quit")
 	
+	def quit(self):
+		pass
+		
 	def finished(self, text):
 		print text
 		#if the continuous isn't pressed
 		if not self.ccheckbox.isChecked():
 			self.lsbutton_stopped()
+		self.label.setText(text)
 		
 	def quit(self):
 		#sys.exit()