gui: simplify the Config's singleton pattern
Use __new__ instead of instance(), this way we can just use the same config object as Config().do_something. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>k
This commit is contained in:
parent
64fcf04e32
commit
10c34bc655
|
@ -23,11 +23,15 @@ logger = logging.getLogger('tuhi.gui.config')
|
||||||
|
|
||||||
|
|
||||||
class Config(GObject.Object):
|
class Config(GObject.Object):
|
||||||
_config_obj = None
|
_instance = None
|
||||||
_base_path = None
|
_base_path = None
|
||||||
|
|
||||||
def __init__(self):
|
def __new__(cls):
|
||||||
super().__init__()
|
if cls._instance is None:
|
||||||
|
cls._instance = super(Config, cls).__new__(cls)
|
||||||
|
|
||||||
|
self = cls._instance
|
||||||
|
self.__init__() # for GObject to initialize
|
||||||
self.path = Path(self._base_path, 'tuhigui.ini')
|
self.path = Path(self._base_path, 'tuhigui.ini')
|
||||||
self.base_path = self._base_path
|
self.base_path = self._base_path
|
||||||
self.config = configparser.ConfigParser()
|
self.config = configparser.ConfigParser()
|
||||||
|
@ -36,6 +40,7 @@ class Config(GObject.Object):
|
||||||
self._drawings = []
|
self._drawings = []
|
||||||
self._load()
|
self._load()
|
||||||
self._load_cached_drawings()
|
self._load_cached_drawings()
|
||||||
|
return cls._instance
|
||||||
|
|
||||||
def _load(self):
|
def _load(self):
|
||||||
if not self.path.exists():
|
if not self.path.exists():
|
||||||
|
@ -122,14 +127,8 @@ class Config(GObject.Object):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def set_base_path(cls, path):
|
def set_base_path(cls, path):
|
||||||
if cls._config_obj is not None:
|
if cls._instance is not None:
|
||||||
logger.error('Trying to set config base path but we already have the singleton object')
|
logger.error('Trying to set config base path but we already have the singleton object')
|
||||||
return
|
return
|
||||||
|
|
||||||
cls._base_path = Path(path)
|
cls._base_path = Path(path)
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def instance(cls):
|
|
||||||
if cls._config_obj is None:
|
|
||||||
cls._config_obj = Config()
|
|
||||||
return cls._config_obj
|
|
||||||
|
|
|
@ -37,8 +37,8 @@ class Drawing(Gtk.EventBox):
|
||||||
|
|
||||||
def __init__(self, json_data, *args, **kwargs):
|
def __init__(self, json_data, *args, **kwargs):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.orientation = Config.instance().orientation
|
self.orientation = Config().orientation
|
||||||
Config.instance().connect('notify::orientation', self._on_orientation_changed)
|
Config().connect('notify::orientation', self._on_orientation_changed)
|
||||||
DATA_PATH.mkdir(parents=True, exist_ok=True)
|
DATA_PATH.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
self.json_data = json_data
|
self.json_data = json_data
|
||||||
|
@ -127,7 +127,7 @@ class Drawing(Gtk.EventBox):
|
||||||
|
|
||||||
@Gtk.Template.Callback('_on_delete_button_clicked')
|
@Gtk.Template.Callback('_on_delete_button_clicked')
|
||||||
def _on_delete_button_clicked(self, button):
|
def _on_delete_button_clicked(self, button):
|
||||||
Config.instance().delete_drawing(self.timestamp)
|
Config().delete_drawing(self.timestamp)
|
||||||
|
|
||||||
@Gtk.Template.Callback('_on_rotate_button_clicked')
|
@Gtk.Template.Callback('_on_rotate_button_clicked')
|
||||||
def _on_rotate_button_clicked(self, button):
|
def _on_rotate_button_clicked(self, button):
|
||||||
|
|
|
@ -80,7 +80,7 @@ class DrawingPerspective(Gtk.Stack):
|
||||||
# The config backend filters duplicates anyway, so don't care here
|
# The config backend filters duplicates anyway, so don't care here
|
||||||
for ts in self.device.drawings_available:
|
for ts in self.device.drawings_available:
|
||||||
json_string = self.device.json(ts)
|
json_string = self.device.json(ts)
|
||||||
Config.instance().add_drawing(ts, json_string)
|
Config().add_drawing(ts, json_string)
|
||||||
|
|
||||||
def _update_drawings(self, config, pspec):
|
def _update_drawings(self, config, pspec):
|
||||||
def _hash(drawing):
|
def _hash(drawing):
|
||||||
|
@ -137,9 +137,9 @@ class DrawingPerspective(Gtk.Stack):
|
||||||
# json itself (once cached) that we then actually use for SVG
|
# json itself (once cached) that we then actually use for SVG
|
||||||
# generation.
|
# generation.
|
||||||
device.connect('notify::drawings-available', self._cache_drawings)
|
device.connect('notify::drawings-available', self._cache_drawings)
|
||||||
Config.instance().connect('notify::drawings', self._update_drawings)
|
Config().connect('notify::drawings', self._update_drawings)
|
||||||
|
|
||||||
self._update_drawings(Config.instance(), None)
|
self._update_drawings(Config(), None)
|
||||||
|
|
||||||
# We always want to sync on startup
|
# We always want to sync on startup
|
||||||
logger.debug(f'{device.name} - starting to listen')
|
logger.debug(f'{device.name} - starting to listen')
|
||||||
|
@ -179,5 +179,5 @@ class DrawingPerspective(Gtk.Stack):
|
||||||
|
|
||||||
@Gtk.Template.Callback('_on_undo_clicked')
|
@Gtk.Template.Callback('_on_undo_clicked')
|
||||||
def _on_undo_clicked(self, button):
|
def _on_undo_clicked(self, button):
|
||||||
Config.instance().undelete_drawing(button.deleted_drawing)
|
Config().undelete_drawing(button.deleted_drawing)
|
||||||
self.overlay_undo.set_reveal_child(False)
|
self.overlay_undo.set_reveal_child(False)
|
||||||
|
|
|
@ -136,7 +136,7 @@ class MainWindow(Gtk.ApplicationWindow):
|
||||||
action = Gio.SimpleAction.new_stateful('orientation', GLib.VariantType('s'),
|
action = Gio.SimpleAction.new_stateful('orientation', GLib.VariantType('s'),
|
||||||
GLib.Variant('s', 'landscape'))
|
GLib.Variant('s', 'landscape'))
|
||||||
action.connect('activate', self._on_orientation_changed)
|
action.connect('activate', self._on_orientation_changed)
|
||||||
action.set_state(GLib.Variant.new_string(Config.instance().orientation))
|
action.set_state(GLib.Variant.new_string(Config().orientation))
|
||||||
self.add_action(action)
|
self.add_action(action)
|
||||||
|
|
||||||
builder = Gtk.Builder.new_from_string(MENU_XML, -1)
|
builder = Gtk.Builder.new_from_string(MENU_XML, -1)
|
||||||
|
@ -234,7 +234,7 @@ class MainWindow(Gtk.ApplicationWindow):
|
||||||
|
|
||||||
def _on_orientation_changed(self, action, label):
|
def _on_orientation_changed(self, action, label):
|
||||||
action.set_state(label)
|
action.set_state(label)
|
||||||
Config.instance().orientation = label.get_string() # this is a GVariant
|
Config().orientation = label.get_string() # this is a GVariant
|
||||||
|
|
||||||
@Gtk.Template.Callback('_on_zoom_changed')
|
@Gtk.Template.Callback('_on_zoom_changed')
|
||||||
def _on_zoom_changed(self, adjustment):
|
def _on_zoom_changed(self, adjustment):
|
||||||
|
|
Loading…
Reference in New Issue