def RefreshVisibleElements(self, xp=None, yp=None):
x, y = self.Editor.CalcUnscrolledPosition(0, 0)
if xp is not None:
x = xp * self.Editor.GetScrollPixelsPerUnit()[0]
if yp is not None:
y = yp * self.Editor.GetScrollPixelsPerUnit()[1]
width, height = self.Editor.GetClientSize()
screen = wx.Rect(int(x / self.ViewScale[0]), int(y / self.ViewScale[1]),
int(width / self.ViewScale[0]), int(height / self.ViewScale[1]))
for comment in self.Comments.itervalues():
comment.TestVisible(screen)
for wire in self.Wires.iterkeys():
wire.TestVisible(screen)
for block in self.Blocks.itervalues():
block.TestVisible(screen)
python类Rect()的实例源码
def GetAddToWireMenuCallBack(self, func, *args):
args += (self.SelectedElement,)
def AddToWireMenuCallBack(event):
func(wx.Rect(0, 0, 0, 0), *args)
return AddToWireMenuCallBack
def GetCurrentExtent(self):
"""
Return the rubberband bounding box
@return: Rubberband bounding box (wx.Rect object)
"""
# In case of rubberband not shown, return the last rubberband
# bounding box
if self.IsShown():
return self.CurrentBBox
return self.LastBBox
def HitTest(self, pt, connectors=True):
if self.Type != "connection":
# Calculate the bounding box of the condition outside the transition
text_width, text_height = self.ConditionSize
text_bbx = wx.Rect(self.Pos.x + self.Size[0] + 5,
self.Pos.y + (self.Size[1] - text_height) / 2,
text_width,
text_height)
test_text = text_bbx.InsideXY(pt.x, pt.y)
else:
test_text = False
return test_text or Graphic_Element.HitTest(self, pt, connectors)
# Refresh the transition bounding box
def RefreshBoundingBox(self):
if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]:
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y,
self.Size[0] + 1, self.Size[1] + 1)
elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]:
self.BoundingBox = wx.Rect(
self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y,
self.Size[0] + 2 * SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Size[1] + 1)
# Refresh the position of wires connected to divergence
def HitTest(self, pt, connectors=True):
# Calculate the bounding box of the condition outside the transition
text_width, text_height = self.TargetSize
text_bbx = wx.Rect(self.Pos.x + self.Size[0] + 2,
self.Pos.y + (self.Size[1] - text_height) / 2,
text_width,
text_height)
return text_bbx.InsideXY(pt.x, pt.y) or Graphic_Element.HitTest(self, pt, connectors)
# Refresh the jump bounding box
def RefreshBoundingBox(self):
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
# Refresh the position of wires connected to action block
def __init__(self, parent, id=None):
ToolTipProducer.__init__(self, parent)
self.Parent = parent
self.Id = id
self.oldPos = None
self.StartPos = None
self.CurrentDrag = None
self.Handle = (None, None)
self.Dragging = False
self.Selected = False
self.Highlighted = False
self.Pos = wx.Point(0, 0)
self.Size = wx.Size(0, 0)
self.BoundingBox = wx.Rect(0, 0, 0, 0)
self.Visible = False
def RefreshBoundingBox(self):
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1])
# Refresh the element connectors position
def HitTest(self, pt, connectors=True):
if connectors:
rect = self.BoundingBox
else:
rect = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1])
return rect.InsideXY(pt.x, pt.y)
# Returns if the point given is in the bounding box
def GetRedrawRect(self, movex=0, movey=0):
scalex, scaley = self.Parent.GetViewScale()
rect = wx.Rect()
rect.x = self.BoundingBox.x - int(HANDLE_SIZE / scalex) - 3 - abs(movex)
rect.y = self.BoundingBox.y - int(HANDLE_SIZE / scaley) - 3 - abs(movey)
rect.width = self.BoundingBox.width + 2 * (int(HANDLE_SIZE / scalex) + abs(movex) + 1) + 4
rect.height = self.BoundingBox.height + 2 * (int(HANDLE_SIZE / scaley) + abs(movey) + 1) + 4
return rect
def GetRedrawRect(self, movex=0, movey=0):
parent_pos = self.ParentBlock.GetPosition()
x = min(parent_pos[0] + self.Pos.x, parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE)
y = min(parent_pos[1] + self.Pos.y, parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE)
has_modifier = self.Negated or self.Edge != "none"
if self.Direction[0] == 0:
width = 10 if has_modifier else 5
else:
width = CONNECTOR_SIZE
if self.Edge == "rising" and self.Direction[0] == 1:
x -= 5
width += 5
if self.Direction[1] == 0:
height = 10 if has_modifier else 5
else:
height = CONNECTOR_SIZE
if self.Edge == "rising" and self.Direction[1] == 1:
y -= 5
height += 5
rect = wx.Rect(x - abs(movex), y - abs(movey), width + 2 * abs(movex), height + 2 * abs(movey))
if self.ValueSize is None and isinstance(self.ComputedValue, (StringType, UnicodeType)):
self.ValueSize = self.ParentBlock.Parent.GetMiniTextExtent(self.ComputedValue)
if self.ValueSize is not None:
width, height = self.ValueSize
rect = rect.Union(
wx.Rect(
parent_pos[0] + self.Pos.x + CONNECTOR_SIZE * self.Direction[0] +
width * (self.Direction[0] - 1) / 2,
parent_pos[1] + self.Pos.y + CONNECTOR_SIZE * self.Direction[1] +
height * (self.Direction[1] - 1),
width, height))
return rect
# Change the connector selection
def GetRedrawRect(self, movex=0, movey=0):
rect = Graphic_Element.GetRedrawRect(self, movex, movey)
if self.StartConnected:
rect = rect.Union(self.StartConnected.GetRedrawRect(movex, movey))
if self.EndConnected:
rect = rect.Union(self.EndConnected.GetRedrawRect(movex, movey))
if self.ValueSize is None and isinstance(self.ComputedValue, (StringType, UnicodeType)):
self.ValueSize = self.Parent.GetMiniTextExtent(self.ComputedValue)
if self.ValueSize is not None:
width, height = self.ValueSize
if self.BoundingBox[2] > width * 4 or self.BoundingBox[3] > height * 4:
x = self.Points[0].x + width * self.StartPoint[1][0] / 2
y = self.Points[0].y + height * (self.StartPoint[1][1] - 1)
rect = rect.Union(wx.Rect(x, y, width, height))
x = self.Points[-1].x + width * self.EndPoint[1][0] / 2
y = self.Points[-1].y + height * (self.EndPoint[1][1] - 1)
rect = rect.Union(wx.Rect(x, y, width, height))
else:
middle = len(self.Segments) / 2 + len(self.Segments) % 2 - 1
x = (self.Points[middle].x + self.Points[middle + 1].x - width) / 2
if self.BoundingBox[3] > height and self.Segments[middle] in [NORTH, SOUTH]:
y = (self.Points[middle].y + self.Points[middle + 1].y - height) / 2
else:
y = self.Points[middle].y - height
rect = rect.Union(wx.Rect(x, y, width, height))
return rect
def RefreshBoundingBox(self):
if len(self.Points) > 0:
# If startpoint or endpoint is connected, save the point radius
start_radius = end_radius = 0
if not self.StartConnected:
start_radius = POINT_RADIUS
if not self.EndConnected:
end_radius = POINT_RADIUS
# Initialize minimum and maximum from the first point
minx, minbbxx = self.Points[0].x, self.Points[0].x - start_radius
maxx, maxbbxx = self.Points[0].x, self.Points[0].x + start_radius
miny, minbbxy = self.Points[0].y, self.Points[0].y - start_radius
maxy, maxbbxy = self.Points[0].y, self.Points[0].y + start_radius
# Actualize minimum and maximum with the other points
for point in self.Points[1:-1]:
minx, minbbxx = min(minx, point.x), min(minbbxx, point.x)
maxx, maxbbxx = max(maxx, point.x), max(maxbbxx, point.x)
miny, minbbxy = min(miny, point.y), min(minbbxy, point.y)
maxy, maxbbxy = max(maxy, point.y), max(maxbbxy, point.y)
if len(self.Points) > 1:
minx, minbbxx = min(minx, self.Points[-1].x), min(minbbxx, self.Points[-1].x - end_radius)
maxx, maxbbxx = max(maxx, self.Points[-1].x), max(maxbbxx, self.Points[-1].x + end_radius)
miny, minbbxy = min(miny, self.Points[-1].y), min(minbbxy, self.Points[-1].y - end_radius)
maxy, maxbbxy = max(maxy, self.Points[-1].y), max(maxbbxy, self.Points[-1].y + end_radius)
self.Pos.x, self.Pos.y = minx, miny
self.Size = wx.Size(maxx - minx, maxy - miny)
self.BoundingBox = wx.Rect(minbbxx, minbbxy, maxbbxx - minbbxx + 1, maxbbxy - minbbxy + 1)
# Refresh the realpoints that permits to keep the proportionality in wire during resizing
def TestPoint(self, pt):
# Test the wire start point
rect = wx.Rect(self.Points[0].x - ANCHOR_DISTANCE, self.Points[0].y - ANCHOR_DISTANCE,
2 * ANCHOR_DISTANCE, 2 * ANCHOR_DISTANCE)
if rect.InsideXY(pt.x, pt.y):
return 0
# Test the wire end point
if len(self.Points) > 1:
rect = wx.Rect(self.Points[-1].x - ANCHOR_DISTANCE, self.Points[-1].y - ANCHOR_DISTANCE,
2 * ANCHOR_DISTANCE, 2 * ANCHOR_DISTANCE)
if rect.InsideXY(pt.x, pt.y):
return -1
return None
# Returns the wire segment if the point given is on it
def TestSegment(self, pt, all=False):
for i in xrange(len(self.Segments)):
# If wire is not in a Ladder Diagram, first and last segments are excluded
if all or 0 < i < len(self.Segments) - 1:
x1, y1 = self.Points[i].x, self.Points[i].y
x2, y2 = self.Points[i + 1].x, self.Points[i + 1].y
# Calculate a rectangle around the segment
rect = wx.Rect(min(x1, x2) - ANCHOR_DISTANCE, min(y1, y2) - ANCHOR_DISTANCE,
abs(x1 - x2) + 2 * ANCHOR_DISTANCE, abs(y1 - y2) + 2 * ANCHOR_DISTANCE)
if rect.InsideXY(pt.x, pt.y):
return i, self.Segments[i]
return None
# Define the wire points
def RefreshBoundingBox(self):
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
# Changes the comment size
def RefreshBoundingBox(self):
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
# Refresh the power rail size
def GetTextBoundingBox(self):
# Calculate the size of the name outside the block
text_width, text_height = self.NameSize
return wx.Rect(self.Pos.x + (self.Size[0] - text_width) / 2,
self.Pos.y - (text_height + 2),
text_width,
text_height)
# Returns the bounding box of function block without name outside
def GetBlockBoundingBox(self, connectors=True):
bbx_x, bbx_y = self.Pos.x, self.Pos.y
bbx_width, bbx_height = self.Size
if connectors:
bbx_x -= min(1, len(self.Inputs)) * CONNECTOR_SIZE
bbx_width += (min(1, len(self.Inputs)) + min(1, len(self.Outputs))) * CONNECTOR_SIZE
if self.ExecutionOrder != 0:
bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0])
bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0])
bbx_height = bbx_height + (self.ExecutionOrderSize[1] + 2)
return wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
# Refresh the block bounding box