diff options
author | florent <florent.pigout@gmail.com> | 2011-09-12 08:13:38 (GMT) |
---|---|---|
committer | florent <florent.pigout@gmail.com> | 2011-09-12 08:13:38 (GMT) |
commit | 1211310e0363e41b2479cd2dbcc5c6b8dee42f54 (patch) | |
tree | ce81216969347ddfc9a8c050b654c99a1d76d6a9 /creactivistore/_templates/+package+/ui/toolbar.py_tmpl | |
parent | 7cc16a4469c6dc40ea4c18eb302df41f3802c14e (diff) |
add template for triplestore web based project
Diffstat (limited to 'creactivistore/_templates/+package+/ui/toolbar.py_tmpl')
-rw-r--r-- | creactivistore/_templates/+package+/ui/toolbar.py_tmpl | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/creactivistore/_templates/+package+/ui/toolbar.py_tmpl b/creactivistore/_templates/+package+/ui/toolbar.py_tmpl new file mode 100644 index 0000000..a088ab5 --- /dev/null +++ b/creactivistore/_templates/+package+/ui/toolbar.py_tmpl @@ -0,0 +1,180 @@ + +# python import +import logging +# ... +from functools import partial +from gettext import gettext as _ + +# gtk import +import gtk, glib + +# sugar import +from sugar.activity import activity + +# sugar import +from sugar.graphics.toolbutton import ToolButton + +# server import +from server.flask import logger + + +def _cb_next(widget, toolbar, next_): + # update current screen + toolbar.activity.change_screen(next_) + + +BUTTONS = { + 'ajax': ['document-generic', _cb_next], + 'gallery': ['document-generic', _cb_next], + } + +TOOLBARS = { + 'menu': [ + ['ajax', 'gallery'], + [] + ], + } + +TITLES = { + 'menu': { + 'toolbox': _('Demo'), + 'buttons': { + 'ajax': _('Ajax'), + 'gallery': _('Gallery'), + } + } + } + + +class Toolbar(gtk.Toolbar): + + def __init__(self, activity, name='default'): + # init parent + gtk.Toolbar.__init__(self) + # keep the name + self.set_name(name) + # keep activity + self.activity = activity + # adjustment + self._adjustment = None + # init widget dict + self._button_dict = dict() + # init buttons + self._init_buttons() + # add tab in toolbox + _toolbox_name = TITLES[self.name]['toolbox'] + if _toolbox_name is None: + pass + else: + _toolbox = self.activity.get_toolbox() + _toolbox.add_toolbar(_toolbox_name, self) + # connect focus event + self.connect('focus', self._on_focus) + # show + self.show() + + def _switch(self, name): + # update toolbar + self._clear_buttons() + # switch name + self.set_name(name) + # reset + self._init_buttons() + + def _clear_buttons(self): + # remove all + for _k in self._button_dict.keys(): + self._remove_button(_k) + # remove separators + for _c in self.get_children(): + # remove it + self.remove(_c) + # and destroy it + _c.destroy() + + def _init_buttons(self): + # add buttons + _b_left, _b_right = TOOLBARS[self.name] + # place left buttons + for _b in _b_left: + # add button + self._add_button(_b) + # add expanded separator + _separator = gtk.SeparatorToolItem() + _separator.set_draw(False) + _separator.set_expand(True) + _separator.show() + self.add(_separator) + # place right buttons + for _b in _b_right: + # add button + self._add_button(_b) + + def _on_focus(self, widget, direction): + # update current screen + self.activity.change_screen(self.name) + + def _has_button(self, button_id): + return button_id in self._button_dict + + def _add_button(self, button_id, index=None): + # get button icon and cb + _icon, _cb = BUTTONS[button_id] + # manage separator + if button_id == 'separator': + _buton = gtk.SeparatorToolItem() + _buton.set_draw(True) + # standard button + elif button_id in BUTTONS: + # get tooltip + _tooltip = TITLES[self.name]['buttons'][button_id] + # set icon + _buton = ToolButton(_icon) + # set tooltip + _buton.set_tooltip(_tooltip) + # do connect + if _cb is None: + pass + else: + _buton.connect('clicked', _cb, self, button_id) + # ?? + else: + # ERROR + logger.error('[toolbar] _add_button - ??: %s' % button_id) + return + # update the button dict + self._button_dict[button_id] = _buton + # add to the toolbar + if index is None: + self.add(_buton) + else: + self.insert(_buton, index) + # show it + _buton.show() + + def _remove_button(self, button_id): + # little check + if button_id in self._button_dict: + # get button + _buton = self._button_dict.pop(button_id) + # do remove - do it safe + glib.idle_add(partial(self.remove, _buton)) + # and destroy - safe + glib.idle_add(_buton.destroy) + # ?? + else: + # ERROR + logger.error('[toolbar] _remove_button - unknown: %s' % button_id) + + def _replace_button(self, button_id_out, button_id_in): + # little check + if button_id_out in self._button_dict: + # ... + _index = self.get_item_index(self._button_dict[button_id_out]) + # do remove + self._remove_button(button_id_out) + # do it safe + glib.idle_add(partial(self._add_button, button_id_in, _index)) + # ?? + else: + pass |