Python future.builtins.round() Examples

The following are 25 code examples of future.builtins.round(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module future.builtins , or try the search function .
Example #1
Source File: LD_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 6 votes vote down vote up
def RefreshConnectors(self):
        scaling = self.Parent.GetScaling()
        height = self.Size[1] - self.Extensions[0] - self.Extensions[1]
        interval = height / max(len(self.Connectors) - 1, 1)
        for i, connector in enumerate(self.Connectors):
            if self.RealConnectors:
                position = self.Extensions[0] + int(round(self.RealConnectors[i] * height))
            else:
                position = self.Extensions[0] + int(round(i * interval))
            if scaling is not None:
                position = round((self.Pos.y + position) / scaling[1]) * scaling[1] - self.Pos.y
            if self.Type == LEFTRAIL:
                connector.SetPosition(wx.Point(self.Size[0], position))
            elif self.Type == RIGHTRAIL:
                connector.SetPosition(wx.Point(0, position))
        self.RefreshConnected()

    # Refresh the position of wires connected to power rail 
Example #2
Source File: Viewer.py    From OpenPLC_Editor with GNU General Public License v3.0 6 votes vote down vote up
def AddVariableBlock(self, x, y, scaling, var_class, var_name, var_type):
        id = self.GetNewId()
        variable = FBD_Variable(self, var_class, var_name, var_type, id)
        width, height = variable.GetMinSize()
        if scaling is not None:
            x = round(x / scaling[0]) * scaling[0]
            y = round(y / scaling[1]) * scaling[1]
            width = round(width / scaling[0] + 0.5) * scaling[0]
            height = round(height / scaling[1] + 0.5) * scaling[1]
        variable.SetPosition(x, y)
        variable.SetSize(width, height)
        self.AddBlock(variable)
        self.Controler.AddEditedElementVariable(self.GetTagName(), id, var_class)
        self.RefreshVariableModel(variable)
        self.RefreshBuffer()
        self.RefreshScrollBars()
        self.RefreshVisibleElements()
        self.Editor.Refresh(False)

    # -------------------------------------------------------------------------------
    #                          Model adding functions
    # ------------------------------------------------------------------------------- 
Example #3
Source File: Viewer.py    From OpenPLC_Editor with GNU General Public License v3.0 6 votes vote down vote up
def EnsureVisible(self, block):
        xstart, ystart = self.GetViewStart()
        window_size = self.Editor.GetClientSize()
        block_bbx = block.GetBoundingBox()

        screen_minx, screen_miny = xstart * SCROLLBAR_UNIT, ystart * SCROLLBAR_UNIT
        screen_maxx, screen_maxy = screen_minx + window_size[0], screen_miny + window_size[1]
        block_minx = int(block_bbx.x * self.ViewScale[0])
        block_miny = int(block_bbx.y * self.ViewScale[1])
        block_maxx = int(round((block_bbx.x + block_bbx.width) * self.ViewScale[0]))
        block_maxy = int(round((block_bbx.y + block_bbx.height) * self.ViewScale[1]))

        xpos, ypos = xstart, ystart
        if block_minx < screen_minx and block_maxx < screen_maxx:
            xpos -= (screen_minx - block_minx) // SCROLLBAR_UNIT + 1
        elif block_maxx > screen_maxx and block_minx > screen_minx:
            xpos += (block_maxx - screen_maxx) // SCROLLBAR_UNIT + 1
        if block_miny < screen_miny and block_maxy < screen_maxy:
            ypos -= (screen_miny - block_miny) // SCROLLBAR_UNIT + 1
        elif block_maxy > screen_maxy and block_miny > screen_miny:
            ypos += (block_maxy - screen_maxy) // SCROLLBAR_UNIT + 1
        self.Scroll(xpos, ypos) 
Example #4
Source File: LD_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 6 votes vote down vote up
def DrawHighlightment(self, dc):
        scalex, scaley = dc.GetUserScale()
        dc.SetUserScale(1, 1)
        dc.SetPen(MiterPen(HIGHLIGHTCOLOR, (3 * scalex + 5), wx.SOLID))
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
        dc.SetLogicalFunction(wx.AND)
        # Draw a two circle arcs for representing the coil
        dc.DrawEllipticArc(round(self.Pos.x * scalex),
                           round((self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1) * scaley),
                           round(self.Size[0] * scalex),
                           round((int(self.Size[1] * sqrt(2)) - 1) * scaley),
                           135, 225)
        dc.DrawEllipticArc(round(self.Pos.x * scalex),
                           round((self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1) * scaley),
                           round(self.Size[0] * scalex),
                           round((int(self.Size[1] * sqrt(2)) - 1) * scaley),
                           -45, 45)
        dc.SetLogicalFunction(wx.COPY)
        dc.SetUserScale(scalex, scaley)

    # Adds an highlight to the connection 
Example #5
Source File: Viewer.py    From OpenPLC_Editor with GNU General Public License v3.0 6 votes vote down vote up
def RefreshScrollBars(self, width_incr=0, height_incr=0):
        xstart, ystart = self.GetViewStart()
        window_size = self.Editor.GetClientSize()
        maxx, maxy = self.GetMaxSize()
        maxx = max(maxx + WINDOW_BORDER, (xstart * SCROLLBAR_UNIT + window_size[0]) / self.ViewScale[0])
        maxy = max(maxy + WINDOW_BORDER, (ystart * SCROLLBAR_UNIT + window_size[1]) / self.ViewScale[1])
        if self.rubberBand.IsShown():
            extent = self.rubberBand.GetCurrentExtent()
            maxx = max(maxx, extent.x + extent.width)
            maxy = max(maxy, extent.y + extent.height)
        maxx = int(maxx * self.ViewScale[0])
        maxy = int(maxy * self.ViewScale[1])
        self.Editor.SetScrollbars(
            SCROLLBAR_UNIT, SCROLLBAR_UNIT,
            round(maxx / SCROLLBAR_UNIT) + width_incr,
            round(maxy / SCROLLBAR_UNIT) + height_incr,
            xstart, ystart, True) 
Example #6
Source File: GraphicCommons.py    From OpenPLC_Editor with GNU General Public License v3.0 6 votes vote down vote up
def DrawHighlightment(self, dc):
        scalex, scaley = dc.GetUserScale()
        dc.SetUserScale(1, 1)
        pen = MiterPen(HIGHLIGHTCOLOR, 2 * scalex + 5)
        pen.SetCap(wx.CAP_BUTT)
        dc.SetPen(pen)
        dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR))
        dc.SetLogicalFunction(wx.AND)
        parent_pos = self.ParentBlock.GetPosition()
        xstart = parent_pos[0] + self.Pos.x
        ystart = parent_pos[1] + self.Pos.y
        if self.Direction[0] < 0:
            xstart += 1
        if self.Direction[1] < 0:
            ystart += 1
        xend = xstart + CONNECTOR_SIZE * self.Direction[0]
        yend = ystart + CONNECTOR_SIZE * self.Direction[1]
        dc.DrawLine(round((xstart + self.Direction[0]) * scalex), round((ystart + self.Direction[1]) * scaley),
                    round(xend * scalex), round(yend * scaley))
        dc.SetLogicalFunction(wx.COPY)
        dc.SetUserScale(scalex, scaley)

    # Adds an highlight to the connector 
