📄 _basic.py
字号:
def OnEndDragRight(self, x, y, keys = 0, attachment = 0):
if self._sensitivity & OP_DRAG_RIGHT != OP_DRAG_RIGHT:
if self._parent:
attachment, dist = self._parent.HitTest(x, y)
self._parent.GetEventHandler().OnEndDragRight(x, y, keys, attachment)
return
def OnDrawOutline(self, dc, x, y, w, h):
points = [[x - w / 2.0, y - h / 2.0],
[x + w / 2.0, y - h / 2.0],
[x + w / 2.0, y + h / 2.0],
[x - w / 2.0, y + h / 2.0],
[x - w / 2.0, y - h / 2.0],
]
dc.DrawLines(points)
def Attach(self, can):
"""Set the shape's internal canvas pointer to point to the given canvas."""
self._canvas = can
def Detach(self):
"""Disassociates the shape from its canvas."""
self._canvas = None
def Move(self, dc, x, y, display = True):
"""Move the shape to the given position.
Redraw if display is TRUE.
"""
old_x = self._xpos
old_y = self._ypos
if not self.GetEventHandler().OnMovePre(dc, x, y, old_x, old_y, display):
return
self._xpos, self._ypos = x, y
self.ResetControlPoints()
if display:
self.Draw(dc)
self.MoveLinks(dc)
self.GetEventHandler().OnMovePost(dc, x, y, old_x, old_y, display)
def MoveLinks(self, dc):
"""Redraw all the lines attached to the shape."""
self.GetEventHandler().OnMoveLinks(dc)
def Draw(self, dc):
"""Draw the whole shape and any lines attached to it.
Do not override this function: override OnDraw, which is called
by this function.
"""
if self._visible:
self.GetEventHandler().OnDraw(dc)
self.GetEventHandler().OnDrawContents(dc)
self.GetEventHandler().OnDrawControlPoints(dc)
self.GetEventHandler().OnDrawBranches(dc)
def Flash(self):
"""Flash the shape."""
if self.GetCanvas():
dc = wx.ClientDC(self.GetCanvas())
self.GetCanvas().PrepareDC(dc)
dc.SetLogicalFunction(OGLRBLF)
self.Draw(dc)
dc.SetLogicalFunction(wx.COPY)
self.Draw(dc)
def Show(self, show):
"""Set a flag indicating whether the shape should be drawn."""
self._visible = show
for child in self._children:
child.Show(show)
def Erase(self, dc):
"""Erase the shape.
Does not repair damage caused to other shapes.
"""
self.GetEventHandler().OnErase(dc)
self.GetEventHandler().OnEraseControlPoints(dc)
self.GetEventHandler().OnDrawBranches(dc, erase = True)
def EraseContents(self, dc):
"""Erase the shape contents, that is, the area within the shape's
minimum bounding box.
"""
self.GetEventHandler().OnEraseContents(dc)
def AddText(self, string):
"""Add a line of text to the shape's default text region."""
if not self._regions:
return
region = self._regions[0]
#region.ClearText()
new_line = ShapeTextLine(0, 0, string)
text = region.GetFormattedText()
text.append(new_line)
self._formatted = False
def SetSize(self, x, y, recursive = True):
"""Set the shape's size."""
self.SetAttachmentSize(x, y)
self.SetDefaultRegionSize()
def SetAttachmentSize(self, w, h):
width, height = self.GetBoundingBoxMin()
if width == 0:
scaleX = 1.0
else:
scaleX = float(w) / width
if height == 0:
scaleY = 1.0
else:
scaleY = float(h) / height
for point in self._attachmentPoints:
point._x = point._x * scaleX
point._y = point._y * scaleY
# Add line FROM this object
def AddLine(self, line, other, attachFrom = 0, attachTo = 0, positionFrom = -1, positionTo = -1):
"""Add a line between this shape and the given other shape, at the
specified attachment points.
The position in the list of lines at each end can also be specified,
so that the line will be drawn at a particular point on its attachment
point.
"""
if positionFrom == -1:
if not line in self._lines:
self._lines.append(line)
else:
# Don't preserve old ordering if we have new ordering instructions
try:
self._lines.remove(line)
except ValueError:
pass
if positionFrom < len(self._lines):
self._lines.insert(positionFrom, line)
else:
self._lines.append(line)
if positionTo == -1:
if not other in other._lines:
other._lines.append(line)
else:
# Don't preserve old ordering if we have new ordering instructions
try:
other._lines.remove(line)
except ValueError:
pass
if positionTo < len(other._lines):
other._lines.insert(positionTo, line)
else:
other._lines.append(line)
line.SetFrom(self)
line.SetTo(other)
line.SetAttachments(attachFrom, attachTo)
dc = wx.ClientDC(self._canvas)
self._canvas.PrepareDC(dc)
self.MoveLinks(dc)
def RemoveLine(self, line):
"""Remove the given line from the shape's list of attached lines."""
if line.GetFrom() == self:
line.GetTo()._lines.remove(line)
else:
line.GetFrom()._lines.remove(line)
self._lines.remove(line)
# Default - make 6 control points
def MakeControlPoints(self):
"""Make a list of control points (draggable handles) appropriate to
the shape.
"""
maxX, maxY = self.GetBoundingBoxMax()
minX, minY = self.GetBoundingBoxMin()
widthMin = minX + CONTROL_POINT_SIZE + 2
heightMin = minY + CONTROL_POINT_SIZE + 2
# Offsets from main object
top = -heightMin / 2.0
bottom = heightMin / 2.0 + (maxY - minY)
left = -widthMin / 2.0
right = widthMin / 2.0 + (maxX - minX)
control = ControlPoint(self._canvas, self, CONTROL_POINT_SIZE, left, top, CONTROL_POINT_DIAGONAL)
self._canvas.AddShape(control)
self._controlPoints.append(control)
control = ControlPoint(self._canvas, self, CONTROL_POINT_SIZE, 0, top, CONTROL_POINT_VERTICAL)
self._canvas.AddShape(control)
self._controlPoints.append(control)
control = ControlPoint(self._canvas, self, CONTROL_POINT_SIZE, right, top, CONTROL_POINT_DIAGONAL)
self._canvas.AddShape(control)
self._controlPoints.append(control)
control = ControlPoint(self._canvas, self, CONTROL_POINT_SIZE, right, 0, CONTROL_POINT_HORIZONTAL)
self._canvas.AddShape(control)
self._controlPoints.append(control)
control = ControlPoint(self._canvas, self, CONTROL_POINT_SIZE, right, bottom, CONTROL_POINT_DIAGONAL)
self._canvas.AddShape(control)
self._controlPoints.append(control)
control = ControlPoint(self._canvas, self, CONTROL_POINT_SIZE, 0, bottom, CONTROL_POINT_VERTICAL)
self._canvas.AddShape(control)
self._controlPoints.append(control)
control = ControlPoint(self._canvas, self, CONTROL_POINT_SIZE, left, bottom, CONTROL_POINT_DIAGONAL)
self._canvas.AddShape(control)
self._controlPoints.append(control)
control = ControlPoint(self._canvas, self, CONTROL_POINT_SIZE, left, 0, CONTROL_POINT_HORIZONTAL)
self._canvas.AddShape(control)
self._controlPoints.append(control)
def MakeMandatoryControlPoints(self):
"""Make the mandatory control points.
For example, the control point on a dividing line should appear even
if the divided rectangle shape's handles should not appear (because
it is the child of a composite, and children are not resizable).
"""
for child in self._children:
child.MakeMandatoryControlPoints()
def ResetMandatoryControlPoints(self):
"""Reset the mandatory control points."""
for child in self._children:
child.ResetMandatoryControlPoints()
def ResetControlPoints(self):
"""Reset the positions of the control points (for instance when the
shape's shape has changed).
"""
self.ResetMandatoryControlPoints()
if len(self._controlPoints) == 0:
return
maxX, maxY = self.GetBoundingBoxMax()
minX, minY = self.GetBoundingBoxMin()
widthMin = minX + CONTROL_POINT_SIZE + 2
heightMin = minY + CONTROL_POINT_SIZE + 2
# Offsets from main object
top = -heightMin / 2.0
bottom = heightMin / 2.0 + (maxY - minY)
left = -widthMin / 2.0
right = widthMin / 2.0 + (maxX - minX)
self._controlPoints[0]._xoffset = left
self._controlPoints[0]._yoffset = top
self._controlPoints[1]._xoffset = 0
self._controlPoints[1]._yoffset = top
self._controlPoints[2]._xoffset = right
self._controlPoints[2]._yoffset = top
self._controlPoints[3]._xoffset = right
self._controlPoints[3]._yoffset = 0
self._controlPoints[4]._xoffset = right
self._controlPoints[4]._yoffset = bottom
self._controlPoints[5]._xoffset = 0
self._controlPoints[5]._yoffset = bottom
self._controlPoints[6]._xoffset = left
self._controlPoints[6]._yoffset = bottom
self._controlPoints[7]._xoffset = left
self._controlPoints[7]._yoffset = 0
def DeleteControlPoints(self, dc = None):
"""Delete the control points (or handles) for the shape.
Does not redraw the shape.
"""
for control in self._controlPoints[:]:
if dc:
control.GetEventHandler().OnErase(dc)
control.Delete()
self._controlPoints.remove(control)
self._controlPoints = []
# Children of divisions are contained objects,
# so stop here
if not isinstance(self, DivisionShape):
for child in self._children:
child.DeleteControlPoints(dc)
def OnDrawControlPoints(self, dc):
if not self._drawHandles:
return
dc.SetBrush(wx.BLACK_BRUSH)
dc.SetPen(wx.BLACK_PEN)
for control in self._controlPoints:
control.Draw(dc)
# Children of divisions are contained objects,
# so stop here.
# This test bypasses the type facility for speed
# (critical when drawing)
if not isinstance(self, DivisionShape):
for child in self._children:
child.GetEventHandler().OnDrawControlPoints(dc)
def OnEraseControlPoints(self, dc):
for control in self._controlPoints:
control.Erase(dc)
if not isinstance(self, DivisionShape):
for child in self._children:
child.GetEventHandler().OnEraseControlPoints(dc)
def Select(self, select, dc = None):
"""Select or deselect the given shape, drawing or erasing control points
(handles) as necessary.
"""
self._selected = select
if select:
self.MakeControlPoints()
# Children of divisions are contained objects,
# so stop here
if not isinstance(self, DivisionShape):
for child in self._children:
child.MakeMandatoryControlPoints()
if dc:
self.GetEventHandler().OnDrawControlPoints(dc)
else:
self.DeleteControlPoints(dc)
if not isinstance(self, DivisionShape):
for child in self._children:
child.DeleteControlPoints(dc)
def Selected(self):
"""TRUE if the shape is currently selected."""
return self._selected
def AncestorSelected(self):
"""TRUE if the shape's ancestor is currently selected."""
if self._selected:
return True
if not self.GetParent():
return False
return self.GetParent().AncestorSelected()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -