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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. from PyQt4 import QtGui
  8. from PyQt4.Qt import Qt
  9. from calibre.gui2 import error_dialog
  10. from calibre.gui2.device import device_name_for_plugboards
  11. from calibre.gui2.preferences import ConfigWidgetBase, test_widget
  12. from calibre.gui2.preferences.plugboard_ui import Ui_Form
  13. from calibre.customize.ui import metadata_writers, device_plugins
  14. from calibre.library.save_to_disk import plugboard_any_format_value, plugboard_any_device_value, plugboard_save_to_disk_value
  15. from calibre.utils.formatter import validation_formatter
  16.  
  17. class ConfigWidget(ConfigWidgetBase, Ui_Form):
  18.     
  19.     def genesis(self, gui):
  20.         self.gui = gui
  21.         self.db = gui.library_view.model().db
  22.         self.current_plugboards = self.db.prefs.get('plugboards', { })
  23.         self.current_device = None
  24.         self.current_format = None
  25.  
  26.     
  27.     def initialize(self):
  28.         
  29.         def field_cmp(x, y):
  30.             if x.startswith('#'):
  31.                 if y.startswith('#'):
  32.                     return cmp(x.lower(), y.lower())
  33.                 return 1
  34.             x.startswith('#')
  35.             if y.startswith('#'):
  36.                 return -1
  37.             return cmp(x.lower(), y.lower())
  38.  
  39.         ConfigWidgetBase.initialize(self)
  40.         if self.gui.device_manager.connected_device is not None:
  41.             self.device_label.setText(_('Device currently connected: ') + self.gui.device_manager.connected_device.__class__.__name__)
  42.         else:
  43.             self.device_label.setText(_('Device currently connected: None'))
  44.         self.devices = [
  45.             '',
  46.             'APPLE',
  47.             'FOLDER_DEVICE']
  48.         for device in device_plugins():
  49.             n = device_name_for_plugboards(device)
  50.             if n not in self.devices:
  51.                 self.devices.append(n)
  52.                 continue
  53.         
  54.         self.devices.sort(cmp = (lambda x, y: cmp(x.lower(), y.lower())))
  55.         self.devices.insert(1, plugboard_save_to_disk_value)
  56.         self.devices.insert(2, plugboard_any_device_value)
  57.         self.new_device.addItems(self.devices)
  58.         self.formats = [
  59.             '']
  60.         for w in metadata_writers():
  61.             for f in w.file_types:
  62.                 self.formats.append(f)
  63.             
  64.         
  65.         self.formats.append('device_db')
  66.         self.formats.sort()
  67.         self.formats.insert(1, plugboard_any_format_value)
  68.         self.new_format.addItems(self.formats)
  69.         self.dest_fields = [
  70.             '',
  71.             'authors',
  72.             'author_sort',
  73.             'language',
  74.             'publisher',
  75.             'tags',
  76.             'title',
  77.             'title_sort']
  78.         self.source_widgets = []
  79.         self.dest_widgets = []
  80.         for i in range(0, len(self.dest_fields) - 1):
  81.             w = QtGui.QLineEdit(self)
  82.             self.source_widgets.append(w)
  83.             self.fields_layout.addWidget(w, 5 + i, 0, 1, 1)
  84.             w = QtGui.QComboBox(self)
  85.             self.dest_widgets.append(w)
  86.             self.fields_layout.addWidget(w, 5 + i, 1, 1, 1)
  87.         
  88.         self.edit_device.currentIndexChanged[str].connect(self.edit_device_changed)
  89.         self.edit_format.currentIndexChanged[str].connect(self.edit_format_changed)
  90.         self.new_device.currentIndexChanged[str].connect(self.new_device_changed)
  91.         self.new_format.currentIndexChanged[str].connect(self.new_format_changed)
  92.         self.existing_plugboards.itemClicked.connect(self.existing_pb_clicked)
  93.         self.ok_button.clicked.connect(self.ok_clicked)
  94.         self.del_button.clicked.connect(self.del_clicked)
  95.         self.refilling = False
  96.         self.refill_all_boxes()
  97.  
  98.     
  99.     def clear_fields(self, edit_boxes = False, new_boxes = False):
  100.         self.ok_button.setEnabled(False)
  101.         self.del_button.setEnabled(False)
  102.         for w in self.source_widgets:
  103.             w.clear()
  104.         
  105.         for w in self.dest_widgets:
  106.             w.clear()
  107.         
  108.         if edit_boxes:
  109.             self.edit_device.setCurrentIndex(0)
  110.             self.edit_format.setCurrentIndex(0)
  111.         
  112.         if new_boxes:
  113.             self.new_device.setCurrentIndex(0)
  114.             self.new_format.setCurrentIndex(0)
  115.         
  116.  
  117.     
  118.     def set_fields(self):
  119.         self.ok_button.setEnabled(True)
  120.         self.del_button.setEnabled(True)
  121.         for w in self.source_widgets:
  122.             w.clear()
  123.         
  124.         for w in self.dest_widgets:
  125.             w.addItems(self.dest_fields)
  126.         
  127.  
  128.     
  129.     def set_field(self, i, src, dst):
  130.         self.source_widgets[i].setText(src)
  131.         idx = self.dest_fields.index(dst)
  132.         self.dest_widgets[i].setCurrentIndex(idx)
  133.  
  134.     
  135.     def edit_device_changed(self, txt):
  136.         self.current_device = None
  137.         if txt == '':
  138.             self.clear_fields(new_boxes = False)
  139.             return None
  140.         self.clear_fields(new_boxes = True)
  141.         self.current_device = unicode(txt)
  142.         fpb = self.current_plugboards.get(self.current_format, None)
  143.         if fpb is None:
  144.             print 'edit_device_changed: none format!'
  145.             return None
  146.         dpb = fpb.get(self.current_device, None)
  147.         if dpb is None:
  148.             print 'edit_device_changed: none device!'
  149.             return None
  150.         self.set_fields()
  151.         for i, op in enumerate(dpb):
  152.             self.set_field(i, op[0], op[1])
  153.         
  154.         self.ok_button.setEnabled(True)
  155.         self.del_button.setEnabled(True)
  156.  
  157.     
  158.     def edit_format_changed(self, txt):
  159.         self.edit_device.setCurrentIndex(0)
  160.         self.current_device = None
  161.         self.current_format = None
  162.         if txt == '':
  163.             self.clear_fields(new_boxes = False)
  164.             return None
  165.         self.clear_fields(new_boxes = True)
  166.         txt = unicode(txt)
  167.         fpb = self.current_plugboards.get(txt, None)
  168.         if fpb is None:
  169.             print 'edit_format_changed: none editable format!'
  170.             return None
  171.         self.current_format = txt
  172.         devices = [
  173.             '']
  174.         for d in fpb:
  175.             devices.append(d)
  176.         
  177.         self.edit_device.clear()
  178.         self.edit_device.addItems(devices)
  179.  
  180.     
  181.     def new_device_changed(self, txt):
  182.         self.current_device = None
  183.         if txt == '':
  184.             self.clear_fields(edit_boxes = False)
  185.             return None
  186.         self.clear_fields(edit_boxes = True)
  187.         self.current_device = unicode(txt)
  188.         error = False
  189.         if self.current_format == plugboard_any_format_value:
  190.             for f in self.current_plugboards:
  191.                 devs = set(self.current_plugboards[f])
  192.                 if self.current_device != plugboard_save_to_disk_value and plugboard_any_device_value in devs:
  193.                     error = True
  194.                     break
  195.                 
  196.                 if self.current_device in devs:
  197.                     error = True
  198.                     break
  199.                 
  200.                 if self.current_device == plugboard_any_device_value:
  201.                     error = True
  202.                     break
  203.                     continue
  204.             
  205.         else:
  206.             for f in self.current_plugboards:
  207.                 devs = set(self.current_plugboards[f])
  208.                 if f == plugboard_any_format_value and self.current_device in devs:
  209.                     error = True
  210.                     break
  211.                 
  212.                 if f == self.current_format and self.current_device in devs:
  213.                     error = True
  214.                     break
  215.                 
  216.                 if f == self.current_format and plugboard_any_device_value in devs:
  217.                     error = True
  218.                     break
  219.                     continue
  220.             
  221.         if error:
  222.             error_dialog(self, '', _('That format and device already has a plugboard or conflicts with another plugboard.'), show = True)
  223.             self.new_device.setCurrentIndex(0)
  224.             return None
  225.         self.set_fields()
  226.  
  227.     
  228.     def new_format_changed(self, txt):
  229.         self.current_format = None
  230.         self.current_device = None
  231.         self.new_device.setCurrentIndex(0)
  232.         if txt:
  233.             self.clear_fields(edit_boxes = True)
  234.             self.current_format = unicode(txt)
  235.         else:
  236.             self.clear_fields(edit_boxes = False)
  237.  
  238.     
  239.     def ok_clicked(self):
  240.         pb = []
  241.         for i in range(0, len(self.source_widgets)):
  242.             s = unicode(self.source_widgets[i].text())
  243.             if s:
  244.                 d = self.dest_widgets[i].currentIndex()
  245.                 if d != 0:
  246.                     
  247.                     try:
  248.                         validation_formatter.validate(s)
  249.                     except Exception:
  250.                         err = None
  251.                         error_dialog(self, _('Invalid template'), '<p>' + _('The template %s is invalid:') % s + '<br>' + str(err), show = True)
  252.                         return None
  253.  
  254.                     pb.append((s, self.dest_fields[d]))
  255.                 else:
  256.                     error_dialog(self, _('Invalid destination'), '<p>' + _('The destination field cannot be blank'), show = True)
  257.                     return None
  258.             d != 0
  259.         
  260.         if len(pb) == 0:
  261.             if self.current_format in self.current_plugboards:
  262.                 fpb = self.current_plugboards[self.current_format]
  263.                 if self.current_device in fpb:
  264.                     del fpb[self.current_device]
  265.                 
  266.                 if len(fpb) == 0:
  267.                     del self.current_plugboards[self.current_format]
  268.                 
  269.             
  270.         elif self.current_format not in self.current_plugboards:
  271.             self.current_plugboards[self.current_format] = { }
  272.         
  273.         fpb = self.current_plugboards[self.current_format]
  274.         fpb[self.current_device] = pb
  275.         self.changed_signal.emit()
  276.         self.refill_all_boxes()
  277.  
  278.     
  279.     def del_clicked(self):
  280.         if self.current_format in self.current_plugboards:
  281.             fpb = self.current_plugboards[self.current_format]
  282.             if self.current_device in fpb:
  283.                 del fpb[self.current_device]
  284.             
  285.             if len(fpb) == 0:
  286.                 del self.current_plugboards[self.current_format]
  287.             
  288.         
  289.         self.changed_signal.emit()
  290.         self.refill_all_boxes()
  291.  
  292.     
  293.     def existing_pb_clicked(self, Qitem):
  294.         item = Qitem.data(Qt.UserRole).toPyObject()
  295.         self.edit_format.setCurrentIndex(self.edit_format.findText(item[0]))
  296.         self.edit_device.setCurrentIndex(self.edit_device.findText(item[1]))
  297.  
  298.     
  299.     def refill_all_boxes(self):
  300.         if self.refilling:
  301.             return None
  302.         self.refilling = True
  303.         self.current_device = None
  304.         self.current_format = None
  305.         self.clear_fields(new_boxes = True)
  306.         self.edit_format.clear()
  307.         self.edit_format.addItem('')
  308.         for format in self.current_plugboards:
  309.             self.edit_format.addItem(format)
  310.         
  311.         self.edit_format.setCurrentIndex(0)
  312.         self.edit_device.clear()
  313.         self.ok_button.setEnabled(False)
  314.         self.del_button.setEnabled(False)
  315.         self.existing_plugboards.clear()
  316.         for f in self.formats:
  317.             if f not in self.current_plugboards:
  318.                 continue
  319.             
  320.             for d in self.devices:
  321.                 if d not in self.current_plugboards[f]:
  322.                     continue
  323.                 
  324.                 ops = []
  325.                 for op in self.current_plugboards[f][d]:
  326.                     ops.append('([' + op[0] + '] -> ' + op[1] + ')')
  327.                 
  328.                 txt = '%s:%s = %s\n' % (f, d, ', '.join(ops))
  329.                 item = QtGui.QListWidgetItem(txt)
  330.                 item.setData(Qt.UserRole, (f, d))
  331.                 self.existing_plugboards.addItem(item)
  332.             
  333.         
  334.         self.refilling = False
  335.  
  336.     
  337.     def restore_defaults(self):
  338.         ConfigWidgetBase.restore_defaults(self)
  339.         self.current_plugboards = { }
  340.         self.refill_all_boxes()
  341.         self.changed_signal.emit()
  342.  
  343.     
  344.     def commit(self):
  345.         self.db.prefs.set('plugboards', self.current_plugboards)
  346.         return ConfigWidgetBase.commit(self)
  347.  
  348.     
  349.     def refresh_gui(self, gui):
  350.         pass
  351.  
  352.  
  353. if __name__ == '__main__':
  354.     from PyQt4.Qt import QApplication
  355.     app = QApplication([])
  356.     test_widget('Import/Export', 'plugboards')
  357.  
  358.