Browse Source

ctrl+q will quit the UI if it is running

Jezra 10 years ago
parent
commit
a230f50f56
3 changed files with 63 additions and 50 deletions
  1. 17
    19
      Blather.py
  2. 24
    15
      GtkUI.py
  3. 22
    16
      QtUI.py

+ 17
- 19
Blather.py View File

@@ -36,7 +36,7 @@ class Blather:
36 36
 		self.read_commands()
37 37
 		self.recognizer = Recognizer(lang_file, dic_file)
38 38
 		self.recognizer.connect('finished',self.recognizer_finished)
39
-		
39
+
40 40
 		if opts.interface != None:
41 41
 			if opts.interface == "q":
42 42
 				#import the ui from qt
@@ -46,18 +46,18 @@ class Blather:
46 46
 			else:
47 47
 				print "no GUI defined"
48 48
 				sys.exit()
49
-				
49
+
50 50
 			self.ui = UI(args,opts.continuous)
51 51
 			self.ui.connect("command", self.process_command)
52 52
 			#can we load the icon resource?
53 53
 			icon = self.load_resource("icon.png")
54 54
 			if icon:
55 55
 				self.ui.set_icon(icon)
56
-		
56
+
57 57
 		if self.opts.history:
58 58
 			self.history = []
59
-		
60
-					
59
+
60
+
61 61
 	def read_commands(self):
62 62
 		#read the.commands file
63 63
 		file_lines = open(command_file)
@@ -75,21 +75,21 @@ class Blather:
75 75
 						strings.write( key.strip()+"\n")
76 76
 		#close the strings file
77 77
 		strings.close()
78
-	
78
+
79 79
 	def log_history(self,text):
80 80
 		if self.opts.history:
81 81
 			self.history.append(text)
82 82
 			if len(self.history) > self.opts.history:
83 83
 				#pop off the first item
84 84
 				self.history.pop(0)
85
-			
85
+
86 86
 			#open and truncate the blather history file
87 87
 			hfile = open(history_file, "w")
88 88
 			for line in self.history:
89 89
 				hfile.write( line+"\n")
90 90
 			#close the  file
91 91
 			hfile.close()
92
-	
92
+
93 93
 	def recognizer_finished(self, recognizer, text):
94 94
 		t = text.lower()
95 95
 		#is there a matching command?
@@ -107,16 +107,14 @@ class Blather:
107 107
 				self.recognizer.pause()
108 108
 			#let the UI know that there is a finish
109 109
 			self.ui.finished(t)
110
-	
110
+
111 111
 	def run(self):
112 112
 		if self.ui:
113 113
 			self.ui.run()
114 114
 		else:
115
-			blather.recognizer.listen()	
115
+			blather.recognizer.listen()
116 116
 
117 117
 	def quit(self):
118
-		if self.ui:
119
-			self.ui.quit()
120 118
 		sys.exit()
121 119
 
122 120
 	def process_command(self, UI, command):
@@ -133,7 +131,7 @@ class Blather:
133 131
 			self.recognizer.pause()
134 132
 		elif command == "quit":
135 133
 			self.quit()
136
-	
134
+
137 135
 	def load_resource(self,string):
138 136
 		local_data = os.path.join(os.path.dirname(__file__), 'data')
139 137
 		paths = ["/usr/share/blather/","/usr/local/share/blather", local_data]
@@ -143,9 +141,9 @@ class Blather:
143 141
 				return resource
144 142
 		#if we get this far, no resource was found
145 143
 		return False
146
-    
147
-    
148
-    
144
+
145
+
146
+
149 147
 if __name__ == "__main__":
150 148
 	parser = OptionParser()
151 149
 	parser.add_option("-i", "--interface",  type="string", dest="interface",
@@ -155,7 +153,7 @@ if __name__ == "__main__":
155 153
 		action="store_true", dest="continuous", default=False,
156 154
 		help="starts interface with 'continuous' listen enabled")
157 155
 	parser.add_option("-H", "--history", type="int",
158
-		action="store", dest="history", 	
156
+		action="store", dest="history",
159 157
 		help="number of commands to store in history file")
