Browse Source

Cleaner quiting for Qt, added Gtk UI

Jezra 11 years ago
parent
commit
c61a0c311f
3 changed files with 118 additions and 27 deletions
  1. 31
    18
      Blather.py
  2. 76
    0
      GtkUI.py
  3. 11
    9
      QtUI.py

+ 31
- 18
Blather.py View File

18
 	os.makedirs(lang_dir)
18
 	os.makedirs(lang_dir)
19
 
19
 
20
 class Blather:
20
 class Blather:
21
-	def __init__(self):
21
+	def __init__(self, args):
22
+		self.ui = None
22
 		self.continuous_listen = False
23
 		self.continuous_listen = False
23
 		self.commands = {}
24
 		self.commands = {}
24
 		self.read_commands()
25
 		self.read_commands()
25
 		self.recognizer = Recognizer(lang_file, dic_file)
26
 		self.recognizer = Recognizer(lang_file, dic_file)
26
 		self.recognizer.connect('finished',self.recognizer_finished)
27
 		self.recognizer.connect('finished',self.recognizer_finished)
28
+		#is there an arg?
29
+		if len(args) > 1:
30
+			if args[1] == "-qt":
31
+				#import the ui from qt
32
+				from QtUI import UI
33
+			elif args[1] == "-gtk":
34
+				from GtkUI import UI
35
+			else:
36
+				print "no GUI defined"
37
+				sys.exit()
38
+			self.ui = UI(args)
39
+			self.ui.connect("command", self.process_command)
27
 
40
 
28
 	def read_commands(self):
41
 	def read_commands(self):
29
 		#read the.commands file
42
 		#read the.commands file
60
 			#let the UI know that there is a finish
73
 			#let the UI know that there is a finish
61
 			self.ui.finished(text)
74
 			self.ui.finished(text)
62
 	
75
 	
63
-	def run(self, args):
64
-		#TODO check for UI request
65
-		#is there an arg?
66
-		if len(args) > 1:
67
-			if args[1] == "-qt":
68
-				#import the ui from qt
69
-				from QtUI import UI
70
-			elif args[1] == "-gtk":
71
-				from GtkUI import UI
72
-			else:
73
-				print "no GUI defined"
74
-				sys.exit()
75
-			self.ui = UI(args)
76
-			self.ui.connect("command", self.process_command)
76
+	def run(self):
77
+		if self.ui:
77
 			self.ui.run()
78
 			self.ui.run()
78
 		else:
79
 		else:
79
 			blather.recognizer.listen()	
80
 			blather.recognizer.listen()	
80
 
81
 
82
+	def quit(self):
83
+		if self.ui:
84
+			self.ui.quit()
85
+		sys.exit()
86
+
81
 	def process_command(self, UI, command):
87
 	def process_command(self, UI, command):
88
+		print command
82
 		if command == "listen":
89
 		if command == "listen":
83
 			self.recognizer.listen()
90
 			self.recognizer.listen()
84
 		elif command == "stop":
91
 		elif command == "stop":
89
 		elif command == "continuous_stop":
96
 		elif command == "continuous_stop":
90
 			self.continuous_listen = False
97
 			self.continuous_listen = False
91
 			self.recognizer.pause()
98
 			self.recognizer.pause()
99
+		elif command == "quit":
100
+			self.quit()
92
 		
101
 		
93
 if __name__ == "__main__":
102
 if __name__ == "__main__":
94
 	#make our blather object
103
 	#make our blather object
95
-	blather = Blather()
104
+	blather = Blather(sys.argv)
96
 	#init gobject threads
105
 	#init gobject threads
97
 	gobject.threads_init()
106
 	gobject.threads_init()
98
 	#we want a main loop
107
 	#we want a main loop
99
 	main_loop = gobject.MainLoop()
108
 	main_loop = gobject.MainLoop()
109
+	#handle sigint
110
+	signal.signal(signal.SIGINT, signal.SIG_DFL)
100
 	#run the blather
111
 	#run the blather
101
-	blather.run(sys.argv)
112
+	blather.run()
102
 	#start the main loop
113
 	#start the main loop
114
+		
103
 	try:
115
 	try:
104
 		main_loop.run()
116
 		main_loop.run()
105
 	except:
117
 	except:
118
+		print "time to quit"
106
 		main_loop.quit()
119
 		main_loop.quit()
107
 		sys.exit()
120
 		sys.exit()
108
-	
121
+		

+ 76
- 0
GtkUI.py View File

