First iteration at an authors management dialog

This commit is contained in:
Charles Haley 2010-06-14 12:29:32 +01:00
parent 5dfbc21472
commit db507d4b13
4 changed files with 79 additions and 17 deletions

View File

@ -3,14 +3,68 @@ __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
__license__ = 'GPL v3' __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 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): class SortFieldDialog(QDialog, Ui_SortFieldDialog):
def __init__(self, parent, text): def __init__(self, parent, db, id_to_select):
QDialog.__init__(self, parent) QDialog.__init__(self, parent)
Ui_SortFieldDialog.__init__(self) Ui_SortFieldDialog.__init__(self)
self.setupUi(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))

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>334</width> <width>518</width>
<height>135</height> <height>262</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
@ -24,13 +24,17 @@
<rect> <rect>
<x>10</x> <x>10</x>
<y>10</y> <y>10</y>
<width>311</width> <width>501</width>
<height>111</height> <height>231</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<widget class="QLineEdit" name="textbox"/> <widget class="QTableWidget" name="table">
<property name="columnCount">
<number>0</number>
</property>
</widget>
</item> </item>
<item> <item>
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">

View File

@ -726,12 +726,15 @@ class TagBrowserMixin(object): # {{{
self.search.clear_to_help() self.search.clear_to_help()
def do_author_sort_edit(self, parent, text, id): 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_() d = editor.exec_()
if d: if d:
print editor.textbox.text() print editor.result
self.library_view.model().db.set_sort_field_for_author \ for (id, old_author, new_author, new_sort) in editor.result:
(id, unicode(editor.textbox.text())) 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() self.tags_view.recount()
# }}} # }}}

View File

@ -1113,7 +1113,6 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
index = index + 1 index = index + 1
self.conn.commit() self.conn.commit()
def delete_series_using_id(self, id): def delete_series_using_id(self, id):
books = self.conn.get('SELECT book from books_series_link WHERE series=?', (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,)) 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.execute('DELETE FROM publishers WHERE id=?', (old_id,))
self.conn.commit() self.conn.commit()
# There is no editor for author, so we do not need get_authors_with_ids or def get_authors_with_ids(self):
# delete_author_using_id. However, we can change the author's sort field, so result = self.conn.get('SELECT id,name,sort FROM authors')
# we provide that setter if not result:
return []
return result
def set_sort_field_for_author(self, old_id, new_sort): def set_sort_field_for_author(self, old_id, new_sort):
self.conn.execute('UPDATE authors SET sort=? WHERE id=?', \ self.conn.execute('UPDATE authors SET sort=? WHERE id=?', \