Exemplo n.º 1
0
    def create_full_diagram(self):
        main_window = self.create_main_window()
        diagram = Diagram(main_window)
        diagram_control = self.create_diagram_control()
        diagram = diagram_control.diagram

        block1 = self.create_block()
        diagram_control.add_block(block1)

        block2 = self.create_block()
        diagram_control.add_block(block2)

        connection = ConnectionModel(diagram, block1, block1.ports[0], block2,
                                     block2.ports[1])
        diagram_control.add_connection(connection)

        connection = ConnectionModel(diagram, block1, block1.ports[1], block2,
                                     block2.ports[0])
        diagram_control.add_connection(connection)

        connection = ConnectionModel(diagram, None, None, None, None)
        diagram_control.add_connection(connection)

        comment = self.create_comment()
        main_window.main_control.add_comment(comment)
        diagram_control.add_comment(comment)
        return diagram
Exemplo n.º 2
0
class TestConnectionModel(TestCase):

    def setUp(self):
        """Do the test basic setup."""
        diagram = Diagram()

        self.connection_model = ConnectionModel()

    # ----------------------------------------------------------------------
    def test_get_diagram(self):
        self.connection_model.get_diagram()

    # ----------------------------------------------------------------------
    def test_type_match(self):
        self.connection_model.type_match()

    # ----------------------------------------------------------------------
    def test_get_source_port_name(self):
        self.connection_model.get_source_port_name()

    # ----------------------------------------------------------------------
    def test_get_sink_port_name(self):
        self.connection_model.get_sink_port_name()

    # ----------------------------------------------------------------------
    def test_set_end(self):
        self.connection_model.set_end()
Exemplo n.º 3
0
    def create_full_diagram(self, main_window=None):
        if main_window is None:
            main_window = self.create_main_window()
        diagram = Diagram(main_window)
        self.assertEquals(diagram.last_id, 0)
        diagram.language = "Test"
        diagram.zoom = 2
        diagram_control = self.create_diagram_control(diagram)

        block0 = self.create_block(diagram_control)
        result = diagram_control.add_block(block0)

        block1 = self.create_block(diagram_control)
        result = diagram_control.add_block(block1)
        assert result

        block2 = self.create_block(diagram_control)
        result = diagram_control.add_block(block2)
        assert result

        connection = ConnectionModel(diagram, block1, block1.ports[0], block2,
                                     block2.ports[0])
        result = diagram_control.add_connection(connection)
        self.assertEquals(result[0], False)

        connection = ConnectionModel(diagram, block1, block1.ports[1], block2,
                                     block2.ports[0])
        result = diagram_control.add_connection(connection)
        self.assertEquals(result[1], "Success")

        connection = ConnectionModel(diagram, block2, block2.ports[1], block1,
                                     block1.ports[1])
        result = diagram_control.add_connection(connection)
        self.assertEquals(result[0], False)

        connection = ConnectionModel(diagram, block1, block1.ports[1], block1,
                                     block1.ports[0])
        result = diagram_control.add_connection(connection)
        self.assertEquals(result[0], False)

        connection = ConnectionModel(diagram, None, None, None, None)
        result = diagram_control.add_connection(connection)
        self.assertEquals(result[0], False)

        comment = self.create_comment(diagram)
        diagram_control.add_comment(comment)
        comment = self.create_comment(diagram)
        diagram_control.add_comment(comment)
        diagram.code_template = self.create_code_template()
        return diagram
Exemplo n.º 4
0
    def __init__(self, diagram, source, source_port, conn_type):
        """
        This method is the constructor.
        """
        GooCanvas.CanvasGroup.__init__(self)
        ConnectionModel.__init__(self, diagram, source, source_port, conn_type)

        self.__from_point = self.source.get_output_pos(self.source_port)
        self.__to_point = (0, 0)

        self.__focus = False
        self.width = 0
        self.height = 0

        self.connect("button-press-event", self.__on_button_press)
        self.connect("enter-notify-event", self.__on_enter_notify)
        self.connect("leave-notify-event", self.__on_leave_notify)
        self.__widgets = {}

        self.update_tracking()
