From f69f6a3dae1288c507c29d9d640b1c92306b6330 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Thu, 30 Dec 2010 12:42:46 +0000 Subject: [PATCH] This time, really fix the sorting problem. --- src/calibre/gui2/library/__init__.py | 2 +- src/calibre/gui2/library/models.py | 7 ++++--- src/calibre/gui2/library/views.py | 8 ++++---- src/calibre/library/caches.py | 5 ++++- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/calibre/gui2/library/__init__.py b/src/calibre/gui2/library/__init__.py index d7180de99a..e1344101ec 100644 --- a/src/calibre/gui2/library/__init__.py +++ b/src/calibre/gui2/library/__init__.py @@ -7,4 +7,4 @@ __docformat__ = 'restructuredtext en' from PyQt4.Qt import Qt -DEFAULT_SORT = ('timestamp', Qt.DescendingOrder) +DEFAULT_SORT = ('timestamp', False) diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 22a9db0fef..49cb1ce182 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -247,12 +247,13 @@ class BooksModel(QAbstractTableModel): # {{{ if not self.db: return self.about_to_be_sorted.emit(self.db.id) - ascending = order == Qt.AscendingOrder + if not isinstance(order, bool): + order = order == Qt.AscendingOrder label = self.column_map[col] - self.db.sort(label, ascending) + self.db.sort(label, order) if reset: self.reset() - self.sorted_on = (label, order == Qt.AscendingOrder) + self.sorted_on = (label, order) self.sort_history.insert(0, self.sorted_on) self.sorting_done.emit(self.db.index) diff --git a/src/calibre/gui2/library/views.py b/src/calibre/gui2/library/views.py index 457cfaf754..322199a4f9 100644 --- a/src/calibre/gui2/library/views.py +++ b/src/calibre/gui2/library/views.py @@ -165,7 +165,7 @@ class BooksView(QTableView): # {{{ partial(self.column_header_context_handler, action='descending', column=col)) if self._model.sorted_on[0] == col: - ac = a if self._model.sorted_on[1] == Qt.AscendingOrder else d + ac = a if self._model.sorted_on[1] else d ac.setCheckable(True) ac.setChecked(True) if col not in ('ondevice', 'rating', 'inlibrary') and \ @@ -282,13 +282,13 @@ class BooksView(QTableView): # {{{ def cleanup_sort_history(self, sort_history): history = [] for col, order in sort_history: + if not isinstance(order, bool): + continue if col == 'date': col = 'timestamp' if col in self.column_map: - if (not history or history[0][0] != col): + if (not history or history[-1][0] != col): history.append([col, order]) - elif isinstance(order, bool) and history[0][1] != order: - history[0][1] = order return history def apply_sort_history(self, saved_history): diff --git a/src/calibre/library/caches.py b/src/calibre/library/caches.py index ff3aa0bf67..a32c45191f 100644 --- a/src/calibre/library/caches.py +++ b/src/calibre/library/caches.py @@ -669,6 +669,9 @@ class ResultCache(SearchQueryParser): # {{{ fields = [('timestamp', False)] keyg = SortKeyGenerator(fields, self.field_metadata, self._data) + # For efficiency, the key generator returns a plain value if only one + # field is in the sort field list. Because the normal cmp function will + # always assume asc, we must deal with asc/desc here. if len(fields) == 1: self._map.sort(key=keyg, reverse=not fields[0][1]) else: @@ -697,7 +700,7 @@ class SortKeyGenerator(object): def __init__(self, fields, field_metadata, data): from calibre.utils.icu import sort_key self.field_metadata = field_metadata - self.orders = [-1 if x[1] else 1 for x in fields] + self.orders = [1 if x[1] else -1 for x in fields] self.entries = [(x[0], field_metadata[x[0]]) for x in fields] self.library_order = tweaks['title_series_sorting'] == 'library_order' self.data = data