📄 _basic.py
字号:
self._sensitivity |= OP_DRAG_LEFT
elif self._sensitivity & OP_DRAG_LEFT:
self._sensitivity -= OP_DRAG_LEFT
if recursive:
for shape in self._children:
shape.SetDraggable(drag, True)
def SetDrawHandles(self, drawH):
"""Set the drawHandles flag for this shape and all descendants.
If drawH is TRUE (the default), any handles (control points) will
be drawn. Otherwise, the handles will not be drawn.
"""
self._drawHandles = drawH
for shape in self._children:
shape.SetDrawHandles(drawH)
def SetShadowMode(self, mode, redraw = False):
"""Set the shadow mode (whether a shadow is drawn or not).
mode can be one of the following:
SHADOW_NONE
No shadow (the default).
SHADOW_LEFT
Shadow on the left side.
SHADOW_RIGHT
Shadow on the right side.
"""
if redraw and self.GetCanvas():
dc = wx.ClientDC(self.GetCanvas())
self.GetCanvas().PrepareDC(dc)
self.Erase(dc)
self._shadowMode = mode
self.Draw(dc)
else:
self._shadowMode = mode
def GetShadowMode(self):
"""Return the current shadow mode setting"""
return self._shadowMode
def SetCanvas(self, theCanvas):
"""Identical to Shape.Attach."""
self._canvas = theCanvas
for shape in self._children:
shape.SetCanvas(theCanvas)
def AddToCanvas(self, theCanvas, addAfter = None):
"""Add the shape to the canvas's shape list.
If addAfter is non-NULL, will add the shape after this one.
"""
theCanvas.AddShape(self, addAfter)
lastImage = self
for object in self._children:
object.AddToCanvas(theCanvas, lastImage)
lastImage = object
def InsertInCanvas(self, theCanvas):
"""Insert the shape at the front of the shape list of canvas."""
theCanvas.InsertShape(self)
lastImage = self
for object in self._children:
object.AddToCanvas(theCanvas, lastImage)
lastImage = object
def RemoveFromCanvas(self, theCanvas):
"""Remove the shape from the canvas."""
if self.Selected():
self.Select(False)
self._canvas = None
theCanvas.RemoveShape(self)
for object in self._children:
object.RemoveFromCanvas(theCanvas)
def ClearAttachments(self):
"""Clear internal custom attachment point shapes (of class
wxAttachmentPoint).
"""
self._attachmentPoints = []
def ClearText(self, regionId = 0):
"""Clear the text from the specified text region."""
if regionId == 0:
self._text = ""
if regionId < len(self._regions):
self._regions[regionId].ClearText()
def ClearRegions(self):
"""Clear the ShapeRegions from the shape."""
self._regions = []
def AddRegion(self, region):
"""Add a region to the shape."""
self._regions.append(region)
def SetDefaultRegionSize(self):
"""Set the default region to be consistent with the shape size."""
if not self._regions:
return
w, h = self.GetBoundingBoxMax()
self._regions[0].SetSize(w, h)
def HitTest(self, x, y):
"""Given a point on a canvas, returns TRUE if the point was on the
shape, and returns the nearest attachment point and distance from
the given point and target.
"""
width, height = self.GetBoundingBoxMax()
if abs(width) < 4:
width = 4.0
if abs(height) < 4:
height = 4.0
width += 4 # Allowance for inaccurate mousing
height += 4
left = self._xpos - width / 2.0
top = self._ypos - height / 2.0
right = self._xpos + width / 2.0
bottom = self._ypos + height / 2.0
nearest_attachment = 0
# If within the bounding box, check the attachment points
# within the object.
if x >= left and x <= right and y >= top and y <= bottom:
n = self.GetNumberOfAttachments()
nearest = 999999
# GetAttachmentPosition[Edge] takes a logical attachment position,
# i.e. if it's rotated through 90%, position 0 is East-facing.
for i in range(n):
e = self.GetAttachmentPositionEdge(i)
if e:
xp, yp = e
l = math.sqrt(((xp - x) * (xp - x)) + (yp - y) * (yp - y))
if l < nearest:
nearest = l
nearest_attachment = i
return nearest_attachment, nearest
return False
# Format a text string according to the region size, adding
# strings with positions to region text list
def FormatText(self, dc, s, i = 0):
"""Reformat the given text region; defaults to formatting the
default region.
"""
self.ClearText(i)
if not self._regions:
return
if i > len(self._regions):
return
region = self._regions[i]
region._regionText = s
dc.SetFont(region.GetFont())
w, h = region.GetSize()
stringList = FormatText(dc, s, (w - 2 * self._textMarginX), (h - 2 * self._textMarginY), region.GetFormatMode())
for s in stringList:
line = ShapeTextLine(0.0, 0.0, s)
region.GetFormattedText().append(line)
actualW = w
actualH = h
# Don't try to resize an object with more than one image (this
# case should be dealt with by overriden handlers)
if (region.GetFormatMode() & FORMAT_SIZE_TO_CONTENTS) and \
len(region.GetFormattedText()) and \
len(self._regions) == 1 and \
not Shape.GraphicsInSizeToContents:
actualW, actualH = GetCentredTextExtent(dc, region.GetFormattedText())
if actualW + 2 * self._textMarginX != w or actualH + 2 * self._textMarginY != h:
# If we are a descendant of a composite, must make sure
# the composite gets resized properly
topAncestor = self.GetTopAncestor()
if topAncestor != self:
Shape.GraphicsInSizeToContents = True
composite = topAncestor
composite.Erase(dc)
self.SetSize(actualW + 2 * self._textMarginX, actualH + 2 * self._textMarginY)
self.Move(dc, self._xpos, self._ypos)
composite.CalculateSize()
if composite.Selected():
composite.DeleteControlPoints(dc)
composite.MakeControlPoints()
composite.MakeMandatoryControlPoints()
# Where infinite recursion might happen if we didn't stop it
composite.Draw(dc)
Shape.GraphicsInSizeToContents = False
else:
self.Erase(dc)
self.SetSize(actualW + 2 * self._textMarginX, actualH + 2 * self._textMarginY)
self.Move(dc, self._xpos, self._ypos)
self.EraseContents(dc)
CentreText(dc, region.GetFormattedText(), self._xpos, self._ypos, actualW - 2 * self._textMarginX, actualH - 2 * self._textMarginY, region.GetFormatMode())
self._formatted = True
def Recentre(self, dc):
"""Do recentring (or other formatting) for all the text regions
for this shape.
"""
w, h = self.GetBoundingBoxMin()
for region in self._regions:
CentreText(dc, region.GetFormattedText(), self._xpos, self._ypos, w - 2 * self._textMarginX, h - 2 * self._textMarginY, region.GetFormatMode())
def GetPerimeterPoint(self, x1, y1, x2, y2):
"""Get the point at which the line from (x1, y1) to (x2, y2) hits
the shape. Returns False if the line doesn't hit the perimeter.
"""
return False
def SetPen(self, the_pen):
"""Set the pen for drawing the shape's outline."""
self._pen = the_pen
def SetBrush(self, the_brush):
"""Set the brush for filling the shape's shape."""
self._brush = the_brush
# Get the top - most (non-division) ancestor, or self
def GetTopAncestor(self):
"""Return the top-most ancestor of this shape (the root of
the composite).
"""
if not self.GetParent():
return self
if isinstance(self.GetParent(), DivisionShape):
return self
return self.GetParent().GetTopAncestor()
# Region functions
def SetFont(self, the_font, regionId = 0):
"""Set the font for the specified text region."""
self._font = the_font
if regionId < len(self._regions):
self._regions[regionId].SetFont(the_font)
def GetFont(self, regionId = 0):
"""Get the font for the specified text region."""
if regionId >= len(self._regions):
return None
return self._regions[regionId].GetFont()
def SetFormatMode(self, mode, regionId = 0):
"""Set the format mode of the default text region. The argument
can be a bit list of the following:
FORMAT_NONE
No formatting.
FORMAT_CENTRE_HORIZ
Horizontal centring.
FORMAT_CENTRE_VERT
Vertical centring.
"""
if regionId < len(self._regions):
self._regions[regionId].SetFormatMode(mode)
def GetFormatMode(self, regionId = 0):
if regionId >= len(self._regions):
return 0
return self._regions[regionId].GetFormatMode()
def SetTextColour(self, the_colour, regionId = 0):
"""Set the colour for the specified text region."""
self._textColour = wx.TheColourDatabase.Find(the_colour)
self._textColourName = the_colour
if regionId < len(self._regions):
self._regions[regionId].SetColour(the_colour)
def GetTextColour(self, regionId = 0):
"""Get the colour for the specified text region."""
if regionId >= len(self._regions):
return ""
return self._regions[regionId].GetColour()
def SetRegionName(self, name, regionId = 0):
"""Set the name for this region.
The name for a region is unique within the scope of the whole
composite, whereas a region id is unique only for a single image.
"""
if regionId < len(self._regions):
self._regions[regionId].SetName(name)
def GetRegionName(self, regionId = 0):
"""Get the region's name.
A region's name can be used to uniquely determine a region within
an entire composite image hierarchy. See also Shape.SetRegionName.
"""
if regionId >= len(self._regions):
return ""
return self._regions[regionId].GetName()
def GetRegionId(self, name):
"""Get the region's identifier by name.
This is not unique for within an entire composite, but is unique
for the image.
"""
for i, r in enumerate(self._regions):
if r.GetName() == name:
return i
return -1
# Name all _regions in all subimages recursively
def NameRegions(self, parentName=""):
"""Make unique names for all the regions in a shape or composite shape."""
n = self.GetNumberOfTextRegions()
for i in range(n):
if parentName:
buff = parentName+"."+str(i)
else:
buff = str(i)
self.SetRegionName(buff, i)
for j, child in enumerate(self._children):
if parentName:
buff = parentName+"."+str(j)
else:
buff = str(j)
child.NameRegions(buff)
# Get a region by name, possibly looking recursively into composites
def FindRegion(self, name):
"""Find the actual image ('this' if non-composite) and region id
for the given region name.
"""
id = self.GetRegionId(name)
if id > -1:
return self, id
for child in self._children:
actualImage, regionId = child.FindRegion(name)
if actualImage:
return actualImage, regionId
return None, -1
# Finds all region names for this image (composite or simple).
def FindRegionNames(self):
"""Get a list of all region names for this image (composite or simple)."""
list = []
n = self.GetNumberOfTextRegions()
for i in range(n):
list.append(self.GetRegionName(i))
for child in self._children:
list += child.FindRegionNames()
return list
def AssignNewIds(self):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -