home · contact · privacy
Browser: In TableConfig, only show attributes of more than one value.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 10 Nov 2024 21:22:18 +0000 (22:22 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 10 Nov 2024 21:22:18 +0000 (22:22 +0100)
browser.py

index b880b206abeddeba0c89be064ace13e4070c2e02..dbbff1d5f511685d52a7704e054278c77f200055 100755 (executable)
@@ -209,7 +209,7 @@ class SorterAndFiltererOrder:
     def __init__(self, as_list: list[SorterAndFilterer]) -> None:
         self._list = as_list
 
-    def __eq__(self, other):
+    def __eq__(self, other) -> bool:
         return self._list == other._list
 
     def __len__(self) -> int:
@@ -222,7 +222,7 @@ class SorterAndFiltererOrder:
         return self._list.__iter__()
 
     @staticmethod
-    def _list_from_store(store) -> list[SorterAndFilterer]:
+    def _list_from_store(store: Gio.ListStore) -> list[SorterAndFilterer]:
         order = []
         for i in range(store.get_n_items()):
             order += [store.get_item(i)]
@@ -256,6 +256,15 @@ class SorterAndFiltererOrder:
         """Create new, of equal order."""
         return self.__class__(self._list[:])
 
+    def sync_from(self, other_order: Self) -> None:
+        """Sync internal state from other order."""
+        self._list = other_order._list
+
+    def remove(self, sorter_name: str) -> None:
+        """Remove sorter of sorter_name from self."""
+        for sorter in [s for s in self._list if sorter_name == s.name]:
+            self._list.remove(sorter)
+
     def update_from_store(self, store: Gio.ListStore) -> None:
         """Update self from store."""
         self._list = self._list_from_store(store)
@@ -591,6 +600,11 @@ class GalleryConfig():
             sort_item: SorterAndFilterer = self._store.get_item(i)
             sort_item.widget.add_css_class('temp')
 
+    def sync_sorter(self, order: SorterAndFiltererOrder) -> None:
+        """Update .order and its dependencies from order."""
+        self.order.sync_from(order)
+        self.update_box()
+
     def update_box(self,
                    alt_order: Optional[SorterAndFiltererOrder] = None,
                    cur_selection: int = 0
@@ -642,6 +656,7 @@ class VerticalLabel(Gtk.DrawingArea):
 class Gallery:
     """Representation of GalleryItems below a directory."""
     update_config_box: Callable
+    sync_sorter: Callable
 
     def __init__(self,
                  on_hit_item: Callable,
@@ -747,6 +762,7 @@ class Gallery:
     def _make_basic_items_attrs(self,
                                 entries: list[GalleryItem]
                                 ) -> BasicItemsAttrs:
+
         basic_items_attrs = {}
         for attr_name in (s.name for s in self._sort_order):
             vals: set[str] = set()
@@ -803,8 +819,14 @@ class Gallery:
                     read_directory(path)
 
         read_directory(self._img_dir_path, make_parent=True)
-        self._basic_items_attrs = self._make_basic_items_attrs(
-                self.dir_entries)
+        basic_items_attrs = self._make_basic_items_attrs(self.dir_entries)
+        ignorable_attrs = []
+        for attr_name, attr_vals in basic_items_attrs.items():
+            if len(attr_vals) < 2:
+                ignorable_attrs += [attr_name]
+        for attr_name in ignorable_attrs:
+            self._sort_order.remove(attr_name)
+        self._basic_items_attrs = basic_items_attrs
         self._cache_db.write()
 
     @property
@@ -937,6 +959,7 @@ class Gallery:
                 for category in ('incl', 'semi', 'excl'):
                     final_values[category].sort()
                 self.items_attrs[attr_name] = final_values
+            self.sync_sorter(self._sort_order)
 
         def filter_entries(items_attrs: ItemsAttrs) -> list[GalleryItem]:
             entries_filtered: list[GalleryItem] = []
@@ -1228,6 +1251,7 @@ class MainWindow(Gtk.Window):
                 update_settings=self.gallery.update_settings,
                 items_attrs=self.gallery.items_attrs)
         self.gallery.update_config_box = self.conf.update_box
+        self.gallery.sync_sorter = self.conf.sync_sorter
         metadata_textview = Gtk.TextView(wrap_mode=Gtk.WrapMode.WORD_CHAR,
                                          editable=False)
         self.metadata = metadata_textview.get_buffer()