1
1
__license__ = 'GPL v3'
2
2
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
3
3
from PyQt4.QtCore import SIGNAL, Qt
4
from PyQt4.QtGui import QDialog, QMessageBox
4
from PyQt4.QtGui import QDialog
6
6
from calibre.gui2.dialogs.tag_editor_ui import Ui_TagEditor
7
7
from calibre.gui2 import qstring_to_unicode
8
8
from calibre.gui2 import question_dialog, error_dialog
10
10
class TagEditor(QDialog, Ui_TagEditor):
12
12
def __init__(self, window, db, index=None):
13
13
QDialog.__init__(self, window)
14
14
Ui_TagEditor.__init__(self)
19
19
if self.index is not None:
27
27
self.applied_tags.addItem(tag)
33
33
all_tags = [tag for tag in self.db.all_tags()]
34
34
all_tags = list(set(all_tags))
36
36
for tag in all_tags:
37
37
if tag not in tags:
38
38
self.available_tags.addItem(tag)
40
40
self.connect(self.apply_button, SIGNAL('clicked()'), self.apply_tags)
41
41
self.connect(self.unapply_button, SIGNAL('clicked()'), self.unapply_tags)
42
42
self.connect(self.add_tag_button, SIGNAL('clicked()'), self.add_tag)
44
44
self.connect(self.add_tag_input, SIGNAL('returnPressed()'), self.add_tag)
45
45
self.connect(self.available_tags, SIGNAL('itemActivated(QListWidgetItem*)'), self.apply_tags)
46
46
self.connect(self.applied_tags, SIGNAL('itemActivated(QListWidgetItem*)'), self.unapply_tags)
49
49
def delete_tags(self, item=None):
50
50
confirms, deletes = [], []
51
51
items = self.available_tags.selectedItems() if item is None else [item]
56
56
if self.db.is_tag_used(qstring_to_unicode(item.text())):
57
57
confirms.append(item)
61
61
ct = ', '.join([qstring_to_unicode(item.text()) for item in confirms])
62
d = question_dialog(self, 'Are your sure?',
63
'<p>The following tags are used by one or more books. Are you certain you want to delete them?<br>'+ct)
64
if d.exec_() == QMessageBox.Yes:
62
if question_dialog(self, _('Are your sure?'),
63
'<p>'+_('The following tags are used by one or more books. '
64
'Are you certain you want to delete them?')+'<br>'+ct):
65
65
deletes += confirms
67
67
for item in deletes:
68
68
self.db.delete_tag(qstring_to_unicode(item.text()))
69
69
self.available_tags.takeItem(self.available_tags.row(item))
72
72
def apply_tags(self, item=None):
73
items = self.available_tags.selectedItems() if item is None else [item]
73
items = self.available_tags.selectedItems() if item is None else [item]
75
75
tag = qstring_to_unicode(item.text())
76
76
self.tags.append(tag)
77
77
self.available_tags.takeItem(self.available_tags.row(item))
80
80
self.applied_tags.clear()
81
81
for tag in self.tags:
82
82
self.applied_tags.addItem(tag)
86
86
def unapply_tags(self, item=None):
87
items = self.applied_tags.selectedItems() if item is None else [item]
87
items = self.applied_tags.selectedItems() if item is None else [item]
89
89
tag = qstring_to_unicode(item.text())
90
90
self.tags.remove(tag)
91
91
self.available_tags.addItem(tag)
94
94
self.applied_tags.clear()
95
95
for tag in self.tags:
96
96
self.applied_tags.addItem(tag)
98
98
self.available_tags.sortItems()
100
100
def add_tag(self):
101
101
tags = qstring_to_unicode(self.add_tag_input.text()).split(',')