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:])