]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/ogl/_diagram.py
Since everything in the submodules is to appear in the pacakge
[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 from __future__ import division
15
16 import wx
17
18 DEFAULT_MOUSE_TOLERANCE = 3
19
20
21
22 class Diagram(object):
23 """Encapsulates an entire diagram, with methods for drawing. A diagram has
24 an associated ShapeCanvas.
25
26 Derived from:
27 Object
28 """
29 def __init__(self):
30 self._diagramCanvas = None
31 self._quickEditMode = False
32 self._snapToGrid = True
33 self._gridSpacing = 5.0
34 self._shapeList = []
35 self._mouseTolerance = DEFAULT_MOUSE_TOLERANCE
36
37 def Redraw(self, dc):
38 """Draw the shapes in the diagram on the specified device context."""
39 if self._shapeList:
40 if self.GetCanvas():
41 self.GetCanvas().SetCursor(wx.HOURGLASS_CURSOR)
42 for object in self._shapeList:
43 object.Draw(dc)
44 if self.GetCanvas():
45 self.GetCanvas().SetCursor(wx.STANDARD_CURSOR)
46
47 def Clear(self, dc):
48 """Clear the specified device context."""
49 dc.Clear()
50
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.
54 """
55 if not object in self._shapeList:
56 if addAfter:
57 self._shapeList.insert(self._shapeList.index(addAfter) + 1, object)
58 else:
59 self._shapeList.append(object)
60
61 object.SetCanvas(self.GetCanvas())
62
63 def InsertShape(self, object):
64 """Insert a shape at the front of the shape list."""
65 self._shapeList.insert(0, object)
66
67 def RemoveShape(self, object):
68 """Remove the shape from the diagram (non-recursively) but do not
69 delete it.
70 """
71 if object in self._shapeList:
72 self._shapeList.remove(object)
73
74 def RemoveAllShapes(self):
75 """Remove all shapes from the diagram but do not delete the shapes."""
76 self._shapeList = []
77
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)
83
84 def ShowAll(self, show):
85 """Call Show for each shape in the diagram."""
86 for shape in self._shapeList:
87 shape.Show()
88
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)
93
94 dc.DrawLines([[x1, y1], [x2, y1], [x2, y2], [x1, y2], [x1, y1]])
95
96 def RecentreAll(self, dc):
97 """Make sure all text that should be centred, is centred."""
98 for shape in self._shapeList:
99 shape.Recentre(dc)
100
101 def SetCanvas(self, canvas):
102 """Set the canvas associated with this diagram."""
103 self._diagramCanvas = canvas
104
105 def GetCanvas(self):
106 """Return the shape canvas associated with this diagram."""
107 return self._diagramCanvas
108
109 def FindShape(self, id):
110 """Return the shape for the given identifier."""
111 for shape in self._shapeList:
112 if shape.GetId() == id:
113 return shape
114 return None
115
116 def Snap(self, x, y):
117 """'Snaps' the coordinate to the nearest grid position, if
118 snap-to-grid is on."""
119 if self._snapToGrid:
120 return self._gridSpacing * int(x / self._gridSpacing + 0.5), self._gridSpacing * int(y / self._gridSpacing + 0.5)
121 return x, y
122
123 def GetGridSpacing(self):
124 """Return the grid spacing."""
125 return self._gridSpacing
126
127 def GetSnapToGrid(self):
128 """Return snap-to-grid mode."""
129 return self._snapToGrid
130
131 def SetQuickEditMode(self, mode):
132 """Set quick-edit-mode on of off.
133
134 In this mode, refreshes are minimized, but the diagram may need
135 manual refreshing occasionally.
136 """
137 self._quickEditMode = mode
138
139 def GetQuickEditMode(self):
140 """Return quick edit mode."""
141 return self._quickEditMode
142
143 def SetMouseTolerance(self, tolerance):
144 """Set the tolerance within which a mouse move is ignored.
145
146 The default is 3 pixels.
147 """
148 self._mouseTolerance = tolerance
149
150 def GetMouseTolerance(self):
151 """Return the tolerance within which a mouse move is ignored."""
152 return self._mouseTolerance
153
154 def GetShapeList(self):
155 """Return the internal shape list."""
156 return self._shapeList
157
158 def GetCount(self):
159 """Return the number of shapes in the diagram."""
160 return len(self._shapeList)