]>
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 #----------------------------------------------------------------------------
16 DEFAULT_MOUSE_TOLERANCE
= 3
20 class Diagram(object):
21 """Encapsulates an entire diagram, with methods for drawing. A diagram has
22 an associated ShapeCanvas.
28 self
._diagramCanvas
= None
29 self
._quickEditMode
= False
30 self
._snapToGrid
= True
31 self
._gridSpacing
= 5.0
33 self
._mouseTolerance
= DEFAULT_MOUSE_TOLERANCE
36 """Draw the shapes in the diagram on the specified device context."""
39 self
.GetCanvas().SetCursor(wx
.HOURGLASS_CURSOR
)
40 for object in self
._shapeList
:
43 self
.GetCanvas().SetCursor(wx
.STANDARD_CURSOR
)
46 """Clear the specified device context."""
49 def AddShape(self
, object, addAfter
= None):
50 """Adds a shape to the diagram. If addAfter is not None, the shape
51 will be added after addAfter.
53 if not object in self
._shapeList
:
55 self
._shapeList
.insert(self
._shapeList
.index(addAfter
) + 1, object)
57 self
._shapeList
.append(object)
59 object.SetCanvas(self
.GetCanvas())
61 def InsertShape(self
, object):
62 """Insert a shape at the front of the shape list."""
63 self
._shapeList
.insert(0, object)
65 def RemoveShape(self
, object):
66 """Remove the shape from the diagram (non-recursively) but do not
69 if object in self
._shapeList
:
70 self
._shapeList
.remove(object)
72 def RemoveAllShapes(self
):
73 """Remove all shapes from the diagram but do not delete the shapes."""
76 def DeleteAllShapes(self
):
77 """Remove and delete all shapes in the diagram."""
78 for shape
in self
._shapeList
[:]:
79 if not shape
.GetParent():
80 self
.RemoveShape(shape
)
82 def ShowAll(self
, show
):
83 """Call Show for each shape in the diagram."""
84 for shape
in self
._shapeList
:
87 def DrawOutLine(self
, dc
, x1
, y1
, x2
, y2
):
88 """Draw an outline rectangle on the current device context."""
89 dc
.SetPen(wx
.Pen(wx
.Color(0, 0, 0), 1, wx
.DOT
))
90 dc
.SetBrush(wx
.TRANSPARENT_BRUSH
)
92 dc
.DrawLines([[x1
, y1
], [x2
, y1
], [x2
, y2
], [x1
, y2
], [x1
, y1
]])
94 def RecentreAll(self
, dc
):
95 """Make sure all text that should be centred, is centred."""
96 for shape
in self
._shapeList
:
99 def SetCanvas(self
, canvas
):
100 """Set the canvas associated with this diagram."""
101 self
._diagramCanvas
= canvas
104 """Return the shape canvas associated with this diagram."""
105 return self
._diagramCanvas
107 def FindShape(self
, id):
108 """Return the shape for the given identifier."""
109 for shape
in self
._shapeList
:
110 if shape
.GetId() == id:
114 def Snap(self
, x
, y
):
115 """'Snaps' the coordinate to the nearest grid position, if
116 snap-to-grid is on."""
118 return self
._gridSpacing
* int(x
/ self
._gridSpacing
+ 0.5), self
._gridSpacing
* int(y
/ self
._gridSpacing
+ 0.5)
121 def GetGridSpacing(self
):
122 """Return the grid spacing."""
123 return self
._gridSpacing
125 def GetSnapToGrid(self
):
126 """Return snap-to-grid mode."""
127 return self
._snapToGrid
129 def SetQuickEditMode(self
, mode
):
130 """Set quick-edit-mode on of off.
132 In this mode, refreshes are minimized, but the diagram may need
133 manual refreshing occasionally.
135 self
._quickEditMode
= mode
137 def GetQuickEditMode(self
):
138 """Return quick edit mode."""
139 return self
._quickEditMode
141 def SetMouseTolerance(self
, tolerance
):
142 """Set the tolerance within which a mouse move is ignored.
144 The default is 3 pixels.
146 self
._mouseTolerance
= tolerance
148 def GetMouseTolerance(self
):
149 """Return the tolerance within which a mouse move is ignored."""
150 return self
._mouseTolerance
152 def GetShapeList(self
):
153 """Return the internal shape list."""
154 return self
._shapeList
157 """Return the number of shapes in the diagram."""
158 return len(self
._shapeList
)