home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1364 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  12.0 KB  |  298 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 functools import partial
  8. from PyQt4.Qt import QAbstractListModel, Qt, QIcon, QVariant, QItemSelectionModel
  9. from calibre.gui2.preferences.toolbar_ui import Ui_Form
  10. from calibre.gui2 import gprefs, NONE, warning_dialog
  11. from calibre.gui2.preferences import ConfigWidgetBase, test_widget
  12.  
  13. class FakeAction(object):
  14.     
  15.     def __init__(self, name, icon, tooltip = None, dont_add_to = frozenset([]), dont_remove_from = frozenset([])):
  16.         self.name = name
  17.         self.action_spec = (name, icon, tooltip, None)
  18.         self.dont_remove_from = dont_remove_from
  19.         self.dont_add_to = dont_add_to
  20.  
  21.  
  22.  
  23. class BaseModel(QAbstractListModel):
  24.     
  25.     def name_to_action(self, name, gui):
  26.         if name == 'Donate':
  27.             return FakeAction(name, 'donate.png', dont_add_to = frozenset([
  28.                 'context-menu',
  29.                 'context-menu-device']))
  30.         if name == 'Location Manager':
  31.             return FakeAction(name, None, _('Switch between library and device views'), dont_remove_from = set([
  32.                 'toolbar-device']))
  33.         if name is None:
  34.             return FakeAction('--- ' + _('Separator') + ' ---', None)
  35.         return gui.iactions[name]
  36.  
  37.     
  38.     def rowCount(self, parent):
  39.         return len(self._data)
  40.  
  41.     
  42.     def data(self, index, role):
  43.         row = index.row()
  44.         action = self._data[row].action_spec
  45.         if role == Qt.DisplayRole:
  46.             text = action[0]
  47.             text = text.replace('&', '')
  48.             if text == _('%d books'):
  49.                 text = _('Choose library')
  50.             
  51.             return QVariant(text)
  52.         if role == Qt.DecorationRole:
  53.             ic = action[1]
  54.             if ic is None:
  55.                 ic = 'blank.png'
  56.             
  57.             return QVariant(QIcon(I(ic)))
  58.         if role == Qt.ToolTipRole and action[2] is not None:
  59.             return QVariant(action[2])
  60.         return NONE
  61.  
  62.     
  63.     def names(self, indexes):
  64.         rows = [ i.row() for i in indexes ]
  65.         ans = []
  66.         for i in rows:
  67.             n = self._data[i].name
  68.             ans.append(n)
  69.         
  70.         return ans
  71.  
  72.  
  73.  
  74. class AllModel(BaseModel):
  75.     
  76.     def __init__(self, key, gui):
  77.         BaseModel.__init__(self)
  78.         self.gprefs_name = 'action-layout-' + key
  79.         current = gprefs[self.gprefs_name]
  80.         self.gui = gui
  81.         self.key = key
  82.         self._data = self.get_all_actions(current)
  83.  
  84.     
  85.     def get_all_actions(self, current):
  86.         all = list(self.gui.iactions.keys()) + [
  87.             'Donate']
  88.         all = _[1] + [
  89.             None]
  90.         all = [ self.name_to_action(x, self.gui) for x in all ]
  91.         all = _[3]
  92.         all.sort()
  93.         return all
  94.  
  95.     
  96.     def add(self, names):
  97.         actions = []
  98.         for name in names:
  99.             if name is None or name.startswith('---'):
  100.                 continue
  101.             
  102.             actions.append(self.name_to_action(name, self.gui))
  103.         
  104.         self._data.extend(actions)
  105.         self._data.sort()
  106.         self.reset()
  107.  
  108.     
  109.     def remove(self, indices, allowed):
  110.         rows = [ i.row() for i in indices ]
  111.         remove = set([])
  112.         for row in rows:
  113.             ac = self._data[row]
  114.             if ac.name in allowed:
  115.                 remove.add(row)
  116.                 continue
  117.             None if ac.name.startswith('---') else []
  118.         
  119.         ndata = []
  120.         for i, ac in enumerate(self._data):
  121.             if i not in remove:
  122.                 ndata.append(ac)
  123.                 continue
  124.         
  125.         self._data = ndata
  126.         self.reset()
  127.  
  128.     
  129.     def restore_defaults(self):
  130.         current = gprefs.defaults[self.gprefs_name]
  131.         self._data = self.get_all_actions(current)
  132.         self.reset()
  133.  
  134.  
  135.  
  136. class CurrentModel(BaseModel):
  137.     
  138.     def __init__(self, key, gui):
  139.         BaseModel.__init__(self)
  140.         self.gprefs_name = 'action-layout-' + key
  141.         current = gprefs[self.gprefs_name]
  142.         self._data = [ self.name_to_action(x, gui) for x in current ]
  143.         self.key = key
  144.         self.gui = gui
  145.  
  146.     
  147.     def move(self, idx, delta):
  148.         row = idx.row()
  149.         if row < 0 or row >= len(self._data):
  150.             return None
  151.         nrow = row + delta
  152.         if nrow < 0 or nrow >= len(self._data):
  153.             return None
  154.         t = self._data[row]
  155.         self._data[row] = self._data[nrow]
  156.         self._data[nrow] = t
  157.         ni = self.index(nrow)
  158.         self.dataChanged.emit(idx, idx)
  159.         self.dataChanged.emit(ni, ni)
  160.         return ni
  161.  
  162.     
  163.     def add(self, names):
  164.         actions = []
  165.         reject = set([])
  166.         for name in names:
  167.             ac = self.name_to_action(name, self.gui)
  168.             if self.key in ac.dont_add_to:
  169.                 reject.add(ac)
  170.                 continue
  171.             actions.append(ac)
  172.         
  173.         self._data.extend(actions)
  174.         self.reset()
  175.         return reject
  176.  
  177.     
  178.     def remove(self, indices):
  179.         rows = [ i.row() for i in indices ]
  180.         remove = set([])
  181.         rejected = set([])
  182.         for row in rows:
  183.             ac = self._data[row]
  184.             remove.add(row)
  185.         
  186.         ndata = []
  187.         for i, ac in enumerate(self._data):
  188.             if i not in remove:
  189.                 ndata.append(ac)
  190.                 continue
  191.             None if self.key in ac.dont_remove_from else []
  192.         
  193.         self._data = ndata
  194.         self.reset()
  195.         return rejected
  196.  
  197.     
  198.     def commit(self):
  199.         old = gprefs[self.gprefs_name]
  200.         new = []
  201.         for x in self._data:
  202.             n = x.name
  203.             if n.startswith('---'):
  204.                 n = None
  205.             
  206.             new.append(n)
  207.         
  208.         new = tuple(new)
  209.         if new != old:
  210.             defaults = gprefs.defaults[self.gprefs_name]
  211.             if defaults == new:
  212.                 del gprefs[self.gprefs_name]
  213.             else:
  214.                 gprefs[self.gprefs_name] = new
  215.         
  216.  
  217.     
  218.     def restore_defaults(self):
  219.         current = gprefs.defaults[self.gprefs_name]
  220.         self._data = [ self.name_to_action(x, self.gui) for x in current ]
  221.         self.reset()
  222.  
  223.  
  224.  
  225. class ConfigWidget(ConfigWidgetBase, Ui_Form):
  226.     LOCATIONS = [
  227.         ('toolbar', _('The main toolbar')),
  228.         ('toolbar-device', _('The main toolbar when a device is connected')),
  229.         ('context-menu', _('The context menu for the books in the calibre library')),
  230.         ('context-menu-device', _('The context menu for the books on the device'))]
  231.     
  232.     def genesis(self, gui):
  233.         self.models = { }
  234.         for key, text in self.LOCATIONS:
  235.             self.what.addItem(text, key)
  236.             all_model = AllModel(key, gui)
  237.             current_model = CurrentModel(key, gui)
  238.             self.models[key] = (all_model, current_model)
  239.         
  240.         self.what.setCurrentIndex(0)
  241.         self.what.currentIndexChanged[int].connect(self.what_changed)
  242.         self.what_changed(0)
  243.         self.add_action_button.clicked.connect(self.add_action)
  244.         self.remove_action_button.clicked.connect(self.remove_action)
  245.         self.action_up_button.clicked.connect(partial(self.move, -1))
  246.         self.action_down_button.clicked.connect(partial(self.move, 1))
  247.  
  248.     
  249.     def what_changed(self, idx):
  250.         key = unicode(self.what.itemData(idx).toString())
  251.         self.all_actions.setModel(self.models[key][0])
  252.         self.current_actions.setModel(self.models[key][1])
  253.  
  254.     
  255.     def add_action(self, *args):
  256.         x = self.all_actions.selectionModel().selectedIndexes()
  257.         names = self.all_actions.model().names(x)
  258.  
  259.     
  260.     def remove_action(self, *args):
  261.         x = self.current_actions.selectionModel().selectedIndexes()
  262.         names = self.current_actions.model().names(x)
  263.  
  264.     
  265.     def move(self, delta, *args):
  266.         ci = self.current_actions.currentIndex()
  267.         m = self.current_actions.model()
  268.         if ci.isValid():
  269.             ni = m.move(ci, delta)
  270.             if ni is not None:
  271.                 self.current_actions.setCurrentIndex(ni)
  272.                 self.current_actions.selectionModel().select(ni, QItemSelectionModel.ClearAndSelect)
  273.                 self.changed_signal.emit()
  274.             
  275.         
  276.  
  277.     
  278.     def commit(self):
  279.         for am, cm in self.models.values():
  280.             cm.commit()
  281.         
  282.         return False
  283.  
  284.     
  285.     def restore_defaults(self):
  286.         for am, cm in self.models.values():
  287.             cm.restore_defaults()
  288.             am.restore_defaults()
  289.         
  290.         self.changed_signal.emit()
  291.  
  292.  
  293. if __name__ == '__main__':
  294.     from PyQt4.Qt import QApplication
  295.     app = QApplication([])
  296.     test_widget('Interface', 'Toolbar')
  297.  
  298.