events from each shape in a composite shape

Hi all, I am trying to make an app which works as: when below code is
executed, 3 shapes (1 white 2 rectangle) are displayed. I want to make
the size of blue rectangle to change when i drag red rectangle. The
size of blue rectangle should change according to change in position
of red rectangle. Unable to get events from red rectangle when
"constrained_shape2.SetDraggable(False)" is commented. Appreciate your
help. Also is it possible to make a shape NOT movable? waiting for
your kind inputs. thank you.

PS:

import wx
import wx.lib.ogl as ogl

import sys
sys.path.append('C:\Python26\wxPython2.8 Docs and Demos\demo')

import images

···

#----------------------------------------------------------------------

class CompositeShape(ogl.CompositeShape):
    def __init__(self, canvas):
        ogl.CompositeShape.__init__(self)

        self.SetCanvas(canvas)

        constraining_shape = ogl.RectangleShape(120, 100)
        constrained_shape1 = ogl.CircleShape(20)
        constrained_shape1.SetX(-60)
        constrained_shape1.SetY(50)
        constrained_shape2 = ogl.RectangleShape(20, 20)
        constrained_shape2.SetX(60)
        constrained_shape2.SetY(-50)

        constraining_shape.SetBrush(wx.BLUE_BRUSH)
        constrained_shape2.SetBrush(wx.RED_BRUSH)

        self.AddChild(constraining_shape)
        self.AddChild(constrained_shape1)
        self.AddChild(constrained_shape2)

        # If we don't do this, the shapes will be able to move on
their
        # own, instead of moving the composite
        constraining_shape.SetDraggable(False)
        constrained_shape1.SetDraggable(False)
        #constrained_shape2.SetDraggable(False)

        # If we don't do this the shape will take all left-clicks for
itself
        #constraining_shape.SetSensitivityFilter(0)

#----------------------------------------------------------------------

class MyEvtHandler(ogl.ShapeEvtHandler):
    def __init__(self, log, frame):
        ogl.ShapeEvtHandler.__init__(self)
        self.log = log
        self.statbarFrame = frame

    def UpdateStatusBar(self, shape):
        x, y = shape.GetX(), shape.GetY()
        width, height = shape.GetBoundingBoxMax()
        self.statbarFrame.SetStatusText("Pos: (%d, %d) Size: (%d,
%d)" %
                                        (x, y, width, height))

    def OnEndDragLeft(self, x, y, keys=0, attachment=0):
        shape = self.GetShape()
        print shape.GetChildren()
        ogl.ShapeEvtHandler.OnEndDragLeft(self, x, y, keys,
attachment)

        if not shape.Selected():
            self.OnLeftClick(x, y, keys, attachment)

        self.UpdateStatusBar(shape)

#----------------------------------------------------------------------