160 158
 
161 159
 	(options, args) = parser.parse_args()
@@ -170,11 +168,11 @@ if __name__ == "__main__":
170 168
 	#run the blather
171 169
 	blather.run()
172 170
 	#start the main loop
173
-		
171
+
174 172
 	try:
175 173
 		main_loop.run()
176 174
 	except:
177 175
 		print "time to quit"
178 176
 		main_loop.quit()
179 177
 		sys.exit()
180
-		
178
+

+ 24
- 15
GtkUI.py View File

@@ -11,7 +11,7 @@ class UI(gobject.GObject):
11 11
 	__gsignals__ = {
12 12
 		'command' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,))
13 13
 	}
14
-	
14
+
15 15
 	def __init__(self,args, continuous):
16 16
 		gobject.GObject.__init__(self)
17 17
 		self.continuous = continuous
@@ -21,7 +21,7 @@ class UI(gobject.GObject):
21 21
 		#give the window a name
22 22
 		self.window.set_title("BlatherGtk")
23 23
 		self.window.set_resizable(False)
24
-		
24
+
25 25
 		layout = gtk.VBox()
26 26
 		self.window.add(layout)
27 27
 		#make a listen/stop button
@@ -30,15 +30,24 @@ class UI(gobject.GObject):
30 30
 		#make a continuous button
31 31
 		self.ccheckbox = gtk.CheckButton("Continuous Listen")
32 32
 		layout.add(self.ccheckbox)
33
-		
34
-		#connect the buttonsc
33
+
34
+		#connect the buttons
35 35
 		self.lsbutton.connect("clicked",self.lsbutton_clicked)
36 36
 		self.ccheckbox.connect("clicked",self.ccheckbox_clicked)
37
-		
37
+
38 38
 		#add a label to the UI to display the last command
39 39
 		self.label = gtk.Label()
40 40
 		layout.add(self.label)
41 41
 
42
+		#create an accellerator group for this window
43
+		accel = gtk.AccelGroup()
44
+		#add the ctrl+q to quit
45
+		accel.connect_group(gtk.keysyms.q, gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE, self.accel_quit )
46
+		#lock the group
47
+		accel.lock()
48
+		#add the group to the window
49
+		self.window.add_accel_group(accel)
50
+
42 51
 	def ccheckbox_clicked(self, widget):
43 52
 		checked = self.ccheckbox.get_active()
44 53
 		self.lsbutton.set_sensitive(not checked)
@@ -47,10 +56,10 @@ class UI(gobject.GObject):
47 56
 			self.emit('command', "continuous_listen")
48 57
 		else:
49 58
 			self.emit('command', "continuous_stop")
50
-	
59
+
51 60
 	def lsbutton_stopped(self):
52 61
 		self.lsbutton.set_label("Listen")
53
-		
62
+
54 63
 	def lsbutton_clicked(self, button):
55 64
 		val = self.lsbutton.get_label()
56 65
 		if val == "Listen":
@@ -61,25 +70,25 @@ class UI(gobject.GObject):
61 70
 		else:
62 71
 			self.lsbutton_stopped()
63 72
 			self.emit("command", "stop")
64
-			
73
+
65 74
 	def run(self):
66 75
 		self.window.show_all()
67 76
 		if self.continuous:
68 77
 			self.ccheckbox.set_active(True)
69
-	
70
-	def quit(self):
71
-		pass
72
-	
78
+
79
+	def accel_quit(self, accel_group, acceleratable, keyval, modifier):
80
+		self.emit("command", "quit")
81
+
73 82
 	def delete_event(self, x, y ):
74 83
 		self.emit("command", "quit")
75
-		
84
+
76 85
 	def finished(self, text):
77 86
 		print text
78 87
 		#if the continuous isn't pressed
79 88
 		if not self.ccheckbox.get_active():
80 89
 			self.lsbutton_stopped()
81 90
 		self.label.set_text(text)
82
-		
91
+
83 92
 	def set_icon(self, icon):
