diff options
| author | Joris Guyonvarch | 2025-12-30 16:21:13 +0100 |
|---|---|---|
| committer | Joris Guyonvarch | 2025-12-30 16:21:13 +0100 |
| commit | 9566791adef22f0f1102bf73f0ba02ae9842b7cf (patch) | |
| tree | cdc425c3278f8d315a94b678bbcec075774e316c /src/book_flow.py | |
| parent | 39445241faa46513bd676e5dfbc485414017299b (diff) | |
Use filter and sort function in flow box
Diffstat (limited to 'src/book_flow.py')
| -rw-r--r-- | src/book_flow.py | 122 |
1 files changed, 83 insertions, 39 deletions
diff --git a/src/book_flow.py b/src/book_flow.py index 9698aab..bfd7114 100644 --- a/src/book_flow.py +++ b/src/book_flow.py @@ -24,9 +24,10 @@ class BookFlow(Gtk.ScrolledWindow): Gtk.ScrolledWindow.__init__(self) self.set_vexpand(True) - self._flow_box = Gtk.FlowBox() - self.set_child(self._flow_box) + self._flowbox = Gtk.FlowBox() + self.set_child(self._flowbox) + self._books = books self._window = window self._resources = resources self._library = library @@ -34,41 +35,82 @@ class BookFlow(Gtk.ScrolledWindow): self._conn = conn self._msg = msg self._picture_cache = PictureCache() + self._flowbox_children = {} - self.reset(books, progress, genre) + self.update_filters(progress, genre) + self._flowbox.set_sort_func(self._sort_books) + for book_id, data in books.items(): + self.add(book_id, data) + + def get(self): + return self._books def select_book(self, book_id): - if book_id in self._flow_box_children: - self._flow_box.select_child(self._flow_box_children[book_id]) - - def reset(self, books, progress, genre, book_id=None): - if book_id: - self._picture_cache.invalidate(f'{self._library}/{book_id}/cover-min.png') - - self._flow_box.remove_all() - self._flow_box_children = {} - for book_id, data in sorted(books.items(), key=book_sort): - if self._is_selected(data, progress, genre): - picture = self._picture_cache.get(f'{self._library}/{book_id}/cover-min.png') - picture.set_can_shrink(False) - utils.set_margin(picture, 10) - - gesture_lclick = Gtk.GestureClick() - gesture_lclick.connect('released', self._on_left_click, book_id, data) - picture.add_controller(gesture_lclick) - - gesture_rclick = Gtk.GestureClick() - gesture_rclick.set_button(3) - gesture_rclick.connect('released', self._on_right_click, picture, book_id, data) - picture.add_controller(gesture_rclick) - - flow_box_child = Gtk.FlowBoxChild() - flow_box_child.set_child(picture) - self._flow_box_children[book_id] = flow_box_child - self._flow_box.append(flow_box_child) + if book_id in self._flowbox_children: + self._flowbox.select_child(self._flowbox_children[book_id]) + + def update_filters(self, progress, genre): + def f(flow_box_child): + book_id = flow_box_child.get_name() + data = self._books[book_id] + return self._is_selected(data, progress, genre) + + self._flowbox.set_filter_func(f) + + def add(self, book_id, data): + self._books[book_id] = data + + picture = self._picture_cache.get(f'{self._library}/{book_id}/cover-min.png') + picture.set_can_shrink(False) + utils.set_margin(picture, 10) + + gesture_lclick = Gtk.GestureClick() + gesture_lclick.connect('released', self._on_left_click, book_id, data) + picture.add_controller(gesture_lclick) + + gesture_rclick = Gtk.GestureClick() + gesture_rclick.set_button(3) + gesture_rclick.connect('released', self._on_right_click, picture, book_id, data) + picture.add_controller(gesture_rclick) + + flow_box_child = Gtk.FlowBoxChild() + flow_box_child.set_child(picture) + flow_box_child.set_name(book_id) + self._flowbox_children[book_id] = flow_box_child + self._flowbox.append(flow_box_child) + + def remove(self, book_id): + del self._books[book_id] + self._flowbox.remove(self._flowbox_children[book_id]) + del self._flowbox_children[book_id] + self._picture_cache.invalidate(f'{self._library}/{book_id}/cover-min.png') # Private + def _sort_books(self, child1, child2): + book_id1 = child1.get_name() + book_id2 = child2.get_name() + data1 = self._books[book_id1] + data2 = self._books[book_id2] + + author1 = author_key(data1) + author2 = author_key(data2) + + if author1 < author2: + return -1 + elif author1 > author2: + return 1 + + year1 = year_key(data1) + year2 = year_key(data2) + + if year1 < year2: + return -1 + elif year1 > year2: + return 1 + + return 0 + def _is_selected(self, data, progress, genre): if data['progress'] != progress: return False @@ -115,7 +157,7 @@ class BookFlow(Gtk.ScrolledWindow): def _update_book(self, book_id, data): book = {'id': book_id, 'data': data } - BookForm(self._window, self._resources, self._library, self._conn, data['progress'], self._msg, book).present() + BookForm(self._window, self._resources, self._library, self._conn, self._msg, book).present() def _confirm_delete_book(self, book_id, data): BookDelete(self._window, self._library, book_id, data, lambda: self._delete_book(book_id, data)).present() @@ -128,14 +170,16 @@ class BookFlow(Gtk.ScrolledWindow): def _transfer_book(self, book_id, data, paths): BookTransfer(self._window, self._ereader, book_id, data, paths).present() -def book_sort(b): - key, data = b - author = author_key(data) - year = data['year'] if 'year' in data else '' - return f'{author}{year}' - def author_key(data): match data['authors']: case [author, *_]: - return author.split()[-1] + return author.split()[-1].lower() return '' + +def year_key(data): + if 'year' in data: + try: + return int(data['year']) + except Exception: + return 0 + return 0 |
