def SetToolTipPosition(self, pos):
"""
Set tool tip position
@param pos: New tool tip position
"""
# Get screen size to prevent tool tip to go out of the screen
screen_width, screen_height = wx.GetDisplaySize()
# Calculate position of tool tip to stay in screen limits
tip_width, tip_height = self.GetToolTipSize()
self.SetPosition(wx.Point(
max(0, min(pos.x, screen_width - tip_width)),
max(0, min(pos.y, screen_height - tip_height))))
python类Point()的实例源码
def GetDraggingAxesPosition(self, panel):
x, y = panel.GetPosition()
return wx.Point(self.DraggingAxesBoundingBox.x - x,
self.DraggingAxesBoundingBox.y - y)
def SetPosition(self, x, y):
"""
Set button position
@param x: X coordinate of Button in Graphic Viewer
@param y: Y coordinate of Button in Graphic Viewer
"""
self.Position = wx.Point(x, y)
def OnLeftDown(self, event):
selected = self.ListBox.HitTest(wx.Point(event.GetX(), event.GetY()))
parent_size = self.Parent.GetSize()
parent_rect = wx.Rect(0, -parent_size[1], parent_size[0], parent_size[1])
if selected != wx.NOT_FOUND:
wx.CallAfter(self.Parent.SetValueFromSelected, self.ListBox.GetString(selected))
elif parent_rect.InsideXY(event.GetX(), event.GetY()):
result, x, y = self.Parent.HitTest(wx.Point(event.GetX(), event.GetY() + parent_size[1]))
if result != wx.TE_HT_UNKNOWN:
self.Parent.SetInsertionPoint(self.Parent.XYToPosition(x, y))
else:
wx.CallAfter(self.Parent.DismissListBox)
event.Skip()
def OnMotion(self, event):
self.ListBox.SetSelection(
self.ListBox.HitTest(wx.Point(event.GetX(), event.GetY())))
event.Skip()
def CreateTransition(self, connector, next=None):
previous = connector.GetParentBlock()
id = self.GetNewId()
transition = SFC_Transition(self, "reference", "", 0, id)
pos = connector.GetPosition(False)
transition.SetPosition(pos.x, pos.y + SFC_WIRE_MIN_SIZE)
transition_connectors = transition.GetConnectors()
wire = self.ConnectConnectors(transition_connectors["input"], connector)
if isinstance(previous, SFC_Divergence):
previous.RefreshConnectedPosition(connector)
else:
previous.RefreshOutputPosition()
wire.SetPoints([wx.Point(pos.x, pos.y + GetWireSize(previous)), wx.Point(pos.x, pos.y)])
self.AddBlock(transition)
self.Controler.AddEditedElementTransition(self.TagName, id)
self.RefreshTransitionModel(transition)
if next:
wire = self.ConnectConnectors(next, transition_connectors["output"])
pos = transition_connectors["output"].GetPosition(False)
next_block = next.GetParentBlock()
next_pos = next.GetPosition(False)
transition.RefreshOutputPosition((0, pos.y + SFC_WIRE_MIN_SIZE - next_pos.y))
wire.SetPoints([wx.Point(pos.x, pos.y + SFC_WIRE_MIN_SIZE), wx.Point(pos.x, pos.y)])
if isinstance(next_block, SFC_Divergence):
next_block.RefreshPosition()
transition.RefreshOutputModel(True)
return transition
def AddStepAction(self):
if isinstance(self.SelectedElement, SFC_Step):
connectors = self.SelectedElement.GetConnectors()
if not connectors["action"]:
dialog = ActionBlockDialog(self.ParentWindow)
dialog.SetQualifierList(self.Controler.GetQualifierTypes())
dialog.SetActionList(self.Controler.GetEditedElementActions(self.TagName, self.Debug))
dialog.SetVariableList(self.Controler.GetEditedElementInterfaceVars(self.TagName, debug=self.Debug))
if dialog.ShowModal() == wx.ID_OK:
actions = dialog.GetValues()
self.SelectedElement.AddAction()
self.RefreshStepModel(self.SelectedElement)
connectors = self.SelectedElement.GetConnectors()
pos = connectors["action"].GetPosition(False)
id = self.GetNewId()
actionblock = SFC_ActionBlock(self, [], id)
actionblock.SetPosition(pos.x + SFC_WIRE_MIN_SIZE, pos.y - SFC_STEP_DEFAULT_SIZE[1] / 2)
actionblock_connector = actionblock.GetConnector()
wire = self.ConnectConnectors(actionblock_connector, connectors["action"])
wire.SetPoints([wx.Point(pos.x + SFC_WIRE_MIN_SIZE, pos.y), wx.Point(pos.x, pos.y)])
actionblock.SetActions(actions)
self.AddBlock(actionblock)
self.Controler.AddEditedElementActionBlock(self.TagName, id)
self.RefreshActionBlockModel(actionblock)
self.RefreshBuffer()
self.RefreshScrollBars()
self.Refresh(False)
dialog.Destroy()
def AddJump(self):
if isinstance(self.SelectedElement, SFC_Step) and not self.SelectedElement.Output:
choices = []
for block in self.Blocks:
if isinstance(block, SFC_Step):
choices.append(block.GetName())
dialog = wx.SingleChoiceDialog(self.ParentWindow,
_("Add a new jump"),
_("Please choose a target"),
choices,
wx.DEFAULT_DIALOG_STYLE | wx.OK | wx.CANCEL)
if dialog.ShowModal() == wx.ID_OK:
value = dialog.GetStringSelection()
self.SelectedElement.AddOutput()
self.RefreshStepModel(self.SelectedElement)
step_connectors = self.SelectedElement.GetConnectors()
transition = self.CreateTransition(step_connectors["output"])
transition_connectors = transition.GetConnectors()
id = self.GetNewId()
jump = SFC_Jump(self, value, id)
pos = transition_connectors["output"].GetPosition(False)
jump.SetPosition(pos.x, pos.y + SFC_WIRE_MIN_SIZE)
self.AddBlock(jump)
self.Controler.AddEditedElementJump(self.TagName, id)
jump_connector = jump.GetConnector()
wire = self.ConnectConnectors(jump_connector, transition_connectors["output"])
transition.RefreshOutputPosition()
wire.SetPoints([wx.Point(pos.x, pos.y + SFC_WIRE_MIN_SIZE), wx.Point(pos.x, pos.y)])
self.RefreshJumpModel(jump)
self.RefreshBuffer()
self.RefreshScrollBars()
self.Refresh(False)
dialog.Destroy()
def stepCreationFunction(viewer, id, specific_values):
step = SFC_Step(viewer,
specific_values.name,
specific_values.initial,
id)
if specific_values.action is not None:
step.AddAction()
connector = step.GetActionConnector()
connector.SetPosition(wx.Point(*specific_values.action.position))
return step
def _init_Editor(self, prnt):
self.Editor = wx.ScrolledWindow(prnt, name="Viewer",
pos=wx.Point(0, 0), size=wx.Size(0, 0),
style=wx.HSCROLL | wx.VSCROLL)
self.Editor.ParentWindow = self
# Create a new Viewer
def SetScale(self, scale_number, refresh=True, mouse_event=None):
new_scale = max(0, min(scale_number, len(ZOOM_FACTORS) - 1))
if self.CurrentScale != new_scale:
if refresh:
dc = self.GetLogicalDC()
self.CurrentScale = new_scale
self.ViewScale = (ZOOM_FACTORS[self.CurrentScale], ZOOM_FACTORS[self.CurrentScale])
if refresh:
self.Editor.Freeze()
if mouse_event is None:
client_size = self.Editor.GetClientSize()
mouse_pos = wx.Point(client_size[0] / 2, client_size[1] / 2)
mouse_event = wx.MouseEvent(wx.EVT_MOUSEWHEEL.typeId)
mouse_event.x = mouse_pos.x
mouse_event.y = mouse_pos.y
else:
mouse_pos = mouse_event.GetPosition()
pos = mouse_event.GetLogicalPosition(dc)
xmax = self.GetScrollRange(wx.HORIZONTAL) - self.GetScrollThumb(wx.HORIZONTAL)
ymax = self.GetScrollRange(wx.VERTICAL) - self.GetScrollThumb(wx.VERTICAL)
scrollx = max(0, round(pos.x * self.ViewScale[0] - mouse_pos.x) / SCROLLBAR_UNIT)
scrolly = max(0, round(pos.y * self.ViewScale[1] - mouse_pos.y) / SCROLLBAR_UNIT)
if scrollx > xmax or scrolly > ymax:
self.RefreshScrollBars(max(0, scrollx - xmax), max(0, scrolly - ymax))
self.Scroll(scrollx, scrolly)
else:
self.Scroll(scrollx, scrolly)
self.RefreshScrollBars()
self.RefreshScaling(refresh)
self.Editor.Thaw()
def AddInput(self):
if not self.Input:
self.Input = Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH)
self.RefreshBoundingBox()
# Remove output connector from step
def AddOutput(self):
if not self.Output:
self.Output = Connector(self, "", None, wx.Point(self.Size[0] / 2, self.Size[1]), SOUTH, onlyone=True)
self.RefreshBoundingBox()
# Remove output connector from step
def RefreshOutputPosition(self, move=None):
if self.Output:
wires = self.Output.GetWires()
if len(wires) != 1:
return
current_pos = self.Output.GetPosition(False)
output = wires[0][0].GetOtherConnected(self.Output)
output_pos = output.GetPosition(False)
diffx = current_pos.x - output_pos.x
output_block = output.GetParentBlock()
wire_size = SFC_WIRE_MIN_SIZE + self.GetActionExtraLineNumber() * SFC_ACTION_MIN_SIZE[1]
diffy = wire_size - output_pos.y + current_pos.y
if diffy != 0:
if isinstance(output_block, SFC_Step):
output_block.MoveActionBlock((diffx, diffy))
wires[0][0].SetPoints([wx.Point(current_pos.x, current_pos.y + wire_size),
wx.Point(current_pos.x, current_pos.y)])
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0:
output_block.Move(diffx, diffy, self.Parent.Wires)
output_block.RefreshOutputPosition((diffx, diffy))
else:
output_block.RefreshPosition()
elif move:
if isinstance(output_block, SFC_Step):
output_block.MoveActionBlock(move)
wires[0][0].Move(move[0], move[1], True)
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0:
output_block.Move(move[0], move[1], self.Parent.Wires)
output_block.RefreshOutputPosition(move)
else:
output_block.RefreshPosition()
elif isinstance(output_block, SFC_Divergence):
output_block.MoveConnector(output, diffx)
else:
if isinstance(output_block, SFC_Step):
output_block.MoveActionBlock((diffx, 0))
output_block.Move(diffx, 0)
output_block.RefreshOutputPosition()
# Refresh action element with this step
def __init__(self, parent, type="reference", condition=None, priority=0, id=None):
Graphic_Element.__init__(self, parent)
DebugDataConsumer.__init__(self)
self.Type = None
self.Id = id
self.Priority = 0
self.Size = wx.Size(SFC_TRANSITION_SIZE[0], SFC_TRANSITION_SIZE[1])
# Create an input and output connector
self.Input = Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH, onlyone=True)
self.Output = Connector(self, "", None, wx.Point(self.Size[0] / 2, self.Size[1]), SOUTH, onlyone=True)
self.SetType(type, condition)
self.SetPriority(priority)
self.Highlights = {}
self.PreviousValue = None
self.PreviousSpreading = False
def MoveConnector(self, connector, movex):
position = connector.GetRelPosition()
connector.SetPosition(wx.Point(position.x + movex, position.y))
minx = self.Size[0]
maxx = 0
for input in self.Inputs:
input_pos = input.GetRelPosition()
minx = min(minx, input_pos.x)
maxx = max(maxx, input_pos.x)
for output in self.Outputs:
output_pos = output.GetRelPosition()
minx = min(minx, output_pos.x)
maxx = max(maxx, output_pos.x)
if minx != 0:
for input in self.Inputs:
input_pos = input.GetRelPosition()
input.SetPosition(wx.Point(input_pos.x - minx, input_pos.y))
for output in self.Outputs:
output_pos = output.GetRelPosition()
output.SetPosition(wx.Point(output_pos.x - minx, output_pos.y))
self.Inputs.sort(lambda x, y: cmp(x.Pos.x, y.Pos.x))
self.Outputs.sort(lambda x, y: cmp(x.Pos.x, y.Pos.x))
self.Pos.x += minx
self.Size[0] = maxx - minx
connector.MoveConnected()
self.RefreshBoundingBox()
# Returns the divergence connector that starts with the point given if it exists
def __init__(self, parent, target, id=None):
Graphic_Element.__init__(self, parent)
self.SetTarget(target)
self.Id = id
self.Size = wx.Size(SFC_JUMP_SIZE[0], SFC_JUMP_SIZE[1])
self.Highlights = []
# Create an input and output connector
self.Input = Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH, onlyone=True)
self.Value = None
self.PreviousValue = None
def RefreshConnectors(self):
scaling = self.Parent.GetScaling()
horizontal_pos = self.Size[0] / 2
if scaling is not None:
horizontal_pos = round(float(self.Pos.x + horizontal_pos) / float(scaling[0])) * scaling[0] - self.Pos.x
self.Input.SetPosition(wx.Point(horizontal_pos, 0))
self.RefreshConnected()
# Refresh the position of wires connected to jump
def Draw(self, dc):
Graphic_Element.Draw(self, dc)
if self.Value:
dc.SetPen(MiterPen(wx.GREEN))
dc.SetBrush(wx.GREEN_BRUSH)
else:
dc.SetPen(MiterPen(wx.BLACK))
dc.SetBrush(wx.BLACK_BRUSH)
if getattr(dc, "printing", False):
target_size = dc.GetTextExtent(self.Target)
else:
target_size = self.TargetSize
# Draw plain rectangle for representing the divergence
dc.DrawLine(self.Pos.x + self.Size[0] / 2, self.Pos.y, self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1])
points = [wx.Point(self.Pos.x, self.Pos.y),
wx.Point(self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1] / 3),
wx.Point(self.Pos.x + self.Size[0], self.Pos.y),
wx.Point(self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1])]
dc.DrawPolygon(points)
target_pos = (self.Pos.x + self.Size[0] + 2,
self.Pos.y + (self.Size[1] - target_size[1]) / 2)
dc.DrawText(self.Target, target_pos[0], target_pos[1])
# Draw input connector
if self.Input:
self.Input.Draw(dc)
if not getattr(dc, "printing", False):
DrawHighlightedText(dc, self.Target, self.Highlights, target_pos[0], target_pos[1])
# -------------------------------------------------------------------------------
# Sequencial Function Chart Action Block
# -------------------------------------------------------------------------------
def __init__(self, parent, actions=[], id=None):
Graphic_Element.__init__(self, parent)
self.Id = id
self.Size = wx.Size(SFC_ACTION_MIN_SIZE[0], SFC_ACTION_MIN_SIZE[1])
self.MinSize = wx.Size(SFC_ACTION_MIN_SIZE[0], SFC_ACTION_MIN_SIZE[1])
self.Highlights = {}
# Create an input and output connector
self.Input = Connector(self, "", None, wx.Point(0, SFC_ACTION_MIN_SIZE[1] / 2), WEST, onlyone=True)
self.SetActions(actions)
self.Value = None
self.PreviousValue = None