diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 909afd01df..a5e68ab6a6 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -867,11 +867,6 @@ class BooksModel(QAbstractTableModel): # {{{ self.dataChanged.emit(index, index) return True - def set_search_restriction(self, s): - self.db.data.set_search_restriction(s) - self.search('') - return self.rowCount(None) - # }}} class OnDeviceSearch(SearchQueryParser): # {{{ @@ -1341,8 +1336,5 @@ class DeviceBooksModel(BooksModel): # {{{ if prefs['manage_device_metadata']=='on_connect': self.editable = [] - def set_search_restriction(self, s): - pass - # }}} diff --git a/src/calibre/gui2/search_restriction_mixin.py b/src/calibre/gui2/search_restriction_mixin.py index 74132ae610..54d584af1a 100644 --- a/src/calibre/gui2/search_restriction_mixin.py +++ b/src/calibre/gui2/search_restriction_mixin.py @@ -11,26 +11,13 @@ class SearchRestrictionMixin(object): def __init__(self): self.search_restriction.initialize(help_text=_('Restrict to')) self.search_restriction.activated[int].connect(self.apply_search_restriction) - self.library_view.model().count_changed_signal.connect(self.restriction_count_changed) - self.search_restriction.setSizeAdjustPolicy(self.search_restriction.AdjustToMinimumContentsLengthWithIcon) + self.library_view.model().count_changed_signal.connect(self.set_number_of_books_shown) + self.search_restriction.setSizeAdjustPolicy( + self.search_restriction.AdjustToMinimumContentsLengthWithIcon) self.search_restriction.setMinimumContentsLength(10) self.search_restriction.setStatusTip(self.search_restriction.toolTip()) self.search_count.setText(_("(all books)")) - ''' - Adding and deleting books while restricted creates a complexity. When added, - they are displayed regardless of whether they match a search restriction. - However, if they do not, they are removed at the next search. The counts - must take this behavior into effect. - ''' - - def restriction_count_changed(self, c): - self.restriction_count_of_books_in_view += \ - c - self.restriction_count_of_books_in_library - self.restriction_count_of_books_in_library = c - if self.restriction_in_effect: - self.set_number_of_books_shown() - def apply_named_search_restriction(self, name): if not name: r = 0 @@ -44,13 +31,10 @@ class SearchRestrictionMixin(object): def apply_search_restriction(self, i): r = unicode(self.search_restriction.currentText()) if r is not None and r != '': - self.restriction_in_effect = True restriction = 'search:"%s"'%(r) else: - self.restriction_in_effect = False restriction = '' - self.restriction_count_of_books_in_view = \ - self.library_view.model().set_search_restriction(restriction) + self.library_view.model().db.data.set_search_restriction(restriction) self.search.clear() self.saved_search.clear() self.tags_view.set_search_restriction(restriction) @@ -58,9 +42,12 @@ class SearchRestrictionMixin(object): self.current_view().setFocus(Qt.OtherFocusReason) def set_number_of_books_shown(self): - if self.current_view() == self.library_view and self.restriction_in_effect: - t = _("({0} of {1})").format(self.current_view().row_count(), - self.restriction_count_of_books_in_view) + db = self.library_view.model().db + if self.current_view() == self.library_view and db is not None and \ + db.data.search_restriction_applied(): + rows = self.current_view().row_count() + rbc = max(rows, db.data.get_search_restriction_book_count()) + t = _("({0} of {1})").format(rows, rbc) self.search_count.setStyleSheet \ ('QLabel { border-radius: 8px; background-color: yellow; }') else: # No restriction or not library view diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index 5ac7e6a45d..fd73601042 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -166,10 +166,6 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{ EmailMixin.__init__(self) DeviceMixin.__init__(self) - self.restriction_count_of_books_in_view = 0 - self.restriction_count_of_books_in_library = 0 - self.restriction_in_effect = False - self.progress_indicator = ProgressIndicator(self) self.progress_indicator.pos = (0, 20) self.verbose = opts.verbose diff --git a/src/calibre/library/caches.py b/src/calibre/library/caches.py index 847a0493eb..45b96bb69f 100644 --- a/src/calibre/library/caches.py +++ b/src/calibre/library/caches.py @@ -181,6 +181,7 @@ class ResultCache(SearchQueryParser): # {{{ self._map = self._map_filtered = [] self.first_sort = True self.search_restriction = '' + self.search_restriction_book_count = 0 self.field_metadata = field_metadata self.all_search_locations = field_metadata.get_search_terms() SearchQueryParser.__init__(self, self.all_search_locations, optimize=True) @@ -618,12 +619,14 @@ class ResultCache(SearchQueryParser): # {{{ return matches def search(self, query, return_matches=False): - ans = self.search_getting_ids(query, self.search_restriction) + ans = self.search_getting_ids(query, self.search_restriction, + set_restriction_count=True) if return_matches: return ans self._map_filtered = ans - def search_getting_ids(self, query, search_restriction): + def search_getting_ids(self, query, search_restriction, + set_restriction_count=False): q = '' if not query or not query.strip(): q = search_restriction @@ -632,16 +635,27 @@ class ResultCache(SearchQueryParser): # {{{ if search_restriction: q = u'%s (%s)' % (search_restriction, query) if not q: + if set_restriction_count: + self.search_restriction_book_count = len(self._map) return list(self._map) matches = self.parse(q) tmap = list(itertools.repeat(False, len(self._data))) for x in matches: tmap[x] = True - return [x for x in self._map if tmap[x]] + rv = [x for x in self._map if tmap[x]] + if set_restriction_count and q == search_restriction: + self.search_restriction_book_count = len(rv) + return rv def set_search_restriction(self, s): self.search_restriction = s + def search_restriction_applied(self): + return bool(self.search_restriction) + + def get_search_restriction_book_count(self): + return self.search_restriction_book_count + # }}} def remove(self, id):