Example #1
0
class MainWindow(Gtk.Window):
    """
    This class contains methods related the MainWindow class.
    """
    def __init__(self):
        """
        This method is constructor.
        """
        System()
        Gtk.Window.__init__(self, title="Mosaicode")
        self.resize(System.properties.width, System.properties.height)
        self.main_control = MainControl(self)

        # GUI components
        self.menu = Menu(self)
        self.toolbar = Toolbar(self)
        self.search = SearchBar(self)
        self.block_notebook = BlockNotebook(self)
        self.property_box = PropertyBox(self)
        self.work_area = WorkArea(self)
        self.status = Status(self)
        self.block_menu = BlockMenu()
        self.menu.add_help()

        System.set_log(self.status)

        # vbox main
        # -----------------------------------------------------
        # | Menu
        # -----------------------------------------------------
        # | Toolbar
        # -----------------------------------------------------
        # | V Paned bottom
        # -----------------------------------------------------

        # First Vertical Box
        vbox_main = Gtk.VBox()
        self.add(vbox_main)
        vbox_main.pack_start(self.menu, False, True, 0)
        vbox_main.pack_start(self.toolbar, False, False, 0)
        self.vpaned_bottom = Gtk.Paned.new(Gtk.Orientation.VERTICAL)
        vbox_main.add(self.vpaned_bottom)

        # vpaned_bottom
        # -----------------------------------------------------
        # | hpaned_work_area
        # =====================================================
        # | status
        # -----------------------------------------------------

        self.hpaned_work_area = Gtk.HPaned()
        self.hpaned_work_area.connect("accept-position", self.__resize)
        self.hpaned_work_area.set_position(System.properties.hpaned_work_area)

        self.vpaned_bottom.add1(self.hpaned_work_area)
        self.vpaned_bottom.add2(self.__create_frame(self.status))
        self.vpaned_bottom.set_position(System.properties.vpaned_bottom)
        self.vpaned_bottom.set_size_request(50, 50)

        # hpaned_work_area
        # -----------------------------------------------------
        # | vbox_left      ||   work_area
        # -----------------------------------------------------
        vbox_left = Gtk.VBox(False, 0)
        self.hpaned_work_area.add1(vbox_left)
        self.hpaned_work_area.add2(self.work_area)

        # vbox_left
        # -----------------------------------------------------
        # |search
        # -----------------------------------------------------
        # |vpaned_left
        # -----------------------------------------------------

        vbox_left.pack_start(self.search, False, False, 0)
        self.vpaned_left = Gtk.VPaned()
        vbox_left.pack_start(self.vpaned_left, True, True, 0)

        # vpaned_left
        # -----------------------------------------------------
        # |blocks_tree_view
        # =====================================================
        # |property_box
        # -----------------------------------------------------

        self.vpaned_left.add1(self.__create_frame(self.block_notebook))
        self.vpaned_left.add2(self.__create_frame(self.property_box))
        self.vpaned_left.set_position(System.properties.vpaned_left)

        self.connect("delete-event", self.main_control.exit)
        self.connect("key-press-event", self.__on_key_press)
        self.connect("check-resize", self.__resize)

        self.main_control.init()

    # ----------------------------------------------------------------------
    def __on_key_press(self, widget, event=None):
        if event.state == \
                Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.MOD2_MASK:
            if event.keyval == Gdk.KEY_a:
                self.main_control.select_all()
                return True

    # ----------------------------------------------------------------------
    def __create_frame(self, widget):
        frame = Gtk.Frame()
        frame.set_shadow_type(Gtk.ShadowType.ETCHED_IN)
        frame.add(widget)
        frame.set_property("border-width", 4)
        return frame

    # ----------------------------------------------------------------------
    def __resize(self, data):
        width, height = self.get_size()
        System.properties.width = width
        System.properties.height = height
        System.properties.hpaned_work_area = self.hpaned_work_area.get_position(
        )
        System.properties.vpaned_bottom = self.vpaned_bottom.get_position()
        System.properties.vpaned_left = self.vpaned_left.get_position()
        self.work_area.resize(data)

    # ----------------------------------------------------------------------
    def update(self):
        self.main_control.update_all()

    # ----------------------------------------------------------------------
    def set_title(self, title):
        """
        This method set title.

            Parameters:
                * **title** (:class:`str<str>`)

        """
        Gtk.Window.set_title(self, "Mosaicode (" + title + ")")
