Gabrielle wrote:
I wrote some code to let user draw a line with arrow from shape1 to shape2.
The code has no error, but it doesn't work, I can draw nothing. So would you
please have look at my code and tell me what's wrong?
You have quite a number of problems.
You do this during the event handler initialization:
self.ms = wx.MouseState()
then you refer to self.ms several times later. What this does is grab a
snapshot of the mouse state at the time the event handler is created.
At that time, the mouse buttons are all up and the mouse location is
0,0, and that's what you were using all throughout your code. That
object is not automatically updated with the latest information.
However, you don't even need this object; each of your drag routines is
handed the current x,y as parameters.
Next, you need to add control points to your line before you starting
changing the ends, using self.line.MakeLineControlPoints(2).
Next, you never added the lines to your canvas. You had a line object,
but it was not connected to any canvas.
Next, after changing the line positions, you never asked for the window
to be redrawn so nothing was ever shown.
I would argue that the HitTest calls in your code are completely
unnecessary. The event handler would not have been called unless the
mouse was impinging on this object. However, they don't hurt anything.
You could have learned a lot of this yourself by doing some very basic
debugging. If you had added print statements in your handler to print
out the values of the variables you were using, you would have seen
right away that the calls to self.ms.GetX() and self.ms.GetY() were not
doing what you thought they were.
This seems to do what you want:
class MyEvtHandler(ogl.ShapeEvtHandler):
def __init__(self, frame,shape):
ogl.ShapeEvtHandler.__init__(self,frame,shape)
self.shape = self.GetShape()
self.shape.SetAttachmentMode(True)
self.line = ogl.LineShape()
self.line.MakeLineControlPoints(2)
self.shape.GetCanvas().diagram.AddShape(self.line)
def OnBeginDragLeft(self, x,y, keys=0, attachment=0):
print "OnBeginDragLeft", x, y
if self.shape.HitTest(x, y):
self.line.AddArrow(ogl.ARROW_ARROW)
self.line.SetFrom(self.shape)
self.line.Show(True)
self.shape.GetCanvas().Refresh()
def OnDragLeft(self,draw,x,y,keys,attachment):
x1 = self.shape.GetX()
y1 = self.shape.GetY()
print "OnDragLeft", x, y, x1, y1
self.line.SetEnds(x,y,x1,y1)
self.line.Show(True)
self.shape.GetCanvas().Refresh()
def OnEndDragLeft(self,x,y,keys,attachment):
print "OnEndDragLeft", x, y
if self.shape.HitTest(x, y):
self.line.SetTo(self.shape)
self.line.Show(True)
self.shape.GetCanvas().Refresh()
def OnMoveLink(self,dc,moveControlPoints):
pass
···
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.