Source

gtkarchitect-main / gtkarchitect / view / palette.py


import gtk, gobject

from kiwi.utils import gsignal

from .base import Slave


class PaletteSection(gobject.GObject):

    gsignal('entity-selected', gobject.TYPE_PYOBJECT)

    def __init__(self, section, items):
        gobject.GObject.__init__(self)
        self.section = section
        self.items = items

    def create_button(self, group):
        button = gtk.RadioButton(group, self.section)
        return button

    def create_items(self):
        sw = gtk.ScrolledWindow()
        vb = gtk.VBox()
        sw.add_with_viewport(vb)
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

        for item in self.items:
            b = gtk.Button(item.entity_name)
            vb.pack_start(b, expand=False)

            b.connect('clicked', self.on_item__clicked, item)
        return sw

    def on_item__clicked(self, button, entitydef):
        self.emit('entity-selected', entitydef)



class Palette(Slave):

    gsignal('entity-selected', gobject.TYPE_PYOBJECT)

    def create_ui(self, catalog):
        self.current_entity_def = None

        self.palettes = {}

        t = self.get_toplevel()
        self.buttons = gtk.VBox()

        self.current_label = gtk.Label()
        t.pack_start(self.current_label, expand=False)

        t.pack_start(self.buttons, expand=False)
        self.sections = gtk.Notebook()
        t.pack_start(self.sections)

        current_button = None
        for section, items in catalog.entity_defs.items():
            section_widget = PaletteSection(section, items)
            section_widget.connect('entity-selected', self.on_entity_selected)

            # The radio button that selects which widget set we are using
            button = section_widget.create_button(current_button)
            self.buttons.pack_start(button, expand=False)
            current_button = button

            # the list of entities for each palette
            entity_list = section_widget.create_items()
            self.sections.append_page(entity_list)

            button.connect('toggled', self.on_section_changed, entity_list)

    def on_section_changed(self, radio, entity_list):
        self.sections.set_current_page(self.sections.page_num(entity_list))

    def on_entity_selected(self, section, entity_def):
        self.set_current_entity_def(entity_def)

    def set_current_entity_def(self, entity_def):
        print entity_def, 'eeee'
        self.current_entity_def = entity_def
        self.emit('entity-selected', entity_def)
        if entity_def is not None:
            current_text = entity_def.entity_name
        else:
            current_text = ''
        print current_text
        self.current_label.set_text(current_text)