Source

gtkarchitect-main / gtkarchitect / view / gadget.py

Full commit

import gtk, gobject, gtk.gdk

from kiwi.utils import gsignal

from base import Slave

from ..controller.command import EntityPropertyChangeCommand
from .placeholder import Placeholder


class GadgetEventbox(gtk.EventBox):

    __gtype_name__ = 'ArchitectGadgetEventBox'


    def __init__(self):
        gtk.EventBox.__init__(self)
        self.set_property('can-focus', True)
        self.set_above_child(True)



class GadgetHolder(gtk.Frame):

    __gtype_name__ = 'ArchitectGadgetHolder'

    gsignal('selected')

    def __init__(self):
        gtk.Frame.__init__(self)
        self.gh = GadgetEventbox()
        self.gh.connect('button-press-event', self.on_button)
        self.do_add(self, self.gh)
        red = gtk.gdk.color_parse("red")
        self.modify_bg(gtk.STATE_NORMAL, red)
        self.set_shadow_type(gtk.SHADOW_NONE)

    def on_button(self, eb, event):
        self.select()
        return True

    def select(self):
        self.set_shadow_type(gtk.SHADOW_IN)
        self.emit('selected')


    def deselect(self):
        self.set_shadow_type(gtk.SHADOW_NONE)

    def set_above_child(self, above):
        self.gh.set_above_child(above)

    def add(self, child):
        self.gh.add(child)



class Gadget(Slave):

    gsignal('placeholder-clicked', gobject.TYPE_PYOBJECT,
                                   gobject.TYPE_PYOBJECT)

    gsignal('selected')

    def __init__(self, entity):
        self.entity = entity
        self.placeholders = []
        Slave.__init__(self)

    def create_toplevel_widget(self):
        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 set_defaults(self):
        pass

    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



# Abstract
class WidgetGadget(Gadget):

    def create_ui(self):
        t = self.get_toplevel()
        self.widget = self.create_widget()
        t.add(self.widget)

    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
    e = Element('a')

    g = Gadget(e)
    g.set_prop('foo', 'blah')
    c = g.set_prop('foo', 'blar')
    print tostring(e)
    c.undo()
    print tostring(e)