home · contact · privacy
In Browser, enable sorting by seed rather than age.
authorChristian Heller <c.heller@plomlompom.de>
Sat, 24 Aug 2024 00:02:59 +0000 (02:02 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 24 Aug 2024 00:02:59 +0000 (02:02 +0200)
browser.py

index 81b089b8b4874786adc83526a69caf2b88c26358..10fecfb95df3ff20fb615980f1029de6d9e065ec 100755 (executable)
@@ -19,11 +19,14 @@ class FileItem(GObject.GObject):
         super().__init__()
         self.name = info.get_name()
         self.last_mod_time = info.get_modification_date_time().format_iso8601()
+        self.seed = ''
         self.full_path = path_join(path, self.name)
         self.metadata = ''
         if self.full_path in cache:
             if self.last_mod_time in cache[self.full_path]:
-                self.metadata = cache[self.full_path][self.last_mod_time]
+                cached = cache[self.full_path][self.last_mod_time]
+                self.seed = cached[0]
+                self.metadata = cached[1]
 
     def set_metadata(self, et):
         self.metadata = 'no SD comment'
@@ -33,6 +36,8 @@ class FileItem(GObject.GObject):
                     self.metadata = ''
                     gen_params = GenParams.from_str(v)
                     for k, v_ in gen_params.as_dict.items():
+                        if k == 'seed':
+                            self.seed = v_
                         self.metadata += f'\n{k}: {v_}'
 
 
@@ -42,10 +47,10 @@ class Window(Gtk.ApplicationWindow):
         super().__init__(**kwargs)
 
         box_buttons = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
-        for args in (('oldest', lambda _: self.move_selection(None, 0)),
-                     ('older', lambda _: self.move_selection(-1, None)),
-                     ('newer', lambda _: self.move_selection(1, None)),
-                     ('newest', lambda _: self.move_selection(None, -1))):
+        for args in (('first', lambda _: self.move_selection(None, 0)),
+                     ('prev', lambda _: self.move_selection(-1, None)),
+                     ('next', lambda _: self.move_selection(1, None)),
+                     ('last', lambda _: self.move_selection(None, -1))):
             btn = Gtk.Button(label=args[0])
             btn.connect('clicked', args[1])
             box_buttons.append(btn)
@@ -58,15 +63,25 @@ class Window(Gtk.ApplicationWindow):
         self.viewer.append(self.metadata)
         self.viewer.append(self.label_nothing_to_show)
 
+        dir_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
+
+        btn_sort_date = Gtk.Button(label='by age')
+        btn_sort_date.connect('clicked', lambda _: self.sort('last_mod_time'))
+        dir_box.append(btn_sort_date)
+        btn_sort_seed = Gtk.Button(label='by seed')
+        btn_sort_seed.connect('clicked', lambda _: self.sort('seed'))
+        dir_box.append(btn_sort_seed)
+
         img_dir_absolute = abspath(IMG_DIR)
         self.dir = Gio.File.new_for_path(img_dir_absolute)
         self.list_store = Gio.ListStore(item_type=FileItem)
         self.selection = Gtk.SingleSelection.new(self.list_store)
         factory = Gtk.SignalListItemFactory()
         self.selector = Gtk.ListView(model=self.selection, factory=factory)
+        dir_box.append(self.selector)
 
         box_outer = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
-        box_outer.append(self.selector)
+        box_outer.append(dir_box)
         box_outer.append(self.viewer)
         self.props.child = box_outer
 
@@ -85,24 +100,26 @@ class Window(Gtk.ApplicationWindow):
         query_attrs = 'standard::name,standard::content-type,time::*'
         enumerator = self.dir.enumerate_children(
                 query_attrs, Gio.FileQueryInfoFlags.NONE, None)
-        to_sort = []
+        self.unsorted = []
         for info in [info for info in enumerator
                      if info.get_content_type().startswith('image/')]:
             item = FileItem(img_dir_absolute, info, cache)
-            to_sort += [item]
+            self.unsorted += [item]
         with ExifToolHelper() as et:
-            for item in [item for item in to_sort if not item.metadata]:
-                if not item.metadata:
-                    item.set_metadata(et)
-                    cache[item.full_path] = {item.last_mod_time: item.metadata}
-        to_sort.sort(key=lambda i: i.last_mod_time)
-        for file_item in to_sort:
-            self.list_store.append(file_item)
-        self.max_index = len(self.list_store) - 1
-        self.update_selected()
+            for item in [item for item in self.unsorted if not item.metadata]:
+                item.set_metadata(et)
+                cache[item.full_path] = {item.last_mod_time: [item.seed, item.metadata]}
+        self.max_index = len(self.unsorted) - 1
+        self.sort('last_mod_time')
         with open('cache.json', 'w') as f:
             json_dump(cache, f)
 
+    def sort(self, attr_name):
+        self.list_store.remove_all()
+        for file_item in sorted(self.unsorted, key=lambda i: getattr(i, attr_name)):
+            self.list_store.append(file_item)
+        self.update_selected()
+
     def update_selected(self, *_args):
         self.item = self.selection.props.selected_item
         self.reload()