📄 _drawn.py
字号:
self._ops.append(op)
def SetTextColour(self, colour):
op = OpSetGDI(DRAWOP_SET_TEXT_COLOUR, self, 0)
op._r, op._g, op._b = colour.Red(), colour.Green(), colour.Blue()
self._ops.append(op)
def SetBackgroundColour(self, colour):
op = OpSetGDI(DRAWOP_SET_BK_COLOUR, self, 0)
op._r, op._g, op._b = colour.Red(), colour.Green(), colour.Blue()
self._ops.append(op)
def SetBackgroundMode(self, mode):
op = OpSetGDI(DRAWOP_SET_BK_MODE, self, 0)
self._ops.append(op)
class DrawnShape(RectangleShape):
"""
Draws a pseudo-metafile shape, which can be loaded from a simple
Windows metafile.
wxDrawnShape allows you to specify a different shape for each of four
orientations (North, West, South and East). It also provides a set of
drawing functions for programmatic drawing of a shape, so that during
construction of the shape you can draw into it as if it were a device
context.
Derived from:
RectangleShape
"""
def __init__(self):
RectangleShape.__init__(self, 100, 50)
self._saveToFile = True
self._currentAngle = DRAWN_ANGLE_0
self._metafiles=PseudoMetaFile(), PseudoMetaFile(), PseudoMetaFile(), PseudoMetaFile()
def OnDraw(self, dc):
# Pass pen and brush in case we have force outline
# and fill colours
if self._shadowMode != SHADOW_NONE:
if self._shadowBrush:
self._metafiles[self._currentAngle]._fillBrush = self._shadowBrush
self._metafiles[self._currentAngle]._outlinePen = wx.Pen(wx.WHITE, 1, wx.TRANSPARENT)
self._metafiles[self._currentAngle].Draw(dc, self._xpos + self._shadowOffsetX, self._ypos + self._shadowOffsetY)
self._metafiles[self._currentAngle]._outlinePen = self._pen
self._metafiles[self._currentAngle]._fillBrush = self._brush
self._metafiles[self._currentAngle].Draw(dc, self._xpos, self._ypos)
def SetSize(self, w, h, recursive = True):
self.SetAttachmentSize(w, h)
if self.GetWidth() == 0.0:
scaleX = 1
else:
scaleX = w / self.GetWidth()
if self.GetHeight() == 0.0:
scaleY = 1
else:
scaleY = h / self.GetHeight()
for i in range(4):
if self._metafiles[i].IsValid():
self._metafiles[i].Scale(scaleX, scaleY)
self._width = w
self._height = h
self.SetDefaultRegionSize()
def Scale(self, sx, sy):
"""Scale the shape by the given amount."""
for i in range(4):
if self._metafiles[i].IsValid():
self._metafiles[i].Scale(sx, sy)
self._metafiles[i].CalculateSize(self)
def Translate(self, x, y):
"""Translate the shape by the given amount."""
for i in range(4):
if self._metafiles[i].IsValid():
self._metafiles[i].Translate(x, y)
self._metafiles[i].CalculateSize(self)
# theta is absolute rotation from the zero position
def Rotate(self, x, y, theta):
"""Rotate about the given axis by the given amount in radians."""
self._currentAngle = self.DetermineMetaFile(theta)
if self._currentAngle == 0:
# Rotate metafile
if not self._metafiles[0].GetRotateable():
return
self._metafiles[0].Rotate(x, y, theta)
actualTheta = theta - self._rotation
# Rotate attachment points
sinTheta = math.sin(actualTheta)
cosTheta = math.cos(actualTheta)
for point in self._attachmentPoints:
x1 = point._x
y1 = point._y
point._x = x1 * cosTheta - y1 * sinTheta + x * (1.0 - cosTheta) + y * sinTheta
point._y = x1 * sinTheta + y1 * cosTheta + y * (1.0 - cosTheta) + x * sinTheta
self._rotation = theta
self._metafiles[self._currentAngle].CalculateSize(self)
# Which metafile do we use now? Based on current rotation and validity
# of metafiles.
def DetermineMetaFile(self, rotation):
tolerance = 0.0001
angles = [0.0, math.pi / 2, math.pi, 3 * math.pi / 2]
whichMetaFile = 0
for i in range(4):
if RoughlyEqual(rotation, angles[i], tolerance):
whichMetaFile = i
break
if whichMetaFile > 0 and not self._metafiles[whichMetaFile].IsValid():
whichMetaFile = 0
return whichMetaFile
def OnDrawOutline(self, dc, x, y, w, h):
if self._metafiles[self._currentAngle].GetOutlineOp() != -1:
op = self._metafiles[self._currentAngle].GetOps()[self._metafiles[self._currentAngle].GetOutlineOp()]
if op.OnDrawOutline(dc, x, y, w, h, self._width, self._height):
return
# Default... just use a rectangle
RectangleShape.OnDrawOutline(self, dc, x, y, w, h)
# Get the perimeter point using the special outline op, if there is one,
# otherwise use default wxRectangleShape scheme
def GetPerimeterPoint(self, x1, y1, x2, y2):
if self._metafiles[self._currentAngle].GetOutlineOp() != -1:
op = self._metafiles[self._currentAngle].GetOps()[self._metafiles[self._currentAngle].GetOutlineOp()]
p = op.GetPerimeterPoint(x1, y1, x2, y2, self.GetX(), self.GetY(), self.GetAttachmentMode())
if p:
return p
return RectangleShape.GetPerimeterPoint(self, x1, y1, x2, y2)
def LoadFromMetaFile(self, filename):
"""Load a (very simple) Windows metafile, created for example by
Top Draw, the Windows shareware graphics package."""
return self._metafiles[0].LoadFromMetaFile(filename)
# Set of functions for drawing into a pseudo metafile.
# They use integers, but doubles are used internally for accuracy
# when scaling.
def DrawLine(self, pt1, pt2):
self._metafiles[self._currentAngle].DrawLine(pt1, pt2)
def DrawRectangle(self, rect):
self._metafiles[self._currentAngle].DrawRectangle(rect)
def DrawRoundedRectangle(self, rect, radius):
"""Draw a rounded rectangle.
radius is the corner radius. If radius is negative, it expresses
the radius as a proportion of the smallest dimension of the rectangle.
"""
self._metafiles[self._currentAngle].DrawRoundedRectangle(rect, radius)
def DrawEllipse(self, rect):
self._metafiles[self._currentAngle].DrawEllipse(rect)
def DrawArc(self, centrePt, startPt, endPt):
"""Draw an arc."""
self._metafiles[self._currentAngle].DrawArc(centrePt, startPt, endPt)
def DrawEllipticArc(self, rect, startAngle, endAngle):
"""Draw an elliptic arc."""
self._metafiles[self._currentAngle].DrawEllipticArc(rect, startAngle, endAngle)
def DrawPoint(self, pt):
self._metafiles[self._currentAngle].DrawPoint(pt)
def DrawText(self, text, pt):
self._metafiles[self._currentAngle].DrawText(text, pt)
def DrawLines(self, pts):
self._metafiles[self._currentAngle].DrawLines(pts)
def DrawPolygon(self, pts, flags = 0):
"""Draw a polygon.
flags can be one or more of:
METAFLAGS_OUTLINE (use this polygon for the drag outline) and
METAFLAGS_ATTACHMENTS (use the vertices of this polygon for attachments).
"""
if flags and METAFLAGS_ATTACHMENTS:
self.ClearAttachments()
for i in range(len(pts)):
self._attachmentPoints.append(AttachmentPoint(i,pts[i][0],pts[i][1]))
self._metafiles[self._currentAngle].DrawPolygon(pts, flags)
def DrawSpline(self, pts):
self._metafiles[self._currentAngle].DrawSpline(pts)
def SetClippingRect(self, rect):
"""Set the clipping rectangle."""
self._metafiles[self._currentAngle].SetClippingRect(rect)
def DestroyClippingRect(self):
"""Destroy the clipping rectangle."""
self._metafiles[self._currentAngle].DestroyClippingRect()
def SetDrawnPen(self, pen, isOutline = False):
"""Set the pen for this metafile.
If isOutline is True, this pen is taken to indicate the outline
(and if the outline pen is changed for the whole shape, the pen
will be replaced with the outline pen).
"""
self._metafiles[self._currentAngle].SetPen(pen, isOutline)
def SetDrawnBrush(self, brush, isFill = False):
"""Set the brush for this metafile.
If isFill is True, the brush is used as the fill brush.
"""
self._metafiles[self._currentAngle].SetBrush(brush, isFill)
def SetDrawnFont(self, font):
self._metafiles[self._currentAngle].SetFont(font)
def SetDrawnTextColour(self, colour):
"""Set the current text colour for the current metafile."""
self._metafiles[self._currentAngle].SetTextColour(colour)
def SetDrawnBackgroundColour(self, colour):
"""Set the current background colour for the current metafile."""
self._metafiles[self._currentAngle].SetBackgroundColour(colour)
def SetDrawnBackgroundMode(self, mode):
"""Set the current background mode for the current metafile."""
self._metafiles[self._currentAngle].SetBackgroundMode(mode)
def CalculateSize(self):
"""Calculate the wxDrawnShape size from the current metafile.
Call this after you have drawn into the shape.
"""
self._metafiles[self._currentAngle].CalculateSize(self)
def DrawAtAngle(self, angle):
"""Set the metafile for the given orientation, which can be one of:
* DRAWN_ANGLE_0
* DRAWN_ANGLE_90
* DRAWN_ANGLE_180
* DRAWN_ANGLE_270
"""
self._currentAngle = angle
def GetAngle(self):
"""Return the current orientation, which can be one of:
* DRAWN_ANGLE_0
* DRAWN_ANGLE_90
* DRAWN_ANGLE_180
* DRAWN_ANGLE_270
"""
return self._currentAngle
def GetRotation(self):
"""Return the current rotation of the shape in radians."""
return self._rotation
def SetSaveToFile(self, save):
"""If save is True, the image will be saved along with the shape's
other attributes. The reason why this might not be desirable is that
if there are many shapes with the same image, it would be more
efficient for the application to save one copy, and not duplicate
the information for every shape. The default is True.
"""
self._saveToFile = save
def GetMetaFile(self, which = 0):
"""Return a reference to the internal 'pseudo-metafile'."""
return self._metafiles[which]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -