Source code for orangecanvas.scheme.link

"""
===========
Scheme Link
===========

"""
import enum

from AnyQt.QtCore import QObject
from AnyQt.QtCore import pyqtSignal as Signal, pyqtProperty as Property

from ..utils import name_lookup
from .errors import IncompatibleChannelTypeError


def compatible_channels(source_channel, sink_channel):
    """
    Do the channels in link have compatible types, i.e. can they be
    connected based on their type.

    """
    source_type = name_lookup(source_channel.type)
    sink_type = name_lookup(sink_channel.type)
    ret = issubclass(source_type, sink_type)
    if source_channel.dynamic:
        ret = ret or issubclass(sink_type, source_type)
    return ret


def can_connect(source_node, sink_node):
    """
    Return True if any output from `source_node` can be connected to
    any input of `sink_node`.

    """
    return bool(possible_links(source_node, sink_node))


def possible_links(source_node, sink_node):
    """
    Return a list of (OutputSignal, InputSignal) tuples, that
    can connect the two nodes.

    """
    possible = []
    for source in source_node.output_channels():
        for sink in sink_node.input_channels():
            if compatible_channels(source, sink):
                possible.append((source, sink))
    return possible