Source

gtkarchitect-main / gtkarchitect / view / main.py

Full commit

import gtk, gobject

from .base import MainView
from .palette import Palette
from .workspace import Workspace
from .projecttree import ProjectTree

from ..model.definitions import entity_catalog, get_entity_def

from .. import gtkentities


class ArchitectMainView(MainView):

    def create_ui(self):
        self.gadgets = {}
        self.selected_gadget = None

        palette = Palette(entity_catalog)
        workspace = Workspace()
        projecttree = ProjectTree()

        vb = gtk.VBox()
        self.get_toplevel().add(vb)
        self.get_toplevel().resize(500, 500)

        p = gtk.HPaned()
        vb.pack_start(p)
        p.pack1(palette.get_toplevel())
        p2 = gtk.HPaned()
        p.pack2(p2)
        p2.pack1(workspace.get_toplevel())
        p2.pack2(projecttree.get_toplevel())

        self.palette = palette
        self.workspace = workspace
        self.projecttree = projecttree

        from ..utils.debug import DebugSlave
        self.debug = DebugSlave(self)
        vb.pack_start(self.debug.get_toplevel())

        vb.show_all()

    def on_palette__entity_selected(self, palette, entity_def):
        if entity_def is not None:
            if entity_def.is_toplevel:
                self.project.create_entity(entity_def)
                gobject.idle_add(self.palette.set_current_entity_def, None)

    def set_project(self, project):
        self.project = project
        self.projecttree.set_project(project)
        self.project.connect('entity-added', self._on_project_entity_added)


    def _on_project_entity_added(self, project, entity, parent, position):
        entity_def = get_entity_def(entity)
        if entity_def.is_toplevel:
            self.add_toplevel_entity(entity, entity_def)
        else:
            self.add_entity(entity, entity_def, parent, position)

    def add_entity(self, entity, entity_def, parent, position=0):
        print 'ae', position
        parent_gadget = self.gadgets.get(parent)
        gadget = entity_def.gadget_type(entity)
        gadget.set_defaults()
        self.register_gadget(gadget)
        parent_gadget.add_gadget(gadget, position)
        self.projecttree.add_entity(entity, parent)

    def add_toplevel_entity(self, entity, entity_def):
        g = entity_def.gadget_type(entity)
        self.register_gadget(g)
        g.set_defaults()
        self.workspace.add_toplevel(g.get_toplevel())
        self.workspace.show_all()
        self.projecttree.add_entity(entity, None)
        print 'top'

    def register_gadget(self, gadget):
        self.gadgets[gadget.entity] = gadget
        gadget.connect('placeholder-clicked',
                            self._on_gadget_placeholder_clicked)
        gadget.connect('selected',
                            self._on_gadget_selected)

    def _on_gadget_selected(self, gadget):
        if self.selected_gadget is not None:
            self.selected_gadget.deselect()
        self.selected_gadget = gadget

    def _on_gadget_placeholder_clicked(self, gadget, placeholder, position):
        entity_def = self.palette.current_entity_def
        if entity_def is None:
            print 'should select'
        else:
            print 'adding ed;'
            print position
            self.project.create_entity(entity_def, gadget.entity, position)

    def on_projecttree__item_selected(self, tree, item):
        self.gadgets.get(item).select()