emmental/emmental/header/settings.py
Anna Schumaker 4cd1e89493 header/dialog: Create a Settings Dialog
This dialog is used to manually edit the settings in the database. I
bind the properties in such a way that changes are seen instantly.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
2023-04-12 10:41:42 -04:00

67 lines
2.8 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""A custom Gtk.Dialog for showing Settings."""
from gi.repository import Gtk
from gi.repository import Adw
from .. import db
from .. import entry
from .. import factory
class ValueRow(factory.ListRow):
"""A Row for displaying settings values."""
def do_bind(self) -> None:
"""Bind a db.Setting to this Row."""
if isinstance(self.item.value, bool):
self.child = Gtk.Switch(halign=Gtk.Align.START)
self.bind_and_set_property("value", "active", bidirectional=True)
elif isinstance(self.item.value, str):
self.child = entry.String(has_frame=False)
self.bind_and_set_property("value", "value", bidirectional=True)
elif isinstance(self.item.value, int):
self.child = entry.Integer(has_frame=False)
self.bind_and_set_property("value", "value", bidirectional=True)
elif isinstance(self.item.value, float):
self.child = entry.Float(has_frame=False)
self.bind_and_set_property("value", "value", bidirectional=True)
class Window(Adw.Window):
"""A custom window that displays the current settings."""
def __init__(self, sql: db.Connection):
"""Initialize the Settings window."""
super().__init__(default_width=500, default_height=500,
title="Emmental Settings", icon_name="settings",
hide_on_close=True,
content=Gtk.Box.new(Gtk.Orientation.VERTICAL, 0))
self._search = entry.Filter(what="settings")
self._header = Gtk.HeaderBar(title_widget=self._search)
self._selection = Gtk.NoSelection(model=sql.settings)
self._view = Gtk.ColumnView(model=self._selection,
show_row_separators=True)
self._scroll = Gtk.ScrolledWindow(child=self._view, vexpand=True)
self._scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
self.__append_column(factory.InscriptionFactory("key"),
"Key", width=400)
self.__append_column(factory.Factory(row_type=ValueRow),
"Value", width=100)
self.get_content().append(self._header)
self.get_content().append(self._scroll)
if __debug__:
self.add_css_class("devel")
self._search.connect("search-changed", self.__filter)
def __append_column(self, factory: factory.Factory,
title: str, *, width: int) -> None:
self._view.append_column(Gtk.ColumnViewColumn(factory=factory,
title=title,
fixed_width=width))
def __filter(self, entry: entry.Filter) -> None:
self._selection.get_model().filter(entry.get_query())