Exemple #1
0
    def __select_save(self, diagram):
        """
        This method selects a name to save the file.
        """
        while True:
            file_name = diagram.file_name
            if file_name == "Untitled":
                file_name = System.get_user_dir() + "/" + diagram.file_name
            self.save_dialog = SaveDialog(self.main_window,
                                          title=_("Save Diagram"),
                                          filename=file_name,
                                          filetype="*.mscd")
            name = self.save_dialog.run()
            if name is None:
                return False, "User canceled the operation"

            if os.path.exists(name) is True:
                msg = _("File exists. Overwrite?")
                self.confirm_dialog = ConfirmDialog(msg, self.main_window)
                result = self.confirm_dialog.run()
                if result == Gtk.ResponseType.CANCEL:
                    continue
            if not name.endswith("mscd"):
                name = name + ".mscd"
            diagram.file_name = name
            break
        return True, "Success"
Exemple #2
0
    def close_tab(self, position=None):
        """
        This method close a tab.

            Parameters:
                * **position**
            Returns:
                * **Types** (:class:`boolean<boolean>`)
        """
        if position is None:
            position = self.get_current_page()
        diagram = self.get_nth_page(position)

        if diagram is None:
            return False

        if diagram.modified:
            message = _("Diagram ") + diagram.file_name + \
                                       _(" is not saved.\nIf you close it"
                                       ", changes will be lost.\n"
                                       "Confirm?")
            self.confirm = ConfirmDialog(message, self.main_window)
            result = self.confirm.run()
            if result == Gtk.ResponseType.CANCEL:
                return False

        self.remove_page(position)
        self.diagrams.pop(position)
        return True
Exemple #3
0
    def export_diagram(self):
        """
        This method exports the diagram.
        """
        diagram = self.main_window.work_area.get_current_diagram()
        if diagram is None:
            return False

        while True:
            name = SaveDialog(self.main_window,
                              title=_("Export diagram as png"),
                              filename=System.get_user_dir() + "/" +
                              diagram.file_name + ".png",
                              filetype="png").run()

            if name is None:
                return
            if name.find(".png") == -1:
                name = name + ".png"
            if name is not None and os.path.exists(name) is True:
                msg = _("File exists. Overwrite?")
                result = ConfirmDialog(msg, self.main_window).run()
                if result == Gtk.ResponseType.OK:
                    break

        result, message = DiagramControl(diagram).export_png(name)

        if not result:
            MessageDialog("Error", message, self.main_window).run()
Exemple #4
0
 def __delete(self, widget=None, data=None):
     name = self.__get_selected()
     if name is None:
         return
     result = ConfirmDialog(_("Are you sure?"), self).run()
     if result == Gtk.ResponseType.OK:
         self.main_window.main_control.delete_code_template(name)
         self.__update()
Exemple #5
0
 def __delete(self, widget=None, data=None):
     block = self.block_notebook.get_selected_block()
     if block is None:
         return
     result = ConfirmDialog(_("Are you sure?"), self).run()
     if result == Gtk.ResponseType.OK:
         self.main_window.main_control.delete_block(block)
         self.update()
class TestConfirmDialog(TestBase):
    def setUp(self):
        self.dialog = ConfirmDialog("Test Confirm Dialog",
                                    self.create_main_window())

    def test_run_ok(self):
        t1 = threading.Thread(target=self.dialog.run, args=())
        t1.start()
        sleep(1)
        self.dialog.response(Gtk.ResponseType.OK)
        self.refresh_gui()
        t1.join()

    def test_run_cancel(self):
        t1 = threading.Thread(target=self.dialog.run, args=())
        t1.start()
        sleep(1)
        self.dialog.response(Gtk.ResponseType.CANCEL)
        self.refresh_gui()
        t1.join()
 def __delete(self, widget=None, data=None):
     treeselection = self.tree_view.get_selection()
     model, iterac = treeselection.get_selected()
     if iterac is None:
         return None
     result = ConfirmDialog(_("Are you sure?"), self.get_toplevel()).run()
     if result != Gtk.ResponseType.OK:
         return
     path = model.get_path(iterac)
     del self.element.get_properties()[int(str(path))]
     self.__populate_property()
     self.__clean_side_panel()
    def __delete(self, widget=None, data=None):
        treeselection = self.tree_view.get_selection()
        model, iterac = treeselection.get_selected()
        if iterac is None:
            return None
        result = ConfirmDialog(_("Are you sure?"), self.block_editor).run()
        if result != Gtk.ResponseType.OK:
            return
        path = model.get_path(iterac)

        del self.block.ports[int(str(path))]
        self.__populate_list()
        self.__clean_side_panel()
