home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1368 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  12.2 KB  |  337 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. import textwrap
  8. from PyQt4.Qt import QWidget, pyqtSignal, QCheckBox, QAbstractSpinBox, QLineEdit, QComboBox, QVariant
  9. from calibre.customize.ui import preferences_plugins
  10. from calibre.utils.config import ConfigProxy
  11.  
  12. class AbortCommit(Exception):
  13.     pass
  14.  
  15.  
  16. class ConfigWidgetInterface(object):
  17.     changed_signal = None
  18.     supports_restoring_to_defaults = True
  19.     restore_defaults_desc = _('Restore settings to default values. You have to click Apply to actually save the default settings.')
  20.     restart_critical = False
  21.     
  22.     def genesis(self, gui):
  23.         raise NotImplementedError()
  24.  
  25.     
  26.     def initialize(self):
  27.         raise NotImplementedError()
  28.  
  29.     
  30.     def restore_defaults(self):
  31.         pass
  32.  
  33.     
  34.     def commit(self):
  35.         return False
  36.  
  37.     
  38.     def refresh_gui(self, gui):
  39.         pass
  40.  
  41.  
  42.  
  43. class Setting(object):
  44.     
  45.     def __init__(self, name, config_obj, widget, gui_name = None, empty_string_is_None = True, choices = None, restart_required = False):
  46.         self.name = name
  47.         self.gui_name = gui_name
  48.         self.empty_string_is_None = empty_string_is_None
  49.         self.restart_required = restart_required
  50.         self.choices = choices
  51.         if gui_name is None:
  52.             self.gui_name = 'opt_' + name
  53.         
  54.         self.config_obj = config_obj
  55.         self.gui_obj = getattr(widget, self.gui_name)
  56.         self.widget = widget
  57.         if isinstance(self.gui_obj, QCheckBox):
  58.             self.datatype = 'bool'
  59.             self.gui_obj.stateChanged.connect(self.changed)
  60.         elif isinstance(self.gui_obj, QAbstractSpinBox):
  61.             self.datatype = 'number'
  62.             self.gui_obj.valueChanged.connect(self.changed)
  63.         elif isinstance(self.gui_obj, QLineEdit):
  64.             self.datatype = 'string'
  65.             self.gui_obj.textChanged.connect(self.changed)
  66.         elif isinstance(self.gui_obj, QComboBox):
  67.             self.datatype = 'choice'
  68.             self.gui_obj.editTextChanged.connect(self.changed)
  69.             self.gui_obj.currentIndexChanged.connect(self.changed)
  70.         else:
  71.             raise ValueError('Unknown data type')
  72.         if isinstance(self.gui_obj, QCheckBox)(self.config_obj, ConfigProxy) and not unicode(self.gui_obj.toolTip()):
  73.             h = self.config_obj.help(self.name)
  74.             if h:
  75.                 self.gui_obj.setToolTip(h)
  76.             
  77.         
  78.         tt = unicode(self.gui_obj.toolTip())
  79.         if tt:
  80.             if not unicode(self.gui_obj.whatsThis()):
  81.                 self.gui_obj.setWhatsThis(tt)
  82.             
  83.             if not unicode(self.gui_obj.statusTip()):
  84.                 self.gui_obj.setStatusTip(tt)
  85.             
  86.             tt = '\n'.join(textwrap.wrap(tt, 70))
  87.             self.gui_obj.setToolTip(tt)
  88.         
  89.  
  90.     
  91.     def changed(self, *args):
  92.         self.widget.changed_signal.emit()
  93.  
  94.     
  95.     def initialize(self):
  96.         self.gui_obj.blockSignals(True)
  97.         if self.datatype == 'choice':
  98.             self.gui_obj.clear()
  99.             for x in self.choices:
  100.                 if isinstance(x, basestring):
  101.                     x = (x, x)
  102.                 
  103.                 self.gui_obj.addItem(x[0], QVariant(x[1]))
  104.             
  105.         
  106.         self.set_gui_val(self.get_config_val(default = False))
  107.         self.gui_obj.blockSignals(False)
  108.         self.initial_value = self.get_gui_val()
  109.  
  110.     
  111.     def commit(self):
  112.         val = self.get_gui_val()
  113.         oldval = self.get_config_val()
  114.         changed = val != oldval
  115.         if changed:
  116.             self.set_config_val(self.get_gui_val())
  117.         
  118.         if changed:
  119.             pass
  120.         return self.restart_required
  121.  
  122.     
  123.     def restore_defaults(self):
  124.         self.set_gui_val(self.get_config_val(default = True))
  125.  
  126.     
  127.     def get_config_val(self, default = False):
  128.         if default:
  129.             val = self.config_obj.defaults[self.name]
  130.         else:
  131.             val = self.config_obj[self.name]
  132.         return val
  133.  
  134.     
  135.     def set_config_val(self, val):
  136.         self.config_obj[self.name] = val
  137.  
  138.     
  139.     def set_gui_val(self, val):
  140.         if self.datatype == 'bool':
  141.             self.gui_obj.setChecked(bool(val))
  142.         elif self.datatype == 'number':
  143.             self.gui_obj.setValue(val)
  144.         elif self.datatype == 'string':
  145.             None(self.gui_obj.setText if val else '')
  146.         elif self.datatype == 'choice':
  147.             idx = self.gui_obj.findData(QVariant(val))
  148.             if idx == -1:
  149.                 idx = 0
  150.             
  151.             self.gui_obj.setCurrentIndex(idx)
  152.         
  153.  
  154.     
  155.     def get_gui_val(self):
  156.         if self.datatype == 'bool':
  157.             val = bool(self.gui_obj.isChecked())
  158.         elif self.datatype == 'number':
  159.             val = self.gui_obj.value()
  160.         elif self.datatype == 'string':
  161.             val = unicode(self.gui_obj.text()).strip()
  162.             if self.empty_string_is_None and not val:
  163.                 val = None
  164.             
  165.         elif self.datatype == 'choice':
  166.             idx = self.gui_obj.currentIndex()
  167.             if idx < 0:
  168.                 idx = 0
  169.             
  170.             val = unicode(self.gui_obj.itemData(idx).toString())
  171.         
  172.         return val
  173.  
  174.  
  175.  
  176. class CommaSeparatedList(Setting):
  177.     
  178.     def set_gui_val(self, val):
  179.         x = ''
  180.         if val:
  181.             x = u', '.join(val)
  182.         
  183.         self.gui_obj.setText(x)
  184.  
  185.     
  186.     def get_gui_val(self):
  187.         val = unicode(self.gui_obj.text()).strip()
  188.         ans = []
  189.         return ans
  190.  
  191.  
  192.  
  193. class ConfigWidgetBase(QWidget, ConfigWidgetInterface):
  194.     changed_signal = pyqtSignal()
  195.     supports_restoring_to_defaults = True
  196.     restart_critical = False
  197.     
  198.     def __init__(self, parent = None):
  199.         QWidget.__init__(self, parent)
  200.         if hasattr(self, 'setupUi'):
  201.             self.setupUi(self)
  202.         
  203.         self.settings = { }
  204.  
  205.     
  206.     def register(self, name, config_obj, gui_name = None, choices = None, restart_required = False, empty_string_is_None = True, setting = Setting):
  207.         setting = setting(name, config_obj, self, gui_name = gui_name, choices = choices, restart_required = restart_required, empty_string_is_None = empty_string_is_None)
  208.         return self.register_setting(setting)
  209.  
  210.     
  211.     def register_setting(self, setting):
  212.         self.settings[setting.name] = setting
  213.         return setting
  214.  
  215.     
  216.     def initialize(self):
  217.         for setting in self.settings.values():
  218.             setting.initialize()
  219.         
  220.  
  221.     
  222.     def commit(self, *args):
  223.         restart_required = False
  224.         for setting in self.settings.values():
  225.             rr = setting.commit()
  226.             if rr:
  227.                 restart_required = True
  228.                 continue
  229.         
  230.         return restart_required
  231.  
  232.     
  233.     def restore_defaults(self, *args):
  234.         for setting in self.settings.values():
  235.             setting.restore_defaults()
  236.         
  237.  
  238.  
  239.  
  240. def get_plugin(category, name):
  241.     for plugin in preferences_plugins():
  242.         if plugin.category == category and plugin.name == name:
  243.             return plugin
  244.     
  245.     raise ValueError('No Preferences Plugin with category: %s and name: %s found' % (category, name))
  246.  
  247.  
  248. def init_gui():
  249.     Main = Main
  250.     import calibre.gui2.ui
  251.     option_parser = option_parser
  252.     import calibre.gui2.main
  253.     db = db
  254.     import calibre.library
  255.     parser = option_parser()
  256.     (opts, args) = parser.parse_args([])
  257.     actions = tuple(Main.create_application_menubar())
  258.     db = db()
  259.     gui = Main(opts)
  260.     gui.initialize(db.library_path, db, None, actions, show_gui = False)
  261.     return gui
  262.  
  263.  
  264. def test_widget(category, name, gui = None):
  265.     QDialog = QDialog
  266.     QVBoxLayout = QVBoxLayout
  267.     QDialogButtonBox = QDialogButtonBox
  268.     import PyQt4.Qt
  269.     
  270.     class Dialog((QDialog,)):
  271.         
  272.         def set_widget(self, w):
  273.             self.w = w
  274.  
  275.         
  276.         def accept(self):
  277.             
  278.             try:
  279.                 self.restart_required = self.w.commit()
  280.             except AbortCommit:
  281.                 return None
  282.  
  283.             QDialog.accept(self)
  284.  
  285.  
  286.     pl = get_plugin(category, name)
  287.     d = Dialog()
  288.     d.resize(750, 550)
  289.     d.setWindowTitle(category + ' - ' + name)
  290.     bb = QDialogButtonBox(d)
  291.     bb.setStandardButtons(bb.Apply | bb.Cancel | bb.RestoreDefaults)
  292.     bb.accepted.connect(d.accept)
  293.     bb.rejected.connect(d.reject)
  294.     w = pl.create_widget(d)
  295.     d.set_widget(w)
  296.     bb.button(bb.RestoreDefaults).clicked.connect(w.restore_defaults)
  297.     bb.button(bb.RestoreDefaults).setEnabled(w.supports_restoring_to_defaults)
  298.     bb.button(bb.Apply).setEnabled(False)
  299.     bb.button(bb.Apply).clicked.connect(d.accept)
  300.     (w.changed_signal.connect,)((lambda : bb.button(bb.Apply).setEnabled(True)))
  301.     l = QVBoxLayout()
  302.     d.setLayout(l)
  303.     l.addWidget(w)
  304.     l.addWidget(bb)
  305.     mygui = gui is None
  306.     if gui is None:
  307.         gui = init_gui()
  308.         mygui = True
  309.     
  310.     w.genesis(gui)
  311.     w.initialize()
  312.     d.exec_()
  313.     if getattr(d, 'restart_required', False):
  314.         warning_dialog = warning_dialog
  315.         import calibre.gui2
  316.         warning_dialog(gui, 'Restart required', 'Restart required', show = True)
  317.     
  318.     if mygui:
  319.         gui.shutdown()
  320.     
  321.  
  322.  
  323. def test_all():
  324.     QApplication = QApplication
  325.     import PyQt4.Qt
  326.     app = QApplication([])
  327.     app
  328.     gui = init_gui()
  329.     for plugin in preferences_plugins():
  330.         test_widget(plugin.category, plugin.name, gui = gui)
  331.     
  332.     gui.shutdown()
  333.  
  334. if __name__ == '__main__':
  335.     test_all()
  336.  
  337.