diff --git a/src/calibre/gui2/dialogs/fetch_metadata.py b/src/calibre/gui2/dialogs/fetch_metadata.py index 3da0e67e3d..426c7b1d60 100644 --- a/src/calibre/gui2/dialogs/fetch_metadata.py +++ b/src/calibre/gui2/dialogs/fetch_metadata.py @@ -16,6 +16,7 @@ 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 _hung_fetchers = set([]) @@ -72,27 +73,33 @@ 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 'y' + elif col == 7 and book.comments: + return 'y' + 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: - return NONE - return QVariant(res) + res = self.data_as_text(book, col) + if col <= 5 and res: + return QVariant(res) + return NONE elif role == Qt.DecorationRole: if col == 6 and book.has_cover: return self.yes_icon @@ -100,6 +107,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 +153,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) + self.matches.horizontalHeader().sectionClicked.connect(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 +265,7 @@ class FetchMetadata(QDialog, Ui_FetchMetadata): def chosen(self, index): self.matches.setCurrentIndex(index) self.accept() + + def show_sort_indicator(self, *args): + self.matches.horizontalHeader().setSortIndicatorShown(True) +