diff --git a/src/calibre/gui2/dialogs/tag_list_editor.py b/src/calibre/gui2/dialogs/tag_list_editor.py index 102dc28f48..4b4ce63a8f 100644 --- a/src/calibre/gui2/dialogs/tag_list_editor.py +++ b/src/calibre/gui2/dialogs/tag_list_editor.py @@ -1,6 +1,6 @@ __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' -from PyQt4.QtCore import SIGNAL +from PyQt4.QtCore import SIGNAL, Qt from PyQt4.QtGui import QDialog from calibre.gui2.dialogs.tag_list_editor_ui import Ui_TagListEditor @@ -11,11 +11,12 @@ class TagListEditor(QDialog, Ui_TagListEditor): def tag_cmp(self, x, y): return cmp(x.lower(), y.lower()) - def __init__(self, window, db): + def __init__(self, window, db, tag_to_match): QDialog.__init__(self, window) Ui_TagListEditor.__init__(self) self.setupUi(self) + self.to_rename = {} self.to_delete = [] self.db = db all_tags = [tag for tag in self.db.all_tags()] @@ -24,7 +25,30 @@ class TagListEditor(QDialog, Ui_TagListEditor): for tag in all_tags: self.available_tags.addItem(tag) + items = self.available_tags.findItems(tag_to_match, Qt.MatchExactly) + if len(items) == 1: + self.available_tags.setCurrentItem(items[0]) + self.connect(self.delete_button, SIGNAL('clicked()'), self.delete_tags) + self.connect(self.rename_button, SIGNAL('clicked()'), self.rename_tag) + self.connect(self.available_tags, SIGNAL('itemDoubleClicked(QListWidgetItem *)'), self._rename_tag) + self.connect(self.available_tags, SIGNAL('itemChanged(QListWidgetItem *)'), self.finish_editing) + + def finish_editing(self, item): + if item.text() != self.item_before_editing: + self.to_rename[self.item_before_editing] = item.text() + + def rename_tag(self): + item = self.available_tags.currentItem() + self._rename_tag(item) + + def _rename_tag(self, item): + if item is None: + error_dialog(self, 'No tag selected', 'You must select one tag from the list of Available tags.').exec_() + return + self.item_before_editing = item.text() + item.setFlags (item.flags() | Qt.ItemIsEditable); + self.available_tags.editItem(item) def delete_tags(self, item=None): confirms, deletes = [], [] @@ -49,6 +73,8 @@ class TagListEditor(QDialog, Ui_TagListEditor): self.available_tags.takeItem(self.available_tags.row(item)) def accept(self): + for item in self.to_rename: + self.db.rename_tag(old=unicode(item), new=unicode(self.to_rename[item])) for item in self.to_delete: self.db.delete_tag(unicode(item.text())) QDialog.accept(self) diff --git a/src/calibre/gui2/dialogs/tag_list_editor.ui b/src/calibre/gui2/dialogs/tag_list_editor.ui index a7d24b02c0..5d7c95db1f 100644 --- a/src/calibre/gui2/dialogs/tag_list_editor.ui +++ b/src/calibre/gui2/dialogs/tag_list_editor.ui @@ -50,18 +50,36 @@ - - - Delete tag from database. This will unapply the tag from all books and then remove it from the database. - - - ... - - - - :/images/trash.svg:/images/trash.svg - - + + + + + Delete tag from database. This will unapply the tag from all books and then remove it from the database. + + + ... + + + + :/images/trash.svg:/images/trash.svg + + + + + + + Rename the tag everywhere it is used. + + + ... + + + + :/images/edit_input.svg:/images/edit_input.svg + + + + @@ -69,7 +87,7 @@ true - QAbstractItemView::MultiSelection + QAbstractItemView::ExtendedSelection QAbstractItemView::SelectRows @@ -92,9 +110,7 @@ - - - + buttonBox diff --git a/src/calibre/gui2/tag_view.py b/src/calibre/gui2/tag_view.py index 856370b7a8..88f82e78c6 100644 --- a/src/calibre/gui2/tag_view.py +++ b/src/calibre/gui2/tag_view.py @@ -24,7 +24,7 @@ class TagsView(QTreeView): # {{{ restriction_set = pyqtSignal(object) tags_marked = pyqtSignal(object, object) user_category_edit = pyqtSignal(object) - tag_list_edit = pyqtSignal() + tag_list_edit = pyqtSignal(object) saved_search_edit = pyqtSignal(object) def __init__(self, *args): @@ -91,7 +91,7 @@ class TagsView(QTreeView): # {{{ return try: if action == 'manage_tags': - self.tag_list_edit.emit(); + self.tag_list_edit.emit(category); return if action == 'manage_categories': self.user_category_edit.emit(category) @@ -137,7 +137,8 @@ class TagsView(QTreeView): # {{{ self.context_menu.addSeparator() self.context_menu.addAction(_('Manage Tags'), - partial(self.context_menu_handler, action='manage_tags')) + partial(self.context_menu_handler, action='manage_tags', + category=tag_name)) if category in prefs['user_categories'].keys(): self.context_menu.addAction(_('Manage User Categories'), @@ -148,14 +149,9 @@ class TagsView(QTreeView): # {{{ partial(self.context_menu_handler, action='manage_categories', category=None)) - if tag_name in saved_searches.names(): - self.context_menu.addAction(_('Manage Saved Searches'), - partial(self.context_menu_handler, action='manage_searches', - category=tag_name)) - else: - self.context_menu.addAction(_('Manage Saved Searches'), - partial(self.context_menu_handler, action='manage_searches', - category=None)) + self.context_menu.addAction(_('Manage Saved Searches'), + partial(self.context_menu_handler, action='manage_searches', + category=tag_name)) self.context_menu.popup(self.mapToGlobal(point)) return True; diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index da522b2153..d7869a57a8 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -655,8 +655,8 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI): self.tags_view.set_new_model() self.tags_view.recount() - def do_tags_list_edit(self): - d = TagListEditor(self, self.library_view.model().db) + def do_tags_list_edit(self, tag): + d = TagListEditor(self, self.library_view.model().db, tag) d.exec_() if d.result() == d.Accepted: self.tags_view.set_new_model() diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index f27a42beee..4e2a18e395 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -980,6 +980,9 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): if notify: self.notify('metadata', [id]) + def rename_tag(self, old, new): + self.conn.execute('UPDATE tags SET name=? WHERE name=?', (new, old)) + def get_tags(self, id): result = self.conn.get( 'SELECT name FROM tags WHERE id IN (SELECT tag FROM books_tags_link WHERE book=?)',