1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------
# Name: diagram.py
# Purpose: Diagram class
#
# Author: Pierre Hjälm (from C++ original by Julian Smart)
#
# Created: 2004-05-08
# Copyright: (c) 2004 Pierre Hjälm - 1998 Julian Smart
# Licence: wxWindows license
# Tags: phoenix-port, unittest, py3-port, documented
#----------------------------------------------------------------------------
"""
The :class:`~lib.ogl.diagram.Diagram` class.
"""
import wx
DEFAULT_MOUSE_TOLERANCE = 3
class Diagram(object):
"""
The :class:`Diagram` encapsulates an entire diagram, with methods for
drawing. A diagram has an associated :class:`ShapeCanvas`.
"""
def __init__(self):
"""
Default class constructor.
"""
self._diagramCanvas = None
self._quickEditMode = False
self._snapToGrid = True
self._gridSpacing = 5.0
self._shapeList = []
self._mouseTolerance = DEFAULT_MOUSE_TOLERANCE
def Redraw(self, dc):
"""Redraw the shapes in the diagram on the specified device context."""
if self._shapeList:
for object in self._shapeList:
object.Draw(dc)
def Clear(self, dc):
"""Clear the specified device context."""
dc.Clear()
def AddShape(self, object, addAfter = None):
"""
Add a shape to the diagram. If addAfter is not None, the shape
will be added after addAfter.
:param `object`: an instance of :class:`~lib.ogl.Shape`
:param `addAfter`: an instance of :class:`~lib.ogl.Shape`
"""
if not object in self._shapeList:
if addAfter:
self._shapeList.insert(self._shapeList.index(addAfter) + 1, object)
else:
self._shapeList.append(object)
object.SetCanvas(self.GetCanvas())
def InsertShape(self, object):
"""
Insert a shape at the front of the shape list.
:param `object`: an instance of :class:`~lib.ogl.Shape`
"""
self._shapeList.insert(0, object)
def RemoveShape(self, object):
"""
Remove the shape from the diagram (non-recursively) but do not
delete it.
:param `object`: an instance of :class:`~lib.ogl.Shape`
"""
if object in self._shapeList:
self._shapeList.remove(object)
def RemoveAllShapes(self):
"""Remove all shapes from the diagram but do not delete the shapes."""
self._shapeList = []
def DeleteAllShapes(self):
"""Remove and delete all shapes in the diagram."""
for shape in self._shapeList[:]:
if not shape.GetParent():
self.RemoveShape(shape)
shape.Delete()
def ShowAll(self, show):
"""Call Show for each shape in the diagram.
:param `show`: True or False
"""
for shape in self._shapeList:
shape.Show(show)
def DrawOutline(self, dc, x1, y1, x2, y2):
"""
Draw an outline rectangle on the current device context.
:param `dc`: the :class:`wx.MemoryDC` device context
:param `x1`: the x1 position
:param `y2`: the y2 position
:param `x1`: the x1 position
:param `y2`: the y2 position
"""
dc.SetPen(wx.Pen(wx.BLACK, 1, wx.PENSTYLE_DOT))
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.DrawLines([[x1, y1], [x2, y1], [x2, y2], [x1, y2], [x1, y1]])
def RecentreAll(self, dc):
"""Recentre all the text that should be centred.
:param `dc`: the :class:`wx.MemoryDC` device context
"""
for shape in self._shapeList:
shape.Recentre(dc)
def SetCanvas(self, canvas):
"""
Set the canvas associated with this diagram.
:param `canvas`: an instance of :class:`~lib.ogl.Canvas`
"""
self._diagramCanvas = canvas
def GetCanvas(self):
"""Return the shape canvas associated with this diagram."""
return self._diagramCanvas
def FindShape(self, id):
"""
Return the shape for the given identifier.
:param `id`: the shape id to find
"""
for shape in self._shapeList:
if shape.GetId() == id:
return shape
return None
def Snap(self, x, y):
"""
'Snaps' the coordinate to the nearest grid position, if
snap-to-grid is on.
:param `x`: the x position
:param `y`: the y position
"""
if self._snapToGrid:
return self._gridSpacing * int(x / self._gridSpacing + 0.5), self._gridSpacing * int(y / self._gridSpacing + 0.5)
return x, y
def SetGridSpacing(self, spacing):
"""
Sets grid spacing.
:param `spacing`: the spacing
"""
self._gridSpacing = spacing
def SetSnapToGrid(self, snap):
"""
Sets snap-to-grid mode.
:param `snap`: `True` to snap to grid or `False` not to snap
"""
self._snapToGrid = snap
def GetGridSpacing(self):
"""Return the grid spacing."""
return self._gridSpacing
def GetSnapToGrid(self):
"""Return snap-to-grid mode."""
return self._snapToGrid
def SetQuickEditMode(self, mode):
"""
Set quick-edit-mode on of off.
In this mode, refreshes are minimized, but the diagram may need
manual refreshing occasionally.
:param `mode`: `True` to quick edit or `False` for normal edit
"""
self._quickEditMode = mode
def GetQuickEditMode(self):
"""Return quick edit mode."""
return self._quickEditMode
def SetMouseTolerance(self, tolerance):
"""Set the tolerance within which a mouse move is ignored.
The default is 3 pixels.
:param `tolerance`: the tolerance level
"""
self._mouseTolerance = tolerance
def GetMouseTolerance(self):
"""Return the tolerance within which a mouse move is ignored."""
return self._mouseTolerance
def GetShapeList(self):
"""Return the internal shape list."""
return self._shapeList
def GetCount(self):
"""Return the number of shapes in the diagram."""
return len(self._shapeList)
|