Exemple #9
0
    def __delete(self, widget=None, data=None):
        treeselection = None
        treeselection = self.tree_view.get_selection()
        model, iterac = treeselection.get_selected()
        if iterac is None:
            return None
        path = model.get_path(iterac)
        name = model.get_value(model.get_iter(path), 0)

        result = ConfirmDialog(_("Are you sure?"), self).run()
        if result != Gtk.ResponseType.OK:
            return
        path = model.get_path(iterac)

        del self.codes[name]
        self.__populate_list()
        self.__clean_side_panel()
Exemple #10
0
    def save(self, save_as=False):
        """
        This method save the file.
        """
        diagram = self.main_window.work_area.get_current_diagram()
        if diagram is None:
            return False

        if diagram.file_name == "Untitled" or save_as:
            while True:
                dialog = SaveDialog(
                    self.main_window,
                    title=_("Save Diagram"),
                    filename=System.get_user_dir() + "/" + diagram.patch_name,
                    filetype="*.mscd")
                name = dialog.run()
                if name is None:
                    System.log("File not saved")
                    return

                if not name.endswith("mscd"):
                    name = (("%s" + ".mscd") % name)

                if os.path.exists(name) is True:
                    msg = _("File exists. Overwrite?")
                    result = ConfirmDialog(msg, self.main_window).run()
                    if result == Gtk.ResponseType.CANCEL:
                        continue

                diagram.file_name = name
                self.main_window.work_area.rename_diagram(diagram)
                break
        result, message = False, ""

        if diagram.file_name is not None:
            if len(diagram.file_name) > 0:
                result, message = DiagramControl(diagram).save()
                self.set_recent_files(diagram.file_name)

        if not result:
            MessageDialog("Error", message, self.main_window).run()
 def setUp(self):
     self.dialog = ConfirmDialog("Test Confirm Dialog",
                                 self.create_main_window())
