Permalink
Browse files

recent files and stat handling done

  • Loading branch information...
1 parent 16c6e8f commit 286f7034a1c6891f23fd4e99578250ff5a4d274c @baverman committed Feb 14, 2011
Showing with 59 additions and 37 deletions.
  1. +24 −1 typetrainer/ui/__init__.py
  2. +2 −0 typetrainer/ui/main.glade
  3. +30 −35 typetrainer/ui/main.py
  4. +3 −1 typetrainer/ui/stat.py
View
@@ -38,4 +38,27 @@ def __getattr__(self, name):
raise AttributeError('Builder have no %s object' % name)
setattr(self, name, obj)
- return obj
+ return obj
+
+class ShortcutActivator(object):
+ def __init__(self, window):
+ self.window = window
+ self.accel_group = gtk.AccelGroup()
+ self.window.add_accel_group(self.accel_group)
+
+ self.shortcuts = {}
+ self.pathes = {}
+
+ def bind(self, accel, callback, *args):
+ key, modifier = gtk.accelerator_parse(accel)
+ self.shortcuts[(key, modifier)] = callback, args
+
+ self.accel_group.connect_group(key, modifier, gtk.ACCEL_VISIBLE, self.activate)
+
+ def get_callback_and_args(self, *key):
+ return self.shortcuts[key]
+
+ def activate(self, group, window, key, modifier):
+ cb, args = self.get_callback_and_args(key, modifier)
+ result = cb(*args)
+ return result is None or result
@@ -244,6 +244,7 @@
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
+ <signal name="clicked" handler="on_recent_bt_clicked"/>
<child>
<object class="GtkArrow" id="arrow1">
<property name="visible">True</property>
@@ -280,6 +281,7 @@
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_underline">True</property>
+ <signal name="clicked" handler="on_stat_bt_clicked"/>
<child>
<object class="GtkLabel" id="label5">
<property name="visible">True</property>
View
@@ -5,7 +5,7 @@
import pango
from typetrainer.i18n import _
-from typetrainer.ui import idle, refresh_gui, BuilderAware, block_handler
+from typetrainer.ui import idle, refresh_gui, BuilderAware, block_handler, ShortcutActivator
from typetrainer.util import join_to_file_dir
from typetrainer.tutors import available_tutors, get_filler
from typetrainer.ui.kbd import n130_dvp_keyboard, n130_keyboard, n130_sdfv_keyboard
@@ -52,6 +52,12 @@ def __init__(self, config, filler, stat, kbd_drawer):
self.update_title()
+ self.activator = ShortcutActivator(self.window)
+ self.activator.bind('<ctrl>q', self.quit)
+ self.activator.bind('<ctrl>s', self.on_stat_bt_clicked)
+ self.activator.bind('<ctrl>r', self.on_recent_bt_clicked)
+ self.activator.bind('<ctrl>o', self.on_open_bt_clicked)
+
idle(self.fill_tutors)
idle(self.fill_layouts)
@@ -89,10 +95,13 @@ def fill(self):
self.type_entry.set_max_length(entry.get_text_length())
self.totype_text = unicode(entry.get_text())
- def on_window_delete_event(self, *args):
+ def quit(self):
self.config.save()
gtk.main_quit()
+ def on_window_delete_event(self, *args):
+ self.quit()
+
def on_type_entry_changed(self, *args):
if not self.start_time:
self.start_time = time.time()
@@ -231,26 +240,15 @@ def fill_tutors(self):
self.fill_levels()
- def fill_levels(self, fallback=None):
+ def fill_levels(self):
self.level_ls.clear()
tutor = self.get_selected_item(self.tutor_cb)
if tutor:
- fit = None
- ait = None
for id, label in tutor.levels:
it = self.level_ls.append((id, label))
if id == self.filler.level:
- ait = it
-
- if id == fallback:
- fit = it
-
- if not ait:
- ait = fit
-
- if ait:
- with block_handler(self.level_cb, self.on_level_cb_changed):
- self.level_cb.set_active_iter(ait)
+ with block_handler(self.level_cb, self.on_level_cb_changed):
+ self.level_cb.set_active_iter(it)
def fill_layouts(self):
self.layout_ls.clear()
@@ -260,22 +258,21 @@ def fill_layouts(self):
with block_handler(self.layout_cb, self.on_layout_cb_changed):
self.layout_cb.set_active_iter(it)
- def noop(self):
+ def on_recent_bt_clicked(self, *args):
if 'RECENT_FILES' in self.config and self.config['RECENT_FILES']:
- item = None
+ menu = gtk.Menu()
+ menu.set_reserve_toggle_size(False)
for fname in self.config['RECENT_FILES']:
+ if fname == self.filler.filename:
+ continue
item = gtk.MenuItem(fname)
item.connect('activate', self.on_filename_activate, fname)
menu.append(item)
- menu.append(gtk.SeparatorMenuItem())
-
-
- menu.show_all()
- menu.popup(None, None, None, event.button, event.time)
- return True
+ menu.show_all()
+ menu.popup(None, None, None, 1, 0)
- def on_open_bt_clicked(self, sender):
+ def on_open_bt_clicked(self, *args):
dialog = gtk.FileChooserDialog(_("Open file..."),
None,
gtk.FILE_CHOOSER_ACTION_OPEN,
@@ -297,17 +294,15 @@ def update_filler(self, tutor, filename):
self.filler = get_filler(tutor, filename)
self.fill()
+ self.fill_tutors()
self.config._add_recent_file(filename)
self.update_title()
self.config['TUTOR'] = tutor
self.config['FILE'] = filename
def get_tutor_for_file(self, filename):
- tutor = self.filler.name
- if tutor not in available_tutors:
- tutor = 'en.basic'
-
+ tutor = self.filler.fullname or 'en.basic'
return self.config._get_tutor_for_file(filename, tutor)
def get_selected_item(self, cb, column=0):
@@ -320,8 +315,11 @@ def get_selected_item(self, cb, column=0):
def on_tutor_cb_changed(self, sender):
tutor = self.get_selected_item(sender)
if tutor:
- self.fill_levels(tutor.levels[0][0])
+ self.fill_levels()
level = self.get_selected_item(self.level_cb)
+ if not level:
+ level = tutor.levels[0][0]
+
tutor = '%s.%s' % (tutor.name, level)
self.config._set_tutor_for_file(self.filler.filename, tutor)
idle(self.update_filler, tutor, self.filler.filename)
@@ -350,13 +348,10 @@ def update_title(self):
else:
self.window.set_title('Typetrainer')
- def on_stat_activate(self, item):
+ def on_stat_bt_clicked(self, *args):
from .stat import StatWindow
- tutor = self.filler.name
- if tutor not in available_tutors:
- tutor = 'en.basic'
-
+ tutor = self.filler.fullname or 'en.basic'
window = StatWindow(self.window, self.stat, tutor)
window.window.show_all()
View
@@ -4,7 +4,7 @@
import gtk
-from . import BuilderAware, idle, refresh_gui
+from . import BuilderAware, idle, refresh_gui, ShortcutActivator
from ..util import join_to_file_dir
def median(values):
@@ -154,6 +154,8 @@ def __init__(self, parent, stat, tutor):
self.drawer = StatDrawer()
self.frame.add(self.drawer)
+ self.activator = ShortcutActivator(self.window)
+ self.activator.bind('Escape', self.window.destroy)
self.tutor = tutor
self.acc_adj.value = 97

0 comments on commit 286f703

Please sign in to comment.