class TestMainControl(TestBase):
    def setUp(self):
        self.main_control = MainControl(self.create_main_window())
        self.main_control.init()
        self.main_control.new()

    def test_about(self):
        self.main_control.about()

    def test_add_block(self):
        self.main_control.add_block(self.create_block())

    def test_add_comment(self):
        self.main_control.add_comment()

    def test_add_extension(self):
        self.main_control.add(self.create_block())
        self.main_control.add(self.create_code_template())
        self.main_control.add(self.create_port())

    def test_align_bottom(self):
        self.main_control.align_bottom()

    def test_align_left(self):
        self.main_control.align_left()

    def test_align_right(self):
        self.main_control.align_right()

    def test_align_top(self):
        self.main_control.align_top()

    def test_clear_console(self):
        self.main_control.clear_console()

    def test_close(self):
        self.main_control.close()

    def test_collapse_all(self):
        self.main_control.collapse_all()

    def test_copy(self):
        self.main_control.copy()

    def test_cut(self):
        self.main_control.cut()

    def test_delete(self):
        self.main_control.delete()

    def test_delete_extension(self):
        self.main_control.delete(self.create_block())
        self.main_control.delete(self.create_code_template())
        self.main_control.delete("port_key")

    def test_exit(self):
        self.main_control.exit()

    def test_export_diagram(self):
        self.main_control.export_diagram()

    def test_get_clipboard(self):
        self.main_control.get_clipboard()

    def test_get_selected_block(self):
        self.main_control.get_selected_block()

    def test_init(self):
        self.main_control.init()

    def test_new(self):
        self.main_control.new()

    def test_open(self):
        self.main_control.open("Test")

    def test_paste(self):
        self.main_control.paste()

    def test_preferences(self):
        self.main_control.preferences()

    def test_publish(self):
        self.main_control.publish()

    def test_redo(self):
        self.main_control.redo()

    def test_redraw(self):
        self.main_control.redraw(True)
        self.main_control.redraw(False)

    def test_reset_clipboard(self):
        self.main_control.reset_clipboard()

    def test_run(self):
        self.main_control.run()

    def test_save(self):
        self.main_control.save()
        self.main_control.save(save_as=True)

    def test_save_as(self):
        self.main_control.save_as()

    def test_save_as_example(self):
        self.main_control.save_as_example()

    def test_save_source(self):
        self.main_control.save_source()
        self.main_control.save_source(codes="Test")
        self.main_control.save_source(generator="Test")

    def test_search(self):
        self.main_control.search("Test")

    def test_select_all(self):
        self.main_control.select_all()

    def test_select_open(self):
        self.main_control.select_open()

    def test_set_block(self):
        self.main_control.set_block(self.create_block())

    def test_set_recent_files(self):
        self.main_control.set_recent_files("Test")

    def test_show_grid(self):
        self.main_control.show_grid(None)

    def test_stop(self):
        self.main_control.stop(None, None)

    def test_uncollapse_all(self):
        self.main_control.uncollapse_all()

    def test_undo(self):
        self.main_control.undo()

    def test_update_all(self):
        self.main_control.update_all()

    def test_update_blocks(self):
        self.main_control.update_blocks()

    def test_view_source(self):
        self.main_control.view_source()

    def test_zoom_in(self):
        self.main_control.zoom_in()

    def test_zoom_normal(self):
        self.main_control.zoom_normal()

    def test_zoom_out(self):
        self.main_control.zoom_out()
