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
52 self
._maxHeight
= 16000
56 """ for Print Preview and Print """
58 self
._canvas
.Redraw(dc
)
62 def OnCreate(self
, doc
, flags
):
63 frame
= wx
.GetApp().CreateDocumentFrame(self
, doc
, flags
)
66 self
._CreateCanvas
(frame
)
67 sizer
.Add(self
._canvas
, 1, wx
.EXPAND
, 0)
74 def OnActivateView(self
, activate
, activeView
, deactiveView
):
75 if activate
and self
._canvas
:
76 # In MDI mode just calling set focus doesn't work and in SDI mode using CallAfter causes an endless loop
77 if self
.GetDocumentManager().GetFlags() & wx
.lib
.docview
.DOC_SDI
:
80 wx
.CallAfter(self
.SetFocus
)
85 self
._canvas
.SetFocus()
88 def OnFocus(self
, event
):
90 self
.FocusColorPropertyShape(True)
94 def OnKillFocus(self
, event
):
95 self
.FocusColorPropertyShape(False)
100 winWithFocus
= wx
.Window
.FindFocus()
104 if winWithFocus
== self
._canvas
:
106 winWithFocus
= winWithFocus
.GetParent()
110 def OnClose(self
, deleteWindow
= True):
111 statusC
= wx
.GetApp().CloseChildDocuments(self
.GetDocument())
112 statusP
= wx
.lib
.docview
.View
.OnClose(self
, deleteWindow
= deleteWindow
)
113 if hasattr(self
, "ClearOutline"):
114 wx
.CallAfter(self
.ClearOutline
) # need CallAfter because when closing the document, it is Activated and then Close, so need to match OnActivateView's CallAfter
115 if not (statusC
and statusP
):
118 if deleteWindow
and self
.GetFrame():
119 self
.GetFrame().Destroy()
123 def _CreateCanvas(self
, parent
):
124 self
._canvas
= ogl
.ShapeCanvas(parent
)
125 wx
.EVT_LEFT_DOWN(self
._canvas
, self
.OnLeftClick
)
126 wx
.EVT_LEFT_UP(self
._canvas
, self
.OnLeftUp
)
127 wx
.EVT_MOTION(self
._canvas
, self
.OnLeftDrag
)
128 wx
.EVT_LEFT_DCLICK(self
._canvas
, self
.OnLeftDoubleClick
)
129 wx
.EVT_KEY_DOWN(self
._canvas
, self
.OnKeyPressed
)
131 # need this otherwise mouse clicks don't set focus to this view
132 wx
.EVT_LEFT_DOWN(self
._canvas
, self
.OnFocus
)
133 wx
.EVT_LEFT_DCLICK(self
._canvas
, self
.OnFocus
)
134 wx
.EVT_RIGHT_DOWN(self
._canvas
, self
.OnFocus
)
135 wx
.EVT_RIGHT_DCLICK(self
._canvas
, self
.OnFocus
)
136 wx
.EVT_MIDDLE_DOWN(self
._canvas
, self
.OnFocus
)
137 wx
.EVT_MIDDLE_DCLICK(self
._canvas
, self
.OnFocus
)
139 wx
.EVT_KILL_FOCUS(self
._canvas
, self
.OnKillFocus
)
140 wx
.EVT_SET_FOCUS(self
._canvas
, self
.OnFocus
)
142 self
._canvas
.SetScrollbars(20, 20, self
._maxWidth
/ 20, self
._maxHeight
/ 20)
144 self
._canvas
.SetBackgroundColour(wx
.WHITE
)
145 self
._diagram
= ogl
.Diagram()
146 self
._canvas
.SetDiagram(self
._diagram
)
147 self
._diagram
.SetCanvas(self
._canvas
)
150 def OnKeyPressed(self
, event
):
151 key
= event
.KeyCode()
152 if key
== wx
.WXK_DELETE
:
158 def OnLeftClick(self
, event
):
159 self
.EraseRubberBand()
161 dc
= wx
.ClientDC(self
._canvas
)
162 self
._canvas
.PrepareDC(dc
)
164 # keep track of mouse down for group select
165 self
._pt
1 = event
.GetLogicalPosition(dc
) # this takes into account scrollbar offset
168 shape
= self
._canvas
.FindShape(self
._pt
1[0], self
._pt
1[1])[0]
170 self
.BringToFront(shape
)
173 event
.Skip() # pass on event to shape handler to take care of selection
176 elif event
.ControlDown() or event
.ShiftDown(): # extend select, don't deselect
179 # click on empty part of canvas, deselect everything
181 for shape
in self
._diagram
.GetShapeList():
182 if hasattr(shape
, "GetModel"):
185 shape
.Select(False, dc
)
187 self
._canvas
.Redraw(dc
)
189 self
.SetPropertyModel(None)
191 if len(self
.GetSelection()) == 0:
192 self
.SetPropertyShape(None)
196 def OnLeftDoubleClick(self
, event
):
197 propertyService
= wx
.GetApp().GetService(PropertyService
.PropertyService
)
199 propertyService
.ShowWindow()
202 def OnLeftDrag(self
, event
):
203 # draw lasso for group select
204 if self
._pt
1 and event
.LeftIsDown(): # we are in middle of lasso selection
205 self
.EraseRubberBand()
207 dc
= wx
.ClientDC(self
._canvas
)
208 self
._canvas
.PrepareDC(dc
)
209 self
._pt
2 = event
.GetLogicalPosition(dc
) # this takes into account scrollbar offset
210 self
.DrawRubberBand()
215 def OnLeftUp(self
, event
):
217 if self
._needEraseLasso
:
218 self
.EraseRubberBand()
220 dc
= wx
.ClientDC(self
._canvas
)
221 self
._canvas
.PrepareDC(dc
)
223 x2
, y2
= event
.GetLogicalPosition(dc
) # this takes into account scrollbar offset
225 tol
= self
._diagram
.GetMouseTolerance()
226 if abs(x1
- x2
) > tol
or abs(y1
- y2
) > tol
:
227 # make sure x1 < x2 and y1 < y2 to make comparison test easier
237 for shape
in self
._diagram
.GetShapeList():
238 if not shape
.GetParent() and hasattr(shape
, "GetModel"): # if part of a composite, don't select it
239 x
, y
= shape
.GetX(), shape
.GetY()
240 width
, height
= shape
.GetBoundingBoxMax()
241 selected
= x1
< x
- width
/2 and x2
> x
+ width
/2 and y1
< y
- height
/2 and y2
> y
+ height
/2
242 if event
.ControlDown() or event
.ShiftDown(): # extend select, don't deselect
244 shape
.Select(selected
, dc
)
245 else: # select items in lasso and deselect items out of lasso
246 shape
.Select(selected
, dc
)
247 self
._canvas
.Redraw(dc
)
254 def EraseRubberBand(self
):
255 if self
._needEraseLasso
:
256 self
._needEraseLasso
= False
258 dc
= wx
.ClientDC(self
._canvas
)
259 self
._canvas
.PrepareDC(dc
)
260 dc
.SetLogicalFunction(wx
.XOR
)
261 pen
= wx
.Pen(wx
.Colour(200, 200, 200), 1, wx
.SHORT_DASH
)
263 brush
= wx
.Brush(wx
.Colour(255, 255, 255), wx
.TRANSPARENT
)
265 dc
.ResetBoundingBox()
271 # make sure x1 < x2 and y1 < y2
272 # this will make (x1, y1) = upper left corner
282 # erase previous outline
283 dc
.SetClippingRegion(x1
, y1
, x2
- x1
, y2
- y1
)
284 dc
.DrawRectangle(x1
, y1
, x2
- x1
, y2
- y1
)
288 def DrawRubberBand(self
):
289 self
._needEraseLasso
= True
291 dc
= wx
.ClientDC(self
._canvas
)
292 self
._canvas
.PrepareDC(dc
)
293 dc
.SetLogicalFunction(wx
.XOR
)
294 pen
= wx
.Pen(wx
.Colour(200, 200, 200), 1, wx
.SHORT_DASH
)
296 brush
= wx
.Brush(wx
.Colour(255, 255, 255), wx
.TRANSPARENT
)
298 dc
.ResetBoundingBox()
304 # make sure x1 < x2 and y1 < y2
305 # this will make (x1, y1) = upper left corner
316 dc
.SetClippingRegion(x1
, y1
, x2
- x1
, y2
- y1
)
317 dc
.DrawRectangle(x1
, y1
, x2
- x1
, y2
- y1
)
321 def FindParkingSpot(self
, width
, height
):
322 """ given a width and height, find a upper left corner where shape can be parked without overlapping other shape """
323 offset
= 30 # space between shapes
326 maxX
= 700 # max distance to the right where we'll place tables
330 point
= self
.isSpotOccupied(x
, y
, width
, height
)
332 x
= point
[0] + offset
335 y
= point
[1] + offset
337 noParkingSpot
= False
342 def isSpotOccupied(self
, x
, y
, width
, height
):
343 """ returns None if at x,y,width,height no object occupies that rectangle,
344 otherwise returns lower right corner of object that occupies given x,y position
349 for shape
in self
._diagram
.GetShapeList():
350 if isinstance(shape
, ogl
.RectangleShape
) or isinstance(shape
, ogl
.EllipseShape
):
351 if shape
.GetParent() and isinstance(shape
.GetParent(), ogl
.CompositeShape
):
352 # skip, part of a composite shape
355 if hasattr(shape
, "GetModel"):
356 other_x
, other_y
, other_width
, other_height
= shape
.GetModel().getEditorBounds()
357 other_x2
= other_x
+ other_width
358 other_y2
= other_y
+ other_height
360 # shapes x,y are at the center of the shape, need to transform to upper left coordinate
361 other_width
, other_height
= shape
.GetBoundingBoxMax()
362 other_x
= shape
.GetX() - other_width
/2
363 other_y
= shape
.GetY() - other_height
/2
365 other_x2
= other_x
+ other_width
366 other_y2
= other_y
+ other_height
368 if ((other_x2
< other_x
or other_x2
> x
) and
369 (other_y2
< other_y
or other_y2
> y
) and
370 (x2
< x
or x2
> other_x
) and
371 (y2
< y
or y2
> other_y
)):
372 return (other_x2
, other_y2
)
376 #----------------------------------------------------------------------------
378 #----------------------------------------------------------------------------
380 def AddShape(self
, shape
, x
= None, y
= None, pen
= None, brush
= None, text
= None, eventHandler
= None):
381 if isinstance(shape
, ogl
.CompositeShape
):
382 dc
= wx
.ClientDC(self
._canvas
)
383 self
._canvas
.PrepareDC(dc
)
386 shape
.SetDraggable(True, True)
387 shape
.SetCanvas(self
._canvas
)
393 shape
.SetCentreResize(False)
397 shape
.SetBrush(brush
)
400 shape
.SetShadowMode(ogl
.SHADOW_RIGHT
)
401 self
._diagram
.AddShape(shape
)
404 eventHandler
= EditorCanvasShapeEvtHandler(self
)
405 eventHandler
.SetShape(shape
)
406 eventHandler
.SetPreviousHandler(shape
.GetEventHandler())
407 shape
.SetEventHandler(eventHandler
)
411 def RemoveShape(self
, model
= None, shape
= None):
412 if not model
and not shape
:
416 shape
= self
.GetShape(model
)
420 self
._diagram
.RemoveShape(shape
)
421 if isinstance(shape
, ogl
.CompositeShape
):
422 shape
.RemoveFromCanvas(self
._canvas
)
425 def UpdateShape(self
, model
):
426 for shape
in self
._diagram
.GetShapeList():
427 if hasattr(shape
, "GetModel") and shape
.GetModel() == model
:
428 x
, y
, w
, h
= model
.getEditorBounds()
432 if isinstance(shape
, ogl
.CompositeShape
):
433 if shape
.GetX() != newX
or shape
.GetY() != newY
:
434 dc
= wx
.ClientDC(self
._canvas
)
435 self
._canvas
.PrepareDC(dc
)
436 shape
.SetSize(w
, h
, True) # wxBug: SetSize must be before Move because links won't go to the right place
437 shape
.Move(dc
, newX
, newY
) # wxBug: Move must be before SetSize because links won't go to the right place
440 oldw
, oldh
= shape
.GetBoundingBoxMax()
443 if oldw
!= w
or oldh
!= h
or oldx
!= newX
or oldy
!= newY
:
449 shape
.ResetControlPoints()
450 self
._canvas
.Refresh()
454 def GetShape(self
, model
):
455 for shape
in self
._diagram
.GetShapeList():
456 if hasattr(shape
, "GetModel") and shape
.GetModel() == model
:
461 def GetSelection(self
):
462 return filter(lambda shape
: shape
.Selected(), self
._diagram
.GetShapeList())
465 def SetSelection(self
, models
, extendSelect
= False):
466 dc
= wx
.ClientDC(self
._canvas
)
467 self
._canvas
.PrepareDC(dc
)
469 if not isinstance(models
, type([])) and not isinstance(models
, type(())):
471 for shape
in self
._diagram
.GetShapeList():
472 if hasattr(shape
, "GetModel"):
473 if shape
.Selected() and not shape
.GetModel() in models
: # was selected, but not in new list, so deselect, unless extend select
475 shape
.Select(False, dc
)
477 elif not shape
.Selected() and shape
.GetModel() in models
: # was not selected and in new list, so select
478 shape
.Select(True, dc
)
480 elif extendSelect
and shape
.Selected() and shape
.GetModel() in models
: # was selected, but extend select means to deselect
481 shape
.Select(False, dc
)
484 self
._canvas
.Redraw(dc
)
487 def BringToFront(self
, shape
):
488 if shape
.GetParent() and isinstance(shape
.GetParent(), ogl
.CompositeShape
):
489 self
._diagram
.RemoveShape(shape
.GetParent())
490 self
._diagram
.AddShape(shape
.GetParent())
492 self
._diagram
.RemoveShape(shape
)
493 self
._diagram
.AddShape(shape
)
496 def SendToBack(self
, shape
):
497 if shape
.GetParent() and isinstance(shape
.GetParent(), ogl
.CompositeShape
):
498 self
._diagram
.RemoveShape(shape
.GetParent())
499 self
._diagram
.InsertShape(shape
.GetParent())
501 self
._diagram
.RemoveShape(shape
)
502 self
._diagram
.InsertShape(shape
)
505 def ScrollVisible(self
, shape
):
506 xUnit
, yUnit
= shape
._canvas
.GetScrollPixelsPerUnit()
507 scrollX
, scrollY
= self
._canvas
.GetViewStart() # in scroll units
508 scrollW
, scrollH
= self
._canvas
.GetSize() # in pixels
509 w
, h
= shape
.GetBoundingBoxMax() # in pixels
510 x
= shape
.GetX() - w
/2 # convert to upper left coordinate from center
511 y
= shape
.GetY() - h
/2 # convert to upper left coordinate from center
513 if x
>= scrollX
*xUnit
and x
<= scrollX
*xUnit
+ scrollW
: # don't scroll if already visible
518 if y
>= scrollY
*yUnit
and y
<= scrollY
*yUnit
+ scrollH
: # don't scroll if already visible
523 self
._canvas
.Scroll(x
, y
) # in scroll units
526 def SetPropertyShape(self
, shape
):
527 # no need to highlight if no PropertyService is running
528 propertyService
= wx
.GetApp().GetService(PropertyService
.PropertyService
)
529 if not propertyService
:
532 if shape
== self
._propShape
:
535 if hasattr(shape
, "GetPropertyShape"):
536 shape
= shape
.GetPropertyShape()
538 dc
= wx
.ClientDC(self
._canvas
)
539 self
._canvas
.PrepareDC(dc
)
542 # erase old selection if it still exists
543 if self
._propShape
and self
._propShape
in self
._diagram
.GetShapeList():
544 self
._propShape
.SetBrush(self
._brush
)
545 if (self
._propShape
._textColourName
in ["BLACK", "WHITE"]): # Would use GetTextColour() but it is broken
546 self
._propShape
.SetTextColour("BLACK", 0)
547 self
._propShape
.Draw(dc
)
550 self
._propShape
= shape
553 if self
._propShape
and self
._propShape
in self
._diagram
.GetShapeList():
555 self
._propShape
.SetBrush(SELECT_BRUSH
)
557 self
._propShape
.SetBrush(INACTIVE_SELECT_BRUSH
)
558 if (self
._propShape
._textColourName
in ["BLACK", "WHITE"]): # Would use GetTextColour() but it is broken
559 self
._propShape
.SetTextColour("WHITE", 0)
560 self
._propShape
.Draw(dc
)
565 def FocusColorPropertyShape(self
, gotFocus
=False):
566 # no need to change highlight if no PropertyService is running
567 propertyService
= wx
.GetApp().GetService(PropertyService
.PropertyService
)
568 if not propertyService
:
571 if not self
._propShape
:
574 dc
= wx
.ClientDC(self
._canvas
)
575 self
._canvas
.PrepareDC(dc
)
578 # draw deactivated selection
579 if self
._propShape
and self
._propShape
in self
._diagram
.GetShapeList():
581 self
._propShape
.SetBrush(SELECT_BRUSH
)
583 self
._propShape
.SetBrush(INACTIVE_SELECT_BRUSH
)
584 if (self
._propShape
._textColourName
in ["BLACK", "WHITE"]): # Would use GetTextColour() but it is broken
585 self
._propShape
.SetTextColour("WHITE", 0)
586 self
._propShape
.Draw(dc
)
591 #----------------------------------------------------------------------------
592 # Property Service methods
593 #----------------------------------------------------------------------------
595 def GetPropertyModel(self
):
596 if hasattr(self
, "_propModel"):
597 return self
._propModel
601 def SetPropertyModel(self
, model
):
602 # no need to set the model if no PropertyService is running
603 propertyService
= wx
.GetApp().GetService(PropertyService
.PropertyService
)
604 if not propertyService
:
607 if hasattr(self
, "_propModel") and model
== self
._propModel
:
610 self
._propModel
= model
611 propertyService
.LoadProperties(self
._propModel
, self
.GetDocument())
614 class EditorCanvasShapeMixin
:
620 def SetModel(self
, model
):
624 class EditorCanvasShapeEvtHandler(ogl
.ShapeEvtHandler
):
626 """ wxBug: Bug in OLG package. With wxShape.SetShadowMode() turned on, when we set the size,
627 the width/height is larger by 6 pixels. Need to subtract this value from width and height when we
633 def __init__(self
, view
):
634 ogl
.ShapeEvtHandler
.__init
__(self
)
638 def OnLeftClick(self
, x
, y
, keys
= 0, attachment
= 0):
639 shape
= self
.GetShape()
640 if hasattr(shape
, "GetModel"): # Workaround, on drag, we should deselect all other objects and select the clicked on object
641 model
= shape
.GetModel()
643 shape
= shape
.GetParent()
645 model
= shape
.GetModel()
647 self
._view
.SetSelection(model
, keys
== self
.SHIFT_KEY
or keys
== self
.CONTROL_KEY
)
648 self
._view
.SetPropertyShape(shape
)
649 self
._view
.SetPropertyModel(model
)
652 def OnEndDragLeft(self
, x
, y
, keys
= 0, attachment
= 0):
653 ogl
.ShapeEvtHandler
.OnEndDragLeft(self
, x
, y
, keys
, attachment
)
654 shape
= self
.GetShape()
655 if hasattr(shape
, "GetModel"): # Workaround, on drag, we should deselect all other objects and select the clicked on object
656 model
= shape
.GetModel()
658 parentShape
= shape
.GetParent()
660 model
= parentShape
.GetModel()
661 self
._view
.SetSelection(model
, keys
== self
.SHIFT_KEY
or keys
== self
.CONTROL_KEY
)
664 def OnMovePre(self
, dc
, x
, y
, oldX
, oldY
, display
):
665 """ Prevent objects from being dragged outside of viewable area """
666 if (x
> self
._view
._maxWidth
) or (y
> self
._view
._maxHeight
):
669 return ogl
.ShapeEvtHandler
.OnMovePre(self
, dc
, x
, y
, oldX
, oldY
, display
)
672 def OnMovePost(self
, dc
, x
, y
, oldX
, oldY
, display
):
673 """ Update the model's record of where the shape should be. Also enable redo/undo. """
674 if x
== oldX
and y
== oldY
:
676 if not self
._view
.GetDocument():
678 shape
= self
.GetShape()
679 if isinstance(shape
, EditorCanvasShapeMixin
) and shape
.Draggable():
680 model
= shape
.GetModel()
681 if hasattr(model
, "getEditorBounds") and model
.getEditorBounds():
682 x
, y
, w
, h
= model
.getEditorBounds()
683 newX
= shape
.GetX() - shape
.GetBoundingBoxMax()[0] / 2
684 newY
= shape
.GetY() - shape
.GetBoundingBoxMax()[1] / 2
685 newWidth
= shape
.GetBoundingBoxMax()[0]
686 newHeight
= shape
.GetBoundingBoxMax()[1]
687 if shape
._shadowMode
!= ogl
.SHADOW_NONE
:
688 newWidth
-= shape
._shadowOffsetX
689 newHeight
-= shape
._shadowOffsetY
690 newbounds
= (newX
, newY
, newWidth
, newHeight
)
692 if x
!= newX
or y
!= newY
or w
!= newWidth
or h
!= newHeight
:
693 self
._view
.GetDocument().GetCommandProcessor().Submit(EditorCanvasUpdateShapeBoundariesCommand(self
._view
.GetDocument(), model
, newbounds
))
700 class EditorCanvasUpdateShapeBoundariesCommand(wx
.lib
.docview
.Command
):
703 def __init__(self
, canvasDocument
, model
, newbounds
):
704 wx
.lib
.docview
.Command
.__init
__(self
, canUndo
= True)
705 self
._canvasDocument
= canvasDocument
707 self
._oldbounds
= model
.getEditorBounds()
708 self
._newbounds
= newbounds
712 name
= self
._canvasDocument
.GetNameForObject(self
._model
)
715 print "ERROR: AbstractEditor.EditorCanvasUpdateShapeBoundariesCommand.GetName: unable to get name for ", self
._model
716 return _("Move/Resize %s") % name
720 return self
._canvasDocument
.UpdateEditorBoundaries(self
._model
, self
._newbounds
)
724 return self
._canvasDocument
.UpdateEditorBoundaries(self
._model
, self
._oldbounds
)