home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1353 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  10.2 KB  |  232 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. import os
  9. from PyQt4.Qt import Qt, QModelIndex, QAbstractItemModel, QVariant, QIcon, QBrush, QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QLineEdit
  10. from calibre.gui2.preferences import ConfigWidgetBase, test_widget
  11. from calibre.gui2.preferences.plugins_ui import Ui_Form
  12. from calibre.customize.ui import initialized_plugins, is_disabled, enable_plugin, disable_plugin, customize_plugin, plugin_customization, add_plugin, remove_plugin
  13. from calibre.gui2 import NONE, error_dialog, info_dialog, choose_files
  14.  
  15. class PluginModel(QAbstractItemModel):
  16.     
  17.     def __init__(self, *args):
  18.         QAbstractItemModel.__init__(self, *args)
  19.         self.icon = QVariant(QIcon(I('plugins.png')))
  20.         p = QIcon(self.icon).pixmap(32, 32, QIcon.Disabled, QIcon.On)
  21.         self.disabled_icon = QVariant(QIcon(p))
  22.         self._p = p
  23.         self.populate()
  24.  
  25.     
  26.     def populate(self):
  27.         self._data = { }
  28.         for plugin in initialized_plugins():
  29.             if plugin.type not in self._data:
  30.                 self._data[plugin.type] = [
  31.                     plugin]
  32.                 continue
  33.             self._data[plugin.type].append(plugin)
  34.         
  35.         self.categories = sorted(self._data.keys())
  36.         for plugins in self._data.values():
  37.             plugins.sort(cmp = (lambda x, y: cmp(x.name.lower(), y.name.lower())))
  38.         
  39.  
  40.     
  41.     def index(self, row, column, parent):
  42.         if not self.hasIndex(row, column, parent):
  43.             return QModelIndex()
  44.         if parent.isValid():
  45.             return self.createIndex(row, column, 1 + parent.row())
  46.         return self.createIndex(row, column, 0)
  47.  
  48.     
  49.     def parent(self, index):
  50.         if not index.isValid() or index.internalId() == 0:
  51.             return QModelIndex()
  52.         return self.createIndex(index.internalId() - 1, 0, 0)
  53.  
  54.     
  55.     def rowCount(self, parent):
  56.         if not parent.isValid():
  57.             return len(self.categories)
  58.         if parent.internalId() == 0:
  59.             category = self.categories[parent.row()]
  60.             return len(self._data[category])
  61.         return 0
  62.  
  63.     
  64.     def columnCount(self, parent):
  65.         return 1
  66.  
  67.     
  68.     def index_to_plugin(self, index):
  69.         category = self.categories[index.parent().row()]
  70.         return self._data[category][index.row()]
  71.  
  72.     
  73.     def plugin_to_index(self, plugin):
  74.         for i, category in enumerate(self.categories):
  75.             parent = self.index(i, 0, QModelIndex())
  76.             for j, p in enumerate(self._data[category]):
  77.                 if plugin == p:
  78.                     return self.index(j, 0, parent)
  79.             
  80.         
  81.         return QModelIndex()
  82.  
  83.     
  84.     def refresh_plugin(self, plugin, rescan = False):
  85.         if rescan:
  86.             self.populate()
  87.         
  88.         idx = self.plugin_to_index(plugin)
  89.         self.dataChanged.emit(idx, idx)
  90.  
  91.     
  92.     def flags(self, index):
  93.         if not index.isValid():
  94.             return 0
  95.         if index.internalId() == 0:
  96.             return Qt.ItemIsEnabled
  97.         flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled
  98.         return flags
  99.  
  100.     
  101.     def data(self, index, role):
  102.         if not index.isValid():
  103.             return NONE
  104.         return NONE
  105.  
  106.  
  107.  
  108. class ConfigWidget(ConfigWidgetBase, Ui_Form):
  109.     supports_restoring_to_defaults = False
  110.     
  111.     def genesis(self, gui):
  112.         self.gui = gui
  113.         self._plugin_model = PluginModel()
  114.         self.plugin_view.setModel(self._plugin_model)
  115.         self.plugin_view.setStyleSheet('QTreeView::item { padding-bottom: 10px;}')
  116.         self.toggle_plugin_button.clicked.connect(self.toggle_plugin)
  117.         self.customize_plugin_button.clicked.connect(self.customize_plugin)
  118.         self.remove_plugin_button.clicked.connect(self.remove_plugin)
  119.         self.button_plugin_browse.clicked.connect(self.find_plugin)
  120.         self.button_plugin_add.clicked.connect(self.add_plugin)
  121.  
  122.     
  123.     def toggle_plugin(self, *args):
  124.         self.modify_plugin(op = 'toggle')
  125.  
  126.     
  127.     def customize_plugin(self, *args):
  128.         self.modify_plugin(op = 'customize')
  129.  
  130.     
  131.     def remove_plugin(self, *args):
  132.         self.modify_plugin(op = 'remove')
  133.  
  134.     
  135.     def add_plugin(self):
  136.         path = unicode(self.plugin_path.text())
  137.         if path and os.access(path, os.R_OK) and path.lower().endswith('.zip'):
  138.             add_plugin(path)
  139.             self._plugin_model.populate()
  140.             self._plugin_model.reset()
  141.             self.changed_signal.emit()
  142.         else:
  143.             error_dialog(self, _('No valid plugin path'), _('%s is not a valid plugin path') % path).exec_()
  144.  
  145.     
  146.     def find_plugin(self):
  147.         path = choose_files(self, 'choose plugin dialog', _('Choose plugin'), filters = [
  148.             ('Plugins', [
  149.                 'zip'])], all_files = False, select_only_single_file = True)
  150.         if path:
  151.             self.plugin_path.setText(path[0])
  152.         
  153.  
  154.     
  155.     def modify_plugin(self, op = ''):
  156.         index = self.plugin_view.currentIndex()
  157.         if index.isValid():
  158.             plugin = self._plugin_model.index_to_plugin(index)
  159.             if op == 'toggle':
  160.                 if not plugin.can_be_disabled:
  161.                     error_dialog(self, _('Plugin cannot be disabled'), _('The plugin: %s cannot be disabled') % plugin.name).exec_()
  162.                     return None
  163.                 if is_disabled(plugin):
  164.                     enable_plugin(plugin)
  165.                 else:
  166.                     disable_plugin(plugin)
  167.                 self._plugin_model.refresh_plugin(plugin)
  168.                 self.changed_signal.emit()
  169.             
  170.             if op == 'customize':
  171.                 if not plugin.is_customizable():
  172.                     info_dialog(self, _('Plugin not customizable'), _('Plugin: %s does not need customization') % plugin.name).exec_()
  173.                     return None
  174.                 self.changed_signal.emit()
  175.                 config_dialog = QDialog(self)
  176.                 button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
  177.                 v = QVBoxLayout(config_dialog)
  178.                 button_box.accepted.connect(config_dialog.accept)
  179.                 button_box.rejected.connect(config_dialog.reject)
  180.                 config_dialog.setWindowTitle(_('Customize') + ' ' + plugin.name)
  181.                 if hasattr(plugin, 'config_widget'):
  182.                     config_widget = plugin.config_widget()
  183.                     v.addWidget(config_widget)
  184.                     v.addWidget(button_box)
  185.                     config_dialog.exec_()
  186.                     if config_dialog.result() == QDialog.Accepted:
  187.                         if hasattr(config_widget, 'validate'):
  188.                             if config_widget.validate():
  189.                                 plugin.save_settings(config_widget)
  190.                             
  191.                         else:
  192.                             plugin.save_settings(config_widget)
  193.                         self._plugin_model.refresh_plugin(plugin)
  194.                     
  195.                 else:
  196.                     help_text = plugin.customization_help(gui = True)
  197.                     help_text = QLabel(help_text, config_dialog)
  198.                     help_text.setWordWrap(True)
  199.                     help_text.setTextInteractionFlags(Qt.LinksAccessibleByMouse | Qt.LinksAccessibleByKeyboard)
  200.                     help_text.setOpenExternalLinks(True)
  201.                     v.addWidget(help_text)
  202.                     sc = plugin_customization(plugin)
  203.                     if not sc:
  204.                         sc = ''
  205.                     
  206.                     sc = sc.strip()
  207.                     sc = QLineEdit(sc, config_dialog)
  208.                     v.addWidget(sc)
  209.                     v.addWidget(button_box)
  210.                     config_dialog.exec_()
  211.                     if config_dialog.result() == QDialog.Accepted:
  212.                         sc = unicode(sc.text()).strip()
  213.                         customize_plugin(plugin, sc)
  214.                     
  215.                     self._plugin_model.refresh_plugin(plugin)
  216.             elif op == 'remove':
  217.                 if remove_plugin(plugin):
  218.                     self._plugin_model.populate()
  219.                     self._plugin_model.reset()
  220.                     self.changed_signal.emit()
  221.                 else:
  222.                     error_dialog(self, _('Cannot remove builtin plugin'), plugin.name + _(' cannot be removed. It is a builtin plugin. Try disabling it instead.')).exec_()
  223.             
  224.         
  225.  
  226.  
  227. if __name__ == '__main__':
  228.     from PyQt4.Qt import QApplication
  229.     app = QApplication([])
  230.     test_widget('Advanced', 'Plugins')
  231.  
  232.