diff --git a/src/calibre/gui2/dialogs/sort_field_dialog.py b/src/calibre/gui2/dialogs/sort_field_dialog.py index d1c6d45ed3..a73d0d8cf4 100644 --- a/src/calibre/gui2/dialogs/sort_field_dialog.py +++ b/src/calibre/gui2/dialogs/sort_field_dialog.py @@ -3,14 +3,68 @@ __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net' __docformat__ = 'restructuredtext en' __license__ = 'GPL v3' -from PyQt4.Qt import QDialog +from PyQt4.Qt import Qt, QDialog, QTableWidgetItem, QAbstractItemView + +from calibre.ebooks.metadata import author_to_author_sort from calibre.gui2.dialogs.sort_field_dialog_ui import Ui_SortFieldDialog +class tableItem(QTableWidgetItem): + def __ge__(self, other): + return unicode(self.text()).lower() >= unicode(other.text()).lower() + + def __lt__(self, other): + return unicode(self.text()).lower() < unicode(other.text()).lower() + class SortFieldDialog(QDialog, Ui_SortFieldDialog): - def __init__(self, parent, text): + def __init__(self, parent, db, id_to_select): QDialog.__init__(self, parent) Ui_SortFieldDialog.__init__(self) self.setupUi(self) - if text is not None: - self.textbox.setText(text) + + self.buttonBox.accepted.connect(self.accepted) + self.table.cellChanged.connect(self.cell_changed) + + self.table.setSelectionMode(QAbstractItemView.SingleSelection) + self.table.setColumnCount(2) + self.table.setHorizontalHeaderLabels([_('Author'), _('Author sort')]) + + self.authors = {} + auts = db.get_authors_with_ids() + self.table.setRowCount(len(auts)) + select_item = None + for row, (id, author, sort) in enumerate(auts): + author = author.replace('|', ',') + self.authors[id] = (author, sort) + aut = tableItem(author) + aut.setData(Qt.UserRole, id) + sort = tableItem(sort) + self.table.setItem(row, 0, aut) + self.table.setItem(row, 1, sort) + if id == id_to_select: + select_item = aut + + if select_item is not None: + self.table.setCurrentItem(select_item) + self.table.resizeColumnsToContents() + self.table.setSortingEnabled(True) + self.table.sortByColumn(1, Qt.AscendingOrder) + + def accepted(self): + print 'accepted!' + self.result = [] + for row in range(0,self.table.rowCount()): + id = self.table.item(row, 0).data(Qt.UserRole).toInt()[0] + aut = unicode(self.table.item(row, 0).text()) + sort = unicode(self.table.item(row, 1).text()) + print id, aut, sort + orig_aut,orig_sort = self.authors[id] + if orig_aut != aut or orig_sort != sort: + self.result.append((id, orig_aut, aut, sort)) + + def cell_changed(self, row, col): + if col == 0: + aut = unicode(self.table.item(row, 0).text()) + c = self.table.item(row, 1) + if c is not None: + c.setText(author_to_author_sort(aut)) diff --git a/src/calibre/gui2/dialogs/sort_field_dialog.ui b/src/calibre/gui2/dialogs/sort_field_dialog.ui index 3fc386d1ef..bc6ffae4fc 100644 --- a/src/calibre/gui2/dialogs/sort_field_dialog.ui +++ b/src/calibre/gui2/dialogs/sort_field_dialog.ui @@ -6,8 +6,8 @@ 0 0 - 334 - 135 + 518 + 262 @@ -24,13 +24,17 @@ 10 10 - 311 - 111 + 501 + 231 - + + + 0 + + diff --git a/src/calibre/gui2/tag_view.py b/src/calibre/gui2/tag_view.py index bae81c79cd..27d73a0af8 100644 --- a/src/calibre/gui2/tag_view.py +++ b/src/calibre/gui2/tag_view.py @@ -726,12 +726,15 @@ class TagBrowserMixin(object): # {{{ self.search.clear_to_help() def do_author_sort_edit(self, parent, text, id): - editor = SortFieldDialog(parent, text) + editor = SortFieldDialog(parent, self.library_view.model().db, id) d = editor.exec_() if d: - print editor.textbox.text() - self.library_view.model().db.set_sort_field_for_author \ - (id, unicode(editor.textbox.text())) + print editor.result + for (id, old_author, new_author, new_sort) in editor.result: + if old_author != new_author: + self.library_view.model().db.rename_author(id, new_author) + self.library_view.model().db.set_sort_field_for_author \ + (id, unicode(new_sort)) self.tags_view.recount() # }}} diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index a4e8b4ff77..794474f821 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -1113,7 +1113,6 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): index = index + 1 self.conn.commit() - def delete_series_using_id(self, id): books = self.conn.get('SELECT book from books_series_link WHERE series=?', (id,)) self.conn.execute('DELETE FROM books_series_link WHERE series=?', (id,)) @@ -1151,9 +1150,11 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): self.conn.execute('DELETE FROM publishers WHERE id=?', (old_id,)) self.conn.commit() - # There is no editor for author, so we do not need get_authors_with_ids or - # delete_author_using_id. However, we can change the author's sort field, so - # we provide that setter + def get_authors_with_ids(self): + result = self.conn.get('SELECT id,name,sort FROM authors') + if not result: + return [] + return result def set_sort_field_for_author(self, old_id, new_sort): self.conn.execute('UPDATE authors SET sort=? WHERE id=?', \