Exemple #12
0
class MainControl():
    """
    This class contains methods related the MainControl class.
    """

    # ----------------------------------------------------------------------

    def __init__(self, main_window):
        self.main_window = main_window
        self.open_dialog = None
        self.save_dialog = None
        self.confirm_dialog = None
        self.preference_window = None
        self.message_dialog = None
        self.about_window = None
        self.code_window = None

        # Clipboard is here because It must be possible to exchange data between diagrams
        self.clipboard = []
        self.threads = {}
        self.publisher = Publisher()

    # ----------------------------------------------------------------------
    def init(self):
        self.update_blocks()
        # Load plugins
        for plugin in System.get_plugins():
            plugin.load(self.main_window)

        self.main_window.menu.update_recent_files()
        self.main_window.menu.update_examples()

    # ----------------------------------------------------------------------
    def get_current_diagram(self):
        return self.main_window.work_area.get_current_diagram()

    # ----------------------------------------------------------------------
    def update_blocks(self):
        System.reload()
        self.main_window.menu.update_blocks()
        self.main_window.block_notebook.update_blocks()

    # ----------------------------------------------------------------------
    def new(self):
        """
        This method create a new the diagram file.
        """
        Diagram(self.main_window)

    # ----------------------------------------------------------------------
    def select_open(self, path=None):
        """
        This method open a selected file.
        """
        if path is None:
            path = System.get_user_dir()
        self.open_dialog = OpenDialog("Open Diagram",
                                      self.main_window,
                                      filetype="mscd",
                                      path=path)
        file_name = self.open_dialog.run()
        if file_name is None or file_name == "":
            return
        self.open(file_name)

    # ----------------------------------------------------------------------
    def open(self, file_name):
        """
        This method open a file.
        """
        diagram = Diagram(self.main_window)
        result = DiagramControl(diagram).load(file_name)
        if result[0] is None:
            self.message_dialog = MessageDialog("Error", result[1],
                                                self.main_window)
            self.message_dialog.run()
            return
        diagram = result[0]
        System.add_recent_files(file_name)

    # ----------------------------------------------------------------------
    def close(self):
        """
        This method closes a tab on the work area.
        """
        self.main_window.work_area.close_tab()

    # ----------------------------------------------------------------------
    def __select_save(self, diagram):
        """
        This method selects a name to save the file.
        """
        while True:
            file_name = diagram.file_name
            if file_name == "Untitled":
                file_name = System.get_user_dir() + "/" + diagram.file_name
            self.save_dialog = SaveDialog(self.main_window,
                                          title=_("Save Diagram"),
                                          filename=file_name,
                                          filetype="*.mscd")
            name = self.save_dialog.run()
            if name is None:
                return False, "User canceled the operation"

            if os.path.exists(name) is True:
                msg = _("File exists. Overwrite?")
                self.confirm_dialog = ConfirmDialog(msg, self.main_window)
                result = self.confirm_dialog.run()
                if result == Gtk.ResponseType.CANCEL:
                    continue
            if not name.endswith("mscd"):
                name = name + ".mscd"
            diagram.file_name = name
            break
        return True, "Success"

    # ----------------------------------------------------------------------
    def save(self, save_as=False):
        """
        This method save the file.
        """
        diagram = self.get_current_diagram()
        if diagram is None:
            return False

        if diagram.file_name is "Untitled" or save_as:
            result, message = self.__select_save(diagram)
            if not result:
                return False

        result, message = DiagramControl(diagram).save()
        if not result:
            self.message_dialog = MessageDialog("Error", message,
                                                self.main_window)
            self.message_dialog.run()
            return False
        System.add_recent_files(diagram.file_name)
        self.main_window.work_area.rename_diagram(diagram)
        return True

    # ----------------------------------------------------------------------
    def save_as(self):
        """
        This method save as.
        """
        self.save(save_as=True)

    # ----------------------------------------------------------------------
    def exit(self, widget=None, data=None):
        """
        This method close main window.

        Returns:

            * **Types** (:class:`boolean<boolean>`)
        """
        PreferencesPersistence.save(System.get_preferences(),
                                    System.get_user_dir())
        if self.main_window.work_area.close_tabs():
            Gtk.main_quit()
        else:
            return True

    # ----------------------------------------------------------------------
    def add_recent_files(self, file_name):
        System.add_recent_files(file_name)
        self.main_window.menu.update_recent_files()
        PreferencesPersistence.save(System.get_preferences(),
                                    System.get_user_dir())

    # ----------------------------------------------------------------------

    def get_clipboard(self):
        """
        This method return the clipboard.
        """
        return self.clipboard

    # ----------------------------------------------------------------------
    def reset_clipboard(self):
        """
        This method clear the clipboard.
        """
        self.clipboard = []

    # ----------------------------------------------------------------------
    def preferences(self):
        """
        """
        self.preference_window = PreferenceWindow(self.main_window)
        self.preference_window.run()

    # ----------------------------------------------------------------------
    def save_source(self, codes=None, generator=None):
        """
        This method saves the source codes.
        """
        diagram = self.get_current_diagram()
        result, msg = CodeGenerator.save_source(diagram=diagram,
                                                codes=codes,
                                                generator=generator)
        if not result:
            self.message_dialog = MessageDialog("Error", msg, self.main_window)
            self.message_dialog.run()

    # ----------------------------------------------------------------------
    def view_source(self):
        """
        This method view the source code.
        """
        diagram = self.get_current_diagram()
        generator, msg = CodeGenerator.get_code_generator(diagram)
        if generator is not None:
            codes = generator.generate_code()
            self.code_window = CodeWindow(self.main_window, codes)
            self.code_window.run()
            self.code_window.close()
            self.code_window.destroy()
        else:
            self.message_dialog = MessageDialog("Error", msg, self.main_window)
            self.message_dialog.run()

    # ----------------------------------------------------------------------
    def run(self, codes=None):
        """
        This method runs the code.
        """
        diagram = self.get_current_diagram()
        generator, message = CodeGenerator.get_code_generator(diagram)
        if generator is None:
            return False

        self.save_source(codes=codes, generator=generator)

        command = diagram.code_template.command
        command = command.replace("$dir_name$", System.get_dir_name(diagram))

        def __run(self):
            process = subprocess.Popen(command,
                                       cwd=System.get_dir_name(diagram),
                                       shell=True,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       preexec_fn=os.setsid)
            self.threads[thread] = diagram, process
            self.main_window.toolbar.update_threads(self.threads)
            (stdout_data, stderr_data) = process.communicate()
            System.log(stdout_data + "\n")
            System.log(stderr_data + "\n")
            del self.threads[thread]
            self.main_window.toolbar.update_threads(self.threads)

        System.log("Executing Code:\n" + command)
        thread = Thread(target=__run, args=(self, ))
        thread.start()

        return True

    # ----------------------------------------------------------------------
    def stop(self, widget, process):
        if process is None:
            return
        pgid = os.getpgid(process.pid)
        os.killpg(pgid, signal.SIGTERM)

    # ----------------------------------------------------------------------
    def publish(self):
        """
        This method run web server.
        """
        if self.publisher.is_running():
            self.publisher.stop()
        else:
            self.publisher.start()

    # ----------------------------------------------------------------------
    def about(self):
        """
        This method open the about window.
        """
        self.about_window = About(self.main_window)
        self.about_window.show_all()

    # ----------------------------------------------------------------------
    def search(self, query):
        """
        This method search the query in the blocks_tree_view.
        """
        self.main_window.block_notebook.search(query)

    # ----------------------------------------------------------------------
    def set_block(self, block):
        """
        This method set the block properties.
        """
        self.main_window.property_box.set_block(block)

    # ----------------------------------------------------------------------
    def get_selected_block(self):
        """
        This method get the tree view block.
        """
        return self.main_window.block_notebook.get_selected_block()

    # ----------------------------------------------------------------------
    def clear_console(self):
        """
        This method clear the console.
        """
        self.main_window.status.clear()

    # ----------------------------------------------------------------------
    def add_block(self, block):
        """
        This method add a block.

        Parameters:

                * **Types** (:class:`block<>`)
        Returns:

            * **Types** (:class:`boolean<boolean>`)
        """
        result = DiagramControl(self.get_current_diagram()).add_block(block)
        if not result[0]:
            message = result[1]
            self.message_dialog = MessageDialog("Error", message,
                                                self.main_window)
            self.message_dialog.run()
            return None
        return block

    # ----------------------------------------------------------------------
    def add_comment(self, comment=None):
        """
        This method add a block.

        Parameters:

                * **Types** (:class:`block<>`)
        Returns:

            * **Types** (:class:`boolean<boolean>`)
        """
        DiagramControl(self.get_current_diagram()).add_comment(comment)
        return True

    # ----------------------------------------------------------------------
    def select_all(self):
        DiagramControl(self.get_current_diagram()).select_all()

    # ----------------------------------------------------------------------
    def cut(self):
        """
        This method cut a block on work area.
        """
        DiagramControl(self.get_current_diagram()).cut()

    # ----------------------------------------------------------------------
    def copy(self):
        """
        This method copy a block.
        """
        DiagramControl(self.get_current_diagram()).copy()

    # ----------------------------------------------------------------------
    def paste(self):
        """
        This method paste a block.
        """
        DiagramControl(self.get_current_diagram()).paste()

    # ----------------------------------------------------------------------
    def delete(self):
        """
        This method delete a block.
        """
        DiagramControl(self.get_current_diagram()).delete()

    # ----------------------------------------------------------------------
    def zoom_in(self):
        """
        This method increases the zoom value.
        """
        DiagramControl(self.get_current_diagram()).change_zoom(System.ZOOM_IN)

    # ----------------------------------------------------------------------
    def zoom_out(self):
        """
        This method decreases the zoom.
        """
        DiagramControl(self.get_current_diagram()).change_zoom(System.ZOOM_OUT)

    # ----------------------------------------------------------------------
    def zoom_normal(self):
        """
        Set the zoom value to normal.
        """
        DiagramControl(self.get_current_diagram()).change_zoom(
            System.ZOOM_ORIGINAL)

    # ----------------------------------------------------------------------
    def undo(self):
        """
        Undo a modification.
        """
        DiagramControl(self.get_current_diagram()).undo()

    # ----------------------------------------------------------------------
    def redo(self):
        """
        Redo a modification.
        """
        DiagramControl(self.get_current_diagram()).redo()

    # ----------------------------------------------------------------------
    def align_top(self):
        DiagramControl(self.get_current_diagram()).align("TOP")

    # ----------------------------------------------------------------------
    def align_bottom(self):
        DiagramControl(self.get_current_diagram()).align("BOTTOM")

    # ----------------------------------------------------------------------
    def align_left(self):
        DiagramControl(self.get_current_diagram()).align("LEFT")

    # ----------------------------------------------------------------------
    def align_right(self):
        DiagramControl(self.get_current_diagram()).align("RIGHT")

    # ----------------------------------------------------------------------
    def collapse_all(self):
        DiagramControl(self.get_current_diagram()).collapse_all(True)

    # ----------------------------------------------------------------------
    def uncollapse_all(self):
        DiagramControl(self.get_current_diagram()).collapse_all(False)

    # ----------------------------------------------------------------------
    def redraw(self, show_grid):
        diagrams = self.main_window.work_area.get_diagrams()
        for diagram in diagrams:
            DiagramControl(diagram).set_show_grid(show_grid)

    # ----------------------------------------------------------------------
    def show_grid(self, event):
        if event is None:
            return
        self.redraw(event.get_active())

    # ----------------------------------------------------------------------
    def add_code_template(self, code_template):
        CodeTemplateControl.add_code_template(code_template)
        System.reload()

    # ----------------------------------------------------------------------
    def delete_code_template(self, code_template_name):
        result = CodeTemplateControl.delete_code_template(code_template_name)
        if not result[0]:
            self.message_dialog = MessageDialog("Error", result[1],
                                                self.main_window)
            self.message_dialog.run()
            return False
        self.message_dialog = MessageDialog("Info", result[1],
                                            self.main_window)
        self.message_dialog.run()
        System.reload()
        return True

    # ----------------------------------------------------------------------
    def add_port(self, port):
        PortControl.add_port(port)
        System.reload()

    # ----------------------------------------------------------------------
    def delete_port(self, port_key):
        result = PortControl.delete_port(port_key)
        if not result[0]:
            self.message_dialog = MessageDialog("Error", result[1],
                                                self.main_window)
            self.message_dialog.run()
        System.reload()

    # ----------------------------------------------------------------------
    def add_new_block(self, block):
        BlockControl.add_new_block(block)
        self.update_blocks()

    # ----------------------------------------------------------------------
    def delete_block(self, block):
        result = BlockControl.delete_block(block)
        if not result[0]:
            self.message_dialog = MessageDialog("Error", result[1],
                                                self.main_window)
            self.message_dialog.run()
        self.update_blocks()

    # ----------------------------------------------------------------------
    def update_all(self):
        for diagram in self.main_window.work_area.get_diagrams():
            diagram.update()
