예제 #1
0
    def set_properties(self, data):
        """
        This method set properties of each block.

            Parameters:
                * **data**
        """
        CommentModel.set_properties(self, data)
예제 #2
0
    def paste(self):
        """
        This method paste a block.
        """
        if self.diagram is None:
            return False
        replace = {}
        self.diagram.deselect_all()
        # interact into blocks, add blocks and change their id
        clipboard = self.diagram.main_window.main_control.get_clipboard()

        for widget in clipboard:
            if not isinstance(widget, BlockModel):
                continue
            block = BlockModel(widget)
            block.x += 20
            block.y += 20
            block.id = -1
            new_block = BlockModel(block)
            new_block = deepcopy(new_block)
            new_block = Block(self.diagram, new_block)
            if not self.add_block(new_block):
                continue
            replace[widget.id] = new_block
            new_block.is_selected = True

        # interact into connections changing block ids
        for widget in clipboard:
            if not isinstance(widget, ConnectionModel):
                continue
            # if a connector is copied without blocks
            if widget.output.id not in replace \
                    or widget.input.id not in replace:
                continue
            output_block = replace[widget.output.id]
            output_port = widget.output_port
            input_block = replace[widget.input.id]
            input_port = widget.input_port
            self.diagram.start_connection(output_block, output_port)
            self.diagram.curr_connector.is_selected = True
            self.diagram.end_connection(input_block, input_port)

        for widget in clipboard:
            if not isinstance(widget, CommentModel):
                continue
            comment = CommentModel(widget)
            comment.x += 20
            comment.y += 20
            comment.is_selected = True
            comment = self.diagram.main_window.main_control.add_comment(comment)

        self.diagram.update_flows()
예제 #3
0
    def get_properties(self):
        """
        This method get properties of each block.

            Returns:
                * **Types** ()
        """
        return CommentModel.get_properties(self)
예제 #4
0
    def __init__(self, diagram, comment):
        """
        This method is the constuctor.
        """
        GooCanvas.CanvasText.__init__(self)
        CommentModel.__init__(self, comment)

        self.diagram = diagram

        self.remember_x = 0
        self.remember_y = 0

        self.is_selected = False
        self.focus = False

        self.connect("button-press-event", self.__on_button_press)
        self.connect("motion-notify-event", self.__on_motion_notify)
        # Mouse over
        self.connect("enter-notify-event", self.__on_enter_notify)
        self.connect("leave-notify-event", self.__on_leave_notify)
        self.move(int(float(self.x)), int(float(self.y)))

        self.__update()
예제 #5
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
예제 #6
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
예제 #7
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
예제 #8
0
    def test_new(self):
        comment = CommentModel()
        comment.set_properties({"text": "Novo Teste"})
        comment.set_properties({"Not here": "Novo Teste"})
        str(comment)

        comment.set_properties(None)
        str(comment)

        comment.properties = []
        str(comment)

        comment.properties = None
        str(comment)

        comment.properties = None
        comment.set_properties({"text": "Novo Teste"})
        str(comment)

        comment.properties = [{"test": "Test"}]
        str(comment)

        comment = CommentModel(comment)
예제 #9
0
 def test_new(self):
     comment = CommentModel()
     str(comment)