Exemplo n.º 1
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)
        """
        if not isinstance(diagram, DiagramModel):
            return False
        from mosaicode.control.diagramcontrol import DiagramControl
        dc = DiagramControl(diagram)
        # load the diagram
        parser = XMLParser(diagram.file_name)

        # Loading from tags
        if parser.getTag(tag_name) is None:
            return False

        zoom = 1.0
        if parser.getTagAttr(tag_name, "zoom"):
            zoom = parser.getTagAttr(tag_name, "zoom")
        diagram.zoom = float(zoom)

        language = ""
        if parser.getTagAttr(tag_name, "language"):
            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
            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:
            try:
                from_block = diagram.blocks[int(conn.from_block)]
                to_block = diagram.blocks[int(conn.to_block)]
                port_index = int(conn.getAttr("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():
                        continue
                else:
                    continue
                port_index = int(conn.getAttr("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():
                        continue
                else:
                    continue
            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.º 2
0
 def align_top(self):
     diagram = self.main_window.work_area.get_current_diagram()
     if diagram is None:
         return False
     DiagramControl(diagram).align("TOP")
Exemplo n.º 3
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:
                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").getChildTags("connection")
        for conn in connections:
            if not hasattr(conn, 'from_block'):
                continue
            elif not hasattr(conn, 'to_block'):
                continue
            from_block = diagram.blocks[int(conn.from_block)]
            from_block_out = int(conn.getAttr("from_out"))
            to_block_in = int(conn.getAttr("to_in"))
            to_block = diagram.blocks[int(conn.to_block)]
            connection = ConnectionModel(diagram, from_block,
                                         from_block.ports[from_block_out],
                                         to_block, to_block.ports[to_block_in])
            DiagramControl.add_connection(diagram, connection)

        comments = parser.getTag(tag_name).getTag("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.º 4
0
 def setUp(self):
     """Do the test basic setup."""
     diagram = Diagram(MainWindow())
     self.diagram_control = DiagramControl(diagram)
Exemplo n.º 5
0
    def test_collapse_all(self):
        self.diagram_control.collapse_all(None)

        diagram_control = DiagramControl(None)
        diagram_control.collapse_all(None)
Exemplo n.º 6
0
 def uncollapse_all(self):
     diagram = self.main_window.work_area.get_current_diagram()
     if diagram is None:
         return
     DiagramControl.collapse_all(diagram, False)
Exemplo n.º 7
0
 def test_cut(self):
     self.diagram_control.cut()
     diagram_control = DiagramControl(None)
     diagram_control.cut()
Exemplo n.º 8
0
 def test_select_all(self):
     self.diagram_control.select_all()
     diagram_control = DiagramControl(None)
     diagram_control.select_all()
Exemplo n.º 9
0
 def setUp(self):
     self.diagram = self.create_full_diagram()
     self.diagram_control = DiagramControl(self.diagram)
Exemplo n.º 10
0
class TestDiagramControl(TestBase):

    def setUp(self):
        self.diagram = self.create_full_diagram()
        self.diagram_control = DiagramControl(self.diagram)


    def test_add_block(self):
        block = self.create_block()
        diagram_control = DiagramControl(None)
        diagram_control.add_block(block)

        self.diagram_control.add_block(None)

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

        block.language = "None"
        self.diagram_control.add_block(block)

        self.diagram.language = None
        self.diagram_control.add_block(block)

    def test_select_all(self):
        self.diagram_control.select_all()
        diagram_control = DiagramControl(None)
        diagram_control.select_all()

    def test_cut(self):
        self.diagram_control.cut()
        diagram_control = DiagramControl(None)
        diagram_control.cut()

    def test_copy_delete_paste(self):
        for key in self.diagram.blocks:
            self.diagram.blocks[key].is_selected = True

        for key in self.diagram.blocks:
            self.diagram.blocks[key].language = "None"
            break

        for con in self.diagram.connectors: 
            con.is_selected = True
            break

        for comment in self.diagram.comments: 
            comment.is_selected = True
            break

        self.diagram_control.copy()
        self.diagram_control.delete()
        self.diagram_control.paste()

        diagram_control = DiagramControl(None)
        diagram_control.copy()
        diagram_control.delete()
        diagram_control.paste()


    def test_add_comment(self):
        comment = Comment(self.create_diagram(), None)
        self.diagram_control.add_comment(comment)
        self.diagram_control.add_comment(None)

    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)

    def test_collapse_all(self):
        self.diagram_control.collapse_all(None)

        diagram_control = DiagramControl(None)
        diagram_control.collapse_all(None)

    def test_align(self):
        for key in self.diagram.blocks:
            self.diagram.blocks[key].is_selected = True
        self.diagram_control.add_block(self.create_block())
        self.diagram_control.align("BOTTOM")
        self.diagram_control.align("TOP")
        self.diagram_control.align("LEFT")
        self.diagram_control.align("RIGHT")

        diagram_control = DiagramControl(None)
        diagram_control.align("RIGHT")

    def test_change_zoom(self):
        self.diagram_control.change_zoom(System.ZOOM_ORIGINAL)
        self.diagram_control.change_zoom(System.ZOOM_IN)
        self.diagram_control.change_zoom(System.ZOOM_OUT)

        diagram_control = DiagramControl(None)
        diagram_control.change_zoom(System.ZOOM_ORIGINAL)

    def test_do(self):
        self.diagram_control.do("Test")

        diagram_control = DiagramControl(None)
        diagram_control.do("Test")

    def test_set_show_grid(self):
        self.diagram_control.set_show_grid(None)
        self.diagram_control.set_show_grid(True)


    def test_undo_redo(self):
        self.diagram_control.redo()

        self.diagram_control.undo()
        self.diagram_control.redo()

        diagram_control = DiagramControl(None)
        diagram_control.undo()
        diagram_control.redo()

        while len(self.diagram.undo_stack) > 1: 
            self.diagram.undo_stack.pop() 
        self.diagram_control.undo()
        self.diagram_control.undo()

    def test_load(self):
        self.diagram.file_name = None
        result = self.diagram_control.load()
        self.assertFalse(result[0], None)

        file_name = get_temp_file() + ".mscd"
        self.diagram_control.save(file_name)
        result = self.diagram_control.load(file_name)
        os.remove(file_name)

        diagram_control_load = self.create_diagram_control()
        result = diagram_control_load.load()

        diagram_control = DiagramControl(None)
        diagram_control.load(None)


    def test_save(self):
        file_name = get_temp_file() + ".mscd"
        result = self.diagram_control.save(file_name)
        os.remove(file_name)
        self.assertTrue(result, "Failed to save diagram")

        file_name = get_temp_file()
        result = self.diagram_control.save(file_name)
        os.remove(file_name + ".mscd")
        self.assertTrue(result, "Failed to save diagram")

        self.diagram.file_name = None
        result = self.diagram_control.save()
        os.remove(self.diagram.file_name)
        self.assertTrue(result, "Failed to save diagram")
Exemplo n.º 11
0
    def test_do(self):
        self.diagram_control.do("Test")

        diagram_control = DiagramControl(None)
        diagram_control.do("Test")