diff options
| author | Joris Guyonvarch | 2025-12-30 17:34:13 +0100 |
|---|---|---|
| committer | Joris Guyonvarch | 2025-12-30 17:34:13 +0100 |
| commit | 65cae8a887504f49735aa9035ae53fcffd10fbc9 (patch) | |
| tree | 49bd8ddca5be035b51a08a9516d1ee78d18941c4 /src/book_flow.py | |
| parent | 9566791adef22f0f1102bf73f0ba02ae9842b7cf (diff) | |
Add textual search
Search in titles, subtitles, authors and years.
Diffstat (limited to 'src/book_flow.py')
| -rw-r--r-- | src/book_flow.py | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/src/book_flow.py b/src/book_flow.py index bfd7114..dc9cad6 100644 --- a/src/book_flow.py +++ b/src/book_flow.py @@ -16,11 +16,12 @@ from src.book_form import BookForm from src.book_transfer import BookTransfer from src.picture_cache import PictureCache import src.book_files as book_files +import src.str_format as str_format class BookFlow(Gtk.ScrolledWindow): - def __init__(self, window, resources, library, ereader, conn, books, progress, genre, msg): + def __init__(self, window, resources, library, ereader, conn, books, progress, genre, search, msg): Gtk.ScrolledWindow.__init__(self) self.set_vexpand(True) @@ -37,7 +38,7 @@ class BookFlow(Gtk.ScrolledWindow): self._picture_cache = PictureCache() self._flowbox_children = {} - self.update_filters(progress, genre) + self.update_filters(progress, genre, search) self._flowbox.set_sort_func(self._sort_books) for book_id, data in books.items(): self.add(book_id, data) @@ -49,11 +50,11 @@ class BookFlow(Gtk.ScrolledWindow): if book_id in self._flowbox_children: self._flowbox.select_child(self._flowbox_children[book_id]) - def update_filters(self, progress, genre): + def update_filters(self, progress, genre, search): def f(flow_box_child): book_id = flow_box_child.get_name() data = self._books[book_id] - return self._is_selected(data, progress, genre) + return self._is_selected(data, progress, genre, search) self._flowbox.set_filter_func(f) @@ -111,14 +112,14 @@ class BookFlow(Gtk.ScrolledWindow): return 0 - def _is_selected(self, data, progress, genre): + def _is_selected(self, data, progress, genre, search): if data['progress'] != progress: return False - if genre == models.all_genres: - return True - if genre == models.no_genre: - return len(data['genres']) == 0 - return genre in data['genres'] + if genre == models.no_genre and len(data['genres']) > 0: + return False + if genre != models.all_genres and not genre in data['genres']: + return False + return match_search(data, search) def _on_left_click(self, gesture, n_press, x, y, book_id, data): if n_press == 2: @@ -183,3 +184,22 @@ def year_key(data): except Exception: return 0 return 0 + +def match_search(data, search): + for word in search.split(): + if not match_word(data, word): + return False + return True + +def match_word(data, word): + s = str_format.for_search(word) + if s in str_format.for_search(data['title']): + return True + if 'subtitle' in data and s in str_format.for_search(data['subtitle']): + return True + for author in data['authors']: + if s in str_format.for_search(author): + return True + if 'year' in data and s == str(data['year']): + return True + return False |