Example #7
Source File: SFC_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 6 votes vote down vote up
def DrawHighlightment(self, dc):
        scalex, scaley = dc.GetUserScale()
        dc.SetUserScale(1, 1)
        dc.SetPen(MiterPen(HIGHLIGHTCOLOR))
        dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR))
        dc.SetLogicalFunction(wx.AND)
        points = [wx.Point(int(round((self.Pos.x - 2) * scalex)) - 3,
                           int(round((self.Pos.y - 2) * scaley)) - 2),
                  wx.Point(int(round((self.Pos.x + self.Size[0] + 2) * scalex)) + 4,
                           int(round((self.Pos.y - 2) * scaley)) - 2),
                  wx.Point(int(round((self.Pos.x + self.Size[0] / 2) * scalex)),
                           int(round((self.Pos.y + self.Size[1] + 3) * scaley)) + 4)]
        dc.DrawPolygon(points)
        dc.SetLogicalFunction(wx.COPY)
        dc.SetUserScale(scalex, scaley)

    # Draws divergence 
Example #8
Source File: SFC_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 6 votes vote down vote up
def RefreshConnectors(self):
        scaling = self.Parent.GetScaling()
        horizontal_pos = self.Size[0] // 2
        vertical_pos = self.Size[1] // 2
        if scaling is not None:
            horizontal_pos = round((self.Pos.x + horizontal_pos) / scaling[0]) * scaling[0] - self.Pos.x
            vertical_pos = round((self.Pos.y + vertical_pos) / scaling[1]) * scaling[1] - self.Pos.y
        # Update input position if it exists
        if self.Input:
            self.Input.SetPosition(wx.Point(horizontal_pos, 0))
        # Update output position
        if self.Output:
            self.Output.SetPosition(wx.Point(horizontal_pos, self.Size[1]))
        # Update action position if it exists
        if self.Action:
            self.Action.SetPosition(wx.Point(self.Size[0], vertical_pos))
        self.RefreshConnected()

    # Refresh the position of wires connected to step 