class TestWindow(ogl.ShapeCanvas):
    def __init__(self, parent, log, frame):
        ogl.ShapeCanvas.__init__(self, parent)

        maxWidth = 1000
        maxHeight = 1000
        self.SetScrollbars(20, 20, maxWidth/20, maxHeight/20)

        self.log = log
        self.frame = frame
        self.SetBackgroundColour("LIGHT BLUE") #wx.WHITE)
        self.diagram = ogl.Diagram()
        self.SetDiagram(self.diagram)
        self.diagram.SetCanvas(self)
        self.shapes = []
        self.save_gdi = []

        rRectBrush = wx.Brush("MEDIUM TURQUOISE", wx.SOLID)
        dsBrush = wx.Brush("WHEAT", wx.SOLID)

        self.MyAddShape(
            CompositeShape(self),
            100, 260, wx.BLACK_PEN, wx.RED_BRUSH, "Composite"
            )

        for x in range(len(self.shapes)):
            fromShape = self.shapes[x]
            if x+1 == len(self.shapes):
                toShape = self.shapes[0]
            else:
                toShape = self.shapes[x+1]

            line = ogl.LineShape()
            line.SetCanvas(self)
            line.SetPen(wx.BLACK_PEN)
            line.SetBrush(wx.BLACK_BRUSH)
            line.AddArrow(ogl.ARROW_ARROW)
            line.MakeLineControlPoints(2)
            fromShape.AddLine(line, toShape)
            self.diagram.AddShape(line)
            line.Show(True)

    def MyAddShape(self, shape, x, y, pen, brush, text):
        # Composites have to be moved for all children to get in place
        if isinstance(shape, ogl.CompositeShape):
            dc = wx.ClientDC(self)
            self.PrepareDC(dc)
            shape.Move(dc, x, y)
            '''shape.Select(False, dc)
            canvas.Redraw(dc)'''
        else:
            shape.SetDraggable(True, True)
        shape.SetCanvas(self)
        shape.SetX(x)
        shape.SetY(y)
        if pen: shape.SetPen(pen)
        if brush: shape.SetBrush(brush)
        if text:
            for line in text.split('\n'):
                shape.AddText(line)
        #shape.SetShadowMode(ogl.SHADOW_RIGHT)
        self.diagram.AddShape(shape)
        shape.Show(True)

        evthandler = MyEvtHandler(self.log, self.frame)
        evthandler.SetShape(shape)
        evthandler.SetPreviousHandler(shape.GetEventHandler())
        shape.SetEventHandler(evthandler)

        self.shapes.append(shape)
        return shape

    def OnBeginDragLeft(self, x, y, keys):
        self.log.write("OnBeginDragLeft: %s, %s, %s\n" % (x, y, keys))

    def OnEndDragLeft(self, x, y, keys):
        self.log.write("OnEndDragLeft: %s, %s, %s\n" % (x, y, keys))

#----------------------------------------------------------------------

def runTest(frame, nb, log):
    # This creates some pens and brushes that the OGL library uses.
    # It should be called after the app object has been created, but
    # before OGL is used.
    ogl.OGLInitialize()

    win = TestWindow(nb, log, frame)
    return win

#----------------------------------------------------------------------

if __name__ == '__main__':
    import sys, os
    import run
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])

Hi all, I am trying to make an app which works as: when below code is
executed, 3 shapes (1 white 2 rectangle) are displayed. I want to make
the size of blue rectangle to change when i drag red rectangle. The
size of blue rectangle should change according to change in position
of red rectangle. Unable to get events from red rectangle when
"constrained_shape2.SetDraggable(False)" is commented.

I don't do anything with OGL anymore so this is mostly guesses, (but all the answers are in the source code which you can read any time you want) but I would start with either pushing a custom ShapeEvtHandler onto your child shapes, or deriving new classes for them that override the appropriate event methods. All the events coming from the composite shape are going to be the composite shape's events, not the components, so you'll need to get them separately.

Appreciate your
help. Also is it possible to make a shape NOT movable?

I'm sure it is, but I don't remember the details.

···

On 10/27/09 9:13 AM, Sandeep wrote:

--
Robin Dunn
Software Craftsman

Robin Dunn <robin@alldunn.com> writes:

> > Appreciate your
> > help. Also is it possible to make a shape NOT movable?
>
> I'm sure it is, but I don't remember the details.

I'm also in the "don't do anything with OGL anymore" category but
looking at my old code revealed that

shape.SetDraggable(False,False)

should do the trick.

···

--
  Pierre Hjälm - More into playing with gstreamer and wxpython these days

Thanks Robin and Pierre.

shape.SetDraggable(False,False) will make the the shapes move. I want only red rectangle to move and size of blue to set postion of red.

···

On Wed, Oct 28, 2009 at 11:44 AM, Pierre Hjälm pierre.hjalm@ekonomikum.uu.se wrote:

Robin Dunn robin@alldunn.com writes:

Appreciate your

help. Also is it possible to make a shape NOT movable?

I’m sure it is, but I don’t remember the details.

I’m also in the “don’t do anything with OGL anymore” category but

looking at my old code revealed that

shape.SetDraggable(False,False)

should do the trick.

Pierre Hjälm - More into playing with gstreamer and wxpython these days