Exemple #1
0
class Main(GladeWrapper):
    def __init__(self):
        model = context.application.model
        model.connect("file-new", self.on_filename_changed)
        model.connect("file-opened", self.on_filename_changed)
        model.connect("file-saved", self.on_filename_changed)

        context.application.action_manager.connect("model-changed", self.on_model_changed)

        GladeWrapper.__init__(self, "zeobuilder.glade", "wi_main", "window")
        self.init_callbacks(self.__class__)
        self.init_proxies(["sw_nodes", "hp_main", "vb_main", "menubar"])

        # add model gui:
        self.init_tree_view()
        self.init_drawing_area()
        #self.update_window_title()

    def init_tree_view(self):
        self.filter_active = False
        self.filter_expression = Expression("True")

        # tree related widgets:
        self.tree_view = gtk.TreeView(context.application.model)
        self.sw_nodes.add(self.tree_view)
        self.tree_view.set_headers_visible(False)
        self.tree_view.connect("button-press-event", self.on_tree_view_button_press_event)
        self.tree_view.connect("row-collapsed", self.on_row_collapsed)

        self.tree_selection = self.tree_view.get_selection()
        self.tree_selection.set_mode(gtk.SELECTION_MULTIPLE)
        self.tree_selection.set_select_function(self.select_path)

        column = gtk.TreeViewColumn("")
        renderer_text = gtk.CellRendererText()
        column.pack_start(renderer_text, expand=False)
        def cell_data_func(column, cell, model, iter):
            cell.set_property("text", model.get_path(iter)[-1])
        column.set_cell_data_func(renderer_text, cell_data_func)
        self.tree_view.append_column(column)

        renderer_pixbuf = gtk.CellRendererPixbuf()
        self.column = gtk.TreeViewColumn()
        self.column.pack_start(renderer_pixbuf, expand=False)
        def render_icon(column, cell, model, iter):
            cell.set_property('pixbuf', model.get_value(iter, 0).icon)
        self.column.set_cell_data_func(renderer_pixbuf, render_icon)

        renderer_text = gtk.CellRendererText()
        self.column.pack_start(renderer_text, expand=False)
        def render_name(column, cell, model, iter):
            cell.set_property('text', model.get_value(iter, 0).get_name())
        self.column.set_cell_data_func(renderer_text, render_name)
        self.tree_view.append_column(self.column)
        self.tree_view.set_expander_column(self.column)

    def init_drawing_area(self):
        self.drawing_area = DrawingArea()
        self.hp_main.pack2(self.drawing_area, resize=True, shrink=False)

    # internal functions
    def update_window_title(self):
        filename = context.application.model.filename
        if filename is None:
            temp = "Unsaved Model"
        else:
            dirname = os.path.dirname(filename)
            homedirname = os.path.expanduser("~")
            dirname = dirname.replace(homedirname, "~")
            temp = "%s (%s)" % (os.path.basename(filename), dirname)
        if context.application.action_manager.model_changed():
            temp = "*" + temp
        temp = temp + " - " + context.title
        self.window.set_title(temp)

    def file_close_check(self):
        if context.application.action_manager.model_changed():
            result = nosave_cancel_save_question(
                "Do you want to save the model?",
                "The current file has not been saved. If you don't save the file, all changes will be lost."
            )
            if (result == gtk.RESPONSE_CANCEL) or (result == gtk.RESPONSE_DELETE_EVENT):
                # No close
                return False
            elif result == gtk.RESPONSE_OK:
                # Try to save, but if not saved, no close
                return self.file_save()
        # Close is accepted, no need to save
        return True

    def get_current_directory(self):
        name = context.application.model.filename
        if name is not None:
            return os.path.dirname(os.path.expanduser(name))

    # high level gui functions
    def file_new(self, universe, folder):
        if self.file_close_check():
            context.application.model.file_close()
            context.application.model.file_new(universe, folder)
            context.application.camera.reset()
            self.drawing_area.queue_draw()

    def file_open(self):
        if self.file_close_check():
            if run_file_dialog(
                context.application.file_open_dialog,
                context.application.model.file_open
            ):
                context.application.camera.reset()
                self.drawing_area.queue_draw()

    def file_save(self):
        if context.application.model.filename is None:
            return self.file_save_as()
        else:
            context.application.model.file_save()
            return True

    def file_save_as(self):
        run_file_dialog(
            context.application.file_save_dialog,
            context.application.model.file_save
        )

    def file_close(self):
        if self.file_close_check():
            context.application.model.file_close()
            return True
        else:
            return False

    def file_quit(self):
        "The user stops the program with the close item in the menu."
        if self.file_close():
            gtk.main_quit()

    def on_window_delete_event(self, widget, event):
        "The user closes the window."
        if self.file_close():
            gtk.main_quit()
        else:
            return True

    # INTERNAL handlers

    def on_filename_changed(self, model):
        self.update_window_title()

    def on_model_changed(self, action_manager):
        self.update_window_title()

    # selection stuff

    def select_path(self, path):
        node = context.application.model[path][0]
        is_selected = self.tree_selection.path_is_selected(path)
        try:
            if not (
                is_selected or (not self.filter_active) or
                self.filter_expression(node)
            ): return False
        except Exception, e:
            ok_error(
                "An error occured while evaluating the filter expression.",
                "This is probably due to a mistake in the expression you entered. The selection filter will be deactivated.\n\n%s\n%s" % (e.__class__, e)
            )
            self.filter_active = False
        node.set_selected(not is_selected)
        return True
