From fc5934436c7335f64ebe69b412081a2f764f5d5f Mon Sep 17 00:00:00 2001 From: Marcell van Geest Date: Tue, 21 Dec 2010 23:39:25 +0100 Subject: [PATCH] Added possibility to sort fetched metadata using the column headers. --- src/calibre/gui2/dialogs/fetch_metadata.py | 57 ++++++++++++++++------ 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/src/calibre/gui2/dialogs/fetch_metadata.py b/src/calibre/gui2/dialogs/fetch_metadata.py index 3da0e67e3d..e6f5e6ae9a 100644 --- a/src/calibre/gui2/dialogs/fetch_metadata.py +++ b/src/calibre/gui2/dialogs/fetch_metadata.py @@ -16,6 +16,8 @@ from calibre.gui2 import error_dialog, NONE, info_dialog, config from calibre.gui2.widgets import ProgressIndicator from calibre import strftime, force_unicode from calibre.customize.ui import get_isbndb_key, set_isbndb_key +from calibre.utils.icu import sort_key +from calibre import force_unicode _hung_fetchers = set([]) @@ -72,25 +74,32 @@ class Matches(QAbstractTableModel): def summary(self, row): return self.matches[row].comments + def data_as_text(self, book, col): + if col == 0 and book.title is not None: + return book.title + elif col == 1: + return ', '.join(book.authors) + elif col == 2 and book.author_sort is not None: + return book.author_sort + elif col == 3 and book.publisher is not None: + return book.publisher + elif col == 4 and book.isbn is not None: + return book.isbn + elif col == 5 and hasattr(book.pubdate, 'timetuple'): + return strftime('%b %Y', book.pubdate.timetuple()) + elif col == 6 and book.has_cover: + return 'has_cover' + elif col == 7 and book.comments is not None: + return book.comments + else: + return '' + def data(self, index, role): row, col = index.row(), index.column() book = self.matches[row] if role == Qt.DisplayRole: - res = None - if col == 0: - res = book.title - elif col == 1: - res = ', '.join(book.authors) - elif col == 2: - res = book.author_sort - elif col == 3: - res = book.publisher - elif col == 4: - res = book.isbn - elif col == 5: - if hasattr(book.pubdate, 'timetuple'): - res = strftime('%b %Y', book.pubdate.timetuple()) - if not res: + res = self.data_as_text(book, col) + if not (col <= 5 and res): return NONE return QVariant(res) elif role == Qt.DecorationRole: @@ -100,6 +109,16 @@ class Matches(QAbstractTableModel): return self.yes_icon return NONE + def sort(self, col, order, reset=True): + if not self.matches: + return + descending = order == Qt.DescendingOrder + self.matches.sort(None, + lambda x: sort_key(unicode(force_unicode(self.data_as_text(x, col)))), + descending) + if reset: + self.reset() + class FetchMetadata(QDialog, Ui_FetchMetadata): HANG_TIME = 75 #seconds @@ -136,6 +155,11 @@ class FetchMetadata(QDialog, Ui_FetchMetadata): self.connect(self.matches, SIGNAL('entered(QModelIndex)'), self.show_summary) self.matches.setMouseTracking(True) + # Enabling sorting and setting a sort column will not change the initial + # order of the results, as they are filled in later + self.matches.setSortingEnabled(True) + QObject.connect(self.matches.horizontalHeader(), SIGNAL('sectionClicked(int)'), self.show_sort_indicator) + self.matches.horizontalHeader().setSortIndicatorShown(False) self.fetch_metadata() self.opt_get_social_metadata.setChecked(config['get_social_metadata']) self.opt_overwrite_author_title_metadata.setChecked(config['overwrite_author_title_metadata']) @@ -243,3 +267,6 @@ class FetchMetadata(QDialog, Ui_FetchMetadata): def chosen(self, index): self.matches.setCurrentIndex(index) self.accept() + + def show_sort_indicator(self): + self.matches.horizontalHeader().setSortIndicatorShown(True)