<commit_before>#
# Copyright 2010-2015
#

import copy

from PySide import QtGui, QtCore

from pyflowgraph.graph_view import GraphView
from pyflowgraph.connection import Connection
from pyflowgraph.selection_rect import SelectionRect
from knode import KNode
from kbackdrop import KBackdrop
from edit_index_widget import EditIndexWidget
from kraken.core.maths import Vec2
from kraken.core.kraken_system import KrakenSystem
from kraken.core.configs.config import Config


class KGraphView(GraphView):

    beginCopyData = QtCore.Signal()
    endCopyData = QtCore.Signal()

    beginPasteData = QtCore.Signal()
    endPasteData = QtCore.Signal()

    _clipboardData = None

    def __init__(self, parent=None):
        super(KGraphView, self).__init__(parent)
        self.__rig = None
        self.setAcceptDrops(True)


    def getRig(self):
        return self.__rig

    # ======
    # Graph
    # ======
    def displayGraph(self, rig):
        """
        Builds the Kraken node graph
        Pass None as rig parameter to refresh current rig graph
        """
        self.reset()

        if rig is not None:
            self.__rig = rig

        guideComponents = self.__rig.getChildrenByType('Component')

        for component in guideComponents:
            node = KNode(self, component)
            self.addNode(node)

        for component in guideComponents:
            for i in range(component.getNumInputs()):
                componentInput = component.getInputByIndex(i)
                if componentInput.isConnected():
                    componentOutput = componentInput.getConnection()

                    self.connectPorts(
                        srcNode = componentOutput.getParent().getDecoratedName(), outputName = componentOutput.getName(),
                        tgtNode = component.getDecoratedName(), inputName=componentInput.getName()
                    )

        # Get backdrops from meta data
        metaData = self.__rig.getMetaData()
        if 'backdrops' in metaData:
            for backdrop in metaData['backdrops']:
                backdropNode = KBackdrop(self, backdrop.get('name', 'Backdrop'))
                self.addNode(backdropNode)
                backdropNode.setData(backdrop)

        self.frameAllNodes()


    def addConnection(self, connection, emitSignal=True):

        result = super(KGraphView, self).addConnection(connection, emitSignal=emitSignal)

        # Indicate that this is an indexed connection.
        outPort = connection.getSrcPortCircle().getPort()
        inPort = connection.getDstPortCircle().getPort()
        if outPort is not None and inPort is not None and outPort.getDataType() != inPort.getDataType():
            if outPort.getDataType().startswith(inPort.getDataType()) and outPort.getDataType().endswith('[]'):
                connection.setPenStyle(QtCore.Qt.DashDotLine)
                connection.setPenWidth(2.5)

        return connection


    def getNodesOfType(self, nodeType):
        """Gets all the nodes of the specified type.<commit_msg>put this back in<commit_after>#
# Copyright 2010-2015
#

import copy

from PySide import QtGui, QtCore

from pyflowgraph.graph_view import GraphView
from pyflowgraph.connection import Connection
from pyflowgraph.selection_rect import SelectionRect
from knode import KNode
from kbackdrop import KBackdrop
from edit_index_widget import EditIndexWidget
from kraken.core.maths import Vec2
from kraken.core.kraken_system import KrakenSystem
from kraken.core.configs.config import Config


class KGraphView(GraphView):

    beginCopyData = QtCore.Signal()
    endCopyData = QtCore.Signal()

    beginPasteData = QtCore.Signal()
    endPasteData = QtCore.Signal()

    _clipboardData = None

    def __init__(self, parent=None):
        super(KGraphView, self).__init__(parent)
        self.__rig = None
        self.setAcceptDrops(True)


    def getRig(self):
        return self.__rig

    # ======
    # Graph
    # ======
    def displayGraph(self, rig):
        """
        Builds the Kraken node graph
        Pass None as rig parameter to refresh current rig graph
        """
        self.reset()

        if rig is not None:
            self.__rig = rig

        guideComponents = self.__rig.getChildrenByType('Component')

        for component in guideComponents:
            node = KNode(self, component)
            self.addNode(node)

        for component in guideComponents:
            for i in range(component.getNumInputs()):
                componentInput = component.getInputByIndex(i)
                if componentInput.isConnected():
                    componentOutput = componentInput.getConnection()

                    self.connectPorts(
                        srcNode = componentOutput.getParent().getDecoratedName(), outputName = componentOutput.getName(),
                        tgtNode = component.getDecoratedName(), inputName=componentInput.getName()
                    )

        # Get backdrops from meta data
        metaData = self.__rig.getMetaData()
        if 'backdrops' in metaData:
            for backdrop in metaData['backdrops']:
                backdropNode = KBackdrop(self, backdrop.get('name', 'Backdrop'))
                self.addNode(backdropNode)
                backdropNode.setData(backdrop)

        if rig is not None:
            self.frameAllNodes()


    def addConnection(self, connection, emitSignal=True):

        result = super(KGraphView, self).addConnection(connection, emitSignal=emitSignal)

        # Indicate that this is an indexed connection.
        outPort = connection.getSrcPortCircle().getPort()
        inPort = connection.getDstPortCircle().getPort()
        if outPort is not None and inPort is not None and outPort.getDataType() != inPort.getDataType():
            if outPort.getDataType().startswith(inPort.getDataType()) and outPort.getDataType().endswith('[]'):
                connection.setPenStyle(QtCore.Qt.DashDotLine)
                connection.setPenWidth(2.5)

        return connection


    def getNodesOfType(self, nodeType):
        """Gets all the nodes of the specified type.
{'fraction_non_alphanumeric': 0.08528400720248813, 'fraction_numerical': 0.0036012440661319363, 'mean_word_length': 2.5338345864661656, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '660631', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1612, 'n_words': 411}