Exemple #2
0
class Main(GladeWrapper):
    def __init__(self):
        model = context.application.model
        model.connect("file-new", self.on_filename_changed)
        model.connect("file-opened", self.on_filename_changed)
        model.connect("file-saved", self.on_filename_changed)

        context.application.action_manager.connect("model-changed",
                                                   self.on_model_changed)

        GladeWrapper.__init__(self, "zeobuilder.glade", "wi_main", "window")
        self.init_callbacks(self.__class__)
        self.init_proxies(["sw_nodes", "hp_main", "vb_main", "menubar"])

        # add model gui:
        self.init_tree_view()
        self.init_drawing_area()
        #self.update_window_title()

    def init_tree_view(self):
        self.filter_active = False
        self.filter_expression = Expression("True")

        # tree related widgets:
        self.tree_view = gtk.TreeView(context.application.model)
        self.sw_nodes.add(self.tree_view)
        self.tree_view.set_headers_visible(False)
        self.tree_view.connect("button-press-event",
                               self.on_tree_view_button_press_event)
        self.tree_view.connect("row-collapsed", self.on_row_collapsed)

        self.tree_selection = self.tree_view.get_selection()
        self.tree_selection.set_mode(gtk.SELECTION_MULTIPLE)
        self.tree_selection.set_select_function(self.select_path)

        column = gtk.TreeViewColumn("")
        renderer_text = gtk.CellRendererText()
        column.pack_start(renderer_text, expand=False)

        def cell_data_func(column, cell, model, iter):
            cell.set_property("text", model.get_path(iter)[-1])

        column.set_cell_data_func(renderer_text, cell_data_func)
        self.tree_view.append_column(column)

        renderer_pixbuf = gtk.CellRendererPixbuf()
        self.column = gtk.TreeViewColumn()
        self.column.pack_start(renderer_pixbuf, expand=False)

        def render_icon(column, cell, model, iter):
            cell.set_property('pixbuf', model.get_value(iter, 0).icon)

        self.column.set_cell_data_func(renderer_pixbuf, render_icon)

        renderer_text = gtk.CellRendererText()
        self.column.pack_start(renderer_text, expand=False)

        def render_name(column, cell, model, iter):
            cell.set_property('text', model.get_value(iter, 0).get_name())

        self.column.set_cell_data_func(renderer_text, render_name)
        self.tree_view.append_column(self.column)
        self.tree_view.set_expander_column(self.column)

    def init_drawing_area(self):
        self.drawing_area = DrawingArea()
        self.hp_main.pack2(self.drawing_area, resize=True, shrink=False)

    # internal functions
    def update_window_title(self):
        filename = context.application.model.filename
        if filename is None:
            temp = "Unsaved Model"
        else:
            dirname = os.path.dirname(filename)
            homedirname = os.path.expanduser("~")
            dirname = dirname.replace(homedirname, "~")
            temp = "%s (%s)" % (os.path.basename(filename), dirname)
        if context.application.action_manager.model_changed():
            temp = "*" + temp
        temp = temp + " - " + context.title
        self.window.set_title(temp)

    def file_close_check(self):
        if context.application.action_manager.model_changed():
            result = nosave_cancel_save_question(
                "Do you want to save the model?",
                "The current file has not been saved. If you don't save the file, all changes will be lost."
            )
            if (result
                    == gtk.RESPONSE_CANCEL) or (result
                                                == gtk.RESPONSE_DELETE_EVENT):
                # No close
                return False
            elif result == gtk.RESPONSE_OK:
                # Try to save, but if not saved, no close
                return self.file_save()
        # Close is accepted, no need to save
        return True

    def get_current_directory(self):
        name = context.application.model.filename
        if name is not None:
            return os.path.dirname(os.path.expanduser(name))

    # high level gui functions
    def file_new(self, universe, folder):
        if self.file_close_check():
            context.application.model.file_close()
            context.application.model.file_new(universe, folder)
            context.application.camera.reset()
            self.drawing_area.queue_draw()

    def file_open(self):
        if self.file_close_check():
            if run_file_dialog(context.application.file_open_dialog,
                               context.application.model.file_open):
                context.application.camera.reset()
                self.drawing_area.queue_draw()

    def file_save(self):
        if context.application.model.filename is None:
            return self.file_save_as()
        else:
            context.application.model.file_save()
            return True

    def file_save_as(self):
        run_file_dialog(context.application.file_save_dialog,
                        context.application.model.file_save)

    def file_close(self):
        if self.file_close_check():
            context.application.model.file_close()
            return True
        else:
            return False

    def file_quit(self):
        "The user stops the program with the close item in the menu."
        if self.file_close():
            gtk.main_quit()

    def on_window_delete_event(self, widget, event):
        "The user closes the window."
        if self.file_close():
            gtk.main_quit()
        else:
            return True

    # INTERNAL handlers

    def on_filename_changed(self, model):
        self.update_window_title()

    def on_model_changed(self, action_manager):
        self.update_window_title()

    # selection stuff

    def select_path(self, path):
        node = context.application.model[path][0]
        is_selected = self.tree_selection.path_is_selected(path)
        try:
            if not (is_selected or
                    (not self.filter_active) or self.filter_expression(node)):
                return False
        except Exception, e:
            ok_error(
                "An error occured while evaluating the filter expression.",
                "This is probably due to a mistake in the expression you entered. The selection filter will be deactivated.\n\n%s\n%s"
                % (e.__class__, e))
            self.filter_active = False
        node.set_selected(not is_selected)
        return True
Exemple #3
0
 def init_drawing_area(self):
     self.drawing_area = DrawingArea()
     self.hp_main.pack2(self.drawing_area, resize=True, shrink=False)
Exemple #4
0
 def init_drawing_area(self):
     self.drawing_area = DrawingArea()
     self.hp_main.pack2(self.drawing_area, resize=True, shrink=False)