Example #9
Source File: SFC_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 6 votes vote down vote up
def RefreshConnectors(self):
        scaling = self.Parent.GetScaling()
        horizontal_pos = self.Size[0] // 2
        vertical_pos = self.Size[1] // 2
        if scaling is not None:
            horizontal_pos = round((self.Pos.x + horizontal_pos) / scaling[0]) * scaling[0] - self.Pos.x
            vertical_pos = round((self.Pos.y + vertical_pos) / scaling[1]) * scaling[1] - self.Pos.y
        # Update input position
        self.Input.SetPosition(wx.Point(horizontal_pos, 0))
        # Update output position
        self.Output.SetPosition(wx.Point(horizontal_pos, self.Size[1]))
        if self.Type == "connection":
            self.Condition.SetPosition(wx.Point(0, vertical_pos))
        self.RefreshConnected()

    # Refresh the position of the wires connected to transition 
Example #10
Source File: SFC_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 6 votes vote down vote up
def SetSize(self, width, height):
        height = self.GetMinSize()[1]
        for i, input in enumerate(self.Inputs):
            position = input.GetRelPosition()
            if self.RealConnectors:
                input.SetPosition(wx.Point(int(round(self.RealConnectors["Inputs"][i] * width)), 0))
            else:
                input.SetPosition(wx.Point(int(round(position.x*width / self.Size[0])), 0))
            input.MoveConnected()
        for i, output in enumerate(self.Outputs):
            position = output.GetRelPosition()
            if self.RealConnectors:
                output.SetPosition(wx.Point(int(round(self.RealConnectors["Outputs"][i] * width)), height))
            else:
                output.SetPosition(wx.Point(int(round(position.x*width / self.Size[0])), height))
            output.MoveConnected()
        self.Size = wx.Size(width, height)
        self.RefreshBoundingBox()

    # Returns the divergence minimum size 
Example #11
Source File: SFC_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 6 votes vote down vote up
def DrawHighlightment(self, dc):
        scalex, scaley = dc.GetUserScale()
        dc.SetUserScale(1, 1)
        dc.SetPen(MiterPen(HIGHLIGHTCOLOR))
        dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR))
        dc.SetLogicalFunction(wx.AND)
        # Draw two rectangles for representing the contact
        posx = self.Pos.x
        width = self.Size[0]
        if self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]:
            posx -= SFC_SIMULTANEOUS_SEQUENCE_EXTRA
            width += SFC_SIMULTANEOUS_SEQUENCE_EXTRA * 2
        dc.DrawRectangle(int(round((posx - 1) * scalex)) - 2,
                         int(round((self.Pos.y - 1) * scaley)) - 2,
                         int(round((width + 3) * scalex)) + 5,
                         int(round((self.Size.height + 3) * scaley)) + 5)
        dc.SetLogicalFunction(wx.COPY)
        dc.SetUserScale(scalex, scaley)

    # Draws divergence 
