1 #----------------------------------------------------------------------------
2 # Name: AbstractEditor.py
3 # Purpose: Non-text editor for DataModel and Process
5 # Author: Peter Yared, Morgan Hua
9 # Copyright: (c) 2004-2005 ActiveGrid, Inc.
10 # License: wxWindows License
11 #----------------------------------------------------------------------------
16 import wx
.lib
.ogl
as ogl
17 import PropertyService
21 SELECT_BRUSH
= wx
.Brush("BLUE", wx
.SOLID
)
22 SHAPE_BRUSH
= wx
.Brush("WHEAT", wx
.SOLID
)
23 LINE_BRUSH
= wx
.BLACK_BRUSH
24 INACTIVE_SELECT_BRUSH
= wx
.Brush("LIGHT BLUE", wx
.SOLID
)
27 def GetRawModel(model
):
28 if hasattr(model
, "GetRawModel"):
29 rawModel
= model
.GetRawModel()
35 class CanvasView(wx
.lib
.docview
.View
):
38 #----------------------------------------------------------------------------
40 #----------------------------------------------------------------------------
43 def __init__(self
, brush
= SHAPE_BRUSH
):
44 wx
.lib
.docview
.View
.__init
__(self
)
49 self
._needEraseLasso
= False
50 self
._propShape
= None
53 def OnCreate(self
, doc
, flags
):
54 frame
= wx
.GetApp().CreateDocumentFrame(self
, doc
, flags
)
57 self
._CreateCanvas
(frame
)
58 sizer
.Add(self
._canvas
, 1, wx
.EXPAND
, 0)
65 def OnActivateView(self
, activate
, activeView
, deactiveView
):
66 if activate
and self
._canvas
:
67 # In MDI mode just calling set focus doesn't work and in SDI mode using CallAfter causes an endless loop
68 if self
.GetDocumentManager().GetFlags() & wx
.lib
.docview
.DOC_SDI
:
71 wx
.CallAfter(self
.SetFocus
)
76 self
._canvas
.SetFocus()
79 def OnFocus(self
, event
):
81 self
.FocusColorPropertyShape(True)
85 def OnKillFocus(self
, event
):
86 self
.FocusColorPropertyShape(False)
91 winWithFocus
= wx
.Window
.FindFocus()
95 if winWithFocus
== self
._canvas
:
97 winWithFocus
= winWithFocus
.GetParent()
101 def OnClose(self
, deleteWindow
= True):
102 statusC
= wx
.GetApp().CloseChildDocuments(self
.GetDocument())
103 statusP
= wx
.lib
.docview
.View
.OnClose(self
, deleteWindow
= deleteWindow
)
104 if hasattr(self
, "ClearOutline"):
105 wx
.CallAfter(self
.ClearOutline
) # need CallAfter because when closing the document, it is Activated and then Close, so need to match OnActivateView's CallAfter
106 if not (statusC
and statusP
):
109 if deleteWindow
and self
.GetFrame():
110 self
.GetFrame().Destroy()
114 def _CreateCanvas(self
, parent
):
115 self
._canvas
= ogl
.ShapeCanvas(parent
)
116 wx
.EVT_LEFT_DOWN(self
._canvas
, self
.OnLeftClick
)
117 wx
.EVT_LEFT_UP(self
._canvas
, self
.OnLeftUp
)
118 wx
.EVT_MOTION(self
._canvas
, self
.OnLeftDrag
)
119 wx
.EVT_LEFT_DCLICK(self
._canvas
, self
.OnLeftDoubleClick
)
120 wx
.EVT_KEY_DOWN(self
._canvas
, self
.OnKeyPressed
)
122 # need this otherwise mouse clicks don't set focus to this view
123 wx
.EVT_LEFT_DOWN(self
._canvas
, self
.OnFocus
)
124 wx
.EVT_LEFT_DCLICK(self
._canvas
, self
.OnFocus
)
125 wx
.EVT_RIGHT_DOWN(self
._canvas
, self
.OnFocus
)
126 wx
.EVT_RIGHT_DCLICK(self
._canvas
, self
.OnFocus
)
127 wx
.EVT_MIDDLE_DOWN(self
._canvas
, self
.OnFocus
)
128 wx
.EVT_MIDDLE_DCLICK(self
._canvas
, self
.OnFocus
)
130 wx
.EVT_KILL_FOCUS(self
._canvas
, self
.OnKillFocus
)
131 wx
.EVT_SET_FOCUS(self
._canvas
, self
.OnFocus
)
135 self
._canvas
.SetScrollbars(20, 20, maxWidth
/ 20, maxHeight
/ 20)
137 self
._canvas
.SetBackgroundColour(wx
.WHITE
)
138 self
._diagram
= ogl
.Diagram()
139 self
._canvas
.SetDiagram(self
._diagram
)
140 self
._diagram
.SetCanvas(self
._canvas
)
143 def OnKeyPressed(self
, event
):
144 key
= event
.KeyCode()
145 if key
== wx
.WXK_DELETE
:
151 def OnLeftClick(self
, event
):
152 self
.EraseRubberBand()
154 dc
= wx
.ClientDC(self
._canvas
)
155 self
._canvas
.PrepareDC(dc
)
157 # keep track of mouse down for group select
158 self
._pt
1 = event
.GetLogicalPosition(dc
) # this takes into account scrollbar offset
161 shape
= self
._canvas
.FindShape(self
._pt
1[0], self
._pt
1[1])[0]
163 self
.BringToFront(shape
)
166 event
.Skip() # pass on event to shape handler to take care of selection
169 elif event
.ControlDown() or event
.ShiftDown(): # extend select, don't deselect
172 # click on empty part of canvas, deselect everything
174 for shape
in self
._diagram
.GetShapeList():
175 if hasattr(shape
, "GetModel"):
178 shape
.Select(False, dc
)
180 self
._canvas
.Redraw(dc
)
182 self
.SetPropertyModel(None)
184 if len(self
.GetSelection()) == 0:
185 self
.SetPropertyShape(None)
189 def OnLeftDoubleClick(self
, event
):
190 propertyService
= wx
.GetApp().GetService(PropertyService
.PropertyService
)
192 propertyService
.ShowWindow()
195 def OnLeftDrag(self
, event
):
196 # draw lasso for group select
197 if self
._pt
1 and event
.LeftIsDown(): # we are in middle of lasso selection
198 self
.EraseRubberBand()
200 dc
= wx
.ClientDC(self
._canvas
)
201 self
._canvas
.PrepareDC(dc
)
202 self
._pt
2 = event
.GetLogicalPosition(dc
) # this takes into account scrollbar offset
203 self
.DrawRubberBand()
208 def OnLeftUp(self
, event
):
210 if self
._needEraseLasso
:
211 self
.EraseRubberBand()
213 dc
= wx
.ClientDC(self
._canvas
)
214 self
._canvas
.PrepareDC(dc
)
216 x2
, y2
= event
.GetLogicalPosition(dc
) # this takes into account scrollbar offset
218 tol
= self
._diagram
.GetMouseTolerance()
219 if abs(x1
- x2
) > tol
or abs(y1
- y2
) > tol
:
220 # make sure x1 < x2 and y1 < y2 to make comparison test easier
230 for shape
in self
._diagram
.GetShapeList():
231 if not shape
.GetParent() and hasattr(shape
, "GetModel"): # if part of a composite, don't select it
232 x
, y
= shape
.GetX(), shape
.GetY()
233 width
, height
= shape
.GetBoundingBoxMax()
234 selected
= x1
< x
- width
/2 and x2
> x
+ width
/2 and y1
< y
- height
/2 and y2
> y
+ height
/2
235 if event
.ControlDown() or event
.ShiftDown(): # extend select, don't deselect
237 shape
.Select(selected
, dc
)
238 else: # select items in lasso and deselect items out of lasso
239 shape
.Select(selected
, dc
)
240 self
._canvas
.Redraw(dc
)
247 def EraseRubberBand(self
):
248 if self
._needEraseLasso
:
249 self
._needEraseLasso
= False
251 dc
= wx
.ClientDC(self
._canvas
)
252 self
._canvas
.PrepareDC(dc
)
253 dc
.SetLogicalFunction(wx
.XOR
)
254 pen
= wx
.Pen(wx
.Colour(200, 200, 200), 1, wx
.SHORT_DASH
)
256 brush
= wx
.Brush(wx
.Colour(255, 255, 255), wx
.TRANSPARENT
)
258 dc
.ResetBoundingBox()
264 # make sure x1 < x2 and y1 < y2
265 # this will make (x1, y1) = upper left corner
275 # erase previous outline
276 dc
.SetClippingRegion(x1
, y1
, x2
- x1
, y2
- y1
)
277 dc
.DrawRectangle(x1
, y1
, x2
- x1
, y2
- y1
)
281 def DrawRubberBand(self
):
282 self
._needEraseLasso
= True
284 dc
= wx
.ClientDC(self
._canvas
)
285 self
._canvas
.PrepareDC(dc
)
286 dc
.SetLogicalFunction(wx
.XOR
)
287 pen
= wx
.Pen(wx
.Colour(200, 200, 200), 1, wx
.SHORT_DASH
)
289 brush
= wx
.Brush(wx
.Colour(255, 255, 255), wx
.TRANSPARENT
)
291 dc
.ResetBoundingBox()
297 # make sure x1 < x2 and y1 < y2
298 # this will make (x1, y1) = upper left corner
309 dc
.SetClippingRegion(x1
, y1
, x2
- x1
, y2
- y1
)
310 dc
.DrawRectangle(x1
, y1
, x2
- x1
, y2
- y1
)
314 def FindParkingSpot(self
, width
, height
):
315 """ given a width and height, find a upper left corner where shape can be parked without overlapping other shape """
316 offset
= 30 # space between shapes
319 maxX
= 700 # max distance to the right where we'll place tables
323 point
= self
.isSpotOccupied(x
, y
, width
, height
)
325 x
= point
[0] + offset
328 y
= point
[1] + offset
330 noParkingSpot
= False
335 def isSpotOccupied(self
, x
, y
, width
, height
):
336 """ returns None if at x,y,width,height no object occupies that rectangle,
337 otherwise returns lower right corner of object that occupies given x,y position
342 for shape
in self
._diagram
.GetShapeList():
343 if isinstance(shape
, ogl
.RectangleShape
) or isinstance(shape
, ogl
.EllipseShape
):
344 if shape
.GetParent() and isinstance(shape
.GetParent(), ogl
.CompositeShape
):
345 # skip, part of a composite shape
348 if hasattr(shape
, "GetModel"):
349 other_x
, other_y
, other_width
, other_height
= shape
.GetModel().getEditorBounds()
350 other_x2
= other_x
+ other_width
351 other_y2
= other_y
+ other_height
353 # shapes x,y are at the center of the shape, need to transform to upper left coordinate
354 other_width
, other_height
= shape
.GetBoundingBoxMax()
355 other_x
= shape
.GetX() - other_width
/2
356 other_y
= shape
.GetY() - other_height
/2
358 other_x2
= other_x
+ other_width
359 other_y2
= other_y
+ other_height
361 if ((other_x2
< other_x
or other_x2
> x
) and
362 (other_y2
< other_y
or other_y2
> y
) and
363 (x2
< x
or x2
> other_x
) and
364 (y2
< y
or y2
> other_y
)):
365 return (other_x2
, other_y2
)
369 #----------------------------------------------------------------------------
371 #----------------------------------------------------------------------------
373 def AddShape(self
, shape
, x
= None, y
= None, pen
= None, brush
= None, text
= None, eventHandler
= None):
374 if isinstance(shape
, ogl
.CompositeShape
):
375 dc
= wx
.ClientDC(self
._canvas
)
376 self
._canvas
.PrepareDC(dc
)
379 shape
.SetDraggable(True, True)
380 shape
.SetCanvas(self
._canvas
)
386 shape
.SetCentreResize(False)
390 shape
.SetBrush(brush
)
393 shape
.SetShadowMode(ogl
.SHADOW_RIGHT
)
394 self
._diagram
.AddShape(shape
)
397 eventHandler
= EditorCanvasShapeEvtHandler(self
)
398 eventHandler
.SetShape(shape
)
399 eventHandler
.SetPreviousHandler(shape
.GetEventHandler())
400 shape
.SetEventHandler(eventHandler
)
404 def RemoveShape(self
, model
= None, shape
= None):
405 if not model
and not shape
:
409 shape
= self
.GetShape(model
)
413 self
._diagram
.RemoveShape(shape
)
414 if isinstance(shape
, ogl
.CompositeShape
):
415 shape
.RemoveFromCanvas(self
._canvas
)
418 def UpdateShape(self
, model
):
419 for shape
in self
._diagram
.GetShapeList():
420 if hasattr(shape
, "GetModel") and shape
.GetModel() == model
:
421 x
, y
, w
, h
= model
.getEditorBounds()
425 if isinstance(shape
, ogl
.CompositeShape
):
426 if shape
.GetX() != newX
or shape
.GetY() != newY
:
427 dc
= wx
.ClientDC(self
._canvas
)
428 self
._canvas
.PrepareDC(dc
)
429 shape
.SetSize(w
, h
, True) # wxBug: SetSize must be before Move because links won't go to the right place
430 shape
.Move(dc
, newX
, newY
) # wxBug: Move must be before SetSize because links won't go to the right place
433 oldw
, oldh
= shape
.GetBoundingBoxMax()
436 if oldw
!= w
or oldh
!= h
or oldx
!= newX
or oldy
!= newY
:
442 shape
.ResetControlPoints()
443 self
._canvas
.Refresh()
447 def GetShape(self
, model
):
448 for shape
in self
._diagram
.GetShapeList():
449 if hasattr(shape
, "GetModel") and shape
.GetModel() == model
:
454 def GetSelection(self
):
455 return filter(lambda shape
: shape
.Selected(), self
._diagram
.GetShapeList())
458 def SetSelection(self
, models
, extendSelect
= False):
459 dc
= wx
.ClientDC(self
._canvas
)
460 self
._canvas
.PrepareDC(dc
)
462 if not isinstance(models
, type([])) and not isinstance(models
, type(())):
464 for shape
in self
._diagram
.GetShapeList():
465 if hasattr(shape
, "GetModel"):
466 if shape
.Selected() and not shape
.GetModel() in models
: # was selected, but not in new list, so deselect, unless extend select
468 shape
.Select(False, dc
)
470 elif not shape
.Selected() and shape
.GetModel() in models
: # was not selected and in new list, so select
471 shape
.Select(True, dc
)
473 elif extendSelect
and shape
.Selected() and shape
.GetModel() in models
: # was selected, but extend select means to deselect
474 shape
.Select(False, dc
)
477 self
._canvas
.Redraw(dc
)
480 def BringToFront(self
, shape
):
481 if shape
.GetParent() and isinstance(shape
.GetParent(), ogl
.CompositeShape
):
482 self
._diagram
.RemoveShape(shape
.GetParent())
483 self
._diagram
.AddShape(shape
.GetParent())
485 self
._diagram
.RemoveShape(shape
)
486 self
._diagram
.AddShape(shape
)
489 def SendToBack(self
, shape
):
490 if shape
.GetParent() and isinstance(shape
.GetParent(), ogl
.CompositeShape
):
491 self
._diagram
.RemoveShape(shape
.GetParent())
492 self
._diagram
.InsertShape(shape
.GetParent())
494 self
._diagram
.RemoveShape(shape
)
495 self
._diagram
.InsertShape(shape
)
498 def ScrollVisible(self
, shape
):
499 xUnit
, yUnit
= shape
._canvas
.GetScrollPixelsPerUnit()
500 scrollX
, scrollY
= self
._canvas
.GetViewStart() # in scroll units
501 scrollW
, scrollH
= self
._canvas
.GetSize() # in pixels
502 w
, h
= shape
.GetBoundingBoxMax() # in pixels
503 x
= shape
.GetX() - w
/2 # convert to upper left coordinate from center
504 y
= shape
.GetY() - h
/2 # convert to upper left coordinate from center
506 if x
>= scrollX
*xUnit
and x
<= scrollX
*xUnit
+ scrollW
: # don't scroll if already visible
511 if y
>= scrollY
*yUnit
and y
<= scrollY
*yUnit
+ scrollH
: # don't scroll if already visible
516 self
._canvas
.Scroll(x
, y
) # in scroll units
519 def SetPropertyShape(self
, shape
):
520 # no need to highlight if no PropertyService is running
521 propertyService
= wx
.GetApp().GetService(PropertyService
.PropertyService
)
522 if not propertyService
:
525 if shape
== self
._propShape
:
528 if hasattr(shape
, "GetPropertyShape"):
529 shape
= shape
.GetPropertyShape()
531 dc
= wx
.ClientDC(self
._canvas
)
532 self
._canvas
.PrepareDC(dc
)
535 # erase old selection if it still exists
536 if self
._propShape
and self
._propShape
in self
._diagram
.GetShapeList():
537 self
._propShape
.SetBrush(self
._brush
)
538 if (self
._propShape
._textColourName
in ["BLACK", "WHITE"]): # Would use GetTextColour() but it is broken
539 self
._propShape
.SetTextColour("BLACK", 0)
540 self
._propShape
.Draw(dc
)
543 self
._propShape
= shape
546 if self
._propShape
and self
._propShape
in self
._diagram
.GetShapeList():
548 self
._propShape
.SetBrush(SELECT_BRUSH
)
550 self
._propShape
.SetBrush(INACTIVE_SELECT_BRUSH
)
551 if (self
._propShape
._textColourName
in ["BLACK", "WHITE"]): # Would use GetTextColour() but it is broken
552 self
._propShape
.SetTextColour("WHITE", 0)
553 self
._propShape
.Draw(dc
)
558 def FocusColorPropertyShape(self
, gotFocus
=False):
559 # no need to change highlight if no PropertyService is running
560 propertyService
= wx
.GetApp().GetService(PropertyService
.PropertyService
)
561 if not propertyService
:
564 if not self
._propShape
:
567 dc
= wx
.ClientDC(self
._canvas
)
568 self
._canvas
.PrepareDC(dc
)
571 # draw deactivated selection
572 if self
._propShape
and self
._propShape
in self
._diagram
.GetShapeList():
574 self
._propShape
.SetBrush(SELECT_BRUSH
)
576 self
._propShape
.SetBrush(INACTIVE_SELECT_BRUSH
)
577 if (self
._propShape
._textColourName
in ["BLACK", "WHITE"]): # Would use GetTextColour() but it is broken
578 self
._propShape
.SetTextColour("WHITE", 0)
579 self
._propShape
.Draw(dc
)
584 #----------------------------------------------------------------------------
585 # Property Service methods
586 #----------------------------------------------------------------------------
588 def GetPropertyModel(self
):
589 if hasattr(self
, "_propModel"):
590 return self
._propModel
594 def SetPropertyModel(self
, model
):
595 # no need to set the model if no PropertyService is running
596 propertyService
= wx
.GetApp().GetService(PropertyService
.PropertyService
)
597 if not propertyService
:
600 if hasattr(self
, "_propModel") and model
== self
._propModel
:
603 self
._propModel
= model
604 propertyService
.LoadProperties(self
._propModel
, self
.GetDocument())
607 class EditorCanvasShapeMixin
:
613 def SetModel(self
, model
):
617 class EditorCanvasShapeEvtHandler(ogl
.ShapeEvtHandler
):
619 """ wxBug: Bug in OLG package. With wxShape.SetShadowMode() turned on, when we set the size,
620 the width/height is larger by 6 pixels. Need to subtract this value from width and height when we
626 def __init__(self
, view
):
627 ogl
.ShapeEvtHandler
.__init
__(self
)
631 def OnLeftClick(self
, x
, y
, keys
= 0, attachment
= 0):
632 shape
= self
.GetShape()
633 if hasattr(shape
, "GetModel"): # Workaround, on drag, we should deselect all other objects and select the clicked on object
634 model
= shape
.GetModel()
636 shape
= shape
.GetParent()
638 model
= shape
.GetModel()
640 self
._view
.SetSelection(model
, keys
== self
.SHIFT_KEY
or keys
== self
.CONTROL_KEY
)
641 self
._view
.SetPropertyShape(shape
)
642 self
._view
.SetPropertyModel(model
)
645 def OnEndDragLeft(self
, x
, y
, keys
= 0, attachment
= 0):
646 ogl
.ShapeEvtHandler
.OnEndDragLeft(self
, x
, y
, keys
, attachment
)
647 shape
= self
.GetShape()
648 if hasattr(shape
, "GetModel"): # Workaround, on drag, we should deselect all other objects and select the clicked on object
649 model
= shape
.GetModel()
651 parentShape
= shape
.GetParent()
653 model
= parentShape
.GetModel()
654 self
._view
.SetSelection(model
, keys
== self
.SHIFT_KEY
or keys
== self
.CONTROL_KEY
)
657 def OnMovePost(self
, dc
, x
, y
, oldX
, oldY
, display
):
658 if x
== oldX
and y
== oldY
:
660 if not self
._view
.GetDocument():
662 shape
= self
.GetShape()
663 if isinstance(shape
, EditorCanvasShapeMixin
) and shape
.Draggable():
664 model
= shape
.GetModel()
665 if hasattr(model
, "getEditorBounds") and model
.getEditorBounds():
666 x
, y
, w
, h
= model
.getEditorBounds()
667 newX
= shape
.GetX() - shape
.GetBoundingBoxMax()[0] / 2
668 newY
= shape
.GetY() - shape
.GetBoundingBoxMax()[1] / 2
669 newWidth
= shape
.GetBoundingBoxMax()[0]
670 newHeight
= shape
.GetBoundingBoxMax()[1]
671 if shape
._shadowMode
!= ogl
.SHADOW_NONE
:
672 newWidth
-= shape
._shadowOffsetX
673 newHeight
-= shape
._shadowOffsetY
674 newbounds
= (newX
, newY
, newWidth
, newHeight
)
676 if x
!= newX
or y
!= newY
or w
!= newWidth
or h
!= newHeight
:
677 self
._view
.GetDocument().GetCommandProcessor().Submit(EditorCanvasUpdateShapeBoundariesCommand(self
._view
.GetDocument(), model
, newbounds
))
684 class EditorCanvasUpdateShapeBoundariesCommand(wx
.lib
.docview
.Command
):
687 def __init__(self
, canvasDocument
, model
, newbounds
):
688 wx
.lib
.docview
.Command
.__init
__(self
, canUndo
= True)
689 self
._canvasDocument
= canvasDocument
691 self
._oldbounds
= model
.getEditorBounds()
692 self
._newbounds
= newbounds
696 name
= self
._canvasDocument
.GetNameForObject(self
._model
)
699 print "ERROR: AbstractEditor.EditorCanvasUpdateShapeBoundariesCommand.GetName: unable to get name for ", self
._model
700 return _("Move/Resize %s") % name
704 return self
._canvasDocument
.UpdateEditorBoundaries(self
._model
, self
._newbounds
)
708 return self
._canvasDocument
.UpdateEditorBoundaries(self
._model
, self
._oldbounds
)