84 93
 		gtk.window_set_default_icon_from_file(icon)
85
-		
94
+

+ 22
- 16
QtUI.py View File

@@ -6,13 +6,13 @@ import gobject
6 6
 # Qt stuff
7 7
 from PySide.QtCore import Signal, Qt
8 8
 from PySide.QtGui import QApplication, QWidget, QMainWindow, QVBoxLayout
9
-from PySide.QtGui import QLabel, QPushButton, QCheckBox, QIcon
9
+from PySide.QtGui import QLabel, QPushButton, QCheckBox, QIcon, QAction
10 10
 
11 11
 class UI(gobject.GObject):
12 12
 	__gsignals__ = {
13 13
 		'command' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,))
14 14
 	}
15
-	
15
+
16 16
 	def __init__(self,args,continuous):
17 17
 		self.continuous = continuous
18 18
 		gobject.GObject.__init__(self)
@@ -25,7 +25,7 @@ class UI(gobject.GObject):
25 25
 		self.window.setMaximumSize(400,200)
26 26
 		center = QWidget()
27 27
 		self.window.setCentralWidget(center)
28
-		
28
+
29 29
 		layout = QVBoxLayout()
30 30
 		center.setLayout(layout)
31 31
 		#make a listen/stop button
@@ -34,15 +34,25 @@ class UI(gobject.GObject):
34 34
 		#make a continuous button
35 35
 		self.ccheckbox = QCheckBox("Continuous Listen")
36 36
 		layout.addWidget(self.ccheckbox)
37
-		
38
-		#connect the buttonsc
37
+
38
+		#connect the buttons
39 39
 		self.lsbutton.clicked.connect(self.lsbutton_clicked)
40 40
 		self.ccheckbox.clicked.connect(self.ccheckbox_clicked)
41
-		
41
+
42 42
 		#add a label to the UI to display the last command
43 43
 		self.label = QLabel()
44 44
 		layout.addWidget(self.label)
45
-	
45
+
46
+		#add the actions for quiting
47
+		quit_action = QAction(self.window)
48
+		quit_action.setShortcut('Ctrl+Q')
49
+		quit_action.triggered.connect(self.accel_quit)
50
+		self.window.addAction(quit_action)
51
+
52
+	def accel_quit(self):
53
+		#emit the quit
54
+		self.emit("command", "quit")
55
+
46 56
 	def ccheckbox_clicked(self):
47 57
 		checked = self.ccheckbox.isChecked()
48 58
 		if checked:
@@ -53,10 +63,10 @@ class UI(gobject.GObject):
53 63
 		else:
54 64
 			self.lsbutton.setEnabled(True)
55 65
 			self.emit('command', "continuous_stop")
56
-	
66
+
57 67
 	def lsbutton_stopped(self):
58 68
 		self.lsbutton.setText("Listen")
59
-		
69
+
60 70
 	def lsbutton_clicked(self):
61 71
 		val = self.lsbutton.text()
62 72
 		if val == "Listen":
@@ -67,7 +77,7 @@ class UI(gobject.GObject):
67 77
 		else:
68 78
 			self.lsbutton_stopped()
69 79
 			self.emit("command", "stop")
70
-			
80
+
71 81
 	def run(self):
72 82
 		self.window.show()
73 83
 		if self.continuous:
@@ -75,17 +85,13 @@ class UI(gobject.GObject):
75 85
 			self.ccheckbox_clicked()
76 86
 		self.app.exec_()
77 87
 		self.emit("command", "quit")
78
-	
88
+
79 89
 	def finished(self, text):
80 90
 		print text
81 91
 		#if the continuous isn't pressed
82 92
 		if not self.ccheckbox.isChecked():
83 93
 			self.lsbutton_stopped()
84 94
 		self.label.setText(text)
85
-		
86
-	def quit(self):
87
-		#sys.exit()
88
-		pass
89 95
 
90 96
 	def set_icon(self, icon):
91
-		self.window.setWindowIcon(QIcon(icon))       
97
+		self.window.setWindowIcon(QIcon(icon))

Loading…
Cancel
Save