Example #12
Source File: Viewer.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def GetScaledSize(self, width, height):
        if self.Scaling is not None:
            width = round(width / self.Scaling[0] + 0.4) * self.Scaling[0]
            height = round(height / self.Scaling[1] + 0.4) * self.Scaling[1]
        return width, height 
Example #13
Source File: Viewer.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
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() 
Example #14
Source File: SFC_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def RefreshConnectors(self):
        scaling = self.Parent.GetScaling()
        vertical_pos = SFC_ACTION_MIN_SIZE[1] // 2
        if scaling is not None:
            vertical_pos = round((self.Pos.y + vertical_pos) / scaling[1]) * scaling[1] - self.Pos.y
        self.Input.SetPosition(wx.Point(0, vertical_pos))
        self.RefreshConnected()

    # Changes the action block actions 
Example #15
Source File: SFC_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def ProcessDragging(self, movex, movey, event, scaling):
        if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
            movex = max(-self.BoundingBox.x, movex)
            if scaling is not None:
                movex = round((self.Pos.x + movex) / scaling[0]) * scaling[0] - self.Pos.x
            self.Move(movex, 0)
            self.RefreshInputPosition()
            return movex, 0
        else:
            return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, width_fac=2)

    # Refresh input element model 
Example #16
Source File: SFC_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def RefreshConnectors(self):
        scaling = self.Parent.GetScaling()
        horizontal_pos = self.Size[0] // 2
        if scaling is not None:
            horizontal_pos = round((self.Pos.x + horizontal_pos) / 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 
Example #17
Source File: SFC_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def ProcessDragging(self, movex, movey, event, scaling):
        if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
            movex = max(-self.BoundingBox.x, movex)
            if scaling is not None:
                movex = round((self.Pos.x + movex) / scaling[0]) * scaling[0] - self.Pos.x
            self.Move(movex, 0)
            self.RefreshInputPosition()
            self.RefreshOutputPosition()
            return movex, 0
        else:
            return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, width_fac=2, height_fac=2)

    # Refresh input element model 
Example #18
Source File: SFC_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def ProcessDragging(self, movex, movey, event, scaling):
        handle_type, _handle = self.Handle
        if handle_type == HANDLE_MOVE:
            movex = max(-self.BoundingBox.x, movex)
            movey = max(-self.BoundingBox.y, movey)
            if scaling is not None:
                movex = round((self.Pos.x + movex) / scaling[0]) * scaling[0] - self.Pos.x
                movey = round((self.Pos.y + movey) / scaling[1]) * scaling[1] - self.Pos.y
            if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
                self.Move(movex, movey)
                self.RefreshConnected()
                return movex, movey
            elif self.Initial:
                self.MoveActionBlock((movex, movey))
                self.Move(movex, movey, self.Parent.Wires)
                self.RefreshOutputPosition((movex, movey))
                return movex, movey
            else:
                self.MoveActionBlock((movex, 0))
                self.Move(movex, 0)
                self.RefreshInputPosition()
                self.RefreshOutputPosition()
                return movex, 0
        else:
            return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling)

    # Refresh input element model 
Example #19
Source File: GraphicCommons.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def DrawHighlightment(self, dc):
        scalex, scaley = dc.GetUserScale()
        dc.SetUserScale(1, 1)
        dc.SetPen(MiterPen(HIGHLIGHTCOLOR))
        dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR))
        dc.SetLogicalFunction(wx.AND)
        dc.DrawRectangle(int(round((self.Pos.x - 1) * scalex)) - 2,
                         int(round((self.Pos.y - 1) * scaley)) - 2,
                         int(round((self.Size.width + 3) * scalex)) + 5,
                         int(round((self.Size.height + 3) * scaley)) + 5)
        dc.SetLogicalFunction(wx.COPY)
        dc.SetUserScale(scalex, scaley)

    # Draws the handles of this element if it is selected 