Exemple #13
0
class WorkArea(Gtk.Notebook):
    """
    This class contains methods related the WorkArea class.
    """

    def __init__(self, main_window):
        Gtk.Notebook.__init__(self)
        self.main_window = main_window
        self.set_scrollable(True)
        self.diagrams = []
        self.connect("switch-page", self.__on_switch_page)
        self.connect("page-removed", self.__on_page_removed)
        self.confirm = None

    # ----------------------------------------------------------------------
    def __on_page_removed(self, notebook, child, page_num):
        if self.get_n_pages() == 0:
            self.main_window.set_title("")

    # ----------------------------------------------------------------------
    def __on_switch_page(self, notebook, child, page_num):
        self.main_window.set_title(child.file_name)

    # ----------------------------------------------------------------------
    def add_diagram(self, diagram):
        """
        This method add a  new diagram page.

            Parameters:
                * **diagram** (:class:`Diagram<mosaicode.GUI.diagram`)
        """
        name = diagram.patch_name
        index = self.append_page(diagram, self.__create_tab_label(name, diagram))
        self.show_all()
        self.diagrams.append(diagram)
        self.set_current_page(self.get_n_pages() - 1)

    # ----------------------------------------------------------------------
    def close_tab(self, position=None):
        """
        This method close a tab.

            Parameters:
                * **position**
            Returns:
                * **Types** (:class:`boolean<boolean>`)
        """
        if position is None:
            position = self.get_current_page()
        diagram = self.get_nth_page(position)

        if diagram is None:
            return False

        if diagram.modified:
            message = _("Diagram ") + diagram.file_name + \
                                       _(" is not saved.\nIf you close it"
                                       ", changes will be lost.\n"
                                       "Confirm?")
            self.confirm = ConfirmDialog(message, self.main_window)
            result = self.confirm.run()
            if result == Gtk.ResponseType.CANCEL:
                return False

        self.remove_page(position)
        self.diagrams.pop(position)
        return True

    # ----------------------------------------------------------------------
    def __create_tab_label(self, text, frame):
        """
        This method create a tab label.
            Parameters:
                * **text** (:class:`str<str>`)
            Returns:
                * **box**
        """
        box = Gtk.HBox()
        button = Gtk.Button()
        image = Gtk.Image().new_from_icon_name(Gtk.STOCK_CLOSE, Gtk.IconSize.MENU)
        button.set_image(image)
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.connect('clicked', self.__on_close_button_clicked, frame)
        label = Gtk.Label.new(text)
        box.add(label)
        box.add(button)
        box.show_all()
        return box

    # ----------------------------------------------------------------------
    def __on_close_button_clicked(self, widget, frame):
        index = -1
        for tab in self.get_children():
            index += 1
            if tab == frame:
                break
        self.close_tab(index)

    # ----------------------------------------------------------------------
    def get_current_diagram(self):
        """
        This method get current diagram page.
            Return
                * **Types** (:class:`int<int>`)
        """

        if self.get_current_page() > -1:
            return self.diagrams[self.get_current_page()]
        else:
            return None

    # ----------------------------------------------------------------------
    def get_diagrams(self):
        return self.diagrams

    # ----------------------------------------------------------------------
    def rename_diagram(self, diagram):
        """
        This method rename a diagram page.

            Parameters:
                * **diagram** (:class:`diagram<mosaicode.GUI.diagram>`)
        """
        index = -1
        for page in self.get_children():
            index += 1
            if page == diagram:
                break
        tab = self.get_nth_page(index)
        if tab is None:
            return
        hbox = self.get_tab_label(tab)
        label = hbox.get_children()[0]
        name = diagram.patch_name
        if diagram.modified:
            name = "* " + name
        label.set_text(name)
        self.main_window.set_title(diagram.file_name)

    # ----------------------------------------------------------------------
    def resize(self, data):
        """
        This method resize a diagram page.

            Parameters:
                * **data**

        """
        for diagram in self.diagrams:
            diagram.resize(data)

    # ----------------------------------------------------------------------
    def close_tabs(self):
        """
        This method close tabs.

            Returns:
               * **boolean** (:class:`boolean<boolean>`)
        """
        n_pages = self.get_n_pages()
        for i in range(n_pages):
            if not self.close_tab(0):
                return False
        return True