home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1311 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  15.9 KB  |  440 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 QIcon, Qt, QWidget, QToolBar, QSize, pyqtSignal, QToolButton, QObject, QVBoxLayout, QSizePolicy, QLabel, QHBoxLayout, QActionGroup, QMenu
  9. from calibre.constants import __appname__
  10. from calibre.gui2.search_box import SearchBox2, SavedSearchBox
  11. from calibre.gui2.throbber import ThrobbingButton
  12. from calibre.gui2 import gprefs
  13. from calibre.gui2.widgets import ComboBoxWithHelp
  14. from calibre import human_readable
  15.  
  16. class LocationManager(QObject):
  17.     locations_changed = pyqtSignal()
  18.     unmount_device = pyqtSignal()
  19.     location_selected = pyqtSignal(object)
  20.     
  21.     def __init__(self, parent = None):
  22.         QObject.__init__(self, parent)
  23.         self.free = [
  24.             -1,
  25.             -1,
  26.             -1]
  27.         self.count = 0
  28.         self.location_actions = QActionGroup(self)
  29.         self.location_actions.setExclusive(True)
  30.         self.current_location = 'library'
  31.         self._mem = []
  32.         self.tooltips = { }
  33.         
  34.         def ac(name, text, icon, tooltip):
  35.             icon = QIcon(I(icon))
  36.             ac = self.location_actions.addAction(icon, text)
  37.             setattr(self, 'location_' + name, ac)
  38.             ac.setAutoRepeat(False)
  39.             ac.setCheckable(True)
  40.             receiver = partial(self._location_selected, name)
  41.             ac.triggered.connect(receiver)
  42.             self.tooltips[name] = tooltip
  43.             if name != 'library':
  44.                 m = QMenu(parent)
  45.                 self._mem.append(m)
  46.                 a = m.addAction(icon, tooltip)
  47.                 a.triggered.connect(receiver)
  48.                 self._mem.append(a)
  49.                 a = m.addAction(QIcon(I('eject.png')), _('Eject this device'))
  50.                 a.triggered.connect(self._eject_requested)
  51.                 ac.setMenu(m)
  52.                 self._mem.append(a)
  53.             else:
  54.                 ac.setToolTip(tooltip)
  55.             ac.calibre_name = name
  56.             return ac
  57.  
  58.         self.library_action = ac('library', _('Library'), 'lt.png', _('Show books in calibre library'))
  59.         ac('main', _('Device'), 'reader.png', _('Show books in the main memory of the device'))
  60.         ac('carda', _('Card A'), 'sd.png', _('Show books in storage card A'))
  61.         ac('cardb', _('Card B'), 'sd.png', _('Show books in storage card B'))
  62.  
  63.     
  64.     def set_switch_actions(self, quick_actions, rename_actions, delete_actions, switch_actions, choose_action):
  65.         self.switch_menu = QMenu()
  66.         self.switch_menu.addAction(choose_action)
  67.         self.cs_menus = []
  68.         for t, acs in [
  69.             (_('Quick switch'), quick_actions),
  70.             (_('Rename library'), rename_actions),
  71.             (_('Delete library'), delete_actions)]:
  72.             if acs:
  73.                 self.cs_menus.append(QMenu(t))
  74.                 for ac in acs:
  75.                     self.cs_menus[-1].addAction(ac)
  76.                 
  77.                 self.switch_menu.addMenu(self.cs_menus[-1])
  78.                 continue
  79.         
  80.         self.switch_menu.addSeparator()
  81.         for ac in switch_actions:
  82.             self.switch_menu.addAction(ac)
  83.         
  84.         self.library_action.setMenu(self.switch_menu)
  85.  
  86.     
  87.     def _location_selected(self, location, *args):
  88.         if location != self.current_location and hasattr(self, 'location_' + location):
  89.             self.current_location = location
  90.             self.location_selected.emit(location)
  91.             getattr(self, 'location_' + location).setChecked(True)
  92.         
  93.  
  94.     
  95.     def _eject_requested(self, *args):
  96.         self.unmount_device.emit()
  97.  
  98.     
  99.     def update_devices(self, cp = (None, None), fs = [
  100.         -1,
  101.         -1,
  102.         -1], icon = None):
  103.         if icon is None:
  104.             icon = I('reader.png')
  105.         
  106.         self.location_main.setIcon(QIcon(icon))
  107.         had_device = self.has_device
  108.         if cp is None:
  109.             cp = (None, None)
  110.         
  111.         if isinstance(cp, (str, unicode)):
  112.             cp = (cp, None)
  113.         
  114.         if len(fs) < 3:
  115.             fs = list(fs) + [
  116.                 0]
  117.         
  118.         self.free[0] = fs[0]
  119.         self.free[1] = fs[1]
  120.         self.free[2] = fs[2]
  121.         (cpa, cpb) = cp
  122.         self.free[1] = None if fs[1] is not None and cpa is not None else -1
  123.         self.free[2] = None if fs[2] is not None and cpb is not None else -1
  124.         self.update_tooltips()
  125.         if self.has_device != had_device:
  126.             self.location_library.setChecked(True)
  127.             self.locations_changed.emit()
  128.             if not self.has_device:
  129.                 self.location_library.trigger()
  130.             
  131.         
  132.  
  133.     
  134.     def update_tooltips(self):
  135.         for i, loc in enumerate(('main', 'carda', 'cardb')):
  136.             t = self.tooltips[loc]
  137.             if self.free[i] > -1:
  138.                 t += u'\n\n%s ' % human_readable(self.free[i]) + _('available')
  139.             
  140.             ac = getattr(self, 'location_' + loc)
  141.             ac.setToolTip(t)
  142.             ac.setWhatsThis(t)
  143.             ac.setStatusTip(t)
  144.         
  145.  
  146.     
  147.     def has_device(self):
  148.         return max(self.free) > -1
  149.  
  150.     has_device = property(has_device)
  151.     
  152.     def available_actions(self):
  153.         ans = [
  154.             self.location_library]
  155.         for i, loc in enumerate(('main', 'carda', 'cardb')):
  156.             if self.free[i] > -1:
  157.                 ans.append(getattr(self, 'location_' + loc))
  158.                 continue
  159.         
  160.         return ans
  161.  
  162.     available_actions = property(available_actions)
  163.  
  164.  
  165. class SearchBar(QWidget):
  166.     
  167.     def __init__(self, parent):
  168.         QWidget.__init__(self, parent)
  169.         self._layout = l = QHBoxLayout()
  170.         self.setLayout(self._layout)
  171.         x = ComboBoxWithHelp(self)
  172.         x.setMaximumSize(QSize(150, 16777215))
  173.         x.setObjectName('search_restriction')
  174.         x.setToolTip(_('Books display will be restricted to those matching the selected saved search'))
  175.         l.addWidget(x)
  176.         parent.search_restriction = x
  177.         x = QLabel(self)
  178.         x.setObjectName('search_count')
  179.         l.addWidget(x)
  180.         parent.search_count = x
  181.         x.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
  182.         parent.advanced_search_button = x = QToolButton(self)
  183.         x.setIcon(QIcon(I('search.png')))
  184.         l.addWidget(x)
  185.         x.setToolTip(_('Advanced search'))
  186.         self.label = x = QLabel(_('&Search:'))
  187.         l.addWidget(self.label)
  188.         x.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
  189.         x = parent.search = SearchBox2(self)
  190.         x.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum)
  191.         x.setObjectName('search')
  192.         x.setToolTip(_('<p>Search the list of books by title, author, publisher, tags, comments, etc.<br><br>Words separated by spaces are ANDed'))
  193.         l.addWidget(x)
  194.         x = parent.clear_button = QToolButton(self)
  195.         x.setIcon(QIcon(I('clear_left.png')))
  196.         x.setObjectName('clear_button')
  197.         l.addWidget(x)
  198.         x.setToolTip(_('Reset Quick Search'))
  199.         x = parent.saved_search = SavedSearchBox(self)
  200.         x.setMaximumSize(QSize(150, 16777215))
  201.         x.setMinimumContentsLength(15)
  202.         x.setObjectName('saved_search')
  203.         l.addWidget(x)
  204.         x = parent.copy_search_button = QToolButton(self)
  205.         x.setIcon(QIcon(I('search_copy_saved.png')))
  206.         x.setObjectName('copy_search_button')
  207.         l.addWidget(x)
  208.         x.setToolTip(_('Copy current search text (instead of search name)'))
  209.         x = parent.save_search_button = QToolButton(self)
  210.         x.setIcon(QIcon(I('search_add_saved.png')))
  211.         x.setObjectName('save_search_button')
  212.         l.addWidget(x)
  213.         x.setToolTip(_('Save current search under the name shown in the box'))
  214.         x = parent.delete_search_button = QToolButton(self)
  215.         x.setIcon(QIcon(I('search_delete_saved.png')))
  216.         x.setObjectName('delete_search_button')
  217.         l.addWidget(x)
  218.         x.setToolTip(_('Delete current saved search'))
  219.         self.label.setBuddy(parent.search)
  220.  
  221.  
  222.  
  223. class Spacer(QWidget):
  224.     
  225.     def __init__(self, parent):
  226.         QWidget.__init__(self, parent)
  227.         self.l = QHBoxLayout()
  228.         self.setLayout(self.l)
  229.         self.l.addStretch(10)
  230.  
  231.  
  232.  
  233. class ToolBar(QToolBar):
  234.     
  235.     def __init__(self, donate, location_manager, child_bar, parent):
  236.         QToolBar.__init__(self, parent)
  237.         self.gui = parent
  238.         self.child_bar = child_bar
  239.         self.setContextMenuPolicy(Qt.PreventContextMenu)
  240.         self.setMovable(False)
  241.         self.setFloatable(False)
  242.         self.setOrientation(Qt.Horizontal)
  243.         self.setAllowedAreas(Qt.TopToolBarArea | Qt.BottomToolBarArea)
  244.         self.setStyleSheet('QToolButton:checked { font-weight: bold }')
  245.         self.donate_button = donate
  246.         self.apply_settings()
  247.         self.location_manager = location_manager
  248.         self.location_manager.locations_changed.connect(self.build_bar)
  249.         donate.setAutoRaise(True)
  250.         donate.setCursor(Qt.PointingHandCursor)
  251.         self.added_actions = []
  252.         self.build_bar()
  253.         self.preferred_width = self.sizeHint().width()
  254.         self.setAcceptDrops(True)
  255.  
  256.     
  257.     def apply_settings(self):
  258.         sz = gprefs['toolbar_icon_size']
  259.         sz = {
  260.             'small': 24,
  261.             'medium': 48,
  262.             'large': 64 }[sz]
  263.         self.setIconSize(QSize(sz, sz))
  264.         self.child_bar.setIconSize(QSize(sz, sz))
  265.         style = Qt.ToolButtonTextUnderIcon
  266.         if gprefs['toolbar_text'] == 'never':
  267.             style = Qt.ToolButtonIconOnly
  268.         
  269.         self.setToolButtonStyle(style)
  270.         self.child_bar.setToolButtonStyle(style)
  271.         self.donate_button.set_normal_icon_size(sz, sz)
  272.  
  273.     
  274.     def contextMenuEvent(self, *args):
  275.         pass
  276.  
  277.     
  278.     def build_bar(self):
  279.         self.child_bar.setVisible(gprefs['show_child_bar'])
  280.         self.showing_donate = False
  281.         showing_device = self.location_manager.has_device
  282.         actions = None if showing_device else ''
  283.         actions = gprefs['action-layout-toolbar' + actions]
  284.         for ac in self.added_actions:
  285.             m = ac.menu()
  286.             if m is not None:
  287.                 m.setVisible(False)
  288.                 continue
  289.         
  290.         self.clear()
  291.         self.child_bar.clear()
  292.         self.added_actions = []
  293.         self.spacers = [
  294.             Spacer(self.child_bar),
  295.             Spacer(self.child_bar),
  296.             Spacer(self),
  297.             Spacer(self)]
  298.         self.child_bar.addWidget(self.spacers[0])
  299.         if gprefs['show_child_bar']:
  300.             self.addWidget(self.spacers[2])
  301.         
  302.         for what in actions:
  303.             if what is None and not gprefs['show_child_bar']:
  304.                 self.addSeparator()
  305.                 continue
  306.             if what == 'Location Manager':
  307.                 for ac in self.location_manager.available_actions:
  308.                     self.addAction(ac)
  309.                     self.added_actions.append(ac)
  310.                     self.setup_tool_button(ac, QToolButton.MenuButtonPopup)
  311.                 
  312.             if what == 'Donate':
  313.                 self.d_widget = QWidget()
  314.                 self.d_widget.setLayout(QVBoxLayout())
  315.                 self.d_widget.layout().addWidget(self.donate_button)
  316.                 self.addWidget(self.d_widget)
  317.                 self.showing_donate = True
  318.                 continue
  319.             if what in self.gui.iactions:
  320.                 action = self.gui.iactions[what]
  321.                 bar = self
  322.                 if action.action_type == 'current' and gprefs['show_child_bar']:
  323.                     bar = self.child_bar
  324.                 
  325.                 bar.addAction(action.qaction)
  326.                 self.added_actions.append(action.qaction)
  327.                 self.setup_tool_button(action.qaction, action.popup_type)
  328.                 continue
  329.         
  330.         self.child_bar.addWidget(self.spacers[1])
  331.         if gprefs['show_child_bar']:
  332.             self.addWidget(self.spacers[3])
  333.         
  334.  
  335.     
  336.     def setup_tool_button(self, ac, menu_mode = None):
  337.         ch = self.widgetForAction(ac)
  338.         if ch is None:
  339.             ch = self.child_bar.widgetForAction(ac)
  340.         
  341.         ch.setCursor(Qt.PointingHandCursor)
  342.         ch.setAutoRaise(True)
  343.         if ac.menu() is not None and menu_mode is not None:
  344.             ch.setPopupMode(menu_mode)
  345.         
  346.  
  347.     
  348.     def resizeEvent(self, ev):
  349.         QToolBar.resizeEvent(self, ev)
  350.         style = Qt.ToolButtonTextUnderIcon
  351.         p = gprefs['toolbar_text']
  352.         if p == 'never':
  353.             style = Qt.ToolButtonIconOnly
  354.         
  355.         if p == 'auto' and self.preferred_width > self.width() + 35 and not gprefs['show_child_bar']:
  356.             style = Qt.ToolButtonIconOnly
  357.         
  358.         self.setToolButtonStyle(style)
  359.  
  360.     
  361.     def database_changed(self, db):
  362.         pass
  363.  
  364.     
  365.     def dragEnterEvent(self, event):
  366.         md = event.mimeData()
  367.         if md.hasFormat('application/calibre+from_library') or md.hasFormat('application/calibre+from_device'):
  368.             event.setDropAction(Qt.CopyAction)
  369.             event.accept()
  370.         else:
  371.             event.ignore()
  372.  
  373.     
  374.     def dragMoveEvent(self, event):
  375.         allowed = False
  376.         md = event.mimeData()
  377.         for ac in self.location_manager.available_actions:
  378.             w = self.widgetForAction(ac)
  379.             if w is not None:
  380.                 if (md.hasFormat('application/calibre+from_library') or md.hasFormat('application/calibre+from_device')) and w.geometry().contains(event.pos()) and isinstance(w, QToolButton) and not w.isChecked():
  381.                     allowed = True
  382.                     break
  383.                 
  384.             not w.isChecked()
  385.         
  386.         if allowed:
  387.             event.acceptProposedAction()
  388.         else:
  389.             event.ignore()
  390.  
  391.     
  392.     def dropEvent(self, event):
  393.         data = event.mimeData()
  394.         mime = 'application/calibre+from_library'
  395.         if data.hasFormat(mime):
  396.             ids = list(map(int, str(data.data(mime)).split()))
  397.             tgt = None
  398.             for ac in self.location_manager.available_actions:
  399.                 w = self.widgetForAction(ac)
  400.                 if w is not None and w.geometry().contains(event.pos()):
  401.                     tgt = ac.calibre_name
  402.                     continue
  403.             
  404.             if tgt is not None:
  405.                 if tgt == 'main':
  406.                     tgt = None
  407.                 
  408.                 self.gui.sync_to_device(tgt, False, send_ids = ids)
  409.                 event.accept()
  410.             
  411.         
  412.         mime = 'application/calibre+from_device'
  413.  
  414.  
  415.  
  416. class MainWindowMixin(object):
  417.     
  418.     def __init__(self, db):
  419.         self.setObjectName('MainWindow')
  420.         self.setWindowIcon(QIcon(I('library.png')))
  421.         self.setWindowTitle(__appname__)
  422.         self.setContextMenuPolicy(Qt.NoContextMenu)
  423.         self.centralwidget = QWidget(self)
  424.         self.setCentralWidget(self.centralwidget)
  425.         self._central_widget_layout = QVBoxLayout()
  426.         self.centralwidget.setLayout(self._central_widget_layout)
  427.         self.resize(1012, 740)
  428.         self.donate_button = ThrobbingButton()
  429.         self.location_manager = LocationManager(self)
  430.         self.iactions['Fetch News'].init_scheduler(db)
  431.         self.search_bar = SearchBar(self)
  432.         self.child_bar = QToolBar(self)
  433.         self.tool_bar = ToolBar(self.donate_button, self.location_manager, self.child_bar, self)
  434.         self.addToolBar(Qt.TopToolBarArea, self.tool_bar)
  435.         self.addToolBar(Qt.BottomToolBarArea, self.child_bar)
  436.         l = self.centralwidget.layout()
  437.         l.addWidget(self.search_bar)
  438.  
  439.  
  440.