import tichy from tichy.service import Service from tichy import gui from tichy.gui import Vect # The default design class ListView(gui.Box): def __init__(self, parent, list, **kargs): super(ListView, self).__init__(parent, item=list, axis=0, border=0, spacing=0) scroll = gui.Scrollable(self, border = 0, item = list, expandable = True) bar = gui.AbsBox(self, optimal_size = Vect(8, 0), border = 0) slide = gui.ScrollableSlide(bar) vbox = gui.Box(scroll, axis = 1, spacing = 0) # We add the already present items : for item in list: item.view(vbox) # This is important, we need to disconnect all the connections we make when the view is destroyed # TODO: could be cool to have this automated. But how to do it ? connections = [ list.connect('appened', self.on_appened, vbox), list.connect('removed', self.on_removed, vbox), list.connect('cleared', self.on_clear, vbox), list.connect_object('modified', self.update_slide, scroll, slide), ] self.connect('destroyed', self.on_view_destroyed, connections) scroll.connect('scrolled', self.update_slide, slide) self.update_slide(scroll, slide) def update_slide(self, scroll, slide): if not scroll.children: return s_size = scroll.size[scroll.axis] if s_size == 0: return c_size = scroll.children[0].size[scroll.axis] s_pos = -scroll.children[0].pos[scroll.axis] b_pos = s_pos * s_size / c_size b_size = s_size * s_size / c_size / 8 * 8 slide.size = Vect(8, b_size) slide.pos = Vect(0, b_pos) def on_appened(self, list, value, view): value.view(view) def on_removed(self, list, value, view): for c in view.children[:]: c.destroy() for item in list: item.view(view) def on_clear(self, list, view): for c in view.children[:]: c.destroy() def on_view_destroyed(self, view, connections): for c in connections: self.item.disconnect(c) class Default(Service): enabled = True service = 'Design' name = 'Default' def view_list(self, parent, list, **kargs): return ListView(parent, list, **kargs) def view_actor_list(self, parent, items, **kargs): return self.view_list(parent, items, **kargs) def view_actor(self, parent, actor, **kargs): ret = gui.Button(parent, item = actor, **kargs) hbox = gui.Box(ret, axis = 0, border = 0) if actor.item.icon: icon_path = actor.item.path(actor.item.icon) icon = gui.Image(icon_path, size = Vect(96,96)).view(hbox) actor.item.get_text().view(hbox) def on_clicked(b): self.select_actor(actor, b) ret.connect('clicked', on_clicked) return ret def select_actor(self, actor, view): # If the actor has a default action and it is the only one, then we start it if actor.default_action and len(actor.actions) == 1: actor.default_action.activate(view.window) return # Otherwise we put the possible actions in the application action_bar action_bar = view.parent_as(gui.ApplicationFrame).action_bar action_bar.set_actor(actor, view)