diff options
author | Jezra <jezra@jezra.net> | 2013-07-23 21:18:37 -0700 |
---|---|---|
committer | Jezra <jezra@jezra.net> | 2013-07-23 21:18:37 -0700 |
commit | a230f50f56aeeef8ef1c8b21ea8384e079d77aae (patch) | |
tree | 9ae371c031927a926c974bbf06e803af347a621c | |
parent | 9a052e1c5c4beaac376b79309556e0d934c95e0e (diff) |
ctrl+q will quit the UI if it is running
-rwxr-xr-x | Blather.py | 36 | ||||
-rw-r--r-- | GtkUI.py | 39 | ||||
-rw-r--r-- | QtUI.py | 38 |
3 files changed, 63 insertions, 50 deletions
diff --git a/Blather.py b/Blather.py index 461a456..f5d8a67 100755 --- a/Blather.py +++ b/Blather.py @@ -36,7 +36,7 @@ class Blather: self.read_commands() self.recognizer = Recognizer(lang_file, dic_file) self.recognizer.connect('finished',self.recognizer_finished) - + if opts.interface != None: if opts.interface == "q": #import the ui from qt @@ -46,18 +46,18 @@ class Blather: else: print "no GUI defined" sys.exit() - + self.ui = UI(args,opts.continuous) self.ui.connect("command", self.process_command) #can we load the icon resource? icon = self.load_resource("icon.png") if icon: self.ui.set_icon(icon) - + if self.opts.history: self.history = [] - - + + def read_commands(self): #read the.commands file file_lines = open(command_file) @@ -75,21 +75,21 @@ class Blather: strings.write( key.strip()+"\n") #close the strings file strings.close() - + def log_history(self,text): if self.opts.history: self.history.append(text) if len(self.history) > self.opts.history: #pop off the first item self.history.pop(0) - + #open and truncate the blather history file hfile = open(history_file, "w") for line in self.history: hfile.write( line+"\n") #close the file hfile.close() - + def recognizer_finished(self, recognizer, text): t = text.lower() #is there a matching command? @@ -107,16 +107,14 @@ class Blather: self.recognizer.pause() #let the UI know that there is a finish self.ui.finished(t) - + def run(self): if self.ui: self.ui.run() else: - blather.recognizer.listen() + blather.recognizer.listen() def quit(self): - if self.ui: - self.ui.quit() sys.exit() def process_command(self, UI, command): @@ -133,7 +131,7 @@ class Blather: self.recognizer.pause() elif command == "quit": self.quit() - + def load_resource(self,string): local_data = os.path.join(os.path.dirname(__file__), 'data') paths = ["/usr/share/blather/","/usr/local/share/blather", local_data] @@ -143,9 +141,9 @@ class Blather: return resource #if we get this far, no resource was found return False - - - + + + if __name__ == "__main__": parser = OptionParser() parser.add_option("-i", "--interface", type="string", dest="interface", @@ -155,7 +153,7 @@ if __name__ == "__main__": action="store_true", dest="continuous", default=False, help="starts interface with 'continuous' listen enabled") parser.add_option("-H", "--history", type="int", - action="store", dest="history", + action="store", dest="history", help="number of commands to store in history file") (options, args) = parser.parse_args() @@ -170,11 +168,11 @@ if __name__ == "__main__": #run the blather 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 index 720fb10..56a6252 100644 --- a/GtkUI.py +++ b/GtkUI.py @@ -11,7 +11,7 @@ class UI(gobject.GObject): __gsignals__ = { 'command' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,)) } - + def __init__(self,args, continuous): gobject.GObject.__init__(self) self.continuous = continuous @@ -21,7 +21,7 @@ class UI(gobject.GObject): #give the window a name self.window.set_title("BlatherGtk") self.window.set_resizable(False) - + layout = gtk.VBox() self.window.add(layout) #make a listen/stop button @@ -30,15 +30,24 @@ class UI(gobject.GObject): #make a continuous button self.ccheckbox = gtk.CheckButton("Continuous Listen") layout.add(self.ccheckbox) - - #connect the buttonsc + + #connect the buttons 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) + #create an accellerator group for this window + accel = gtk.AccelGroup() + #add the ctrl+q to quit + accel.connect_group(gtk.keysyms.q, gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE, self.accel_quit ) + #lock the group + accel.lock() + #add the group to the window + self.window.add_accel_group(accel) + def ccheckbox_clicked(self, widget): checked = self.ccheckbox.get_active() self.lsbutton.set_sensitive(not checked) @@ -47,10 +56,10 @@ class UI(gobject.GObject): 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": @@ -61,25 +70,25 @@ class UI(gobject.GObject): else: self.lsbutton_stopped() self.emit("command", "stop") - + def run(self): self.window.show_all() if self.continuous: self.ccheckbox.set_active(True) - - def quit(self): - pass - + + def accel_quit(self, accel_group, acceleratable, keyval, modifier): + self.emit("command", "quit") + 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) - + def set_icon(self, icon): gtk.window_set_default_icon_from_file(icon) - + diff --git a/QtUI.py b/QtUI.py index 071dab4..c772abf 100644 --- a/QtUI.py +++ b/QtUI.py @@ -6,13 +6,13 @@ import gobject # Qt stuff from PySide.QtCore import Signal, Qt from PySide.QtGui import QApplication, QWidget, QMainWindow, QVBoxLayout -from PySide.QtGui import QLabel, QPushButton, QCheckBox, QIcon +from PySide.QtGui import QLabel, QPushButton, QCheckBox, QIcon, QAction class UI(gobject.GObject): __gsignals__ = { 'command' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,)) } - + def __init__(self,args,continuous): self.continuous = continuous gobject.GObject.__init__(self) @@ -25,7 +25,7 @@ class UI(gobject.GObject): self.window.setMaximumSize(400,200) center = QWidget() self.window.setCentralWidget(center) - + layout = QVBoxLayout() center.setLayout(layout) #make a listen/stop button @@ -34,15 +34,25 @@ class UI(gobject.GObject): #make a continuous button self.ccheckbox = QCheckBox("Continuous Listen") layout.addWidget(self.ccheckbox) - - #connect the buttonsc + + #connect the buttons 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) - + + #add the actions for quiting + quit_action = QAction(self.window) + quit_action.setShortcut('Ctrl+Q') + quit_action.triggered.connect(self.accel_quit) + self.window.addAction(quit_action) + + def accel_quit(self): + #emit the quit + self.emit("command", "quit") + def ccheckbox_clicked(self): checked = self.ccheckbox.isChecked() if checked: @@ -53,10 +63,10 @@ class UI(gobject.GObject): else: self.lsbutton.setEnabled(True) self.emit('command', "continuous_stop") - + def lsbutton_stopped(self): self.lsbutton.setText("Listen") - + def lsbutton_clicked(self): val = self.lsbutton.text() if val == "Listen": @@ -67,7 +77,7 @@ class UI(gobject.GObject): else: self.lsbutton_stopped() self.emit("command", "stop") - + def run(self): self.window.show() if self.continuous: @@ -75,17 +85,13 @@ class UI(gobject.GObject): self.ccheckbox_clicked() self.app.exec_() self.emit("command", "quit") - + 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() - pass def set_icon(self, icon): - self.window.setWindowIcon(QIcon(icon)) + self.window.setWindowIcon(QIcon(icon)) |