home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1274 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  5.7 KB  |  131 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
  5. __docformat__ = 'restructuredtext en'
  6. __license__ = 'GPL v3'
  7. from PyQt4.Qt import Qt, QDialog, QTableWidgetItem, QAbstractItemView
  8. from calibre.ebooks.metadata import author_to_author_sort
  9. from calibre.gui2 import error_dialog
  10. from calibre.gui2.dialogs.edit_authors_dialog_ui import Ui_EditAuthorsDialog
  11.  
  12. class tableItem(QTableWidgetItem):
  13.     
  14.     def __ge__(self, other):
  15.         return unicode(self.text()).lower() >= unicode(other.text()).lower()
  16.  
  17.     
  18.     def __lt__(self, other):
  19.         return unicode(self.text()).lower() < unicode(other.text()).lower()
  20.  
  21.  
  22.  
  23. class EditAuthorsDialog(QDialog, Ui_EditAuthorsDialog):
  24.     
  25.     def __init__(self, parent, db, id_to_select):
  26.         QDialog.__init__(self, parent)
  27.         Ui_EditAuthorsDialog.__init__(self)
  28.         self.setupUi(self)
  29.         icon = self.windowIcon()
  30.         self.setWindowFlags(self.windowFlags() & ~(Qt.WindowContextHelpButtonHint))
  31.         self.setWindowIcon(icon)
  32.         self.buttonBox.accepted.connect(self.accepted)
  33.         self.table.setSelectionMode(QAbstractItemView.SingleSelection)
  34.         self.table.setColumnCount(2)
  35.         self.table.setHorizontalHeaderLabels([
  36.             _('Author'),
  37.             _('Author sort')])
  38.         self.authors = { }
  39.         auts = db.get_authors_with_ids()
  40.         self.table.setRowCount(len(auts))
  41.         select_item = None
  42.         for id, author, sort in enumerate(auts):
  43.             author = author.replace('|', ',')
  44.             self.authors[id] = (author, sort)
  45.             aut = tableItem(author)
  46.             aut.setData(Qt.UserRole, id)
  47.             sort = tableItem(sort)
  48.             self.table.setItem(row, 0, aut)
  49.             self.table.setItem(row, 1, sort)
  50.             if id == id_to_select:
  51.                 select_item = sort
  52.                 continue
  53.         
  54.         self.table.resizeColumnsToContents()
  55.         self.table.cellChanged.connect(self.cell_changed)
  56.         self.sort_by_author.setCheckable(True)
  57.         self.sort_by_author.setChecked(False)
  58.         self.sort_by_author.clicked.connect(self.do_sort_by_author)
  59.         self.author_order = 1
  60.         self.table.sortByColumn(1, Qt.AscendingOrder)
  61.         self.sort_by_author_sort.clicked.connect(self.do_sort_by_author_sort)
  62.         self.sort_by_author_sort.setCheckable(True)
  63.         self.sort_by_author_sort.setChecked(True)
  64.         self.author_sort_order = 1
  65.         self.recalc_author_sort.clicked.connect(self.do_recalc_author_sort)
  66.         if select_item is not None:
  67.             self.table.setCurrentItem(select_item)
  68.             self.table.editItem(select_item)
  69.         else:
  70.             self.table.setCurrentCell(0, 0)
  71.  
  72.     
  73.     def do_sort_by_author(self):
  74.         self.author_order = None if self.author_order == 0 else 0
  75.         self.table.sortByColumn(0, self.author_order)
  76.         self.sort_by_author.setChecked(True)
  77.         self.sort_by_author_sort.setChecked(False)
  78.  
  79.     
  80.     def do_sort_by_author_sort(self):
  81.         self.author_sort_order = None if self.author_sort_order == 0 else 0
  82.         self.table.sortByColumn(1, self.author_sort_order)
  83.         self.sort_by_author.setChecked(False)
  84.         self.sort_by_author_sort.setChecked(True)
  85.  
  86.     
  87.     def accepted(self):
  88.         self.result = []
  89.         for row in range(0, self.table.rowCount()):
  90.             id = self.table.item(row, 0).data(Qt.UserRole).toInt()[0]
  91.             aut = unicode(self.table.item(row, 0).text()).strip()
  92.             sort = unicode(self.table.item(row, 1).text()).strip()
  93.             (orig_aut, orig_sort) = self.authors[id]
  94.             if orig_aut != aut or orig_sort != sort:
  95.                 self.result.append((id, orig_aut, aut, sort))
  96.                 continue
  97.         
  98.  
  99.     
  100.     def do_recalc_author_sort(self):
  101.         self.table.cellChanged.disconnect()
  102.         for row in range(0, self.table.rowCount()):
  103.             item = self.table.item(row, 0)
  104.             aut = unicode(item.text()).strip()
  105.             c = self.table.item(row, 1)
  106.             c.setText(author_to_author_sort(aut).rstrip(','))
  107.         
  108.         self.table.setFocus(Qt.OtherFocusReason)
  109.         self.table.cellChanged.connect(self.cell_changed)
  110.  
  111.     
  112.     def cell_changed(self, row, col):
  113.         if col == 0:
  114.             item = self.table.item(row, 0)
  115.             aut = unicode(item.text()).strip()
  116.             amper = aut.find('&')
  117.             if amper >= 0:
  118.                 error_dialog(self.parent(), _('Invalid author name'), _('Author names cannot contain & characters.')).exec_()
  119.                 aut = aut.replace('&', '%')
  120.                 self.table.item(row, 0).setText(aut)
  121.             
  122.             c = self.table.item(row, 1)
  123.             c.setText(author_to_author_sort(aut))
  124.             item = c
  125.         else:
  126.             item = self.table.item(row, 1)
  127.         self.table.setCurrentItem(item)
  128.         self.table.scrollToItem(item)
  129.  
  130.  
  131.