Example #20
Source File: GraphicCommons.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def GetScaledEventPosition(event, dc, scaling):
    """
    Function that calculates the nearest point of the grid defined by scaling for the given point
    """
    pos = event.GetLogicalPosition(dc)
    if scaling:
        pos.x = round(pos.x / scaling[0]) * scaling[0]
        pos.y = round(pos.y / scaling[1]) * scaling[1]
    return pos 
Example #21
Source File: LD_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def RefreshConnectors(self):
        scaling = self.Parent.GetScaling()
        position = self.Size[1] // 2 + 1
        if scaling is not None:
            position = round((self.Pos.y + position) / scaling[1]) * scaling[1] - self.Pos.y
        self.Input.SetPosition(wx.Point(0, position))
        self.Output.SetPosition(wx.Point(self.Size[0], position))
        self.RefreshConnected()

    # Changes the coil name 
Example #22
Source File: LD_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def RefreshConnectors(self):
        scaling = self.Parent.GetScaling()
        position = self.Size[1] // 2 + 1
        if scaling is not None:
            position = round((self.Pos.y + position) / scaling[1]) * scaling[1] - self.Pos.y
        self.Input.SetPosition(wx.Point(0, position))
        self.Output.SetPosition(wx.Point(self.Size[0], position))
        self.RefreshConnected()

    # Changes the contact name 
Example #23
Source File: LD_Objects.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def ProcessDragging(self, movex, movey, event, scaling):
        handle_type, handle = self.Handle
        # A connector has been handled
        if handle_type == HANDLE_CONNECTOR:
            movey = max(-self.BoundingBox.y, movey)
            if scaling is not None:
                position = handle.GetRelPosition()
                movey = round((self.Pos.y + position.y + movey) / scaling[1]) * scaling[1] - self.Pos.y - position.y
            self.MoveConnector(handle, movey)
            return 0, movey
        elif self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
            return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling)
        return 0, 0

    # Refreshes the power rail model 
Example #24
Source File: GraphicCommons.py    From OpenPLC_Editor with GNU General Public License v3.0 4 votes vote down vote up
def OnLeftDClick(self, event, dc, scaling):
        rect = self.GetRedrawRect()
        if event.ControlDown():
            direction = (self.StartPoint[1], self.EndPoint[1])
            if direction in [(EAST, WEST), (WEST, EAST)]:
                avgy = (self.StartPoint[0].y + self.EndPoint[0].y) // 2
                if scaling is not None:
                    avgy = round(avgy / scaling[1]) * scaling[1]
                if self.StartConnected is not None:
                    movey = avgy - self.StartPoint[0].y
                    startblock = self.StartConnected.GetParentBlock()
                    startblock.Move(0, movey)
                    startblock.RefreshModel()
                    rect.Union(startblock.GetRedrawRect(0, movey))
                else:
                    self.MoveStartPoint(wx.Point(self.StartPoint[0].x, avgy))
                if self.EndConnected is not None:
                    movey = avgy - self.EndPoint[0].y
                    endblock = self.EndConnected.GetParentBlock()
                    endblock.Move(0, movey)
                    endblock.RefreshModel()
                    rect.Union(endblock.GetRedrawRect(0, movey))
                else:
                    self.MoveEndPoint(wx.Point(self.EndPoint[0].x, avgy))
                self.Parent.RefreshBuffer()
            elif direction in [(NORTH, SOUTH), (SOUTH, NORTH)]:
                avgx = (self.StartPoint[0].x + self.EndPoint[0].x) // 2
                if scaling is not None:
                    avgx = round(avgx / scaling[0]) * scaling[0]
                if self.StartConnected is not None:
                    movex = avgx - self.StartPoint[0].x
                    startblock = self.StartConnected.GetParentBlock()
                    startblock.Move(movex, 0)
                    startblock.RefreshModel()
                    rect.Union(startblock.GetRedrawRect(movex, 0))
                else:
                    self.MoveStartPoint(wx.Point(avgx, self.StartPoint[0].y))
                if self.EndConnected is not None:
                    movex = avgx - self.EndPoint[0].x
                    endblock = self.EndConnected.GetParentBlock()
                    endblock.Move(movex, 0)
                    endblock.RefreshModel()
                    rect.Union(endblock.GetRedrawRect(movex, 0))
                else:
                    self.MoveEndPoint(wx.Point(avgx, self.EndPoint[0].y))
                self.Parent.RefreshBuffer()
        else:
            self.ResetPoints()
            self.GeneratePoints()
            self.RefreshModel()
            self.Parent.RefreshBuffer()
        rect.Union(self.GetRedrawRect())
        self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False)

    # Method called when a Motion event has been generated 
