From 399d94bd3f2d0af2b0cf5da7077d70f5c187683b Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Fri, 6 May 2011 06:04:16 +0100 Subject: [PATCH] In edit metadata single: 1) Correct implementation of author case change 2) Add a cancel button to the 'save' dialogs 3) Select the first author in Manage Authors --- .../gui2/dialogs/edit_authors_dialog.py | 7 ++- src/calibre/gui2/metadata/basic_widgets.py | 51 ++++++++++++++----- src/calibre/gui2/tag_view.py | 4 +- src/calibre/library/database2.py | 6 +++ 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/src/calibre/gui2/dialogs/edit_authors_dialog.py b/src/calibre/gui2/dialogs/edit_authors_dialog.py index eae189e04c..bc1b83cfc9 100644 --- a/src/calibre/gui2/dialogs/edit_authors_dialog.py +++ b/src/calibre/gui2/dialogs/edit_authors_dialog.py @@ -19,7 +19,7 @@ class tableItem(QTableWidgetItem): class EditAuthorsDialog(QDialog, Ui_EditAuthorsDialog): - def __init__(self, parent, db, id_to_select): + def __init__(self, parent, db, id_to_select, select_sort): QDialog.__init__(self, parent) Ui_EditAuthorsDialog.__init__(self) self.setupUi(self) @@ -48,7 +48,10 @@ class EditAuthorsDialog(QDialog, Ui_EditAuthorsDialog): self.table.setItem(row, 0, aut) self.table.setItem(row, 1, sort) if id == id_to_select: - select_item = sort + if select_sort: + select_item = sort + else: + select_item = aut self.table.resizeColumnsToContents() # set up the cellChanged signal only after the table is filled diff --git a/src/calibre/gui2/metadata/basic_widgets.py b/src/calibre/gui2/metadata/basic_widgets.py index 50a8e7c0b8..bd438c2a19 100644 --- a/src/calibre/gui2/metadata/basic_widgets.py +++ b/src/calibre/gui2/metadata/basic_widgets.py @@ -8,8 +8,9 @@ __copyright__ = '2011, Kovid Goyal ' __docformat__ = 'restructuredtext en' import textwrap, re, os +from functools import partial -from PyQt4.Qt import (Qt, QDateEdit, QDate, pyqtSignal, +from PyQt4.Qt import (Qt, QDateEdit, QDate, pyqtSignal, QMessageBox, QIcon, QToolButton, QWidget, QLabel, QGridLayout, QDoubleSpinBox, QListWidgetItem, QSize, QPixmap, QPushButton, QSpinBox, QLineEdit, QSizePolicy) @@ -31,6 +32,16 @@ from calibre.utils.date import utcfromtimestamp from calibre.gui2.comments_editor import Editor from calibre.library.comments import comments_to_html from calibre.gui2.dialogs.tag_editor import TagEditor +from calibre.utils.icu import strcmp + +def save_dialog(parent, title, msg, det_msg=''): + d = QMessageBox(parent) + d.setWindowTitle(title) + d.setText(msg) + d.setStandardButtons(QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) + return d.exec_() + + ''' The interface common to all widgets used to set basic metadata @@ -168,16 +179,22 @@ class AuthorsEdit(MultiCompleteComboBox): def manage_authors(self): if self.original_val != self.current_val: - if (question_dialog(self, _('Authors changed'), + d = save_dialog(self, _('Authors changed'), _('You have changed the authors for this book. You must save ' 'these changes before you can use Manage authors. Do you ' - 'want to save these changes?'), show_copy_button=False)): + 'want to save these changes?')) + if d == QMessageBox.Cancel: + return + if d == QMessageBox.Yes: self.commit(self.db, self.id_) self.db.commit() self.original_val = self.current_val else: self.current_val = self.original_val - self.dialog.parent().do_author_sort_edit(self, self.id_) + first_author = self.current_val[0] if len(self.current_val) else None + self.dialog.parent().do_author_sort_edit(self, + self.db.get_author_id(first_author), + select_sort=False) self.initialize(self.db, self.id_) self.dialog.author_sort.initialize(self.db, self.id_) @@ -256,13 +273,13 @@ class AuthorSortEdit(EnLineEdit): 'No action is required if this is what you want.')) self.tooltips = (ok_tooltip, bad_tooltip) - self.authors_edit.editTextChanged.connect(self.update_state) - self.textChanged.connect(self.update_state) + self.authors_edit.editTextChanged.connect(partial(self.update_state, True)) + self.textChanged.connect(partial(self.update_state, False)) autogen_button.clicked.connect(self.auto_generate) copy_a_to_as_action.triggered.connect(self.auto_generate) copy_as_to_a_action.triggered.connect(self.copy_to_authors) - self.update_state() + self.update_state(False) @dynamic_property def current_val(self): @@ -278,12 +295,16 @@ class AuthorSortEdit(EnLineEdit): return property(fget=fget, fset=fset) - def update_state(self, *args): + def update_state(self, modify_aus, *args): au = unicode(self.authors_edit.text()) + # Handle case change if the authors box changed + if modify_aus and strcmp(au, self.current_val) == 0: + self.current_val = au + au = re.sub(r'\s+et al\.$', '', au) au = self.db.author_sort_from_authors(string_to_authors(au)) - normal = au == self.current_val + normal = strcmp(au, self.current_val) == 0 if normal: col = 'rgb(0, 255, 0, 20%)' else: @@ -316,12 +337,11 @@ class AuthorSortEdit(EnLineEdit): self.current_val = self.db.author_sort_from_authors(authors) def initialize(self, db, id_): - self.current_val = self.original_val = db.author_sort(id_, index_is_id=True) + self.current_val = db.author_sort(id_, index_is_id=True) def commit(self, db, id_): aus = self.current_val - if aus != self.original_val: - db.set_author_sort(id_, aus, notify=False, commit=False) + db.set_author_sort(id_, aus, notify=False, commit=False) return True # }}} @@ -919,10 +939,13 @@ class TagsEdit(MultiCompleteLineEdit): # {{{ def edit(self, db, id_): if self.changed: - if question_dialog(self, _('Tags changed'), + d = save_dialog(self, _('Tags changed'), _('You have changed the tags. In order to use the tags' ' editor, you must either discard or apply these ' - 'changes. Apply changes?'), show_copy_button=False): + 'changes. Apply changes?')) + if d == QMessageBox.Cancel: + return + if d == QMessageBox.Yes: self.commit(db, id_) db.commit() self.original_val = self.current_val diff --git a/src/calibre/gui2/tag_view.py b/src/calibre/gui2/tag_view.py index 7b68229da0..a3e39aefd2 100644 --- a/src/calibre/gui2/tag_view.py +++ b/src/calibre/gui2/tag_view.py @@ -2048,12 +2048,12 @@ class TagBrowserMixin(object): # {{{ self.library_view.select_rows(ids) # refreshing the tags view happens at the emit()/call() site - def do_author_sort_edit(self, parent, id): + def do_author_sort_edit(self, parent, id, select_sort=True): ''' Open the manage authors dialog ''' db = self.library_view.model().db - editor = EditAuthorsDialog(parent, db, id) + editor = EditAuthorsDialog(parent, db, id, select_sort) d = editor.exec_() if d: for (id, old_author, new_author, new_sort) in editor.result: diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 065f0e8446..ac36335b79 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -2285,6 +2285,12 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): return [] return result + def get_author_id(self, author): + author = author.replace(',', '|') + result = self.conn.get('SELECT id FROM authors WHERE name=?', + (author,), all=False) + return result + def set_sort_field_for_author(self, old_id, new_sort, commit=True, notify=False): self.conn.execute('UPDATE authors SET sort=? WHERE id=?', \ (new_sort.strip(), old_id))