Add Most Recently Used object functionality to Go menu

svn: r14061
This commit is contained in:
Nick Hall 2010-01-13 15:59:36 +00:00
parent a4b22193c2
commit 4a87cbbe61
10 changed files with 119 additions and 42 deletions

View File

@ -78,7 +78,7 @@ class History(gen.utils.Callback):
__signals__ = {
'active-changed' : (str, ),
'menu-changed' : (list, ),
'mru-changed' : (list, )
}
def __init__(self):
@ -90,7 +90,7 @@ class History(gen.utils.Callback):
Clears the history, resetting the values back to their defaults
"""
self.history = []
self.mhistory = []
self.mru = []
self.index = -1
self.lock = False
@ -108,12 +108,12 @@ class History(gen.utils.Callback):
self.history.remove(del_id)
self.index -= 1
mhc = self.mhistory.count(del_id)
mhc = self.mru.count(del_id)
for c in range(mhc):
self.mhistory.remove(del_id)
self.mru.remove(del_id)
self.emit('mru-changed', (self.mru, ))
if self.history:
self.emit('active-changed', (self.history[self.index],))
self.emit('menu-changed', (self.mhistory, ))
def push(self, handle):
"""
@ -122,13 +122,13 @@ class History(gen.utils.Callback):
self.prune()
if len(self.history) == 0 or handle != self.history[-1]:
self.history.append(str(handle))
if handle in self.mhistory:
self.mhistory.remove(handle)
self.mhistory.append(handle)
if handle in self.mru:
self.mru.remove(handle)
self.mru.append(handle)
self.emit('mru-changed', (self.mru, ))
self.index += 1
if self.history:
self.emit('active-changed', (self.history[self.index],))
self.emit('menu-changed', (self.mhistory, ))
def forward(self, step=1):
"""
@ -136,9 +136,10 @@ class History(gen.utils.Callback):
"""
self.index += step
handle = self.history[self.index]
if handle not in self.mhistory:
self.mhistory.append(handle)
self.emit('menu-changed', (self.mhistory, ))
if handle in self.mru:
self.mru.remove(handle)
self.mru.append(handle)
self.emit('mru-changed', (self.mru, ))
if self.history:
self.emit('active-changed', (self.history[self.index],))
return str(self.history[self.index])
@ -150,9 +151,10 @@ class History(gen.utils.Callback):
self.index -= step
try:
handle = self.history[self.index]
if handle not in self.mhistory:
self.mhistory.append(handle)
self.emit('menu-changed', (self.mhistory, ))
if handle in self.mru:
self.mru.remove(handle)
self.mru.append(handle)
self.emit('mru-changed', (self.mru, ))
if self.history:
self.emit('active-changed', (self.history[self.index],))
return str(self.history[self.index])
@ -190,7 +192,6 @@ class History(gen.utils.Callback):
if not self.at_end():
self.history = self.history[0:self.index+1]
#-------------------------------------------------------------------------
#
# Recent Docs Menu

View File