Example #3
0
class TestMainControl(TestBase):
    def setUp(self):
        self.main_window = self.create_main_window()
        self.main_control = MainControl(self.main_window)
        self.main_control.init()
        self.diagram = self.create_full_diagram(main_window=self.main_window)
        self.main_window.work_area.add_diagram(self.diagram)
        self.diagram.set_modified(False)

    def test_init(self):
        System()
        System.add_plugin(Plugin())
        self.main_control.init()

    def test_select_open(self):
        t1 = threading.Thread(target=self.main_control.select_open,
                              args=(None, ))
        t1.start()
        sleep(1)
        if self.main_control.open_dialog:
            self.main_control.open_dialog.response(Gtk.ResponseType.CANCEL)
        t1.join()

        diagram = self.create_full_diagram(main_window=self.main_window)
        diagram.file_name = "/tmp/Test.mscd"
        DiagramControl(diagram).save()
        path = "/tmp/"
        System.set_user_dir(path)

        t1 = threading.Thread(target=self.main_control.select_open,
                              args=(path, ))
        t1.start()
        sleep(1)
        if self.main_control.open_dialog:
            self.main_control.open_dialog.set_filename("/tmp/Test.mscd")
            self.main_control.open_dialog.response(Gtk.ResponseType.OK)
        t1.join()
        os.remove("/tmp/Test.mscd")

    def test_open(self):
        t1 = threading.Thread(target=self.main_control.open, args=("Test", ))
        t1.start()
        sleep(1)
        if self.main_control.message_dialog:
            self.main_control.message_dialog.response(Gtk.ResponseType.CANCEL)
        self.refresh_gui()
        t1.join()

    def test_save(self):
        # diagram is null
        self.diagram.set_modified(False)
        self.main_window.work_area.close_tabs()
        t1 = threading.Thread(target=self.main_control.save, args=(None, ))
        t1.start()
        sleep(1)
        if self.main_control.save_dialog:
            self.main_control.save_dialog.response(Gtk.ResponseType.CANCEL)
        self.refresh_gui()
        t1.join()

        self.main_control.new()
        t1 = threading.Thread(target=self.main_control.save, args=(None, ))
        t1.start()
        sleep(1)
        if self.main_control.save_dialog:
            self.main_control.save_dialog.response(Gtk.ResponseType.OK)
        self.refresh_gui()
        t1.join()

        # Cancel button
        self.main_control.new()
        t1 = threading.Thread(target=self.main_control.save, args=(None, ))
        t1.start()
        sleep(1)
        if self.main_control.save_dialog:
            self.main_control.save_dialog.response(Gtk.ResponseType.CANCEL)
        self.refresh_gui()
        t1.join()

    def test_save_as(self):
        t1 = threading.Thread(target=self.main_control.save_as, args=())
        t1.start()
        sleep(1)
        if self.main_control.save_dialog:
            self.main_control.save_dialog.response(Gtk.ResponseType.CANCEL)
        self.refresh_gui()
        t1.join()

    def test_exit(self):
        self.main_control.exit()

        self.main_window = self.create_main_window()
        self.main_control = MainControl(self.main_window)
        self.main_control.init()
        self.main_control.new()

        diagram = self.main_window.work_area.get_current_diagram()
        diagram.set_modified(True)

        t1 = threading.Thread(target=self.main_control.exit, args=())
        t1.start()
        sleep(1)
        if self.main_window.work_area.confirm:
            self.main_window.work_area.confirm.response(
                Gtk.ResponseType.CANCEL)
        self.refresh_gui()
        t1.join()

    def test_add_recent_files(self):
        self.main_control.add_recent_files("Test")
        self.main_control.add_recent_files("Test")
        for i in range(0, 20):
            System.get_preferences().recent_files.append("Some File")
        self.main_control.add_recent_files("Test")

    def test_clipboard(self):
        self.main_control.get_clipboard()
        self.main_control.reset_clipboard()

    def test_preferences(self):
        t1 = threading.Thread(target=self.main_control.preferences, args=())
        t1.start()
        sleep(1)
        if self.main_control.preference_window:
            self.main_control.preference_window.close()
        self.refresh_gui()
        t1.join()

    def test_save_source(self):
        self.main_control.save_source()

        self.diagram.set_modified(False)
        self.main_window.work_area.close_tabs()
        t1 = threading.Thread(target=self.main_control.save_source, args=())
        t1.start()
        sleep(1)
        if self.main_control.message_dialog:
            self.main_control.message_dialog.close()
        t1.join()

    def test_view_source(self):
        t1 = threading.Thread(target=self.main_control.view_source, args=())
        t1.start()
        sleep(2)
        if self.main_control.code_window:
            self.main_control.code_window.close()
        self.refresh_gui()
        t1.join()

        self.diagram.set_modified(False)
        self.main_window.work_area.close_tabs()
        t1 = threading.Thread(target=self.main_control.view_source, args=())
        t1.start()
        sleep(1)
        if self.main_control.message_dialog:
            self.main_control.message_dialog.response(Gtk.ResponseType.CANCEL)
        self.refresh_gui()
        t1.join()

    def test_run_stop(self):
        self.main_control.run()
        sleep(1)
        self.diagram.set_modified(False)
        self.main_window.work_area.close_tabs()
        self.main_control.run()
        self.main_control.stop(None, None)
        self.main_control.update_blocks()
        self.main_control.close()
        self.main_control.new()

    def test_publish(self):
        self.main_control.publish()
        # Twice to start / stop
        self.main_control.publish()

    def test_about(self):
        t1 = threading.Thread(target=self.main_control.about, args=())
        t1.start()
        sleep(1)
        if self.main_control.about_window:
            self.main_control.about_window.close()
        self.refresh_gui()
        t1.join()

    def test_search_clear(self):
        self.main_control.search("Test")
        self.main_control.set_block(self.create_block())
        self.main_control.get_selected_block()
        self.main_control.clear_console()
        self.main_control.show_grid(None)
        self.main_window.menu.show_grid.emit("activate")

    def test_add_block(self):
        self.main_control.add_block(self.create_block())

        self.diagram.set_modified(False)
        self.main_window.work_area.close_tabs()

        t1 = threading.Thread(target=self.main_control.add_block,
                              args=(self.create_block(), ))
        t1.start()
        sleep(1)
        if self.main_control.message_dialog:
            self.main_control.message_dialog.response(Gtk.ResponseType.CANCEL)
        self.refresh_gui()
        t1.join()

    def test_edit(self):
        self.main_control.add_comment()
        self.main_control.select_all()
        self.main_control.cut()
        self.main_control.copy()
        self.main_control.paste()
        self.main_control.delete()

        self.diagram.set_modified(False)
        self.main_window.work_area.close_tabs()
        self.main_control.add_comment()
        self.main_control.select_all()
        self.main_control.cut()
        self.main_control.copy()
        self.main_control.paste()
        self.main_control.delete()

    def test_properties(self):
        self.main_control.zoom_in()
        self.main_control.zoom_out()
        self.main_control.zoom_normal()
        self.main_control.align_bottom()
        self.main_control.align_top()
        self.main_control.align_left()
        self.main_control.align_right()
        self.main_control.collapse_all()
        self.main_control.uncollapse_all()

        self.diagram.set_modified(False)
        self.main_window.work_area.close_tabs()
        self.main_control.zoom_in()
        self.main_control.zoom_out()
        self.main_control.zoom_normal()
        self.main_control.align_bottom()
        self.main_control.align_top()
        self.main_control.align_left()
        self.main_control.align_right()
        self.main_control.collapse_all()
        self.main_control.uncollapse_all()

    def test_undo_redo(self):
        self.main_control.undo()
        self.main_control.redo()

        self.diagram.set_modified(False)
        self.main_window.work_area.close_tabs()
        self.main_control.undo()
        self.main_control.redo()

    def test_redraw_update(self):
        self.main_control.redraw(True)
        self.main_control.redraw(False)
        self.main_control.update_all()

    def test_add_delete_code_template(self):
        self.main_control.add_code_template(self.create_code_template())

        t1 = threading.Thread(target=self.main_control.delete_code_template,
                              args=(self.create_code_template(), ))
        t1.start()
        sleep(1)
        if self.main_control.message_dialog:
            self.main_control.message_dialog.response(Gtk.ResponseType.CANCEL)
        self.refresh_gui()
        t1.join()

        code_template = self.create_code_template()
        System.add_code_template(code_template)
        t1 = threading.Thread(target=self.main_control.delete_code_template,
                              args=(code_template, ))
        t1.start()
        sleep(1)
        if self.main_control.message_dialog:
            self.main_control.message_dialog.response(Gtk.ResponseType.CANCEL)
        self.refresh_gui()
        t1.join()

    def test_add__delete_port(self):
        self.main_control.add_port(self.create_port())

        t1 = threading.Thread(target=self.main_control.delete_port,
                              args=("port", ))
        t1.start()
        sleep(1)
        if self.main_control.message_dialog:
            self.main_control.message_dialog.response(Gtk.ResponseType.CANCEL)
        self.refresh_gui()
        t1.join()

    def test_add_delete_block(self):
        self.main_control.add_new_block(self.create_block())

        t1 = threading.Thread(target=self.main_control.delete_block,
                              args=(self.create_block(), ))
        t1.start()
        sleep(1)
        if self.main_control.message_dialog:
            self.main_control.message_dialog.response(Gtk.ResponseType.CANCEL)
        self.refresh_gui()
        t1.join()