home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1216 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  8.0 KB  |  202 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 PyQt4.Qt import QWidget, QSpinBox, QDoubleSpinBox, QLineEdit, QTextEdit, QCheckBox, QComboBox, Qt, QIcon, SIGNAL
  10. from calibre.customize.conversion import OptionRecommendation
  11. from calibre.ebooks.conversion.config import load_defaults, save_defaults as save_defaults_, load_specifics, GuiRecommendations
  12. from calibre import prepare_string_for_xml
  13.  
  14. class Widget(QWidget):
  15.     TITLE = _('Unknown')
  16.     ICON = I('config.svg')
  17.     HELP = ''
  18.     
  19.     def __init__(self, parent, name, options):
  20.         QWidget.__init__(self, parent)
  21.         self.setupUi(self)
  22.         self._options = options
  23.         self._name = name
  24.         self._icon = QIcon(self.ICON)
  25.         for name in self._options:
  26.             if not hasattr(self, 'opt_' + name):
  27.                 raise Exception('Option %s missing in %s' % (name, self.__class__.__name__))
  28.             hasattr(self, 'opt_' + name)
  29.         
  30.  
  31.     
  32.     def initialize_options(self, get_option, get_help, db = None, book_id = None):
  33.         defaults = load_defaults(self._name)
  34.         defaults.merge_recommendations(get_option, OptionRecommendation.LOW, self._options)
  35.         if db is not None:
  36.             specifics = load_specifics(db, book_id)
  37.             specifics.merge_recommendations(get_option, OptionRecommendation.HIGH, self._options, only_existing = True)
  38.             defaults.update(specifics)
  39.         
  40.         self.apply_recommendations(defaults)
  41.         self.setup_help(get_help)
  42.  
  43.     
  44.     def commit_options(self, save_defaults = False):
  45.         recs = self.create_recommendations()
  46.         if save_defaults:
  47.             save_defaults_(self._name, recs)
  48.         
  49.         return recs
  50.  
  51.     
  52.     def create_recommendations(self):
  53.         recs = GuiRecommendations()
  54.         for name in self._options:
  55.             gui_opt = getattr(self, 'opt_' + name, None)
  56.             if gui_opt is None:
  57.                 continue
  58.             
  59.             recs[name] = self.get_value(gui_opt)
  60.         
  61.         return recs
  62.  
  63.     
  64.     def apply_recommendations(self, recs):
  65.         for name, val in recs.items():
  66.             gui_opt = getattr(self, 'opt_' + name, None)
  67.             if gui_opt is None:
  68.                 continue
  69.             
  70.             self.set_value(gui_opt, val)
  71.             if name in getattr(recs, 'disabled_options', []):
  72.                 gui_opt.setDisabled(True)
  73.                 continue
  74.         
  75.  
  76.     
  77.     def get_value(self, g):
  78.         XPathEdit = XPathEdit
  79.         import calibre.gui2.convert.xpath_wizard
  80.         RegexEdit = RegexEdit
  81.         import calibre.gui2.convert.regex_builder
  82.         ret = self.get_value_handler(g)
  83.         if ret != 'this is a dummy return value, xcswx1avcx4x':
  84.             return ret
  85.         if isinstance(g, (QSpinBox, QDoubleSpinBox)):
  86.             return g.value()
  87.         if isinstance(g, (QLineEdit, QTextEdit)):
  88.             func = getattr(g, 'toPlainText', getattr(g, 'text', None))()
  89.             ans = unicode(func).strip()
  90.             return ans
  91.         if isinstance(g, QComboBox):
  92.             return unicode(g.currentText())
  93.         if isinstance(g, QCheckBox):
  94.             return bool(g.isChecked())
  95.         if isinstance(g, XPathEdit):
  96.             if g.xpath:
  97.                 return g.xpath
  98.             return None
  99.         if isinstance(g, RegexEdit):
  100.             if g.regex:
  101.                 return g.regex
  102.             return None
  103.         raise Exception("Can't get value from %s" % type(g))
  104.  
  105.     
  106.     def set_value(self, g, val):
  107.         XPathEdit = XPathEdit
  108.         import calibre.gui2.convert.xpath_wizard
  109.         RegexEdit = RegexEdit
  110.         import calibre.gui2.convert.regex_builder
  111.         if self.set_value_handler(g, val):
  112.             return None
  113.         if isinstance(g, (QSpinBox, QDoubleSpinBox)):
  114.             g.setValue(val)
  115.         elif isinstance(g, (QLineEdit, QTextEdit)):
  116.             if not val:
  117.                 val = ''
  118.             
  119.             getattr(g, 'setPlainText', g.setText)(val)
  120.             getattr(g, 'setCursorPosition', (lambda x: x))(0)
  121.         elif isinstance(g, QComboBox) and val:
  122.             idx = g.findText(val, Qt.MatchFixedString)
  123.             if idx < 0:
  124.                 g.addItem(val)
  125.                 idx = g.findText(val, Qt.MatchFixedString)
  126.             
  127.             g.setCurrentIndex(idx)
  128.         elif isinstance(g, QCheckBox):
  129.             None(g.setCheckState if bool(val) else Qt.Unchecked)
  130.         elif isinstance(g, (XPathEdit, RegexEdit)):
  131.             None(g.edit.setText if val else '')
  132.         else:
  133.             raise Exception("Can't set value %s in %s" % (repr(val), unicode(g.objectName())))
  134.         val.post_set_value(g, val)
  135.  
  136.     
  137.     def set_help(self, msg):
  138.         if msg and getattr(msg, 'strip', (lambda : True))():
  139.             
  140.             try:
  141.                 self.emit(SIGNAL('set_help(PyQt_PyObject)'), msg)
  142.  
  143.         
  144.  
  145.     
  146.     def setup_help(self, help_provider):
  147.         w = textwrap.TextWrapper(80)
  148.         for name in self._options:
  149.             g = getattr(self, 'opt_' + name, None)
  150.             if g is None:
  151.                 continue
  152.             
  153.             help = help_provider(name)
  154.             if not help:
  155.                 continue
  156.             
  157.             g._help = help
  158.             htext = u'<div>%s</div>' % prepare_string_for_xml('\n'.join(w.wrap(help)))
  159.             g.setToolTip(htext)
  160.             g.setWhatsThis(htext)
  161.             
  162.             g.__class__.enterEvent = lambda obj, event: self.set_help(getattr(obj, '_help', obj.toolTip()))
  163.         
  164.  
  165.     
  166.     def set_value_handler(self, g, val):
  167.         return False
  168.  
  169.     
  170.     def post_set_value(self, g, val):
  171.         pass
  172.  
  173.     
  174.     def get_value_handler(self, g):
  175.         return 'this is a dummy return value, xcswx1avcx4x'
  176.  
  177.     
  178.     def post_get_value(self, g):
  179.         pass
  180.  
  181.     
  182.     def break_cycles(self):
  183.         self.db = None
  184.  
  185.     
  186.     def pre_commit_check(self):
  187.         return True
  188.  
  189.     
  190.     def commit(self, save_defaults = False):
  191.         return self.commit_options(save_defaults)
  192.  
  193.     
  194.     def config_title(self):
  195.         return self.TITLE
  196.  
  197.     
  198.     def config_icon(self):
  199.         return self._icon
  200.  
  201.  
  202.