Exemplo n.º 5
0
    def __init__(self, diagram, output, output_port):
        """
        This method is the constructor.
        """
        GooCanvas.CanvasGroup.__init__(self)
        ConnectionModel.__init__(self, diagram, output, output_port)

        self.__from_point = (0, 0)
        self.__to_point = (0, 0)

        self.__focus = False
        self.is_selected = False
        self.width = 0
        self.height = 0

        self.connect("button-press-event", self.__on_button_press)
        self.connect("enter-notify-event", self.__on_enter_notify)
        self.connect("leave-notify-event", self.__on_leave_notify)
        self.__widgets = {}

        self.update_flow()
Exemplo n.º 6
0
    def test_load_save(self):
        file_name = get_temp_file() + ".mscd"
        diagram_control = self.create_diagram_control()
        diagram = diagram_control.diagram

        diagram.file_name = "."
        DiagramPersistence.save(diagram)

        comment = Comment(diagram)
        diagram.file_name = file_name
        DiagramControl.add_comment(diagram, comment)

        System()
        blocks = System.get_blocks()
        for key in blocks:
            block1 = Block(diagram, blocks[key])
            DiagramControl.add_block(diagram, block1)

        source = None
        source_port = None
        for key in diagram.blocks:
            for port in diagram.blocks[key].ports:
                if not port.is_input():
                    source = diagram.blocks[key]
                    source_port = port
                    break

        sink = None
        sink_port = None
        for key in diagram.blocks:
            for port in diagram.blocks[key].ports:
                if port.is_input():
                    sink = diagram.blocks[key]
                    sink_port = port
                    break

        connection = ConnectionModel(diagram, source, source_port, sink,
                                     sink_port)
        DiagramControl.add_connection(diagram, connection)

        block0 = self.create_block(diagram_control)

        DiagramPersistence.save(diagram)

        System.remove_block(blocks.values()[0])
        result = DiagramPersistence.load(diagram)

        os.remove(file_name)
Exemplo n.º 7
0
    def load(cls, diagram):
        """
        This method load the xml file that represents the diagram.

            :param diagram: diagram to load.
            :return: operation status (True or False)
        """
        from mosaicode.control.diagramcontrol import DiagramControl
        # load the diagram
        parser = XMLParser(diagram.file_name)

        zoom = parser.getTag(tag_name).getTag("zoom").getAttr("value")
        diagram.zoom = float(zoom)
        try:
            language = parser.getTag(tag_name).getTag("language").getAttr(
                "value")
            diagram.language = language
        except:
            pass

        # new version load
        blocks = parser.getTag(tag_name).getTag("blocks").getChildTags("block")
        system_blocks = System.get_blocks()
        for block in blocks:
            block_type = block.getAttr("type")
            if block_type not in system_blocks:

                System.log("Block " + block_type + " not found")
                continue
            block_id = int(block.getAttr("id"))
            collapsed = False
            if hasattr(block, "collapsed"):
                collapsed = block.collapsed == "True"
            position = block.getTag("position")
            x = position.getAttr("x")
            y = position.getAttr("y")
            properties = block.getChildTags("property")
            props = {}
            for prop in properties:
                if hasattr(prop, 'key') and hasattr(prop, 'value'):
                    props[prop.key] = prop.value
            new_block = deepcopy(system_blocks[block_type])
            new_block.set_properties(props)
            new_block.id = block_id
            new_block.x = float(x)
            new_block.y = float(y)
            new_block.is_collapsed = collapsed
            DiagramControl.add_block(diagram, new_block)

        connections = parser.getTag(tag_name).getTag("connections")
        connections = connections.getChildTags("connection")
        for conn in connections:
            if not hasattr(conn, 'from_block'):
                continue
            elif not hasattr(conn, 'to_block'):
                continue
            try:
                from_block = diagram.blocks[int(conn.from_block)]
                to_block = diagram.blocks[int(conn.to_block)]
                from_block_out = from_block.ports[int(
                    conn.getAttr("from_out"))]
                to_block_in = to_block.ports[int(conn.getAttr("to_in"))]
            except:
                continue
            connection = ConnectionModel(diagram, from_block, from_block_out,
                                         to_block, to_block_in)
            DiagramControl.add_connection(diagram, connection)

        comments = parser.getTag(tag_name).getTag("comments")
        if comments is not None:
            comments = comments.getChildTags("comment")
            for com in comments:
                comment = CommentModel()
                comment.x = float(com.getAttr("x"))
                comment.y = float(com.getAttr("y"))
                comment.text = com.getAttr("text")
                diagram.comments.append(comment)

        return True
