소스 검색

Cleaner quiting for Qt, added Gtk UI

Jezra 11 년 전
부모
커밋
c61a0c311f
3개의 변경된 파일118개의 추가작업 그리고 27개의 파일을 삭제
  1. 31
    18
      Blather.py
  2. 76
    0
      GtkUI.py
  3. 11
    9
      QtUI.py

+ 31
- 18
Blather.py 파일 보기

@@ -18,12 +18,25 @@ if not os.path.exists(lang_dir):
18 18
 	os.makedirs(lang_dir)
19 19
 
20 20
 class Blather:
21
-	def __init__(self):
21
+	def __init__(self, args):
22
+		self.ui = None
22 23
 		self.continuous_listen = False
23 24
 		self.commands = {}
24 25
 		self.read_commands()
25 26
 		self.recognizer = Recognizer(lang_file, dic_file)
26 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 41
 	def read_commands(self):
29 42
 		#read the.commands file
@@ -60,25 +73,19 @@ class Blather:
60 73
 			#let the UI know that there is a finish
61 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 78
 			self.ui.run()
78 79
 		else:
79 80
 			blather.recognizer.listen()	
80 81
 
82
+	def quit(self):
83
+		if self.ui:
84
+			self.ui.quit()
85
+		sys.exit()
86
+
81 87
 	def process_command(self, UI, command):
88
+		print command
82 89
 		if command == "listen":
83 90
 			self.recognizer.listen()
84 91
 		elif command == "stop":
@@ -89,20 +96,26 @@ class Blather:
89 96
 		elif command == "continuous_stop":
90 97
 			self.continuous_listen = False
91 98
 			self.recognizer.pause()
99
+		elif command == "quit":
100
+			self.quit()
92 101
 		
93 102
 if __name__ == "__main__":
94 103
 	#make our blather object
95
-	blather = Blather()
104
+	blather = Blather(sys.argv)
96 105
 	#init gobject threads
97 106
 	gobject.threads_init()
98 107
 	#we want a main loop
99 108
 	main_loop = gobject.MainLoop()
109
+	#handle sigint
110
+	signal.signal(signal.SIGINT, signal.SIG_DFL)
100 111
 	#run the blather
101
-	blather.run(sys.argv)
112
+	blather.run()
102 113
 	#start the main loop
114
+		
103 115
 	try:
104 116
 		main_loop.run()
105 117
 	except:
118
+		print "time to quit"
106 119
 		main_loop.quit()
107 120
 		sys.exit()
108
-	
121
+		

+ 76
- 0
GtkUI.py 파일 보기

@@ -0,0 +1,76 @@
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 파일 보기

@@ -1,6 +1,5 @@
1 1
 #!/usr/bin/env python2
2 2
 import sys
3
-import signal
4 3
 import gobject
5 4
 # Qt stuff
6 5
 from PySide.QtCore import Signal, Qt
@@ -35,14 +34,11 @@ class UI(gobject.GObject):
35 34
 		#connect the buttonsc
36 35
 		self.lsbutton.clicked.connect(self.lsbutton_clicked)
37 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 42
 	def ccheckbox_clicked(self):
47 43
 		checked = self.ccheckbox.isChecked()
48 44
 		if checked:
@@ -59,10 +55,11 @@ class UI(gobject.GObject):
59 55
 		
60 56
 	def lsbutton_clicked(self):
61 57
 		val = self.lsbutton.text()
62
-		print val
63 58
 		if val == "Listen":
64 59
 			self.emit("command", "listen")
65 60
 			self.lsbutton.setText("Stop")
61
+			#clear the label
62
+			self.label.setText("")
66 63
 		else:
67 64
 			self.lsbutton_stopped()
68 65
 			self.emit("command", "stop")
@@ -70,12 +67,17 @@ class UI(gobject.GObject):
70 67
 	def run(self):
71 68
 		self.window.show()
72 69
 		self.app.exec_()
70
+		self.emit("command", "quit")
73 71
 	
72
+	def quit(self):
73
+		pass
74
+		
74 75
 	def finished(self, text):
75 76
 		print text
76 77
 		#if the continuous isn't pressed
77 78
 		if not self.ccheckbox.isChecked():
78 79
 			self.lsbutton_stopped()
80
+		self.label.setText(text)
79 81
 		
80 82
 	def quit(self):
81 83
 		#sys.exit()

Loading…
취소
저장