]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/ogl/_diagram.py
wxCheckListBox doesn't require wxUSE_OWNER_DRAWN when using WXUNIVERSAL
[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
82 def ShowAll(self, show):
83 """Call Show for each shape in the diagram."""
84 for shape in self._shapeList:
85 shape.Show(show)
86
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)
91
92 dc.DrawLines([[x1, y1], [x2, y1], [x2, y2], [x1, y2], [x1, y1]])
93
94 def RecentreAll(self, dc):
95 """Make sure all text that should be centred, is centred."""
96 for shape in self._shapeList:
97 shape.Recentre(dc)
98
99 def SetCanvas(self, canvas):
100 """Set the canvas associated with this diagram."""
101 self._diagramCanvas = canvas
102
103 def GetCanvas(self):
104 """Return the shape canvas associated with this diagram."""
105 return self._diagramCanvas
106
107 def FindShape(self, id):
108 """Return the shape for the given identifier."""
109 for shape in self._shapeList:
110 if shape.GetId() == id:
111 return shape
112 return None
113
114 def Snap(self, x, y):
115 """'Snaps' the coordinate to the nearest grid position, if
116 snap-to-grid is on."""
117 if self._snapToGrid:
118 return self._gridSpacing * int(x / self._gridSpacing + 0.5), self._gridSpacing * int(y / self._gridSpacing + 0.5)
119 return x, y
120
121 def SetGridSpacing(self, spacing):
122 """Sets grid spacing."""
123 self._gridSpacing = spacing
124
125 def SetSnapToGrid(self, snap):
126 """Sets snap-to-grid mode."""
127 self._snapToGrid = snap
128
129 def GetGridSpacing(self):
130 """Return the grid spacing."""
131 return self._gridSpacing
132
133 def GetSnapToGrid(self):
134 """Return snap-to-grid mode."""
135 return self._snapToGrid
136
137 def SetQuickEditMode(self, mode):
138 """Set quick-edit-mode on of off.
139
140 In this mode, refreshes are minimized, but the diagram may need
141 manual refreshing occasionally.
142 """
143 self._quickEditMode = mode
144
145 def GetQuickEditMode(self):
146 """Return quick edit mode."""
147 return self._quickEditMode
148
149 def SetMouseTolerance(self, tolerance):
150 """Set the tolerance within which a mouse move is ignored.
151
152 The default is 3 pixels.
153 """
154 self._mouseTolerance = tolerance
155
156 def GetMouseTolerance(self):
157 """Return the tolerance within which a mouse move is ignored."""
158 return self._mouseTolerance
159
160 def GetShapeList(self):
161 """Return the internal shape list."""
162 return self._shapeList
163
164 def GetCount(self):
165 """Return the number of shapes in the diagram."""
166 return len(self._shapeList)