graphics/FBD_Objects.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Thu, 04 Oct 2018 12:07:16 +0300
changeset 2432 dbc065a2f7a5
parent 1881 091005ec69c4
child 2437 105c20fdeb19
permissions -rw-r--r--
python3 support: pylint, W1613 # (xrange-builtin) xrange built-in referenced
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     3
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     6
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     8
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
     9
# See COPYING file for copyrights details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    10
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
    11
# This program is free software; you can redistribute it and/or
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
    12
# modify it under the terms of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
    13
# as published by the Free Software Foundation; either version 2
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
    14
# of the License, or (at your option) any later version.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    15
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
    16
# This program is distributed in the hope that it will be useful,
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
    17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
    19
# GNU General Public License for more details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    20
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
    21
# You should have received a copy of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
    22
# along with this program; if not, write to the Free Software
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1176
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    24
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1872
diff changeset
    25
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1872
diff changeset
    26
from __future__ import absolute_import
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    27
import wx
2432
dbc065a2f7a5 python3 support: pylint, W1613 # (xrange-builtin) xrange built-in referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    28
from six.moves import xrange
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    29
1176
f4b434672204 Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents: 1133
diff changeset
    30
from graphics.GraphicCommons import *
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    31
from plcopen.structures import *
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    32
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
    33
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
    34
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    35
#                         Function Block Diagram Block
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
    36
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    37
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    38
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    39
def TestConnectorName(name, block_type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    40
    return name in ["OUT", "MN", "MX"] or name.startswith("IN") and (block_type, name) != ("EXPT", "IN2")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    41
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    42
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    43
class FBD_Block(Graphic_Element):
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    44
    """
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    45
    Class that implements the graphic representation of a function block
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    46
    """
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
    47
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    48
    # Create a new block
1852
70c1cc354a8f fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
    49
    def __init__(self, parent, type, name, id=None, extension=0, inputs=None, connectors=None, executionControl=False, executionOrder=0):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    50
        Graphic_Element.__init__(self, parent)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    51
        self.Type = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    52
        self.Description = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    53
        self.Extension = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    54
        self.ExecutionControl = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    55
        self.Id = id
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    56
        self.SetName(name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    57
        self.SetExecutionOrder(executionOrder)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    58
        self.Inputs = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    59
        self.Outputs = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    60
        self.Colour = wx.BLACK
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    61
        self.Pen = MiterPen(wx.BLACK)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    62
        self.SetType(type, extension, inputs, connectors, executionControl)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    63
        self.Highlights = {}
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
    64
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    65
    # Make a clone of this FBD_Block
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
    66
    def Clone(self, parent, id=None, name="", pos=None):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    67
        if self.Name != "" and name == "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    68
            name = self.Name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    69
        block = FBD_Block(parent, self.Type, name, id, self.Extension)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    70
        block.SetSize(self.Size[0], self.Size[1])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    71
        if pos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    72
            block.SetPosition(pos.x, pos.y)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    73
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    74
            block.SetPosition(self.Pos.x, self.Pos.y)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    75
        block.Inputs = [input.Clone(block) for input in self.Inputs]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    76
        block.Outputs = [output.Clone(block) for output in self.Outputs]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    77
        return block
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
    78
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    79
    def GetConnectorTranslation(self, element):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    80
        return dict(zip(self.Inputs + self.Outputs, element.Inputs + element.Outputs))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
    81
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    82
    def Flush(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    83
        for input in self.Inputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    84
            input.Flush()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    85
        self.Inputs = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    86
        for output in self.Outputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    87
            output.Flush()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    88
        self.Outputs = []
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
    89
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    90
    # Returns the RedrawRect
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
    91
    def GetRedrawRect(self, movex=0, movey=0):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    92
        rect = Graphic_Element.GetRedrawRect(self, movex, movey)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    93
        if movex != 0 or movey != 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    94
            for input in self.Inputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    95
                if input.IsConnected():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    96
                    rect = rect.Union(input.GetConnectedRedrawRect(movex, movey))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    97
            for output in self.Outputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    98
                if output.IsConnected():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    99
                    rect = rect.Union(output.GetConnectedRedrawRect(movex, movey))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   100
        return rect
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   101
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   102
    # Delete this block by calling the appropriate method
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   103
    def Delete(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   104
        self.Parent.DeleteBlock(self)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   105
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   106
    # Unconnect all inputs and outputs
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   107
    def Clean(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   108
        for input in self.Inputs:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   109
            input.UnConnect(delete=True)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   110
        for output in self.Outputs:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   111
            output.UnConnect(delete=True)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   112
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   113
    # Refresh the size of text for name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   114
    def RefreshNameSize(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   115
        self.NameSize = self.Parent.GetTextExtent(self.Name)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   116
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   117
    # Refresh the size of text for execution order
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   118
    def RefreshExecutionOrderSize(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   119
        self.ExecutionOrderSize = self.Parent.GetTextExtent(str(self.ExecutionOrder))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   120
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   121
    # Returns if the point given is in the bounding box
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   122
    def HitTest(self, pt, connectors=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   123
        if self.Name != "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   124
            test_text = self.GetTextBoundingBox().InsideXY(pt.x, pt.y)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   125
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   126
            test_text = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   127
        test_block = self.GetBlockBoundingBox(connectors).InsideXY(pt.x, pt.y)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   128
        return test_text or test_block
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   129
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   130
    # Returns the bounding box of the name outside the block
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   131
    def GetTextBoundingBox(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   132
        # Calculate the size of the name outside the block
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   133
        text_width, text_height = self.NameSize
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   134
        return wx.Rect(self.Pos.x + (self.Size[0] - text_width) / 2,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   135
                       self.Pos.y - (text_height + 2),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   136
                       text_width,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   137
                       text_height)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   138
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   139
    # Returns the bounding box of function block without name outside
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   140
    def GetBlockBoundingBox(self, connectors=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   141
        bbx_x, bbx_y = self.Pos.x, self.Pos.y
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   142
        bbx_width, bbx_height = self.Size
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   143
        if connectors:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   144
            bbx_x -= min(1, len(self.Inputs)) * CONNECTOR_SIZE
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   145
            bbx_width += (min(1, len(self.Inputs)) + min(1, len(self.Outputs))) * CONNECTOR_SIZE
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   146
        if self.ExecutionOrder != 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   147
            bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   148
            bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   149
            bbx_height = bbx_height + (self.ExecutionOrderSize[1] + 2)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   150
        return wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   151
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   152
    # Refresh the block bounding box
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   153
    def RefreshBoundingBox(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   154
        self.BoundingBox = self.GetBlockBoundingBox()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   155
        if self.Name != "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   156
            self.BoundingBox.Union(self.GetTextBoundingBox())
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   157
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   158
    # Refresh the positions of the block connectors
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   159
    def RefreshConnectors(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   160
        scaling = self.Parent.GetScaling()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   161
        # Calculate the size for the connector lines
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   162
        lines = max(len(self.Inputs), len(self.Outputs))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   163
        if lines > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   164
            linesize = max((self.Size[1] - BLOCK_LINE_SIZE) / lines, BLOCK_LINE_SIZE)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   165
            # Update inputs and outputs positions
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   166
            position = BLOCK_LINE_SIZE + linesize / 2
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   167
            for i in xrange(lines):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   168
                if scaling is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   169
                    ypos = round_scaling(self.Pos.y + position, scaling[1]) - self.Pos.y
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   170
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   171
                    ypos = position
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   172
                if i < len(self.Inputs):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   173
                    self.Inputs[i].SetPosition(wx.Point(0, ypos))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   174
                if i < len(self.Outputs):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   175
                    self.Outputs[i].SetPosition(wx.Point(self.Size[0], ypos))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   176
                position += linesize
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   177
        self.RefreshConnected()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   178
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   179
    # Refresh the positions of wires connected to inputs and outputs
1852
70c1cc354a8f fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
   180
    def RefreshConnected(self, exclude=None):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   181
        for input in self.Inputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   182
            input.MoveConnected(exclude)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   183
        for output in self.Outputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   184
            output.MoveConnected(exclude)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   185
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   186
    # Returns the block connector that starts with the point given if it exists
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   187
    def GetConnector(self, position, output_name=None, input_name=None):
1130
f96e0254f0ce Fixed loading of Blocks in Viewer when block interface have changed
Laurent Bessard
parents: 1054
diff changeset
   188
        if input_name is not None:
f96e0254f0ce Fixed loading of Blocks in Viewer when block interface have changed
Laurent Bessard
parents: 1054
diff changeset
   189
            # Test each input connector
f96e0254f0ce Fixed loading of Blocks in Viewer when block interface have changed
Laurent Bessard
parents: 1054
diff changeset
   190
            for input in self.Inputs:
f96e0254f0ce Fixed loading of Blocks in Viewer when block interface have changed
Laurent Bessard
parents: 1054
diff changeset
   191
                if input_name == input.GetName():
f96e0254f0ce Fixed loading of Blocks in Viewer when block interface have changed
Laurent Bessard
parents: 1054
diff changeset
   192
                    return input
f96e0254f0ce Fixed loading of Blocks in Viewer when block interface have changed
Laurent Bessard
parents: 1054
diff changeset
   193
        if output_name is not None:
f96e0254f0ce Fixed loading of Blocks in Viewer when block interface have changed
Laurent Bessard
parents: 1054
diff changeset
   194
            # Test each output connector
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   195
            for output in self.Outputs:
1130
f96e0254f0ce Fixed loading of Blocks in Viewer when block interface have changed
Laurent Bessard
parents: 1054
diff changeset
   196
                if output_name == output.GetName():
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   197
                    return output
1133
d81d99fd1932 Fixed bug in loading block in Viewer when block interface has changed
Laurent Bessard
parents: 1130
diff changeset
   198
        if input_name is None and output_name is None:
d81d99fd1932 Fixed bug in loading block in Viewer when block interface has changed
Laurent Bessard
parents: 1130
diff changeset
   199
            return self.FindNearestConnector(position, self.Inputs + self.Outputs)
d81d99fd1932 Fixed bug in loading block in Viewer when block interface has changed
Laurent Bessard
parents: 1130
diff changeset
   200
        return None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   201
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   202
    def GetInputTypes(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   203
        return tuple([input.GetType(True) for input in self.Inputs if input.GetName() != "EN"])
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   204
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   205
    def SetOutputValues(self, values):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   206
        for output in self.Outputs:
1872
866fb3ab8778 fix pylint error "(undefined-variable) Undefined variable 'X'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1852
diff changeset
   207
            output.SetValue(values.get(output.getName(), None))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   208
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   209
    def GetConnectionResultType(self, connector, connectortype):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   210
        if not TestConnectorName(connector.GetName(), self.Type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   211
            return connectortype
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   212
        resulttype = connectortype
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   213
        for input in self.Inputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   214
            if input != connector and input.GetType(True) == "ANY" and TestConnectorName(input.GetName(), self.Type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   215
                inputtype = input.GetConnectedType()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   216
                if resulttype is None or inputtype is not None and self.IsOfType(inputtype, resulttype):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   217
                    resulttype = inputtype
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   218
        for output in self.Outputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   219
            if output != connector and output.GetType(True) == "ANY" and TestConnectorName(output.GetName(), self.Type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   220
                outputtype = output.GetConnectedType()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   221
                if resulttype is None or outputtype is not None and self.IsOfType(outputtype, resulttype):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   222
                    resulttype = outputtype
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   223
        return resulttype
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   224
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   225
    # Returns all the block connectors
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   226
    def GetConnectors(self):
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   227
        return {"inputs": self.Inputs, "outputs": self.Outputs}
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   228
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   229
    # Test if point given is on one of the block connectors
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   230
    def TestConnector(self, pt, direction=None, exclude=True):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   231
        # Test each input connector
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   232
        for input in self.Inputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   233
            if input.TestPoint(pt, direction, exclude):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   234
                return input
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   235
        # Test each output connector
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   236
        for output in self.Outputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   237
            if output.TestPoint(pt, direction, exclude):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   238
                return output
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   239
        return None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   240
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   241
    # Changes the block type
1852
70c1cc354a8f fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
   242
    def SetType(self, type, extension, inputs=None, connectors=None, executionControl=False):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   243
        if type != self.Type or self.Extension != extension or executionControl != self.ExecutionControl:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   244
            if type != self.Type:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   245
                self.Type = type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   246
                self.TypeSize = self.Parent.GetTextExtent(self.Type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   247
            self.Extension = extension
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   248
            self.ExecutionControl = executionControl
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   249
            # Find the block definition from type given and create the corresponding
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   250
            # inputs and outputs
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   251
            blocktype = self.Parent.GetBlockType(type, inputs)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   252
            if blocktype:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   253
                self.Colour = wx.BLACK
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   254
                inputs = [input for input in blocktype["inputs"]]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   255
                outputs = [output for output in blocktype["outputs"]]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   256
                if blocktype["extensible"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   257
                    start = int(inputs[-1][0].replace("IN", ""))
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
   258
                    for dummy in xrange(self.Extension - len(blocktype["inputs"])):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   259
                        start += 1
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   260
                        inputs.append(("IN%d" % start, inputs[-1][1], inputs[-1][2]))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   261
                comment = blocktype["comment"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   262
                self.Description = _(comment) + blocktype.get("usage", "")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   263
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   264
                self.Colour = wx.RED
1852
70c1cc354a8f fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
   265
                connectors = {} if connectors is None else connectors
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   266
                inputs = connectors.get("inputs", [])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   267
                outputs = connectors.get("outputs", [])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   268
                self.Description = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   269
            if self.ExecutionControl:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   270
                inputs.insert(0,  ("EN",   "BOOL", "none"))
1761
8c98bad90b8d clean-up: fix PEP8 E203 whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1753
diff changeset
   271
                outputs.insert(0, ("ENO",  "BOOL", "none"))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   272
            self.Pen = MiterPen(self.Colour)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   273
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   274
            # Extract the inputs properties and create or modify the corresponding connector
1054
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   275
            input_connectors = []
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   276
            for input_name, input_type, input_modifier in inputs:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   277
                connector = Connector(self, input_name, input_type, wx.Point(0, 0), WEST, onlyone=True)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   278
                if input_modifier == "negated":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   279
                    connector.SetNegated(True)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   280
                elif input_modifier != "none":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   281
                    connector.SetEdge(input_modifier)
1054
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   282
                for input in self.Inputs:
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   283
                    if input.GetName() == input_name:
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   284
                        wires = input.GetWires()[:]
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   285
                        input.UnConnect()
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   286
                        for wire in wires:
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   287
                            connector.Connect(wire)
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   288
                        break
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   289
                input_connectors.append(connector)
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   290
            for input in self.Inputs:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   291
                input.UnConnect(delete=True)
1054
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   292
            self.Inputs = input_connectors
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   293
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   294
            # Extract the outputs properties and create or modify the corresponding connector
1054
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   295
            output_connectors = []
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   296
            for output_name, output_type, output_modifier in outputs:
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   297
                connector = Connector(self, output_name, output_type, wx.Point(0, 0), EAST)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   298
                if output_modifier == "negated":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   299
                    connector.SetNegated(True)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   300
                elif output_modifier != "none":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   301
                    connector.SetEdge(output_modifier)
1054
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   302
                for output in self.Outputs:
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   303
                    if output.GetName() == output_name:
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   304
                        wires = output.GetWires()[:]
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   305
                        output.UnConnect()
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   306
                        for wire in wires:
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   307
                            connector.Connect(wire)
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   308
                        break
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   309
                output_connectors.append(connector)
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   310
            for output in self.Outputs:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   311
                output.UnConnect(delete=True)
1054
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   312
            self.Outputs = output_connectors
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   313
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   314
            self.RefreshMinSize()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   315
            self.RefreshConnectors()
1054
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   316
            for output in self.Outputs:
ef514eaacd8c Fixed connections of block when changing block type
Laurent Bessard
parents: 873
diff changeset
   317
                output.RefreshWires()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   318
            self.RefreshBoundingBox()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   319
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   320
    # Returns the block type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   321
    def GetType(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   322
        return self.Type
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   323
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   324
    # Changes the block name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   325
    def SetName(self, name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   326
        self.Name = name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   327
        self.RefreshNameSize()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   328
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   329
    # Returs the block name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   330
    def GetName(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   331
        return self.Name
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   332
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   333
    # Changes the extension name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   334
    def SetExtension(self, extension):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   335
        self.Extension = extension
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   336
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   337
    # Returs the extension name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   338
    def GetExtension(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   339
        return self.Extension
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   340
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   341
    # Changes the execution order
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   342
    def SetExecutionOrder(self, executionOrder):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   343
        self.ExecutionOrder = executionOrder
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   344
        self.RefreshExecutionOrderSize()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   345
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   346
    # Returs the execution order
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   347
    def GetExecutionOrder(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   348
        return self.ExecutionOrder
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   349
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   350
    # Returs the execution order
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   351
    def GetExecutionControl(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   352
        return self.ExecutionControl
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   353
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   354
    # Refresh the block minimum size
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   355
    def RefreshMinSize(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   356
        # Calculate the inputs maximum width
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   357
        max_input = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   358
        for input in self.Inputs:
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
   359
            w, _h = input.GetNameSize()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   360
            max_input = max(max_input, w)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   361
        # Calculate the outputs maximum width
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   362
        max_output = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   363
        for output in self.Outputs:
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
   364
            w, _h = output.GetNameSize()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   365
            max_output = max(max_output, w)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   366
        width = max(self.TypeSize[0] + 10, max_input + max_output + 15)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   367
        height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   368
        self.MinSize = width, height
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   369
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   370
    # Returns the block minimum size
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   371
    def GetMinSize(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   372
        return self.MinSize
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   373
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   374
    # Changes the negated property of the connector handled
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   375
    def SetConnectorNegated(self, negated):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   376
        handle_type, handle = self.Handle
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   377
        if handle_type == HANDLE_CONNECTOR:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   378
            handle.SetNegated(negated)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   379
            self.RefreshModel(False)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   380
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   381
    # Changes the edge property of the connector handled
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   382
    def SetConnectorEdge(self, edge):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   383
        handle_type, handle = self.Handle
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   384
        if handle_type == HANDLE_CONNECTOR:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   385
            handle.SetEdge(edge)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   386
            self.RefreshModel(False)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   387
1753
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   388
#    # Method called when a Motion event have been generated
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   389
#    def OnMotion(self, event, dc, scaling):
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   390
#        if not event.Dragging():
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   391
#            pos = event.GetLogicalPosition(dc)
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   392
#            for input in self.Inputs:
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   393
#                rect = input.GetRedrawRect()
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   394
#                if rect.InsideXY(pos.x, pos.y):
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   395
#                    print "Find input"
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   396
#                    tip = wx.TipWindow(self.Parent, "Test")
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   397
#                    tip.SetBoundingRect(rect)
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   398
#        return Graphic_Element.OnMotion(self, event, dc, scaling)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   399
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   400
    # Method called when a LeftDClick event have been generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   401
    def OnLeftDClick(self, event, dc, scaling):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   402
        # Edit the block properties
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   403
        self.Parent.EditBlockContent(self)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   404
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   405
    # Method called when a RightUp event have been generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   406
    def OnRightUp(self, event, dc, scaling):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   407
        pos = GetScaledEventPosition(event, dc, scaling)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   408
        # Popup the menu with special items for a block and a connector if one is handled
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   409
        connector = self.TestConnector(pos, exclude=False)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   410
        if connector:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   411
            self.Handle = (HANDLE_CONNECTOR, connector)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   412
            self.Parent.PopupBlockMenu(connector)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   413
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   414
            self.Parent.PopupBlockMenu()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   415
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   416
    # Refreshes the block model
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   417
    def RefreshModel(self, move=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   418
        self.Parent.RefreshBlockModel(self)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   419
        # If block has moved, refresh the model of wires connected to outputs
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   420
        if move:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   421
            for output in self.Outputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   422
                output.RefreshWires()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   423
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   424
    def GetToolTipValue(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   425
        return self.Description
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   426
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   427
    # Adds an highlight to the block
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   428
    def AddHighlight(self, infos, start, end, highlight_type):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   429
        if infos[0] in ["type", "name"] and start[0] == 0 and end[0] == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   430
            highlights = self.Highlights.setdefault(infos[0], [])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   431
            AddHighlight(highlights, (start, end, highlight_type))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   432
        elif infos[0] == "input" and infos[1] < len(self.Inputs):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   433
            self.Inputs[infos[1]].AddHighlight(infos[2:], start, end, highlight_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   434
        elif infos[0] == "output" and infos[1] < len(self.Outputs):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   435
            self.Outputs[infos[1]].AddHighlight(infos[2:], start, end, highlight_type)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   436
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   437
    # Removes an highlight from the block
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   438
    def RemoveHighlight(self, infos, start, end, highlight_type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   439
        if infos[0] in ["type", "name"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   440
            highlights = self.Highlights.get(infos[0], [])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   441
            if RemoveHighlight(highlights, (start, end, highlight_type)) and len(highlights) == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   442
                self.Highlights.pop(infos[0])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   443
        elif infos[0] == "input" and infos[1] < len(self.Inputs):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   444
            self.Inputs[infos[1]].RemoveHighlight(infos[2:], start, end, highlight_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   445
        elif infos[0] == "output" and infos[1] < len(self.Outputs):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   446
            self.Outputs[infos[1]].RemoveHighlight(infos[2:], start, end, highlight_type)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   447
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   448
    # Removes all the highlights of one particular type from the block
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   449
    def ClearHighlight(self, highlight_type=None):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   450
        if highlight_type is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   451
            self.Highlights = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   452
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   453
            highlight_items = self.Highlights.items()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   454
            for name, highlights in highlight_items:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   455
                highlights = ClearHighlights(highlights, highlight_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   456
                if len(highlights) == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   457
                    self.Highlights.pop(name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   458
        for input in self.Inputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   459
            input.ClearHighlights(highlight_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   460
        for output in self.Outputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   461
            output.ClearHighlights(highlight_type)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   462
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   463
    # Draws block
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   464
    def Draw(self, dc):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   465
        Graphic_Element.Draw(self, dc)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   466
        dc.SetPen(self.Pen)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   467
        dc.SetBrush(wx.WHITE_BRUSH)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   468
        dc.SetTextForeground(self.Colour)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   469
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   470
        if getattr(dc, "printing", False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   471
            name_size = dc.GetTextExtent(self.Name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   472
            type_size = dc.GetTextExtent(self.Type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   473
            executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   474
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   475
            name_size = self.NameSize
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   476
            type_size = self.TypeSize
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   477
            executionorder_size = self.ExecutionOrderSize
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   478
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   479
        # Draw a rectangle with the block size
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   480
        dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   481
        # Draw block name and block type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   482
        name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   483
                    self.Pos.y - (name_size[1] + 2))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   484
        type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   485
                    self.Pos.y + 5)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   486
        dc.DrawText(self.Name, name_pos[0], name_pos[1])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   487
        dc.DrawText(self.Type, type_pos[0], type_pos[1])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   488
        # Draw inputs and outputs connectors
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   489
        for input in self.Inputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   490
            input.Draw(dc)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   491
        for output in self.Outputs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   492
            output.Draw(dc)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   493
        if self.ExecutionOrder != 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   494
            # Draw block execution order
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   495
            dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0],
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1761
diff changeset
   496
                        self.Pos.y + self.Size[1] + 2)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   497
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   498
        if not getattr(dc, "printing", False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   499
            DrawHighlightedText(dc, self.Name, self.Highlights.get("name", []), name_pos[0], name_pos[1])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   500
            DrawHighlightedText(dc, self.Type, self.Highlights.get("type", []), type_pos[0], type_pos[1])
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   501
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   502
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
   503
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   504
#                        Function Block Diagram Variable
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
   505
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   506
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   507
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   508
class FBD_Variable(Graphic_Element):
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   509
    """
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   510
    Class that implements the graphic representation of a variable
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   511
    """
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   512
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   513
    # Create a new variable
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   514
    def __init__(self, parent, type, name, value_type, id=None, executionOrder=0):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   515
        Graphic_Element.__init__(self, parent)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   516
        self.Type = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   517
        self.ValueType = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   518
        self.Id = id
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   519
        self.SetName(name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   520
        self.SetExecutionOrder(executionOrder)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   521
        self.Input = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   522
        self.Output = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   523
        self.SetType(type, value_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   524
        self.Highlights = []
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   525
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   526
    # Make a clone of this FBD_Variable
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   527
    def Clone(self, parent, id=None, pos=None):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   528
        variable = FBD_Variable(parent, self.Type, self.Name, self.ValueType, id)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   529
        variable.SetSize(self.Size[0], self.Size[1])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   530
        if pos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   531
            variable.SetPosition(pos.x, pos.y)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   532
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   533
            variable.SetPosition(self.Pos.x, self.Pos.y)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   534
        if self.Input:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   535
            variable.Input = self.Input.Clone(variable)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   536
        if self.Output:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   537
            variable.Output = self.Output.Clone(variable)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   538
        return variable
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   539
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   540
    def GetConnectorTranslation(self, element):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   541
        connectors = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   542
        if self.Input is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   543
            connectors[self.Input] = element.Input
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   544
        if self.Output is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   545
            connectors[self.Output] = element.Output
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   546
        return connectors
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   547
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   548
    def Flush(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   549
        if self.Input is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   550
            self.Input.Flush()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   551
            self.Input = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   552
        if self.Output is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   553
            self.Output.Flush()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   554
            self.Output = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   555
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   556
    # Returns the RedrawRect
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   557
    def GetRedrawRect(self, movex=0, movey=0):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   558
        rect = Graphic_Element.GetRedrawRect(self, movex, movey)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   559
        if movex != 0 or movey != 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   560
            if self.Input and self.Input.IsConnected():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   561
                rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   562
            if self.Output and self.Output.IsConnected():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   563
                rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   564
        return rect
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   565
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   566
    # Unconnect connector
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   567
    def Clean(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   568
        if self.Input:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   569
            self.Input.UnConnect(delete=True)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   570
        if self.Output:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   571
            self.Output.UnConnect(delete=True)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   572
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   573
    # Delete this variable by calling the appropriate method
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   574
    def Delete(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   575
        self.Parent.DeleteVariable(self)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   576
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   577
    # Refresh the size of text for name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   578
    def RefreshNameSize(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   579
        self.NameSize = self.Parent.GetTextExtent(self.Name)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   580
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   581
    # Refresh the size of text for execution order
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   582
    def RefreshExecutionOrderSize(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   583
        self.ExecutionOrderSize = self.Parent.GetTextExtent(str(self.ExecutionOrder))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   584
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   585
    # Refresh the variable bounding box
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   586
    def RefreshBoundingBox(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   587
        if self.Type in (OUTPUT, INOUT):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   588
            bbx_x = self.Pos.x - CONNECTOR_SIZE
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   589
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   590
            bbx_x = self.Pos.x
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   591
        if self.Type == INOUT:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   592
            bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   593
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   594
            bbx_width = self.Size[0] + CONNECTOR_SIZE
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   595
        bbx_x = min(bbx_x, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   596
        bbx_width = max(bbx_width, self.NameSize[0])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   597
        bbx_height = self.Size[1]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   598
        if self.ExecutionOrder != 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   599
            bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   600
            bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   601
            bbx_height = bbx_height + (self.ExecutionOrderSize[1] + 2)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   602
        self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width + 1, bbx_height + 1)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   603
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   604
    # Refresh the position of the variable connector
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   605
    def RefreshConnectors(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   606
        scaling = self.Parent.GetScaling()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   607
        if scaling is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   608
            position = round_scaling(self.Pos.y + self.Size[1] / 2, scaling[1]) - self.Pos.y
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   609
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   610
            position = self.Size[1] / 2
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   611
        if self.Input:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   612
            self.Input.SetPosition(wx.Point(0, position))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   613
        if self.Output:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   614
            self.Output.SetPosition(wx.Point(self.Size[0], position))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   615
        self.RefreshConnected()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   616
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   617
    # Refresh the position of wires connected to connector
1852
70c1cc354a8f fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
   618
    def RefreshConnected(self, exclude=None):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   619
        if self.Input:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   620
            self.Input.MoveConnected(exclude)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   621
        if self.Output:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   622
            self.Output.MoveConnected(exclude)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   623
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   624
    # Test if point given is on the variable connector
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   625
    def TestConnector(self, pt, direction=None, exclude=True):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   626
        if self.Input and self.Input.TestPoint(pt, direction, exclude):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   627
            return self.Input
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   628
        if self.Output and self.Output.TestPoint(pt, direction, exclude):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   629
            return self.Output
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   630
        return None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   631
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   632
    # Returns the block connector that starts with the point given if it exists
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   633
    def GetConnector(self, position, name=None):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   634
        # if a name is given
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   635
        if name is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   636
            # Test input and output connector if they exists
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
   637
            # if self.Input and name == self.Input.GetName():
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   638
            #    return self.Input
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   639
            if self.Output and name == self.Output.GetName():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   640
                return self.Output
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   641
        connectors = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   642
        # Test input connector if it exists
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   643
        if self.Input:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   644
            connectors.append(self.Input)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   645
        # Test output connector if it exists
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   646
        if self.Output:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   647
            connectors.append(self.Output)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   648
        return self.FindNearestConnector(position, connectors)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   649
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   650
    # Returns all the block connectors
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   651
    def GetConnectors(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   652
        connectors = {"inputs": [], "outputs": []}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   653
        if self.Input:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   654
            connectors["inputs"].append(self.Input)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   655
        if self.Output:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   656
            connectors["outputs"].append(self.Output)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   657
        return connectors
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   658
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   659
    # Changes the negated property of the variable connector if handled
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   660
    def SetConnectorNegated(self, negated):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   661
        handle_type, handle = self.Handle
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   662
        if handle_type == HANDLE_CONNECTOR:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   663
            handle.SetNegated(negated)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   664
            self.RefreshModel(False)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   665
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   666
    # Changes the variable type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   667
    def SetType(self, type, value_type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   668
        if type != self.Type:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   669
            self.Type = type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   670
            # Create an input or output connector according to variable type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   671
            if self.Type != INPUT:
857
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   672
                if self.Input is None:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   673
                    self.Input = Connector(self, "", value_type, wx.Point(0, 0), WEST, onlyone=True)
857
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   674
            elif self.Input:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   675
                self.Input.UnConnect(delete=True)
857
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   676
                self.Input = None
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   677
            if self.Type != OUTPUT:
857
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   678
                if self.Output is None:
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   679
                    self.Output = Connector(self, "", value_type, wx.Point(0, 0), EAST)
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   680
            elif self.Output:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   681
                self.Output.UnConnect(delete=True)
857
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   682
                self.Output = None
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   683
            self.RefreshConnectors()
873
8737f1554494 Fix bug Variable and Continuation BoundingBox not refreshed when using Type modification shortcuts
Laurent Bessard
parents: 857
diff changeset
   684
            self.RefreshBoundingBox()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   685
        elif value_type != self.ValueType:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   686
            if self.Input:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   687
                self.Input.SetType(value_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   688
            if self.Output:
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   689
                self.Output.SetType(value_type)
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   690
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   691
    # Returns the variable type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   692
    def GetType(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   693
        return self.Type
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   694
857
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   695
    # Returns the variable value type
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   696
    def GetValueType(self):
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   697
        return self.ValueType
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   698
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   699
    # Changes the variable name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   700
    def SetName(self, name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   701
        self.Name = name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   702
        self.RefreshNameSize()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   703
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   704
    # Returns the variable name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   705
    def GetName(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   706
        return self.Name
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   707
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   708
    # Changes the execution order
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   709
    def SetExecutionOrder(self, executionOrder):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   710
        self.ExecutionOrder = executionOrder
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   711
        self.RefreshExecutionOrderSize()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   712
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   713
    # Returs the execution order
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   714
    def GetExecutionOrder(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   715
        return self.ExecutionOrder
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   716
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   717
    # Returns the variable minimum size
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   718
    def GetMinSize(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   719
        return self.NameSize[0] + 10, self.NameSize[1] + 10
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   720
852
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   721
    # Set size of the variable to the minimum size
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   722
    def SetBestSize(self, scaling):
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   723
        if self.Type == INPUT:
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   724
            return Graphic_Element.SetBestSize(self, scaling, x_factor=1.)
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   725
        elif self.Type == OUTPUT:
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   726
            return Graphic_Element.SetBestSize(self, scaling, x_factor=0.)
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   727
        else:
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   728
            return Graphic_Element.SetBestSize(self, scaling)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   729
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   730
    # Method called when a LeftDClick event have been generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   731
    def OnLeftDClick(self, event, dc, scaling):
857
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   732
        if event.ControlDown():
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   733
            # Change variable type
857
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   734
            types = [INPUT, OUTPUT, INOUT]
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1761
diff changeset
   735
            self.Parent.ChangeVariableType(
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1761
diff changeset
   736
                self, types[(types.index(self.Type) + 1) % len(types)])
857
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   737
        else:
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   738
            # Edit the variable properties
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   739
            self.Parent.EditVariableContent(self)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   740
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   741
    # Method called when a RightUp event have been generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   742
    def OnRightUp(self, event, dc, scaling):
857
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   743
        self.Parent.PopupVariableMenu()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   744
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   745
    # Refreshes the variable model
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   746
    def RefreshModel(self, move=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   747
        self.Parent.RefreshVariableModel(self)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   748
        # If variable has moved and variable is not of type OUTPUT, refresh the model
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   749
        # of wires connected to output connector
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   750
        if move and self.Type != OUTPUT:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   751
            if self.Output:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   752
                self.Output.RefreshWires()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   753
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   754
    # Adds an highlight to the variable
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   755
    def AddHighlight(self, infos, start, end, highlight_type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   756
        if infos[0] == "expression" and start[0] == 0 and end[0] == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   757
            AddHighlight(self.Highlights, (start, end, highlight_type))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   758
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   759
    # Removes an highlight from the variable
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   760
    def RemoveHighlight(self, infos, start, end, highlight_type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   761
        if infos[0] == "expression":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   762
            RemoveHighlight(self.Highlights, (start, end, highlight_type))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   763
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   764
    # Removes all the highlights of one particular type from the variable
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   765
    def ClearHighlight(self, highlight_type=None):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   766
        ClearHighlights(self.Highlights, highlight_type)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   767
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   768
    # Draws variable
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   769
    def Draw(self, dc):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   770
        Graphic_Element.Draw(self, dc)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   771
        dc.SetPen(MiterPen(wx.BLACK))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   772
        dc.SetBrush(wx.WHITE_BRUSH)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   773
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   774
        if getattr(dc, "printing", False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   775
            name_size = dc.GetTextExtent(self.Name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   776
            executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   777
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   778
            name_size = self.NameSize
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   779
            executionorder_size = self.ExecutionOrderSize
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   780
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   781
        text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   782
                    self.Pos.y + (self.Size[1] - name_size[1]) / 2)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   783
        # Draw a rectangle with the variable size
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   784
        dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   785
        # Draw variable name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   786
        dc.DrawText(self.Name, text_pos[0], text_pos[1])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   787
        # Draw connectors
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   788
        if self.Input:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   789
            self.Input.Draw(dc)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   790
        if self.Output:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   791
            self.Output.Draw(dc)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   792
        if self.ExecutionOrder != 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   793
            # Draw variable execution order
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   794
            dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0],
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1761
diff changeset
   795
                        self.Pos.y + self.Size[1] + 2)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   796
        if not getattr(dc, "printing", False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   797
            DrawHighlightedText(dc, self.Name, self.Highlights, text_pos[0], text_pos[1])
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   798
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
   799
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
   800
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   801
#                        Function Block Diagram Connector
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
   802
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   803
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   804
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   805
class FBD_Connector(Graphic_Element):
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   806
    """
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   807
    Class that implements the graphic representation of a connection
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   808
    """
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   809
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   810
    # Create a new connection
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   811
    def __init__(self, parent, type, name, id=None):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   812
        Graphic_Element.__init__(self, parent)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   813
        self.Type = type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   814
        self.Id = id
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   815
        self.SetName(name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   816
        self.Pos = wx.Point(0, 0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   817
        self.Size = wx.Size(0, 0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   818
        self.Highlights = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   819
        # Create an input or output connector according to connection type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   820
        if self.Type == CONNECTOR:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   821
            self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone=True)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   822
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   823
            self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   824
        self.RefreshConnectors()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   825
        self.RefreshNameSize()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   826
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   827
    def Flush(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   828
        if self.Connector:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   829
            self.Connector.Flush()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   830
            self.Connector = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   831
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   832
    # Returns the RedrawRect
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   833
    def GetRedrawRect(self, movex=0, movey=0):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   834
        rect = Graphic_Element.GetRedrawRect(self, movex, movey)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   835
        if movex != 0 or movey != 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   836
            if self.Connector and self.Connector.IsConnected():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   837
                rect = rect.Union(self.Connector.GetConnectedRedrawRect(movex, movey))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   838
        return rect
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   839
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   840
    # Make a clone of this FBD_Connector
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   841
    def Clone(self, parent, id=None, pos=None):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   842
        connection = FBD_Connector(parent, self.Type, self.Name, id)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   843
        connection.SetSize(self.Size[0], self.Size[1])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   844
        if pos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   845
            connection.SetPosition(pos.x, pos.y)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   846
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   847
            connection.SetPosition(self.Pos.x, self.Pos.y)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   848
        connection.Connector = self.Connector.Clone(connection)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   849
        return connection
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   850
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   851
    def GetConnectorTranslation(self, element):
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   852
        return {self.Connector: element.Connector}
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   853
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   854
    # Unconnect connector
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   855
    def Clean(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   856
        if self.Connector:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   857
            self.Connector.UnConnect(delete=True)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   858
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   859
    # Delete this connection by calling the appropriate method
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   860
    def Delete(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   861
        self.Parent.DeleteConnection(self)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   862
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   863
    # Refresh the size of text for name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   864
    def RefreshNameSize(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   865
        self.NameSize = self.Parent.GetTextExtent(self.Name)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   866
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   867
    # Refresh the connection bounding box
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   868
    def RefreshBoundingBox(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   869
        if self.Type == CONNECTOR:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   870
            bbx_x = self.Pos.x - CONNECTOR_SIZE
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   871
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   872
            bbx_x = self.Pos.x
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   873
        bbx_width = self.Size[0] + CONNECTOR_SIZE
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   874
        self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width, self.Size[1])
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   875
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   876
    # Refresh the position of the connection connector
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   877
    def RefreshConnectors(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   878
        scaling = self.Parent.GetScaling()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   879
        if scaling is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   880
            position = round_scaling(self.Pos.y + self.Size[1] / 2, scaling[1]) - self.Pos.y
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   881
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   882
            position = self.Size[1] / 2
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   883
        if self.Type == CONNECTOR:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   884
            self.Connector.SetPosition(wx.Point(0, position))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   885
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   886
            self.Connector.SetPosition(wx.Point(self.Size[0], position))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   887
        self.RefreshConnected()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   888
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   889
    # Refresh the position of wires connected to connector
1852
70c1cc354a8f fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
   890
    def RefreshConnected(self, exclude=None):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   891
        if self.Connector:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   892
            self.Connector.MoveConnected(exclude)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   893
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   894
    # Test if point given is on the connection connector
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   895
    def TestConnector(self, pt, direction=None, exclude=True):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   896
        if self.Connector and self.Connector.TestPoint(pt, direction, exclude):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   897
            return self.Connector
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   898
        return None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   899
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   900
    # Returns the connection connector
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   901
    def GetConnector(self, position=None, name=None):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   902
        return self.Connector
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   903
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   904
        # Returns all the block connectors
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   905
    def GetConnectors(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   906
        connectors = {"inputs": [], "outputs": []}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   907
        if self.Type == CONNECTOR:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   908
            connectors["inputs"].append(self.Connector)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   909
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   910
            connectors["outputs"].append(self.Connector)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   911
        return connectors
1605
0b6b60241230 fix issue that in Debug mode LD instance doesn't show correctly its state after
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   912
0b6b60241230 fix issue that in Debug mode LD instance doesn't show correctly its state after
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   913
    def SpreadCurrent(self):
0b6b60241230 fix issue that in Debug mode LD instance doesn't show correctly its state after
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   914
        if self.Type == CONNECTOR:
0b6b60241230 fix issue that in Debug mode LD instance doesn't show correctly its state after
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   915
            continuations = self.Parent.GetContinuationByName(self.Name)
0b6b60241230 fix issue that in Debug mode LD instance doesn't show correctly its state after
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   916
            if continuations is not None:
0b6b60241230 fix issue that in Debug mode LD instance doesn't show correctly its state after
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   917
                value = self.Connector.ReceivingCurrent()
0b6b60241230 fix issue that in Debug mode LD instance doesn't show correctly its state after
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   918
                for cont in continuations:
0b6b60241230 fix issue that in Debug mode LD instance doesn't show correctly its state after
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   919
                    cont.Connector.SpreadCurrent(value)
0b6b60241230 fix issue that in Debug mode LD instance doesn't show correctly its state after
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   920
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   921
    # Changes the variable type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   922
    def SetType(self, type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   923
        if type != self.Type:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   924
            self.Type = type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   925
            self.Clean()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   926
            # Create an input or output connector according to connection type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   927
            if self.Type == CONNECTOR:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   928
                self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone=True)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   929
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   930
                self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   931
            self.RefreshConnectors()
873
8737f1554494 Fix bug Variable and Continuation BoundingBox not refreshed when using Type modification shortcuts
Laurent Bessard
parents: 857
diff changeset
   932
            self.RefreshBoundingBox()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   933
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   934
    # Returns the connection type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   935
    def GetType(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   936
        return self.Type
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   937
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   938
    def GetConnectionResultType(self, connector, connectortype):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   939
        if self.Type == CONTINUATION:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   940
            connector = self.Parent.GetConnectorByName(self.Name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   941
            if connector is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   942
                return connector.Connector.GetConnectedType()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   943
        return connectortype
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   944
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   945
    # Changes the connection name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   946
    def SetName(self, name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   947
        self.Name = name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   948
        self.RefreshNameSize()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   949
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   950
    # Returns the connection name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   951
    def GetName(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   952
        return self.Name
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   953
852
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   954
    # Set size of the variable to the minimum size
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   955
    def SetBestSize(self, scaling):
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   956
        if self.Type == CONTINUATION:
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   957
            return Graphic_Element.SetBestSize(self, scaling, x_factor=1.)
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   958
        else:
1009f956d2ee Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents: 814
diff changeset
   959
            return Graphic_Element.SetBestSize(self, scaling, x_factor=0.)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   960
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   961
    # Returns the connection minimum size
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   962
    def GetMinSize(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   963
        text_width, text_height = self.NameSize
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   964
        if text_height % 2 == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   965
            text_height += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   966
        return text_width + text_height + 20, text_height + 10
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   967
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   968
    # Method called when a LeftDClick event have been generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   969
    def OnLeftDClick(self, event, dc, scaling):
857
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   970
        if event.ControlDown():
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   971
            # Change connection type
857
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   972
            if self.Type == CONNECTOR:
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   973
                self.Parent.ChangeConnectionType(self, CONTINUATION)
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   974
            else:
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   975
                self.Parent.ChangeConnectionType(self, CONNECTOR)
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   976
        else:
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   977
            # Edit the connection properties
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   978
            self.Parent.EditConnectionContent(self)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   979
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   980
    # Method called when a RightUp event have been generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   981
    def OnRightUp(self, event, dc, scaling):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   982
        # Popup the default menu
857
9695969796d0 Adding support for quickly changing variable and connection type
Laurent Bessard
parents: 852
diff changeset
   983
        self.Parent.PopupConnectionMenu()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   984
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   985
    # Refreshes the connection model
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   986
    def RefreshModel(self, move=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   987
        self.Parent.RefreshConnectionModel(self)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   988
        # If connection has moved and connection is of type CONTINUATION, refresh
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   989
        # the model of wires connected to connector
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   990
        if move and self.Type == CONTINUATION:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   991
            if self.Connector:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   992
                self.Connector.RefreshWires()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   993
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   994
    # Adds an highlight to the connection
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   995
    def AddHighlight(self, infos, start, end, highlight_type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   996
        if infos[0] == "name" and start[0] == 0 and end[0] == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   997
            AddHighlight(self.Highlights, (start, end, highlight_type))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
   998
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   999
    # Removes an highlight from the connection
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1000
    def RemoveHighlight(self, infos, start, end, highlight_type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1001
        if infos[0] == "name":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1002
            RemoveHighlight(self.Highlights, (start, end, highlight_type))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
  1003
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1004
    # Removes all the highlights of one particular type from the connection
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1005
    def ClearHighlight(self, highlight_type=None):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1006
        ClearHighlights(self.Highlights, highlight_type)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
  1007
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1008
    # Draws connection
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1009
    def Draw(self, dc):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1010
        Graphic_Element.Draw(self, dc)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1011
        dc.SetPen(MiterPen(wx.BLACK))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1012
        dc.SetBrush(wx.WHITE_BRUSH)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
  1013
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1014
        if getattr(dc, "printing", False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1015
            name_size = dc.GetTextExtent(self.Name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1016
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1017
            name_size = self.NameSize
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
  1018
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1019
        # Draw a rectangle with the connection size with arrows inside
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1020
        dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1021
        arrowsize = min(self.Size[1] / 2, (self.Size[0] - name_size[0] - 10) / 2)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
  1022
        dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1761
diff changeset
  1023
                    self.Pos.y + self.Size[1] / 2)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
  1024
        dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1761
diff changeset
  1025
                    self.Pos.x, self.Pos.y + self.Size[1])
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
  1026
        dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1761
diff changeset
  1027
                    self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
  1028
        dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1761
diff changeset
  1029
                    self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1])
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1030
        # Draw connection name
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
  1031
        text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1032
                    self.Pos.y + (self.Size[1] - name_size[1]) / 2)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1033
        dc.DrawText(self.Name, text_pos[0], text_pos[1])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1034
        # Draw connector
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1035
        if self.Connector:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1036
            self.Connector.Draw(dc)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1605
diff changeset
  1037
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1038
        if not getattr(dc, "printing", False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1039
            DrawHighlightedText(dc, self.Name, self.Highlights, text_pos[0], text_pos[1])