Exemplo n.º 8
0
    def load(cls, diagram):
        """
        This method load the xml file that represents the diagram.

            :param diagram: diagram to load.
            :return: operation status (True or False)
        """
        from mosaicode.control.diagramcontrol import DiagramControl
        dc = DiagramControl(diagram)
        # load the diagram
        parser = XMLParser(diagram.file_name)

        # Loading from tags
        zoom = parser.getTag(tag_name).getTag("zoom")
        if zoom is not None:
            zoom = zoom.getAttr("value")
        else:
            zoom = parser.getTagAttr(tag_name, "zoom")
        diagram.zoom = float(zoom)

        language = parser.getTag(tag_name).getTag("language")
        if language is not None:
            language = language.getAttr("value")
        else:
            language = parser.getTagAttr(tag_name, "language")
        diagram.language = language

        code_template = parser.getTag(tag_name).getTag("code_template")
        if code_template is not None and hasattr(code_template, "value"):
            code_template = code_template.getAttr("value")
            if code_template not in System.get_code_templates():
                System.log("Code Template " + code_template + " not found")
            else:
                code_template = System.get_code_templates()[code_template]
                diagram.code_template = deepcopy(code_template)

        parser = parser.getTag(tag_name)
        # new version load
        blocks = parser.getTag("blocks").getChildTags("block")
        system_blocks = System.get_blocks()
        for block in blocks:
            block_type = block.getAttr("type")
            if block_type not in system_blocks:
                System.log("Block " + block_type + " not found")
                continue
            block_id = int(block.getAttr("id"))
            collapsed = False
            if hasattr(block, "collapsed"):
                collapsed = block.collapsed == "True"
            if hasattr(block, "x"):
                x = block.x
            if hasattr(block, "y"):
                y = block.y
            position = block.getTag("position")
            if position is not None:
                x = position.getAttr("x")
                y = position.getAttr("y")
            properties = block.getChildTags("property")
            props = {}
            for prop in properties:
                if hasattr(prop, 'key') and hasattr(prop, 'value'):
                    props[prop.key] = prop.value
            new_block = deepcopy(system_blocks[block_type])
            new_block.set_properties(props)
            new_block.id = block_id
            new_block.x = float(x)
            new_block.y = float(y)
            new_block.is_collapsed = collapsed
            dc.add_block(new_block)

        connections = parser.getTag("connections")
        connections = connections.getChildTags("connection")
        for conn in connections:
            if not hasattr(conn, 'from_block'):
                continue
            elif not hasattr(conn, 'to_block'):
                continue
            try:
                from_block = diagram.blocks[int(conn.from_block)]
                to_block = diagram.blocks[int(conn.to_block)]
                from_block_out = from_block.ports[int(
                    conn.getAttr("from_out"))]
                to_block_in = to_block.ports[int(conn.getAttr("to_in"))]
            except:
                continue
            connection = ConnectionModel(diagram, from_block, from_block_out,
                                         to_block, to_block_in)
            dc.add_connection(connection)

        comments = parser.getTag("comments")
        if comments is not None:
            comments = comments.getChildTags("comment")
            for com in comments:
                comment = CommentModel()
                comment.x = float(com.getAttr("x"))
                comment.y = float(com.getAttr("y"))
                properties = com.getChildTags("property")
                props = {}
                for prop in properties:
                    if hasattr(prop, 'key') and hasattr(prop, 'value'):
                        props[prop.key] = prop.value
                comment.set_properties(props)
                dc.add_comment(comment)

        authors = parser.getTag("authors")
        if authors is not None:
            authors = authors.getChildTags("author")
            for author in authors:
                auth = AuthorModel()
                auth.name = author.getAttr("author")
                auth.license = author.getAttr("license")
                auth.date = author.getAttr("date")
                diagram.authors.append(auth)

        diagram.redraw()
        return True
