From: Christian Heller Date: Thu, 12 Sep 2024 17:59:47 +0000 (+0200) Subject: In browser.py, to sort attribute listing add diversity count. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7Bdb.prefix%7D%7D/index.html?a=commitdiff_plain;h=b715b2ed1acf19190acf426360f7736f97f205ca;p=stable_plom In browser.py, to sort attribute listing add diversity count. --- diff --git a/browser.py b/browser.py index 792fc4b..d95eb10 100755 --- a/browser.py +++ b/browser.py @@ -200,7 +200,9 @@ class MainWindow(Gtk.Window): self.update_gallery() item.props.item.box = item - label = f'{item.props.item.name} ' + sorter_name = item.props.item.name + diversity = self.sort_attribute_diversities.get(sorter_name, 0) + label = f'{sorter_name} ({diversity}) ' item.props.item.filterer = item.props.child.get_last_child() item.props.child.get_first_child().set_text(label) filter_entry = item.props.child.get_last_child() @@ -227,6 +229,7 @@ class MainWindow(Gtk.Window): 'clicked', lambda _: self.activate_sort_order()) sort_box.append(self.button_activate_sort) self.filter_inputs = {} + self.sort_attribute_diversities = {} return sort_box def init_gallery_content(): @@ -286,7 +289,6 @@ class MainWindow(Gtk.Window): init_key_control() init_gallery_content() self.load_directory(update_gallery=False) - self.update_sort_order_box(self.sort_order) GLib.idle_add(self.update_gallery) def on_resize(self): @@ -317,8 +319,9 @@ class MainWindow(Gtk.Window): json_dump(list(bookmarks), f) self.update_file_selection() self.update_gallery_view() + self.update_sort_order_box(self.sort_order) - def update_gallery(self, suggested_selection=None, sort=True): + def update_gallery(self, suggested_selection=None): """Build gallery based on .per_row and .gallery_selection.""" def sorter(a, b): @@ -367,8 +370,7 @@ class MainWindow(Gtk.Window): return slot self.gallery.bind_model(None, lambda _: Gtk.Box()) - if sort: - self.gallery_store.sort(sorter) + self.gallery_store.sort(sorter) self.gallery.bind_model(self.gallery_selection, init_gallery_slot) to_select = self.gallery.get_child_at_index(0) if suggested_selection: @@ -387,6 +389,9 @@ class MainWindow(Gtk.Window): to_select.activate() else: self.counter.set_text(' (nothing) ') + for i in range(self.gallery_store_filtered.get_n_items()): + item = self.gallery.get_child_at_index(i).props.child.item + self.update_sort_order_box(self.sort_order) self.update_gallery_view() def update_gallery_view(self, refocus=False): @@ -485,7 +490,20 @@ class MainWindow(Gtk.Window): self.counter.set_text(f' {idx} of {total} ') def update_sort_order_box(self, sort_order_source, cur_selection=0): - """Rebuild self.sort_store from self.sort_order.""" + """Rebuild .sort_store from .sort_order, update diversity counts.""" + values = {} + for k in sort_order_source: + values[k] = set() + for i in range(self.gallery_store_filtered.get_n_items()): + item = self.gallery.get_child_at_index(i).props.child.item + if isinstance(item, ImgItem): + for attr_name in values.keys(): + val = None + if hasattr(item, attr_name): + val = getattr(item, attr_name) + values[attr_name].add(val) + for attr_name in values.keys(): + self.sort_attribute_diversities[attr_name] = len(values[attr_name]) self.sort_store.remove_all() for s in sort_order_source: self.sort_store.append(SortLabelItem(s))