]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/ogl/diagram.py
1 # -*- coding: iso-8859-1 -*-
2 #----------------------------------------------------------------------------
4 # Purpose: Diagram class
6 # Author: Pierre Hjälm (from C++ original by Julian Smart)
10 # Copyright: (c) 2004 Pierre Hjälm - 1998 Julian Smart
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------------
14 from __future__
import division
18 DEFAULT_MOUSE_TOLERANCE
= 3
22 class Diagram(object):
23 """Encapsulates an entire diagram, with methods for drawing. A diagram has
24 an associated ShapeCanvas.
30 self
._diagramCanvas
= None
31 self
._quickEditMode
= False
32 self
._snapToGrid
= True
33 self
._gridSpacing
= 5.0
35 self
._mouseTolerance
= DEFAULT_MOUSE_TOLERANCE
38 """Draw the shapes in the diagram on the specified device context."""
41 self
.GetCanvas().SetCursor(wx
.HOURGLASS_CURSOR
)
42 for object in self
._shapeList
:
45 self
.GetCanvas().SetCursor(wx
.STANDARD_CURSOR
)
48 """Clear the specified device context."""
51 def AddShape(self
, object, addAfter
= None):
52 """Adds a shape to the diagram. If addAfter is not None, the shape
53 will be added after addAfter.
55 if not object in self
._shapeList
:
57 self
._shapeList
.insert(self
._shapeList
.index(addAfter
) + 1, object)
59 self
._shapeList
.append(object)
61 object.SetCanvas(self
.GetCanvas())
63 def InsertShape(self
, object):
64 """Insert a shape at the front of the shape list."""
65 self
._shapeList
.insert(0, object)
67 def RemoveShape(self
, object):
68 """Remove the shape from the diagram (non-recursively) but do not
71 if object in self
._shapeList
:
72 self
._shapeList
.remove(object)
74 def RemoveAllShapes(self
):
75 """Remove all shapes from the diagram but do not delete the shapes."""
78 def DeleteAllShapes(self
):
79 """Remove and delete all shapes in the diagram."""
80 for shape
in self
._shapeList
[:]:
81 if not shape
.GetParent():
82 self
.RemoveShape(shape
)
84 def ShowAll(self
, show
):
85 """Call Show for each shape in the diagram."""
86 for shape
in self
._shapeList
:
89 def DrawOutLine(self
, dc
, x1
, y1
, x2
, y2
):
90 """Draw an outline rectangle on the current device context."""
91 dc
.SetPen(wx
.Pen(wx
.Color(0, 0, 0), 1, wx
.DOT
))
92 dc
.SetBrush(wx
.TRANSPARENT_BRUSH
)
94 dc
.DrawLines([[x1
, y1
], [x2
, y1
], [x2
, y2
], [x1
, y2
], [x1
, y1
]])
96 def RecentreAll(self
, dc
):
97 """Make sure all text that should be centred, is centred."""
98 for shape
in self
._shapeList
:
101 def SetCanvas(self
, canvas
):
102 """Set the canvas associated with this diagram."""
103 self
._diagramCanvas
= canvas
106 """Return the shape canvas associated with this diagram."""
107 return self
._diagramCanvas
109 def FindShape(self
, id):
110 """Return the shape for the given identifier."""
111 for shape
in self
._shapeList
:
112 if shape
.GetId() == id:
116 def Snap(self
, x
, y
):
117 """'Snaps' the coordinate to the nearest grid position, if
118 snap-to-grid is on."""
120 return self
._gridSpacing
* int(x
/ self
._gridSpacing
+ 0.5), self
._gridSpacing
* int(y
/ self
._gridSpacing
+ 0.5)
123 def GetGridSpacing(self
):
124 """Return the grid spacing."""
125 return self
._gridSpacing
127 def GetSnapToGrid(self
):
128 """Return snap-to-grid mode."""
129 return self
._snapToGrid
131 def SetQuickEditMode(self
, mode
):
132 """Set quick-edit-mode on of off.
134 In this mode, refreshes are minimized, but the diagram may need
135 manual refreshing occasionally.
137 self
._quickEditMode
= mode
139 def GetQuickEditMode(self
):
140 """Return quick edit mode."""
141 return self
._quickEditMode
143 def SetMouseTolerance(self
, tolerance
):
144 """Set the tolerance within which a mouse move is ignored.
146 The default is 3 pixels.
148 self
._mouseTolerance
= tolerance
150 def GetMouseTolerance(self
):
151 """Return the tolerance within which a mouse move is ignored."""
152 return self
._mouseTolerance
154 def GetShapeList(self
):
155 """Return the internal shape list."""
156 return self
._shapeList
159 """Return the number of shapes in the diagram."""
160 return len(self
._shapeList
)