Exemplo n.º 9
0
    def load(cls, diagram):
        """
        This method load the JSON file that represents the diagram.

            :param diagram: diagram to load.
            :return: operation status (True or False)
        """
        if os.path.exists(diagram.file_name) is False:
            System.log("Problem loading the diagram. File does not exist.")
            return None

        if not isinstance(diagram, DiagramModel):
            System.log("Problem loading the diagram. Is this a Diagram?")
            return False
        from mosaicode.control.diagramcontrol import DiagramControl
        dc = DiagramControl(diagram)
        # load the diagram

        data = ""

        try:
            data_file = open(diagram.file_name, 'r')
            data = json.load(data_file)
            data_file.close()

            if data["data"] != "DIAGRAM":
                System.log("Problem loading the diagram. Are you sure this is a valid file?")
                return False

            if "zoom" in data:
                diagram.zoom = float(data["zoom"])
            if "language" in data:
                diagram.language = data["language"]

            # Loading Code Template
            if "code_template" in data:
                code_template_data = data["code_template"]
                if "type" in code_template_data:
                    code_template = code_template_data["type"]
                    if code_template not in System.get_code_templates():
                        System.log("Code Template " + code_template + " not found")
                    else:
                        code_template = System.get_code_templates()[code_template]
                        diagram.code_template = deepcopy(code_template)
                if "properties" in code_template_data:
                    properties = code_template_data["properties"]
                    props = {}
                    for prop in properties:
                        props[prop["key"]] = prop["value"]
                    diagram.code_template.set_properties(props)

            # Loading Blocks
            if "blocks" in data:        
                blocks = data["blocks"]
                system_blocks = System.get_blocks()
                for block in blocks:
                    block_type = block["type"]
                    if block_type not in system_blocks:
                        System.log("Block " + block_type + " not found")
                        continue
                    block_id = int(block["id"])
                    collapsed = block["collapsed"]
                    x = block["x"]
                    y = block["y"]
                    properties = block["properties"]
                    props = {}
                    for prop in properties:
                        props[prop["key"]] = prop["value"]
                    new_block = deepcopy(system_blocks[block_type])
                    new_block.set_properties(props)
                    new_block.id = block_id
                    new_block.x = float(x)
                    new_block.y = float(y)
                    new_block.is_collapsed = collapsed
                    dc.add_block(new_block)

            # Loading connections
            connections = data["connections"]
            for conn in connections:
                try:
                    from_block = diagram.blocks[int(conn["from_block"])]
                    to_block = diagram.blocks[int(conn["to_block"])]
                    port_index = int(conn["from_out"])
                    if port_index >= 0 and port_index < len(from_block.ports):
                        from_block_out = from_block.ports[port_index]
                        if from_block_out.is_input():
                            System.log("Diagram error: Output port is an input port")
                            continue
                    else:
                        System.log("Diagram error: invalid output port index " + str(port_index))
                        continue
                    port_index = int(conn["to_in"])
                    if port_index >= 0 and port_index < len(to_block.ports):
                        to_block_in = to_block.ports[port_index]
                        if not to_block_in.is_input():
                            System.log("Diagram error: Input port is an output port")
                            continue
                    else:
                        System.log("Diagram error: invalid input port index " + str(port_index))
                        continue
                except Exception as e:
                    System.log("Diagram error:" + str(e))
                    continue
                connection = ConnectionModel(diagram,
                                    from_block,
                                    from_block_out,
                                    to_block,
                                    to_block_in)
                dc.add_connection(connection)

            # Loading comments
            comments = data["comments"]
            for com in comments:
                comment = CommentModel()
                comment.x = float(com["x"])
                comment.y = float(com["y"])
                properties = com["properties"]
                props = {}
                for prop in properties:
                    props[prop["key"]] = prop["value"]
                comment.set_properties(props)
                dc.add_comment(comment)

            # Loading authors
            authors = data["authors"]
            for author in authors:
                auth = AuthorModel()
                auth.name = author["author"]
                auth.license = author["license"]
                auth.date = author["date"]
                diagram.authors.append(auth)

            diagram.redraw()


        except Exception as e:
            System.log(e)
            return False

        return True
Exemplo n.º 10
0
 def test_init(self):
     ConnectionModel(None, None, None)
Exemplo n.º 11
0
    def setUp(self):
        """Do the test basic setup."""
        diagram = Diagram()

        self.connection_model = ConnectionModel()
Exemplo n.º 12
0
 def test_add_connection(self):
     self.diagram_control.add_connection(None)
     connection = ConnectionModel(None, None, None, None, None)
     result = self.diagram_control.add_connection(connection)