Tidy up navigation sidebar code

svn: r15621
This commit is contained in:
Nick Hall 2010-07-06 14:30:38 +00:00
parent 16f95e157c
commit 7f83fc7fcb
6 changed files with 84 additions and 69 deletions

View File

@ -314,11 +314,6 @@ class PluginData(object):
.. attribute:: viewclass
A class of type ViewCreator that holds the needed info of the
view to be created: icon, viewclass that derives from pageview, ...
.. attribute:: order
order can be START or END. Default is END. For END, on registering,
the view is appended to the list of views. If START, then the view is
prepended. Only set START if you want a view to be the first in the
order of views
.. attribute:: stock_icon
The icon in the toolbar or sidebar used to select the view
@ -327,6 +322,13 @@ class PluginData(object):
The class that defines the sidebar.
.. attribute:: menu_label
A label to use on the seltion menu.
Attributes for VIEW and SIDEBAR plugins
.. attribute:: order
order can be START or END. Default is END. For END, on registering,
the plugin is appended to the list of plugins. If START, then the
plugin is prepended. Only set START if you want a plugin to be the
first in the order of plugins
"""
def __init__(self):
@ -389,11 +391,12 @@ class PluginData(object):
self._help_url = None
#VIEW attr
self._viewclass = None
self._order = END
self._stock_icon = None
#SIDEBAR attr
self._sidebarclass = None
self._menu_label = ''
#VIEW and SIDEBAR attr
self._order = END
def _set_id(self, id):
self._id = id
@ -833,14 +836,6 @@ class PluginData(object):
def _get_viewclass(self):
return self._viewclass
def _set_order(self, order):
if not self._ptype == VIEW:
raise ValueError, 'order may only be set for VIEW plugins'
self._order = order
def _get_order(self):
return self._order
def _set_stock_icon(self, stock_icon):
if not self._ptype == VIEW:
raise ValueError, 'stock_icon may only be set for VIEW plugins'
@ -850,7 +845,6 @@ class PluginData(object):
return self._stock_icon
viewclass = property(_get_viewclass, _set_viewclass)
order = property(_get_order, _set_order)
stock_icon = property(_get_stock_icon, _set_stock_icon)
#SIDEBAR attributes
@ -872,6 +866,17 @@ class PluginData(object):
sidebarclass = property(_get_sidebarclass, _set_sidebarclass)
menu_label = property(_get_menu_label, _set_menu_label)
#VIEW and SIDEBAR attributes
def _set_order(self, order):
if not self._ptype in (VIEW, SIDEBAR):
raise ValueError, 'order may only be set for VIEW and SIDEBAR plugins'
self._order = order
def _get_order(self):
return self._order
order = property(_get_order, _set_order)
def newplugin():
"""

View File

