Commits

Ali Afshar committed fe2fa5a

handle containers with multiple items

Comments (0)

Files changed (5)

gtkarchitect/gtkentities/__init__.py

 import gtk
 
 from ..model.definitions import EntityDefinition, entity_catalog
-from ..view.gadget import Gadget, ContainerGadget
+from ..view.gadget import Gadget, ContainerGadget, WidgetGadget
 from ..view.placeholder import Placeholder
 
 
-class ButtonGadget(Gadget):
+class ButtonGadget(WidgetGadget):
 
-    def create_ui(self):
-        t = self.get_toplevel()
-        self.widget = gtk.Button()
-        t.add(self.widget)
+    def create_widget(self):
+        return gtk.Button()
 
 
 class WindowGadget(ContainerGadget):
 
     def create_ui(self):
         t = self.get_toplevel()
-        self.widget = gtk.Frame('hello')
+        self.widget = gtk.Frame('')
         t.add(self.widget)
 
 
+class BoxGadget(ContainerGadget):
+    """Abstract"""
+
+    def set_defaults(self):
+        for i in range(3):
+            self.create_placeholder()
+
+    def add_widget(self, widget, position):
+        ContainerGadget.add_widget(self, widget, position)
+        self.widget.reorder_child(widget, position)
+
+
+class VBoxGadget(BoxGadget):
+
+    def create_widget(self):
+        return gtk.VBox()
+
+
+class HBoxGadget(BoxGadget):
+
+    def create_widget(self):
+        return gtk.HBox()
+
+
+class NotebookGadget(ContainerGadget):
+
+    def set_defaults(self):
+        self.create_placeholder()
+        self.create_placeholder()
+        self.create_placeholder()
+
+    def create_widget(self):
+        return gtk.Notebook()
+
+    def add_widget(self, widget, position):
+        ContainerGadget.add_widget(self, widget, position)
+        self.widget.reorder_child(widget, position)
+
+
 entity_catalog.register_entity_def(
     'GTK Widgets', EntityDefinition('GtkButton', gadget_type=ButtonGadget)
 )
     'GTK Widgets', EntityDefinition('GtkFrame', gadget_type=FrameGadget)
 )
 
+entity_catalog.register_entity_def(
+    'GTK Widgets', EntityDefinition('GtkVBox', gadget_type=VBoxGadget)
+)
+
+entity_catalog.register_entity_def(
+    'GTK Widgets', EntityDefinition('GtkHBox', gadget_type=HBoxGadget)
+)
+
+entity_catalog.register_entity_def(
+    'GTK Widgets', EntityDefinition('GtkNotebook', gadget_type=NotebookGadget)
+)
+
+
+
+class ObjectTreeGadget(Gadget):
+
+    def create_widget(self):
+        from kiwi.ui.objectlist import ObjectList
+        o = ObjectList()
+        o.show_all()
+        return o
+
+entity_catalog.register_entity_def(
+    'Kiwi Widgets', EntityDefinition('ObjectList',
+    gadget_type=ObjectTreeGadget)
+)

gtkarchitect/model/project.py

 
 class Project(gobject.GObject):
 
-    gsignal('entity-added', gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT)
+    gsignal('entity-added', gobject.TYPE_PYOBJECT,
+                            gobject.TYPE_PYOBJECT,
+                            gobject.TYPE_PYOBJECT)
 
     def __init__(self, tree=None):
         gobject.GObject.__init__(self)
             tree = Element('interface')
         self.tree = tree
 
-    def add_entity(self, entity, parent=None):
+    def add_entity(self, entity, parent=None, position=0):
         if parent is None:
             parent_entity = self.tree
             no_child = True
             parent_entity = parent
             no_child = False
         add_entity_child(parent_entity, entity, no_child=no_child)
-        self.emit('entity-added', entity, parent)
+        self.emit('entity-added', entity, parent, position)
 
-    def create_entity(self, definition, parent=None):
+    def create_entity(self, definition, parent=None, position=0):
+        print 'ce', position
         entity = definition.create_entity()
-        self.add_entity(entity, parent)
+        self.add_entity(entity, parent, position)
 
     def dump(self):
         return tostring(self.tree)

gtkarchitect/view/gadget.py

 
-import gtk, gobject
+import gtk, gobject, gtk.gdk
 
 from kiwi.utils import gsignal
 
 
 class GadgetHolder(gtk.EventBox):
 
+    __gtype_name__ = 'ArchitectGadgetHolder'
+
+    gsignal('selected')
+
     def __init__(self):
         gtk.EventBox.__init__(self)
+        self.set_property('can-focus', True)
+        self.set_above_child(True)
+        self.selected = False
+
+    def do_button_press_event(self, event):
+        self.select()
+        return True
+
+    def select(self):
+        self.emit('selected')
+        self.selected = True
+        self.drag_highlight()
+
+    def deselect(self):
+        self.selected = False
+        self.drag_unhighlight()
+
+
+    def do_fiocus_in_event(self, event):
+        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('red'))
+
+    def do_fiocus_out_event(self, event):
+        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('black'))
 
 
 class Gadget(Slave):
 
