def test_save(self): file_name = get_temp_file() + ".mscd" diagram_control = self.create_diagram_control() comment = Comment(diagram_control.diagram) DiagramControl.add_comment(comment.diagram, comment) result = diagram_control.save(file_name) os.remove(file_name) self.assertTrue(result, "Failed to save diagram")
def test_save(self): file_name = get_temp_file() + ".mscd" diagram_control = self.create_diagram_control() comment = Comment(diagram_control.diagram) comment.diagram.file_name = file_name DiagramControl.add_comment(comment.diagram, comment) result = DiagramPersistence.save(comment.diagram) os.remove(file_name) self.assertTrue(result, "Failed to save preferences")
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)
def add_comment(self): """ This method add a block. Parameters: * **Types** (:class:`block<>`) Returns: * **Types** (:class:`boolean<boolean>`) """ diagram = self.main_window.work_area.get_current_diagram() if diagram is None: return False comment = Comment(diagram) DiagramControl.add_comment(diagram, comment) diagram.redraw() return True
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
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
class TestDiagramControl(TestBase): def setUp(self): self.diagram_control = DiagramControl(self.create_diagram()) def test_add_block(self): block = self.create_block() self.diagram_control.add_block(block) def test_add_comment(self): comment = Comment(self.create_diagram(), None) self.diagram_control.add_comment(comment) def test_add_connection(self): self.diagram_control.add_connection(None) def test_align(self): self.diagram_control.align(None) def test_collapse_all(self): self.diagram_control.collapse_all(None) def test_copy(self): self.diagram_control.copy() def test_cut(self): self.diagram_control.cut() def test_delete(self): self.diagram_control.delete() def test_do(self): self.diagram_control.do("Test") def test_export_png(self): file_name = get_temp_file() + ".mscd" block = self.create_block() self.diagram_control.add_block(block) result = self.diagram_control.export_png(file_name) result = True self.assertTrue(result, "Failed to export diagram") def test_get_min_max(self): self.diagram_control.get_min_max() def test_load(self): file_name = get_temp_file() + ".mscd" diagram_control = self.create_diagram_control() comment = Comment(diagram_control.diagram, None) diagram_control.add_comment(comment) diagram_control.save(file_name) diagram_control_load = self.create_diagram_control() result = diagram_control_load.load(file_name) os.remove(file_name) self.assertTrue(result, "Failed to load diagram") def test_paste(self): self.diagram_control.paste() def test_redo(self): self.diagram_control.redo() def test_save(self): file_name = get_temp_file() + ".mscd" diagram_control = self.create_diagram_control() comment = Comment(diagram_control.diagram, None) diagram_control.add_comment(comment) result = diagram_control.save(file_name) os.remove(file_name) self.assertTrue(result, "Failed to save diagram") def test_set_show_grid(self): self.diagram_control.set_show_grid(None) def test_undo(self): self.diagram_control.undo()
def test_add_comment(self): comment = Comment(self.create_diagram()) self.assertTrue(DiagramControl.add_comment(comment.diagram, comment), "Failed to add comment")
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")