1
+#!/usr/bin/env python2
2
+import sys
3
+import gobject
4
+#Gtk
5
+import pygtk
6
+import gtk
7
+
8
+class UI(gobject.GObject):
9
+	__gsignals__ = {
10
+		'command' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,))
11
+	}
12
+	
13
+	def __init__(self,args):
14
+		gobject.GObject.__init__(self)
15
+		#make a window
16
+		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
17
+		self.window.connect("delete_event", self.delete_event)
18
+		#give the window a name
19
+		self.window.set_title("BlatherGtk")
20
+		
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
+		
30
+		#connect the buttonsc
31
+		self.lsbutton.connect("clicked",self.lsbutton_clicked)
32
+		self.ccheckbox.connect("clicked",self.ccheckbox_clicked)
33
+		
34
+		#add a label to the UI to display the last command
35
+		self.label = gtk.Label()
36
+		layout.add(self.label)
37
+	
38
+	def ccheckbox_clicked(self, widget):
39
+		checked = self.ccheckbox.get_active()
40
+		self.lsbutton.set_sensitive(not checked)
41
+		if checked:
42
+			self.lsbutton_stopped()
43
+			self.emit('command', "continuous_listen")
44
+		else:
45
+			self.emit('command', "continuous_stop")
46
+	
47
+	def lsbutton_stopped(self):
48
+		self.lsbutton.set_label("Listen")
49
+		
50
+	def lsbutton_clicked(self, button):
51
+		val = self.lsbutton.get_label()
52
+		if val == "Listen":
53
+			self.emit("command", "listen")
54
+			self.lsbutton.set_label("Stop")
55
+			#clear the label
56
+			self.label.set_text("")
57
+		else:
58
+			self.lsbutton_stopped()
59
+			self.emit("command", "stop")
60
+			
61
+	def run(self):
62
+		self.window.show_all()
63
+	
64
+	def quit(self):
65
+		pass
66
+	
67
+	def delete_event(self, x, y ):
68
+		self.emit("command", "quit")
69
+		
70
+	def finished(self, text):
71
+		print text
72
+		#if the continuous isn't pressed
73
+		if not self.ccheckbox.get_active():
74
+			self.lsbutton_stopped()
75
+		self.label.set_text(text)
76
+		

+ 11
- 9
QtUI.py View File

1
 #!/usr/bin/env python2
1
 #!/usr/bin/env python2
2
 import sys
2
 import sys
3
-import signal
4
 import gobject
3
 import gobject
5
 # Qt stuff
4
 # Qt stuff
6
 from PySide.QtCore import Signal, Qt
5
 from PySide.QtCore import Signal, Qt
35
 		#connect the buttonsc
34
 		#connect the buttonsc
36
 		self.lsbutton.clicked.connect(self.lsbutton_clicked)
35
 		self.lsbutton.clicked.connect(self.lsbutton_clicked)
37
 		self.ccheckbox.clicked.connect(self.ccheckbox_clicked)
36
 		self.ccheckbox.clicked.connect(self.ccheckbox_clicked)
37
+		
38
+		#add a label to the UI to display the last command
39
+		self.label = QLabel()
40
+		layout.addWidget(self.label)
38
 	
41
 	
39
-	def recognizer_finished(self, x, y):
40
-		if self.ccheckbox.isChecked():
41
-			pass
42
-		else:
43
-			self.lsbutton_stopped()
44
-			
45
-			
46
 	def ccheckbox_clicked(self):
42
 	def ccheckbox_clicked(self):
47
 		checked = self.ccheckbox.isChecked()
43
 		checked = self.ccheckbox.isChecked()
48
 		if checked:
44
 		if checked:
59
 		
55
 		
60
 	def lsbutton_clicked(self):
56
 	def lsbutton_clicked(self):
61
 		val = self.lsbutton.text()
57
 		val = self.lsbutton.text()
62
-		print val
63
 		if val == "Listen":
58
 		if val == "Listen":
64
 			self.emit("command", "listen")
59
 			self.emit("command", "listen")
65
 			self.lsbutton.setText("Stop")
60
 			self.lsbutton.setText("Stop")
61
+			#clear the label
62
+			self.label.setText("")
66
 		else:
63
 		else:
67
 			self.lsbutton_stopped()
64
 			self.lsbutton_stopped()
68
 			self.emit("command", "stop")
65
 			self.emit("command", "stop")
70
 	def run(self):
67
 	def run(self):
71
 		self.window.show()
68
 		self.window.show()
72
 		self.app.exec_()
69
 		self.app.exec_()
70
+		self.emit("command", "quit")
73
 	
71
 	
72
+	def quit(self):
73
+		pass
74
+		
74
 	def finished(self, text):
75
 	def finished(self, text):
75
 		print text
76
 		print text
76
 		#if the continuous isn't pressed
77
 		#if the continuous isn't pressed
77
 		if not self.ccheckbox.isChecked():
78
 		if not self.ccheckbox.isChecked():
78
 			self.lsbutton_stopped()
79
 			self.lsbutton_stopped()
80
+		self.label.setText(text)
79
 		
81
 		
80
 	def quit(self):
82
 	def quit(self):
81
 		#sys.exit()
83
 		#sys.exit()

Loading…
Cancel
Save