Example #1
0
 def __init__(self, parent):
     self.parent = parent
     self.opt = parent.opt
     # We will edit the macros over our own instance of MacroManager,
     # ensuring that all validations take place. After, we copy the
     # changed macros back to the main MacroManager.
     self.macros = parent.macros
     self.temp_macros = MacroManager()
     self._load_interface()
     self._set_widget_values()
Example #2
0
class PreferencesDialog(BaseDialog):

    def __init__(self, parent):
        self.parent = parent
        self.opt = parent.opt
        # We will edit the macros over our own instance of MacroManager,
        # ensuring that all validations take place. After, we copy the
        # changed macros back to the main MacroManager.
        self.macros = parent.macros
        self.temp_macros = MacroManager()
        self._load_interface()
        self._set_widget_values()
        
    def show(self):
        self.dlg.set_transient_for(self.parent.window)
        self.dlg.set_modal(True)
        self.dlg.show()

    def _load_interface(self):
        self.ui_init("preferences-dialog.ui")
        self.dlg = self.ui.get_object("dlgPreferences")
        self.spFormatTime = self.ui.get_object("spFormatTime")
        self.spSaveTime = self.ui.get_object("spSaveTime")
        self.spUndoLevels = self.ui.get_object("spUndoLevels")
        self.spHistorySize = self.ui.get_object("spHistorySize")
        self.cbShowEdit = self.ui.get_object("cbShowEdit")
        self.cbShowSidebar = self.ui.get_object("cbShowSidebar")
        self.clbStandard = self.ui.get_object("clbStandard")
        self.clbHeader1 = self.ui.get_object("clbHeader1")
        self.clbHeader2 = self.ui.get_object("clbHeader2")
        self.clbHeader3 = self.ui.get_object("clbHeader3")
        self.clbCode = self.ui.get_object("clbCode")
        self.clbLink = self.ui.get_object("clbLink")
        self.clbNewLink = self.ui.get_object("clbNewLink")
        self.clbFormat = self.ui.get_object("clbFormat")
        self.clbURL = self.ui.get_object("clbURL")
        self.fbStandard = self.ui.get_object("fbStandard")
        self.fbHeader1 = self.ui.get_object("fbHeader1")
        self.fbHeader2 = self.ui.get_object("fbHeader2")
        self.fbHeader3 = self.ui.get_object("fbHeader3")
        self.fbCode = self.ui.get_object("fbCode")
        self.fbLink = self.ui.get_object("fbLink")
        self.fbNewLink = self.ui.get_object("fbNewLink")
        self.fbFormat = self.ui.get_object("fbFormat")
        self.fbURL = self.ui.get_object("fbURL")
        self.btAddMacro = self.ui.get_object("btAddMacro")
        self.btDeleteMacro = self.ui.get_object("btDeleteMacro")
        self.txMacroName = self.ui.get_object("txMacroName")
        self.txMacroValue = self.ui.get_object("txMacroValue")
        self.rbOpenToday = self.ui.get_object("rbOpenToday")
        self.rbOpenIndex = self.ui.get_object("rbOpenIndex")
        self.rbOpenLast = self.ui.get_object("rbOpenLast")
        self.rbOpenOther = self.ui.get_object("rbOpenOther")
        self.txOpenPageName = self.ui.get_object("txOpenPageName")

        self.lsMacros = self.ui.get_object("lsMacros")
        self.macro_store = gtk.ListStore(str, str)

        self.lsMacros.set_model(self.macro_store)
        self.clMacroName = gtk.TreeViewColumn("Name")
        self.clMacroValue = gtk.TreeViewColumn("Value")
        self.lsMacros.append_column(self.clMacroName)
        self.lsMacros.append_column(self.clMacroValue)
        self.rdrMacroName = gtk.CellRendererText()
        self.clMacroName.pack_start(self.rdrMacroName, True)
        self.clMacroName.add_attribute(self.rdrMacroName, "text", 0)
        self.clMacroName.set_property("sizing", gtk.TREE_VIEW_COLUMN_AUTOSIZE)
        self.rdrMacroValue = gtk.CellRendererText()
        self.clMacroValue.pack_start(self.rdrMacroValue, True)
        self.clMacroValue.add_attribute(self.rdrMacroValue, "text", 1)
        self.clMacroValue.set_property("sizing", gtk.TREE_VIEW_COLUMN_AUTOSIZE)

        self.txNewPageTemplate = self.ui.get_object("txNewPageTemplate")
        self.txRedirectPageTemplate = self.ui.get_object("txRedirectPageTemplate")

        self.ui.connect_signals(self)
        
    def on_cmd_ok(self, widget = None, data = None):
        pagename = self.txOpenPageName.get_text().strip()
        if self.rbOpenOther.get_active() and len(pagename) < 1:
            error_dialog(self.dlg, "No page name was given")
            self.txOpenPageName.grab_focus()
            return
        self._save_widget_values()
        self.dlg.destroy()
        self.parent.update_options()
        
    def on_cmd_cancel(self, widget = None, data = None):
        self.dlg.destroy()
        return False

    def on_cmd_add_macro(self, widget = None, data = None):
        name = self.txMacroName.get_text()
        value = self.txMacroValue.get_text()
        self.temp_macros.add(name, value)
        self.txMacroName.set_text("")
        self.txMacroValue.set_text("")
        self.txMacroName.grab_focus()
        self._update_macros_list_model()

    def on_cmd_delete_macro(self, widget = None, data = None):
        name = self.txMacroName.get_text()
        self.temp_macros.remove(name)
        self.txMacroName.set_text("")
        self.txMacroValue.set_text("")
        self.txMacroName.grab_focus()
        self._update_macros_list_model()

    def on_cmd_select_macro(self, widget = None, data = None):
        model, iter = self.lsMacros.get_selection().get_selected()
        if iter:
            self.txMacroName.set_text(model.get_value(iter, 0))
            self.txMacroValue.set_text(model.get_value(iter, 1))

    def on_cmd_restore_default_templates(self, widget = None, data = None):
        self.txNewPageTemplate.get_buffer().set_text(
            self.parent.DEFAULT_NEW_PAGE_TEMPLATE)
        self.txRedirectPageTemplate.get_buffer().set_text(
            self.parent.DEFAULT_REDIRECT_PAGE_TEMPLATE)

    def on_rbStartup(self, widget = None, data = None):
        self.txOpenPageName.set_property("sensitive",
            self.rbOpenOther.get_active())

    def _update_macros_list_model(self):
        self.macro_store.clear()
        for name, value in self.temp_macros.iterate():
            self.macro_store.append((name, value))

    def _set_widget_values(self):
        self.spFormatTime.set_value(self.opt.get_int("format_time"))
        self.spSaveTime.set_value(self.opt.get_int("save_time"))
        self.spUndoLevels.set_value(self.opt.get_int("undo_levels"))
        self.spHistorySize.set_value(self.opt.get_int("max_history"))
        self.cbShowEdit.set_active(self.opt.get_bool("show_edit_buttons"))
        self.cbShowSidebar.set_active(self.opt.get_bool("show_sidebar"))

        self.clbStandard.set_color(self.opt.get_color("std_color"))
        self.clbHeader1.set_color(self.opt.get_color("header1_color"))
        self.clbHeader2.set_color(self.opt.get_color("header2_color"))
        self.clbHeader3.set_color(self.opt.get_color("header3_color"))
        self.clbCode.set_color(self.opt.get_color("code_color"))
        self.clbLink.set_color(self.opt.get_color("link_color"))
        self.clbNewLink.set_color(self.opt.get_color("new_link_color"))
        self.clbFormat.set_color(self.opt.get_color("format_color"))
        self.clbURL.set_color(self.opt.get_color("url_link_color"))

        self.fbStandard.set_font_name(self.opt.get_str("std_font"))
        self.fbHeader1.set_font_name(self.opt.get_str("header1_font"))
        self.fbHeader2.set_font_name(self.opt.get_str("header2_font"))
        self.fbHeader3.set_font_name(self.opt.get_str("header3_font"))
        self.fbCode.set_font_name(self.opt.get_str("code_font"))
        self.fbLink.set_font_name(self.opt.get_str("link_font"))
        self.fbNewLink.set_font_name(self.opt.get_str("new_link_font"))
        self.fbFormat.set_font_name(self.opt.get_str("format_font"))
        self.fbURL.set_font_name(self.opt.get_str("url_link_font"))
        
        self.txOpenPageName.set_text(self.opt.get_str("startup_other"))
        sp = self.opt.get_int("startup_page")
        if sp == self.parent.STARTUP_PAGE_INDEX:
            self.rbOpenIndex.set_active(True)
        elif sp == self.parent.STARTUP_PAGE_OTHER:
            self.rbOpenOther.set_active(True)
        elif sp == self.parent.STARTUP_PAGE_LAST:
            self.rbOpenLast.set_active(True)
        else:
            self.rbOpenToday.set_active(True)
        self.on_rbStartup()

        for name, value in self.macros.iterate():
            self.temp_macros.add(name, value)
        self._update_macros_list_model()

        self.txNewPageTemplate.get_buffer().set_text(
            self.opt.get_str("new_page_template"))
        self.txRedirectPageTemplate.get_buffer().set_text(
            self.opt.get_str("redirect_page_template"))
        
    def _save_widget_values(self):
        self.opt.set_int("format_time", self.spFormatTime.get_value_as_int())
        self.opt.set_int("save_time", self.spSaveTime.get_value_as_int())
        self.opt.set_int("undo_levels", self.spUndoLevels.get_value_as_int())
        self.opt.set_int("max_history", self.spHistorySize.get_value_as_int())
        self.opt.set_bool("show_edit_buttons", self.cbShowEdit.get_active())
        self.opt.set_bool("show_sidebar", self.cbShowSidebar.get_active())

        self.opt.set_color("std_color", self.clbStandard.get_color())
        self.opt.set_color("header1_color", self.clbHeader1.get_color())
        self.opt.set_color("header2_color", self.clbHeader2.get_color())
        self.opt.set_color("header3_color", self.clbHeader3.get_color())
        self.opt.set_color("code_color", self.clbCode.get_color())
        self.opt.set_color("link_color", self.clbLink.get_color())
        self.opt.set_color("new_link_color", self.clbNewLink.get_color())
        self.opt.set_color("format_color", self.clbFormat.get_color())
        self.opt.set_color("url_link_color", self.clbURL.get_color())

        self.opt.set_str("std_font", self.fbStandard.get_font_name())
        self.opt.set_str("header1_font", self.fbHeader1.get_font_name())
        self.opt.set_str("header2_font", self.fbHeader2.get_font_name())
        self.opt.set_str("header3_font", self.fbHeader3.get_font_name())
        self.opt.set_str("code_font", self.fbCode.get_font_name())
        self.opt.set_str("link_font", self.fbLink.get_font_name())
        self.opt.set_str("new_link_font", self.fbNewLink.get_font_name())
        self.opt.set_str("format_font", self.fbFormat.get_font_name())
        self.opt.set_str("url_link_font", self.fbURL.get_font_name())

        sp = self.parent.STARTUP_PAGE_TODAY
        if self.rbOpenIndex.get_active():
            sp = self.parent.STARTUP_PAGE_INDEX
        if self.rbOpenLast.get_active():
            sp = self.parent.STARTUP_PAGE_LAST
        elif self.rbOpenOther.get_active():
            sp = self.parent.STARTUP_PAGE_OTHER
        self.opt.set_int("startup_page", sp)
        self.opt.set_str("startup_other", self.txOpenPageName.get_text())

        self.macros.clear()
        for name, value in self.temp_macros.iterate():
            self.macros.add(name, value)
        self.opt.set_str("macros", self.macros.dump_string())

        buf = self.txNewPageTemplate.get_buffer()
        start, end = buf.get_bounds()
        self.opt.set_str("new_page_template",
            buf.get_text(start, end).decode("utf-8"))

        buf = self.txRedirectPageTemplate.get_buffer()
        start, end = buf.get_bounds()
        self.opt.set_str("redirect_page_template",
            buf.get_text(start, end).decode("utf-8"))