@ -37,20 +37,14 @@ class BaseSidebar(object):
"""
raise NotImplementedError
def loaded(self):
"""
Called after all the sidebar plugins have been loaded.
"""
raise NotImplementedError
def view_changed(self, page_num):
"""
Called when the active view is changed.
"""
raise NotImplementedError
def handlers_block(self):
"""
Block signals to the buttons to prevent spurious events.
"""
raise NotImplementedError
def handlers_unblock(self):
"""
Unblock signals to the buttons.
"""
raise NotImplementedError

View File

@ -30,6 +30,13 @@ manage pages in the main Gramps window.
#-------------------------------------------------------------------------
import gtk
#-------------------------------------------------------------------------
#
# Gramps modules
#
#-------------------------------------------------------------------------
from gen.plug import (START, END)
#-------------------------------------------------------------------------
#
# Sidebar class
@ -87,7 +94,7 @@ class Sidebar(object):
"""
return self.top
def add(self, title, sidebar):
def add(self, title, sidebar, order):
"""
Add a page to the sidebar for a plugin.
"""
@ -95,31 +102,28 @@ class Sidebar(object):
index = self.notebook.append_page(sidebar.get_top(), gtk.Label(title))
menu_item = gtk.MenuItem(title)
if order == START:
self.menu.prepend(menu_item)
self.notebook.set_current_page(index)
else:
self.menu.append(menu_item)
menu_item.connect('activate', self.cb_menu_activate, index)
menu_item.show()
self.menu.append(menu_item)
def loaded(self):
"""
Called after all the sidebar plugins have been loaded.
"""
for page in self.pages:
page[1].loaded()
def view_changed(self, page_num):
"""
Called when a Gramps view is changed.
"""
for page in self.pages:
page[1].view_changed(page_num)
def handlers_block(self):
"""
Block signals to the buttons to prevent spurious events.
"""
for page in self.pages:
page[1].handlers_block()
def handlers_unblock(self):
"""
Unblock signals to the buttons.
"""
for page in self.pages:
page[1].handlers_unblock()
def __menu_button_pressed(self, button, event):
"""
Called when the button to select a sidebar page is pressed.

View File

@ -295,7 +295,6 @@ class ViewManager(CLIManager):
self.notebook.set_scrollable(True)
self.notebook.set_show_tabs(False)
self.notebook.show()
self.notebook_cat = []
self.__init_lists()
self.__build_ui_manager()
@ -361,7 +360,7 @@ class ViewManager(CLIManager):
sidebar_class = getattr(module, pdata.sidebarclass)
sidebar_page = sidebar_class(self.dbstate, self.uistate)
self.sidebar.add(pdata.menu_label, sidebar_page)
self.sidebar.add(pdata.menu_label, sidebar_page, pdata.order)
def __setup_statusbar(self):
"""
@ -557,9 +556,7 @@ class ViewManager(CLIManager):
new_page = 0
else:
new_page = current_page + 1
self.sidebar.handlers_block()
self.notebook.set_current_page(new_page)
self.sidebar.handlers_unblock()
def __prev_view(self, action):
"""
@ -572,15 +569,12 @@ class ViewManager(CLIManager):
new_page = len(self.pages)-1
else:
new_page = current_page - 1
self.sidebar.handlers_block()
self.notebook.set_current_page(new_page)
self.sidebar.handlers_unblock()
def init_interface(self):
"""
Initialize the interface, creating the pages as given in vieworder
"""
self.__init_lists()
self.__load_sidebar_plugins()
if not self.file_loaded:
@ -616,6 +610,7 @@ class ViewManager(CLIManager):
Showing the main window is deferred so that
ArgHandler can work without it always shown
"""
self.sidebar.loaded()
self.window.show()
if not self.dbstate.db.is_open() and show_manager:
self.__open_activate(None)
@ -827,6 +822,9 @@ class ViewManager(CLIManager):
config.save()
def create_page(self, pdata, page_def):
"""
Create a new page and set it as the current page.
"""
try:
page = page_def(self.dbstate, self.uistate)
except:
@ -863,16 +861,18 @@ class ViewManager(CLIManager):
hbox.show_all()
page_num = self.notebook.append_page(page_display, hbox)
return page_num
self.notebook.set_current_page(page_num)
def goto_page(self, page_num):
self.sidebar.handlers_block()
"""
Change the current page.
"""
self.notebook.set_current_page(page_num)
self.sidebar.handlers_unblock()
self.__change_page(page_num)
def __change_page(self, page_num):
"""
Perform necessary actions when a page is changed.
"""
self.__disconnect_previous_page()
self.active_page = self.pages[page_num]
@ -885,6 +885,12 @@ class ViewManager(CLIManager):
self.active_page.change_page()
def get_n_pages(self):
"""
Return the total number of pages.
"""
return self.notebook.get_n_pages()
def __delete_pages(self):
"""
Calls on_delete() for each view

View File

@ -153,8 +153,6 @@ class CategorySidebar(BaseSidebar):
#allow for switching views in a category
self.ui_category[cat_num] = UICATEGORY % (uimenuitems,
uitoolitems)
# Open the default view
self.__category_clicked(self.buttons[defaults[0]], defaults[0])
def get_top(self):
"""
@ -162,6 +160,14 @@ class CategorySidebar(BaseSidebar):
"""
return self.window
def loaded(self):
"""
Open the default view after all the sidebar plugins have been loaded.
"""
defaults = views_to_show(self.views,
config.get('preferences.use-last-view'))
self.__category_clicked(self.buttons[defaults[0]], defaults[0])
def view_changed(self, page_num):
"""
Called when the active view is changed.
@ -202,22 +208,22 @@ class CategorySidebar(BaseSidebar):
self.merge_ids.append(mergeid)
# Set new button as selected
self.handlers_block()
self.__handlers_block()
for index, button in enumerate(self.buttons):
if index == cat_num:
button.set_active(True)
else:
button.set_active(False)
self.handlers_unblock()
def handlers_block(self):
self.__handlers_unblock()
def __handlers_block(self):
"""
Block signals to the buttons to prevent spurious events.
"""
for idx in range(len(self.buttons)):
self.buttons[idx].handler_block(self.button_handlers[idx])
def handlers_unblock(self):
def __handlers_unblock(self):
"""
Unblock signals to the buttons.
"""
@ -252,11 +258,10 @@ class CategorySidebar(BaseSidebar):
page_num = self.pages.get((cat_num, view_num))
if page_num is None:
page = self.page_defs[(cat_num, view_num)]
page_num = self.viewmanager.create_page(page[0], page[1])
self.pages[(cat_num, view_num)] = page_num
self.current_views[cat_num] = view_num
self.viewmanager.goto_page(page_num)
self.pages[(cat_num, view_num)] = self.viewmanager.get_n_pages()
self.viewmanager.create_page(page[0], page[1])
else:
self.viewmanager.goto_page(page_num)
def __make_sidebar_button(self, use_text, index, page_title, page_stock):
"""

View File

@ -36,5 +36,6 @@ fname = 'categorysidebar.py',
authors = [u"Nick Hall"],
authors_email = ["nick__hall@hotmail.com"],
sidebarclass = 'CategorySidebar',
menu_label = _('Category')
menu_label = _('Category'),
order = START
)