1
# -*- coding: utf-8 -*-
2
#----------------------------------------------------------------------------
4
# Purpose: Diagram class
6
# Author: Pierre Hjälm (from C++ original by Julian Smart)
9
# RCS-ID: $Id: _diagram.py 63496 2010-02-16 06:47:55Z RD $
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)
83
def ShowAll(self, show):
84
"""Call Show for each shape in the diagram."""
85
for shape in self._shapeList:
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.Colour(0, 0, 0), 1, wx.DOT))
91
dc.SetBrush(wx.TRANSPARENT_BRUSH)
93
dc.DrawLines([[x1, y1], [x2, y1], [x2, y2], [x1, y2], [x1, y1]])
95
def RecentreAll(self, dc):
96
"""Make sure all text that should be centred, is centred."""
97
for shape in self._shapeList:
100
def SetCanvas(self, canvas):
101
"""Set the canvas associated with this diagram."""
102
self._diagramCanvas = canvas
105
"""Return the shape canvas associated with this diagram."""
106
return self._diagramCanvas
108
def FindShape(self, id):
109
"""Return the shape for the given identifier."""
110
for shape in self._shapeList:
111
if shape.GetId() == id:
115
def Snap(self, x, y):
116
"""'Snaps' the coordinate to the nearest grid position, if
117
snap-to-grid is on."""
119
return self._gridSpacing * int(x / self._gridSpacing + 0.5), self._gridSpacing * int(y / self._gridSpacing + 0.5)
122
def SetGridSpacing(self, spacing):
123
"""Sets grid spacing."""
124
self._gridSpacing = spacing
126
def SetSnapToGrid(self, snap):
127
"""Sets snap-to-grid mode."""
128
self._snapToGrid = snap
130
def GetGridSpacing(self):
131
"""Return the grid spacing."""
132
return self._gridSpacing
134
def GetSnapToGrid(self):
135
"""Return snap-to-grid mode."""
136
return self._snapToGrid
138
def SetQuickEditMode(self, mode):
139
"""Set quick-edit-mode on of off.
141
In this mode, refreshes are minimized, but the diagram may need
142
manual refreshing occasionally.
144
self._quickEditMode = mode
146
def GetQuickEditMode(self):
147
"""Return quick edit mode."""
148
return self._quickEditMode
150
def SetMouseTolerance(self, tolerance):
151
"""Set the tolerance within which a mouse move is ignored.
153
The default is 3 pixels.
155
self._mouseTolerance = tolerance
157
def GetMouseTolerance(self):
158
"""Return the tolerance within which a mouse move is ignored."""
159
return self._mouseTolerance
161
def GetShapeList(self):
162
"""Return the internal shape list."""
163
return self._shapeList
166
"""Return the number of shapes in the diagram."""
167
return len(self._shapeList)