-    gsignal('placeholder-clicked', gobject.TYPE_PYOBJECT)
+    gsignal('placeholder-clicked', gobject.TYPE_PYOBJECT,
+                                   gobject.TYPE_PYOBJECT)
+
+    gsignal('selected')
 
     def __init__(self, entity):
         self.entity = entity
         Slave.__init__(self)
 
     def create_toplevel_widget(self):
-        return GadgetHolder()
+        holder = GadgetHolder()
+        holder.connect('selected', self._on_holder_selected)
+        return holder
+
+    def _on_holder_selected(self, holder):
+        self.emit('selected')
 
     def create_ui(self):
         pass
 
-    def fill_empty(self):
+    def set_defaults(self):
         pass
 
-    def register_placeholder(self, placeholder):
-        placeholder.connect('clicked', self._on_placeholder_clicked)
-        self.placeholders.append(placeholder)
+    def deselect(self):
+        self.get_toplevel().deselect()
+
+    def select(self):
+        self.get_toplevel().select()
 
     def set_prop(self, property, value):
         c = EntityPropertyChangeCommand(None)
         c.do(self.entity, property, value)
         return c
 
-    def _on_placeholder_clicked(self, placeholder):
-        print 'ph clicked'
-        self.emit('placeholder-clicked', placeholder)
 
-    def add_gadget(self, gadget):
-        p = self.placeholders.pop(0)
-        self.widget.remove(p)
-        p.unparent()
-        print gadget.get_toplevel()
-        self.widget.add(gadget.get_toplevel())
-        self.widget.show_all()
 
+# Abstract
+class WidgetGadget(Gadget):
 
-class ContainerGadget(Gadget):
+    def create_ui(self):
+        t = self.get_toplevel()
+        self.widget = self.create_widget()
+        t.add(self.widget)
 
-    def fill_empty(self):
-        print 'fill empty'
+    def create_widget(self):
+        raise NotImplementedError
+
+
+class ContainerGadget(WidgetGadget):
+
+    def create_toplevel_widget(self):
+        holder = WidgetGadget.create_toplevel_widget(self)
+        holder.set_above_child(False)
+        return holder
+
+    def set_defaults(self):
+        self.create_placeholder()
+
+    def create_placeholder(self):
         p = Placeholder()
         self.register_placeholder(p)
         self.widget.add(p)
         p.show()
 
+    def register_placeholder(self, placeholder):
+        placeholder.connect('clicked', self._on_placeholder_clicked)
+        self.placeholders.append(placeholder)
+
+    def _on_child_property_set(self, child, property, value):
+        pass
+
+    def set_child_property(self, child, property, value):
+        self.widget.set_child_property(child, property, value)
+
+    def _on_placeholder_clicked(self, placeholder):
+        position = self.widget.get_children().index(placeholder)
+        self.emit('placeholder-clicked', placeholder, position)
+
+    def add_gadget(self, gadget, position=0):
+        p = self.widget.get_children()[position]
+        self.widget.remove(p)
+        p.unparent()
+        self.add_widget(gadget.get_toplevel(), position)
+        self.widget.show_all()
+
+    def add_widget(self, widget, position):
+        """Overridable"""
+        self.widget.add(widget)
 
 if __name__ == '__main__':
     from lxml.etree import Element, tostring

gtkarchitect/view/main.py

 
     def create_ui(self):
         self.gadgets = {}
+        self.selected_gadget = None
 
         palette = Palette(entity_catalog)
         workspace = Workspace()
 
         vb = gtk.VBox()
         self.get_toplevel().add(vb)
+        self.get_toplevel().resize(500, 500)
 
         p = gtk.HPaned()
         vb.pack_start(p)
         self.project.connect('entity-added', self._on_project_entity_added)
 
 
-    def _on_project_entity_added(self, project, entity, parent):
+    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)
+            self.add_entity(entity, entity_def, parent, position)
 
-    def add_entity(self, entity, entity_def, parent):
+    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.fill_empty()
+        gadget.set_defaults()
         self.register_gadget(gadget)
-        parent_gadget.add_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.fill_empty()
+        g.set_defaults()
         self.workspace.add_toplevel(g.get_toplevel())
         self.workspace.show_all()
         self.projecttree.add_entity(entity, None)
         self.gadgets[gadget.entity] = gadget
         gadget.connect('placeholder-clicked',
                             self._on_gadget_placeholder_clicked)
-        
-    def _on_gadget_placeholder_clicked(self, gadget, placeholder):
+        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;'
-            self.project.create_entity(entity_def, gadget.entity)
+            print position
+            self.project.create_entity(entity_def, gadget.entity, position)
 
+    def on_projecttree__item_selected(self, tree, item):
+        self.gadgets.get(item).select()

gtkarchitect/view/projecttree.py

+
+import gtk, gobject
 
 from kiwi.ui.objectlist import ObjectTree, Column
 
+from kiwi.utils import gsignal
+
 from ..model.entity import get_entity_type
 from .base import Slave
 
 
 class ProjectTree(Slave):
 
+    gsignal('item-selected', gobject.TYPE_PYOBJECT)
+
     def create_ui(self):
         self.tree = ObjectTree(EntityColumn('name'))
         t = self.get_toplevel()
         t.add(self.tree)
 
+    def on_tree__selection_changed(self, tree, item):
+        self.emit('item-selected', item)
+
     def set_project(self, project):
         self.project = project
         self.add_entity(self.project.tree)