File: node_state.py

package info (click to toggle)
python-qtpynodeeditor 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 604 kB
  • sloc: python: 5,038; makefile: 14; sh: 1
file content (174 lines) | stat: -rw-r--r-- 4,369 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from collections import OrderedDict

from .base import ConnectionBase
from .enums import ReactToConnectionState
from .node_data import NodeDataType
from .port import Port, PortType


class NodeState:
    def __init__(self, node):
        '''
        node_state

        Parameters
        ----------
        model : NodeDataModel
        '''
        self._ports = {PortType.input: OrderedDict(),
                       PortType.output: OrderedDict()
                       }

        model = node.model
        for port_type in self._ports:
            num_ports = model.num_ports[port_type]
            self._ports[port_type] = OrderedDict(
                (i, Port(node, port_type=port_type, index=i))
                for i in range(num_ports)
            )

        self._reaction = ReactToConnectionState.not_reacting
        self._reacting_port_type = PortType.none
        self._reacting_data_type = None
        self._resizing = False

    def __getitem__(self, key):
        return self._ports[key]

    @property
    def ports(self):
        yield from self.input_ports
        yield from self.output_ports

    @property
    def input_ports(self):
        yield from self[PortType.input].values()

    @property
    def output_ports(self):
        yield from self[PortType.output].values()

    @property
    def output_connections(self):
        """All output connections"""
        return [
            connection
            for idx, port in self._ports[PortType.output].items()
            for connection in port.connections
        ]

    @property
    def input_connections(self):
        """All input connections"""
        return [
            connection
            for idx, port in self._ports[PortType.input].items()
            for connection in port.connections
        ]

    @property
    def all_connections(self):
        """All input and output connections"""
        return self.input_connections + self.output_connections

    def connections(self, port_type: PortType, port_index: int) -> list:
        """
        Connections

        Parameters
        ----------
        port_type : PortType
        port_index : int

        Returns
        -------
        value : list
        """
        return list(self._ports[port_type][port_index].connections)

    def erase_connection(self, port_type: PortType, port_index: int, connection: ConnectionBase):
        """
        Erase connection

        Parameters
        ----------
        port_type : PortType
        port_index : int
        connection : Connection
        """
        self._ports[port_type][port_index].remove_connection(connection)

    @property
    def reaction(self) -> ReactToConnectionState:
        """
        Reaction

        Returns
        -------
        value : NodeState.ReactToConnectionState
        """
        return self._reaction

    @property
    def reacting_port_type(self) -> PortType:
        """
        Reacting port type

        Returns
        -------
        value : PortType
        """
        return self._reacting_port_type

    @property
    def reacting_data_type(self) -> NodeDataType:
        """
        Reacting data type

        Returns
        -------
        value : NodeDataType
        """
        return self._reacting_data_type

    def set_reaction(self, reaction: ReactToConnectionState,
                     reacting_port_type: PortType = PortType.none,
                     reacting_data_type: NodeDataType = None):
        """
        Set reaction

        Parameters
        ----------
        reaction : NodeState.ReactToConnectionState
        reacting_port_type : PortType, optional
        reacting_data_type : NodeDataType
        """
        self._reaction = ReactToConnectionState(reaction)
        self._reacting_port_type = reacting_port_type
        self._reacting_data_type = reacting_data_type

    @property
    def is_reacting(self) -> bool:
        """
        Is the node reacting to a mouse event?

        Returns
        -------
        value : bool
        """
        return self._reaction == ReactToConnectionState.reacting

    @property
    def resizing(self) -> bool:
        """
        Resizing

        Returns
        -------
        value : bool
        """
        return self._resizing

    @resizing.setter
    def resizing(self, resizing: bool):
        self._resizing = resizing