@ -48,7 +48,24 @@ import gtk
#----------------------------------------------------------------
from gui.views.pageview import PageView
from TransUtils import sgettext as _
from Utils import navigation_label
DISABLED = -1
MRU_SIZE = 10
MRU_TOP = [
'<ui>'
'<menubar name="MenuBar">'
'<menu action="GoMenu">'
'<placeholder name="CommonHistory">'
]
MRU_BTM = [
'</placeholder>'
'</menu>'
'</menubar>'
'</ui>'
]
#------------------------------------------------------------------------------
#
# NavigationView
@ -71,7 +88,9 @@ class NavigationView(PageView):
self.book_action = None
self.other_action = None
self.active_signal = None
self.mru_signal = None
self.nav_group = nav_group
self.mru_active = DISABLED
self.uistate.register(self.navigation_type(), self.nav_group)
@ -126,6 +145,8 @@ class NavigationView(PageView):
hobj = self.get_history()
self.active_signal = hobj.connect('active-changed', self.goto_active)
self.mru_signal = hobj.connect('mru-changed', self.update_mru_menu)
self.update_mru_menu(hobj.mru)
self.goto_active(None)
@ -138,6 +159,8 @@ class NavigationView(PageView):
self.bookmarks.undisplay()
hobj = self.get_history()
hobj.disconnect(self.active_signal)
hobj.disconnect(self.mru_signal)
self.mru_disable()
def navigation_group(self):
"""
@ -337,19 +360,10 @@ class NavigationView(PageView):
hobj = self.get_history()
hobj.lock = True
if not hobj.at_end():
try:
handle = hobj.forward()
hobj.forward()
self.uistate.modify_statusbar(self.dbstate)
hobj.mhistory.append(hobj.history[hobj.index])
self.fwd_action.set_sensitive(not hobj.at_end())
self.back_action.set_sensitive(True)
except:
hobj.clear()
self.fwd_action.set_sensitive(False)
self.back_action.set_sensitive(False)
else:
self.fwd_action.set_sensitive(False)
self.back_action.set_sensitive(True)
hobj.lock = False
def back_clicked(self, obj):
@ -359,21 +373,60 @@ class NavigationView(PageView):
hobj = self.get_history()
hobj.lock = True
if not hobj.at_front():
try:
handle = hobj.back()
hobj.back()
self.uistate.modify_statusbar(self.dbstate)
hobj.mhistory.append(hobj.history[hobj.index])
self.back_action.set_sensitive(not hobj.at_front())
self.fwd_action.set_sensitive(True)
except:
hobj.clear()
self.fwd_action.set_sensitive(False)
self.back_action.set_sensitive(False)
else:
self.back_action.set_sensitive(False)
self.fwd_action.set_sensitive(True)
hobj.lock = False
####################################################################
# MRU functions
####################################################################
def mru_disable(self):
"""
Remove the UI and action groups for the MRU list.
"""
if self.mru_active != DISABLED:
self.uistate.uimanager.remove_ui(self.mru_active)
self.uistate.uimanager.remove_action_group(self.mru_action)
self.mru_active = DISABLED
def mru_enable(self):
"""
Enables the UI and action groups for the MRU list.
"""
if self.mru_active == DISABLED:
self.uistate.uimanager.insert_action_group(self.mru_action, 1)
self.mru_active = self.uistate.uimanager.add_ui_from_string(self.mru_ui)
self.uistate.uimanager.ensure_update()
def update_mru_menu(self, items):
"""
Builds the UI and action group for the MRU list.
"""
self.mru_disable()
nav_type = self.navigation_type()
hobj = self.get_history()
menu_len = min(len(items) - 1, MRU_SIZE)
entry = '<menuitem action="%s%02d"/>'
data = [entry % (nav_type, index) for index in range(0, menu_len)]
self.mru_ui = "".join(MRU_TOP) + "".join(data) + "".join(MRU_BTM)
mitems = items[-MRU_SIZE - 1:-1] # Ignore current handle
mitems.reverse()
data = []
for index, handle in enumerate(mitems):
name, obj = navigation_label(self.dbstate.db, nav_type, handle)
data.append(('%s%02d'%(nav_type, index), None, name,
"<alt>%d" % index, None,
make_callback(hobj.push, handle)))
self.mru_action = gtk.ActionGroup(nav_type)
self.mru_action.add_actions(data)
self.mru_enable()
####################################################################
# Template functions
####################################################################
@ -422,3 +475,8 @@ class NavigationView(PageView):
"""
raise NotImplementedError
def make_callback(func, handle):
"""
Generates a callback function based off the passed arguments
"""
return lambda x: func(handle)

View File

@ -302,6 +302,7 @@ class PlaceBaseView(ListView):
<placeholder name="CommonGo">
<menuitem action="Back"/>
<menuitem action="Forward"/>
<separator/>
</placeholder>
</menu>
<menu action="EditMenu">

View File

@ -156,6 +156,7 @@ class EventView(ListView):
<placeholder name="CommonGo">
<menuitem action="Back"/>
<menuitem action="Forward"/>
<separator/>
</placeholder>
</menu>
<menu action="FileMenu">

View File

@ -134,6 +134,7 @@ class FamilyView(ListView):
<placeholder name="CommonGo">
<menuitem action="Back"/>
<menuitem action="Forward"/>
<separator/>
</placeholder>
</menu>
<menu action="EditMenu">

View File

@ -599,6 +599,17 @@ class FanChartView(NavigationView):
def ui_definition(self):
return '''<ui>
<menubar name="MenuBar">
<menu action="GoMenu">
<placeholder name="CommonGo">
<menuitem action="Back"/>
<menuitem action="Forward"/>
<separator/>
<menuitem action="HomePerson"/>
<separator/>
</placeholder>
</menu>
</menubar>
<toolbar name="ToolBar">
<placeholder name="CommonNavigation">
<toolitem action="Back"/>

View File

@ -367,6 +367,7 @@ class MediaView(ListView):
<placeholder name="CommonGo">
<menuitem action="Back"/>
<menuitem action="Forward"/>
<separator/>
</placeholder>
</menu>
</menubar>

View File

@ -153,6 +153,7 @@ class NoteView(ListView):
<placeholder name="CommonGo">
<menuitem action="Back"/>
<menuitem action="Forward"/>
<separator/>
</placeholder>
</menu>
<menu action="EditMenu">

View File

@ -167,6 +167,7 @@ class RepositoryView(ListView):
<placeholder name="CommonGo">
<menuitem action="Back"/>
<menuitem action="Forward"/>
<separator/>
</placeholder>
</menu>
<menu action="EditMenu">

View File

@ -162,6 +162,7 @@ class SourceView(ListView):
<placeholder name="CommonGo">
<menuitem action="Back"/>
<menuitem action="Forward"/>
<separator/>
</placeholder>
</menu>
<menu action="EditMenu">