]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/ogl/_diagram.py
Patch from Andrea that fixes the following problems/issues:
[wxWidgets.git] / wxPython / wx / lib / ogl / _diagram.py
1 # -*- coding: iso-8859-1 -*-
2 #----------------------------------------------------------------------------
3 # Name: diagram.py
4 # Purpose: Diagram class
5 #
6 # Author: Pierre Hjälm (from C++ original by Julian Smart)
7 #
8 # Created: 2004-05-08
9 # RCS-ID: $Id$
10 # Copyright: (c) 2004 Pierre Hjälm - 1998 Julian Smart
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------------
13
14 import wx
15
16 DEFAULT_MOUSE_TOLERANCE = 3
17
18
19
20 class Diagram(object):
21 """Encapsulates an entire diagram, with methods for drawing. A diagram has
22 an associated ShapeCanvas.
23
24 Derived from:
25 Object
26 """
27 def __init__(self):
28 self._diagramCanvas = None
29 self._quickEditMode = False
30 self._snapToGrid = True
31 self._gridSpacing = 5.0
32 self._shapeList = []
33 self._mouseTolerance = DEFAULT_MOUSE_TOLERANCE
34
35 def Redraw(self, dc):
36 """Draw the shapes in the diagram on the specified device context."""
37 if self._shapeList:
38 if self.GetCanvas():
39 self.GetCanvas().SetCursor(wx.HOURGLASS_CURSOR)
40 for object in self._shapeList:
41 object.Draw(dc)
42 if self.GetCanvas():
43 self.GetCanvas().SetCursor(wx.STANDARD_CURSOR)
44
45 def Clear(self, dc):
46 """Clear the specified device context."""
47 dc.Clear()
48
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.
52 """
53 if not object in self._shapeList:
54 if addAfter:
55 self._shapeList.insert(self._shapeList.index(addAfter) + 1, object)
56 else:
57 self._shapeList.append(object)
58
59 object.SetCanvas(self.GetCanvas())
60
61 def InsertShape(self, object):
62 """Insert a shape at the front of the shape list."""
63 self._shapeList.insert(0, object)
64
65 def RemoveShape(self, object):
66 """Remove the shape from the diagram (non-recursively) but do not
67 delete it.
68 """
69 if object in self._shapeList:
70 self._shapeList.remove(object)
71
72 def RemoveAllShapes(self):
73 """Remove all shapes from the diagram but do not delete the shapes."""
74 self._shapeList = []
75
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)
81 shape.Delete()
82
83 def ShowAll(self, show):
84 """Call Show for each shape in the diagram."""
85 for shape in self._shapeList:
86 shape.Show(show)
87
88 def DrawOutline(self, dc, x1, y1, x2, y2):
89 """Draw an outline rectangle on the current device context."""
90 dc.SetPen(wx.Pen(wx.Color(0, 0, 0), 1, wx.DOT))
91 dc.SetBrush(wx.TRANSPARENT_BRUSH)
92
93 dc.DrawLines([[x1, y1], [x2, y1], [x2, y2], [x1, y2], [x1, y1]])
94
95 def RecentreAll(self, dc):
96 """Make sure all text that should be centred, is centred."""
97 for shape in self._shapeList:
98 shape.Recentre(dc)
99
100 def SetCanvas(self, canvas):
101 """Set the canvas associated with this diagram."""
102 self._diagramCanvas = canvas
103
104 def GetCanvas(self):
105 """Return the shape canvas associated with this diagram."""
106 return self._diagramCanvas
107
108 def FindShape(self, id):
109 """Return the shape for the given identifier."""
110 for shape in self._shapeList:
111 if shape.GetId() == id:
112 return shape
113 return None
114
115 def Snap(self, x, y):
116 """'Snaps' the coordinate to the nearest grid position, if
117 snap-to-grid is on."""
118 if self._snapToGrid:
119 return self._gridSpacing * int(x / self._gridSpacing + 0.5), self._gridSpacing * int(y / self._gridSpacing + 0.5)
120 return x, y
121
122 def SetGridSpacing(self, spacing):
123 """Sets grid spacing."""
124 self._gridSpacing = spacing
125
126 def SetSnapToGrid(self, snap):
127 """Sets snap-to-grid mode."""
128 self._snapToGrid = snap
129
130 def GetGridSpacing(self):
131 """Return the grid spacing."""
132 return self._gridSpacing
133
134 def GetSnapToGrid(self):
135 """Return snap-to-grid mode."""
136 return self._snapToGrid
137
138 def SetQuickEditMode(self, mode):
139 """Set quick-edit-mode on of off.
140
141 In this mode, refreshes are minimized, but the diagram may need
142 manual refreshing occasionally.
143 """
144 self._quickEditMode = mode
145
146 def GetQuickEditMode(self):
147 """Return quick edit mode."""
148 return self._quickEditMode
149
150 def SetMouseTolerance(self, tolerance):
151 """Set the tolerance within which a mouse move is ignored.
152
153 The default is 3 pixels.
154 """
155 self._mouseTolerance = tolerance
156
157 def GetMouseTolerance(self):
158 """Return the tolerance within which a mouse move is ignored."""
159 return self._mouseTolerance
160
161 def GetShapeList(self):
162 """Return the internal shape list."""
163 return self._shapeList
164
165 def GetCount(self):
166 """Return the number of shapes in the diagram."""
167 return len(self._shapeList)