Example #25
Source File: GraphicCommons.py    From OpenPLC_Editor with GNU General Public License v3.0 4 votes vote down vote up
def Resize(self, x, y, width, height):
        if len(self.Points) > 1:
            # Calculate the new position of each point for testing the new size
            minx, miny = self.Pos.x, self.Pos.y
            lastwidth, lastheight = self.Size.width, self.Size.height
            for i, point in enumerate(self.RealPoints):
                # If start or end point is connected, it's not calculate
                if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected):
                    if i == 0:
                        dir = self.StartPoint[1]
                    elif i == len(self.Points) - 1:
                        dir = self.EndPoint[1]
                    else:
                        dir = (0, 0)
                    pointx = max(-dir[0] * MIN_SEGMENT_SIZE, min(int(round(point[0] * width / max(lastwidth, 1))),
                                                                 width - dir[0] * MIN_SEGMENT_SIZE))
                    pointy = max(-dir[1] * MIN_SEGMENT_SIZE, min(int(round(point[1] * height / max(lastheight, 1))),
                                                                 height - dir[1] * MIN_SEGMENT_SIZE))
                    self.Points[i] = wx.Point(minx + x + pointx, miny + y + pointy)
            self.StartPoint[0] = self.Points[0]
            self.EndPoint[0] = self.Points[-1]
            self.GeneratePoints(False)
            # Test if the wire position or size have changed
            if x != 0 and minx == self.Pos.x:
                x = 0
                width = lastwidth
            if y != 0 and miny == self.Pos.y:
                y = 0
                height = lastwidth
            if width != lastwidth and lastwidth == self.Size.width:
                width = lastwidth
            if height != lastheight and lastheight == self.Size.height:
                height = lastheight
            # Calculate the real points from the new size, it's important for
            # keeping a proportionality in the points position with the size
            # during a resize dragging
            for i, point in enumerate(self.RealPoints):
                if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected):
                    point[0] = point[0] * width / max(lastwidth, 1)
                    point[1] = point[1] * height / max(lastheight, 1)
            # Calculate the correct position of the points from real points
            for i, point in enumerate(self.RealPoints):
                if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected):
                    if i == 0:
                        dir = self.StartPoint[1]
                    elif i == len(self.Points) - 1:
                        dir = self.EndPoint[1]
                    else:
                        dir = (0, 0)
                    realpointx = max(-dir[0] * MIN_SEGMENT_SIZE,
                                     min(int(round(point[0])),
                                         width - dir[0] * MIN_SEGMENT_SIZE))
                    realpointy = max(-dir[1] * MIN_SEGMENT_SIZE,
                                     min(int(round(point[1])),
                                         height - dir[1] * MIN_SEGMENT_SIZE))
                    self.Points[i] = wx.Point(minx + x + realpointx, miny + y + realpointy)
            self.StartPoint[0] = self.Points[0]
            self.EndPoint[0] = self.Points[-1]
            self.GeneratePoints(False)

    # Moves the wire start point and update the wire points