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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import textwrap
  9. from functools import partial
  10. from PyQt4.Qt import QWidget, QSpinBox, QDoubleSpinBox, QLineEdit, QTextEdit, QCheckBox, QComboBox, Qt, QIcon, pyqtSignal
  11. from calibre.customize.conversion import OptionRecommendation
  12. from calibre.ebooks.conversion.config import load_defaults, save_defaults as save_defaults_, load_specifics, GuiRecommendations
  13. from calibre import prepare_string_for_xml
  14. from calibre.customize.ui import plugin_for_input_format
  15.  
  16. def config_widget_for_input_plugin(plugin):
  17.     name = plugin.name.lower().replace(' ', '_')
  18.     
  19.     try:
  20.         return __import__('calibre.gui2.convert.' + name, fromlist = [
  21.             1]).PluginWidget
  22.     except ImportError:
  23.         pass
  24.  
  25.  
  26.  
  27. def bulk_defaults_for_input_format(fmt):
  28.     plugin = plugin_for_input_format(fmt)
  29.     if plugin is not None:
  30.         w = config_widget_for_input_plugin(plugin)
  31.         if w is not None:
  32.             return load_defaults(w.COMMIT_NAME)
  33.     
  34.     return { }
  35.  
  36.  
  37. class Widget(QWidget):
  38.     TITLE = _('Unknown')
  39.     ICON = I('config.png')
  40.     HELP = ''
  41.     COMMIT_NAME = None
  42.     changed_signal = pyqtSignal()
  43.     set_help = pyqtSignal(object)
  44.     
  45.     def __init__(self, parent, options):
  46.         QWidget.__init__(self, parent)
  47.         self.setupUi(self)
  48.         self._options = options
  49.         self._name = self.commit_name = self.COMMIT_NAME
  50.         self._icon = QIcon(self.ICON)
  51.         for name in self._options:
  52.             if not hasattr(self, 'opt_' + name):
  53.                 raise Exception('Option %s missing in %s' % (name, self.__class__.__name__))
  54.             hasattr(self, 'opt_' + name)
  55.             self.connect_gui_obj(getattr(self, 'opt_' + name))
  56.         
  57.  
  58.     
  59.     def initialize_options(self, get_option, get_help, db = None, book_id = None):
  60.         defaults = load_defaults(self._name)
  61.         defaults.merge_recommendations(get_option, OptionRecommendation.LOW, self._options)
  62.         if db is not None:
  63.             specifics = load_specifics(db, book_id)
  64.             specifics.merge_recommendations(get_option, OptionRecommendation.HIGH, self._options, only_existing = True)
  65.             defaults.update(specifics)
  66.         
  67.         self.apply_recommendations(defaults)
  68.         self.setup_help(get_help)
  69.  
  70.     
  71.     def restore_defaults(self, get_option):
  72.         defaults = GuiRecommendations()
  73.         defaults.merge_recommendations(get_option, OptionRecommendation.LOW, self._options)
  74.         self.apply_recommendations(defaults)
  75.  
  76.     
  77.     def commit_options(self, save_defaults = False):
  78.         recs = self.create_recommendations()
  79.         if save_defaults:
  80.             save_defaults_(self.commit_name, recs)
  81.         
  82.         return recs
  83.  
  84.     
  85.     def create_recommendations(self):
  86.         recs = GuiRecommendations()
  87.         for name in self._options:
  88.             gui_opt = getattr(self, 'opt_' + name, None)
  89.             if gui_opt is None:
  90.                 continue
  91.             
  92.             recs[name] = self.get_value(gui_opt)
  93.         
  94.         return recs
  95.  
  96.     
  97.     def apply_recommendations(self, recs):
  98.         for name, val in recs.items():
  99.             gui_opt = getattr(self, 'opt_' + name, None)
  100.             if gui_opt is None:
  101.                 continue
  102.             
  103.             self.set_value(gui_opt, val)
  104.             if name in getattr(recs, 'disabled_options', []):
  105.                 gui_opt.setDisabled(True)
  106.                 continue
  107.         
  108.  
  109.     
  110.     def get_value(self, g):
  111.         XPathEdit = XPathEdit
  112.         import calibre.gui2.convert.xpath_wizard
  113.         RegexEdit = RegexEdit
  114.         import calibre.gui2.convert.regex_builder
  115.         ret = self.get_value_handler(g)
  116.         if ret != 'this is a dummy return value, xcswx1avcx4x':
  117.             return ret
  118.         if isinstance(g, (QSpinBox, QDoubleSpinBox)):
  119.             return g.value()
  120.         if isinstance(g, (QLineEdit, QTextEdit)):
  121.             func = getattr(g, 'toPlainText', getattr(g, 'text', None))()
  122.             ans = unicode(func).strip()
  123.             return ans
  124.         if isinstance(g, QComboBox):
  125.             return unicode(g.currentText())
  126.         if isinstance(g, QCheckBox):
  127.             return bool(g.isChecked())
  128.         if isinstance(g, XPathEdit):
  129.             if g.xpath:
  130.                 return g.xpath
  131.             return None
  132.         if isinstance(g, RegexEdit):
  133.             if g.regex:
  134.                 return g.regex
  135.             return None
  136.         raise Exception("Can't get value from %s" % type(g))
  137.  
  138.     
  139.     def gui_obj_changed(self, gui_obj, *args):
  140.         self.changed_signal.emit()
  141.  
  142.     
  143.     def connect_gui_obj(self, g):
  144.         f = partial(self.gui_obj_changed, g)
  145.         
  146.         try:
  147.             self.connect_gui_obj_handler(g, f)
  148.             return None
  149.         except NotImplementedError:
  150.             pass
  151.  
  152.         XPathEdit = XPathEdit
  153.         import calibre.gui2.convert.xpath_wizard
  154.         RegexEdit = RegexEdit
  155.         import calibre.gui2.convert.regex_builder
  156.         if isinstance(g, (QSpinBox, QDoubleSpinBox)):
  157.             g.valueChanged.connect(f)
  158.         elif isinstance(g, (QLineEdit, QTextEdit)):
  159.             g.textChanged.connect(f)
  160.         elif isinstance(g, QComboBox):
  161.             g.editTextChanged.connect(f)
  162.             g.currentIndexChanged.connect(f)
  163.         elif isinstance(g, QCheckBox):
  164.             g.stateChanged.connect(f)
  165.         elif isinstance(g, (XPathEdit, RegexEdit)):
  166.             g.edit.editTextChanged.connect(f)
  167.             g.edit.currentIndexChanged.connect(f)
  168.         else:
  169.             raise Exception("Can't connect %s" % type(g))
  170.         return isinstance(g, (QSpinBox, QDoubleSpinBox))
  171.  
  172.     
  173.     def connect_gui_obj_handler(self, gui_obj, slot):
  174.         raise NotImplementedError()
  175.  
  176.     
  177.     def set_value(self, g, val):
  178.         XPathEdit = XPathEdit
  179.         import calibre.gui2.convert.xpath_wizard
  180.         RegexEdit = RegexEdit
  181.         import calibre.gui2.convert.regex_builder
  182.         if self.set_value_handler(g, val):
  183.             return None
  184.         if isinstance(g, (QSpinBox, QDoubleSpinBox)):
  185.             g.setValue(val)
  186.         elif isinstance(g, (QLineEdit, QTextEdit)):
  187.             if not val:
  188.                 val = ''
  189.             
  190.             getattr(g, 'setPlainText', g.setText)(val)
  191.             getattr(g, 'setCursorPosition', (lambda x: x))(0)
  192.         elif isinstance(g, QComboBox) and val:
  193.             idx = g.findText(val, Qt.MatchFixedString)
  194.             if idx < 0:
  195.                 g.addItem(val)
  196.                 idx = g.findText(val, Qt.MatchFixedString)
  197.             
  198.             g.setCurrentIndex(idx)
  199.         elif isinstance(g, QCheckBox):
  200.             None(g.setCheckState if bool(val) else Qt.Unchecked)
  201.         elif isinstance(g, (XPathEdit, RegexEdit)):
  202.             None(g.edit.setText if val else '')
  203.         else:
  204.             raise Exception("Can't set value %s in %s" % (repr(val), unicode(g.objectName())))
  205.         val.post_set_value(g, val)
  206.  
  207.     
  208.     def set_help(self, msg):
  209.         if msg and getattr(msg, 'strip', (lambda : True))():
  210.             
  211.             try:
  212.                 self.set_help.emit(msg)
  213.  
  214.         
  215.  
  216.     
  217.     def setup_help(self, help_provider):
  218.         w = textwrap.TextWrapper(80)
  219.         for name in self._options:
  220.             g = getattr(self, 'opt_' + name, None)
  221.             if g is None:
  222.                 continue
  223.             
  224.             help = help_provider(name)
  225.             if not help:
  226.                 continue
  227.             
  228.             g._help = help
  229.             htext = u'<div>%s</div>' % prepare_string_for_xml('\n'.join(w.wrap(help)))
  230.             g.setToolTip(htext)
  231.             g.setWhatsThis(htext)
  232.             
  233.             g.__class__.enterEvent = lambda obj, event: self.set_help(getattr(obj, '_help', obj.toolTip()))
  234.         
  235.  
  236.     
  237.     def set_value_handler(self, g, val):
  238.         return False
  239.  
  240.     
  241.     def post_set_value(self, g, val):
  242.         pass
  243.  
  244.     
  245.     def get_value_handler(self, g):
  246.         return 'this is a dummy return value, xcswx1avcx4x'
  247.  
  248.     
  249.     def post_get_value(self, g):
  250.         pass
  251.  
  252.     
  253.     def break_cycles(self):
  254.         self.db = None
  255.  
  256.     
  257.     def pre_commit_check(self):
  258.         return True
  259.  
  260.     
  261.     def commit(self, save_defaults = False):
  262.         return self.commit_options(save_defaults)
  263.  
  264.     
  265.     def config_title(self):
  266.         return self.TITLE
  267.  
  268.     
  269.     def config_icon(self):
  270.         return self._icon
  271.  
  272.  
  273.