def __init__ (self, bus, engine, datadir, gettext_package): self.__config = Config(bus, engine, self.on_value_changed) ui_file = GLib.build_filenamev([datadir, "setup.ui"]) self.__builder = Gtk.Builder() self.__builder.set_translation_domain(gettext_package) self.__builder.add_from_file(ui_file) for option in options: prepare_func = getattr(self, "prepare_%s" % option["widget"]) prepare_func(option) self.__window = self.__builder.get_object("setup_dialog") self.__window.set_title(titles[engine]) try: # Pretend to be a GNOME Control Center dialog if appropriate self.gnome_cc_xid = int(GLib.getenv('GNOME_CONTROL_CENTER_XID')) self.__window.set_wmclass('gnome-control-center', 'Gnome-control-center') self.__window.set_modal(True) self.__window.connect('notify::window', self.set_transient) self.__window.set_type_hint(Gdk.WindowTypeHint.DIALOG) except: # No problem here, we're just a normal dialog pass self.__window.show()
def __init__(self, bus, engine): self.__config = Config(bus, engine, self.on_value_changed) ui_file = GLib.build_filenamev([datadir, "setup.ui"]) self.__builder = Gtk.Builder() self.__builder.set_translation_domain(gettext_package) self.__builder.add_from_file(ui_file) for option in options: if option["name"] == "punctuation_chars": self.__prepare_combo(option) else: self.__prepare_button(option) self.__window = self.__builder.get_object("setup_dialog") self.__window.set_title("%s settings" % engine.capitalize()) self.__window.show()
class Setup(object): def __init__(self, bus, engine): self.__config = Config(bus, engine, self.on_value_changed) ui_file = GLib.build_filenamev([datadir, "setup.ui"]) self.__builder = Gtk.Builder() self.__builder.set_translation_domain(gettext_package) self.__builder.add_from_file(ui_file) for option in options: if option["name"] == "punctuation_chars": self.__prepare_combo(option) else: self.__prepare_button(option) self.__window = self.__builder.get_object("setup_dialog") self.__window.set_title("%s settings" % engine.capitalize()) self.__window.show() def __prepare_button(self, option): """Prepare a Gtk.CheckButton Set the button named `option['name']` (in)active based on the current engine config value. """ name = option["name"] button = self.__builder.get_object(name) v = self.__config.read(name) button.set_active(v.unpack()) button.connect("toggled", self.on_widget_changed, name, option["type"]) setattr(self, name, button) def __prepare_combo(self, option): """Prepare a Gtk.ComboBox Set the combobox named `name` to the current engine config value, or to the provided `default_value` as a fallback. """ name = option["name"] values = option["values"] combo = self.__builder.get_object(name) store = Gtk.ListStore(*[type(v) for v in values[0]]) for n, v in values: store.append((n, v)) combo.set_model(store) cell = Gtk.CellRendererText() combo.pack_start(cell, True) combo.add_attribute(cell, "text", 1) v = self.__config.read(name) combo.set_active(v.unpack()) combo.connect("changed", self.on_widget_changed, name, option["type"]) setattr(self, name, combo) def run(self): res = self.__window.run() self.__window.destroy() def on_value_changed(self, config, section, name, value, data): """Callback when the value of a widget is changed. We need to react, in case the value was changed from somewhere else, for example from another setup UI. """ if section != self.__config.config_section: return value = value.unpack() widget = getattr(self, name) if widget.get_active() != value: widget.set_active(value) def on_widget_changed(self, widget, setting_name, variant_type): self.__config.write(setting_name, GLib.Variant(variant_type, widget.get_active()))
class Setup(object): def __init__ (self, bus, engine, datadir, gettext_package): self.__config = Config(bus, engine, self.on_value_changed) ui_file = GLib.build_filenamev([datadir, "setup.ui"]) self.__builder = Gtk.Builder() self.__builder.set_translation_domain(gettext_package) self.__builder.add_from_file(ui_file) for option in options: prepare_func = getattr(self, "prepare_%s" % option["widget"]) prepare_func(option) self.__window = self.__builder.get_object("setup_dialog") self.__window.set_title(titles[engine]) try: # Pretend to be a GNOME Control Center dialog if appropriate self.gnome_cc_xid = int(GLib.getenv('GNOME_CONTROL_CENTER_XID')) self.__window.set_wmclass('gnome-control-center', 'Gnome-control-center') self.__window.set_modal(True) self.__window.connect('notify::window', self.set_transient) self.__window.set_type_hint(Gdk.WindowTypeHint.DIALOG) except: # No problem here, we're just a normal dialog pass self.__window.show() def set_transient(self, obj, pspec): from gi.repository import GdkX11 window = self.__window.get_window() if window != None: parent = GdkX11.X11Window.foreign_new_for_display( Gdk.Display.get_default(), self.gnome_cc_xid) if parent != None: window.set_transient_for(parent) def prepare_switch(self, option): """Prepare a Gtk.Switch Set the switch named `option["name"]` (in)active based on the current engine config value. """ name = option["name"] switch = self.__builder.get_object(name) v = self.__config.read(name) switch.set_active(v.unpack()) switch.connect("notify::active", self.on_switch_toggled, name, option["type"]) setattr(self, name, switch) def prepare_combo(self, option): """Prepare a Gtk.ComboBox Set the combobox named `option['name']` to the current engine config value. """ name = option["name"] current_value = self.__config.read(name).unpack() combo = self.__builder.get_object(name) store = combo.get_model() active_index = self.__get_active_index(store, current_value) combo.set_active(active_index) combo.connect("changed", self.on_combo_changed, name, option) setattr(self, name, combo) def __get_active_index(self, store, value): for i, n in enumerate(store): v = store.get_value(store.get_iter(i), 0) if v == value: return i def run(self): res = self.__window.run() self.__window.destroy() def on_value_changed(self, config, section, name, value, data): """Callback when the value of a widget is changed. We need to react, in case the value was changed from somewhere else, for example from another setup UI. """ if section != self.__config.config_section: return try: widget = getattr(self, name) except AttributeError: return value = value.unpack() if isinstance(widget, Gtk.ComboBox): store = widget.get_model() v = store.get_value(store.get_iter(widget.get_active()), 0) if v != value: widget.set_active(self.__get_active_index(widget.get_model(), value)) else: if widget.get_active() != value: widget.set_active(value) def on_switch_toggled(self, widget, active, setting_name, variant_type): self.__config.write(setting_name, GLib.Variant(variant_type, widget.get_active())) def on_combo_changed(self, widget, setting_name, option): store = widget.get_model() value = store.get_value(store.get_iter(widget.get_active()), 0) self.__config.write(setting_name, GLib.Variant(option["type"], value))