def create_donate_widget(button): w = QWidget() w.setLayout(QVBoxLayout()) w.layout().addWidget(button) if isosx: w.setStyleSheet('QWidget, QToolButton {background-color: none; border: none; }') w.layout().setContentsMargins(0,0,0,0) w.setContentsMargins(0,0,0,0) w.filler = QLabel(u'\u00a0') w.layout().addWidget(w.filler) return w
def create_donate_widget(button): w = QWidget() w.setLayout(QVBoxLayout()) w.layout().addWidget(button) if isosx: w.setStyleSheet( 'QWidget, QToolButton {background-color: none; border: none; }') w.layout().setContentsMargins(0, 0, 0, 0) w.setContentsMargins(0, 0, 0, 0) w.filler = QLabel(u'\u00a0') w.layout().addWidget(w.filler) return w
class ToolBar(QToolBar): # {{{ def __init__(self, donate, location_manager, parent): QToolBar.__init__(self, parent) self.setMovable(False) self.setFloatable(False) self.setOrientation(Qt.Horizontal) self.setAllowedAreas(Qt.TopToolBarArea | Qt.BottomToolBarArea) self.setStyleSheet('QToolButton:checked { font-weight: bold }') self.preferred_width = self.sizeHint().width() self.gui = parent self.donate_button = donate self.added_actions = [] self.location_manager = location_manager donate.setAutoRaise(True) donate.setCursor(Qt.PointingHandCursor) self.setAcceptDrops(True) self.showing_donate = False def resizeEvent(self, ev): QToolBar.resizeEvent(self, ev) style = self.get_text_style() self.setToolButtonStyle(style) if hasattr(self, 'd_widget') and hasattr(self.d_widget, 'filler'): self.d_widget.filler.setVisible(style != Qt.ToolButtonIconOnly) def get_text_style(self): style = Qt.ToolButtonTextUnderIcon s = gprefs['toolbar_icon_size'] if s != 'off': p = gprefs['toolbar_text'] if p == 'never': style = Qt.ToolButtonIconOnly elif p == 'auto' and self.preferred_width > self.width() + 35: style = Qt.ToolButtonIconOnly return style def contextMenuEvent(self, ev): ac = self.actionAt(ev.pos()) if ac is None: return ch = self.widgetForAction(ac) sm = getattr(ch, 'showMenu', None) if callable(sm): ev.accept() sm() def update_lm_actions(self): for ac in self.added_actions: if ac in self.location_manager.all_actions: ac.setVisible(ac in self.location_manager.available_actions) def init_bar(self, actions): self.showing_donate = False for ac in self.added_actions: m = ac.menu() if m is not None: m.setVisible(False) self.clear() self.added_actions = [] bar = self for what in actions: if what is None: bar.addSeparator() elif what == 'Location Manager': for ac in self.location_manager.all_actions: bar.addAction(ac) bar.added_actions.append(ac) bar.setup_tool_button(bar, ac, QToolButton.MenuButtonPopup) ac.setVisible(False) elif what == 'Donate': self.d_widget = QWidget() self.d_widget.setLayout(QVBoxLayout()) self.d_widget.layout().addWidget(self.donate_button) if isosx: self.d_widget.setStyleSheet( 'QWidget, QToolButton {background-color: none; border: none; }' ) self.d_widget.layout().setContentsMargins(0, 0, 0, 0) self.d_widget.setContentsMargins(0, 0, 0, 0) self.d_widget.filler = QLabel(u'\u00a0') self.d_widget.layout().addWidget(self.d_widget.filler) bar.addWidget(self.d_widget) self.showing_donate = True elif what in self.gui.iactions: action = self.gui.iactions[what] bar.addAction(action.qaction) self.added_actions.append(action.qaction) self.setup_tool_button(bar, action.qaction, action.popup_type) self.preferred_width = self.sizeHint().width() def setup_tool_button(self, bar, ac, menu_mode=None): ch = bar.widgetForAction(ac) if ch is None: ch = self.child_bar.widgetForAction(ac) ch.setCursor(Qt.PointingHandCursor) ch.setAutoRaise(True) if ac.menu() is not None and menu_mode is not None: ch.setPopupMode(menu_mode) return ch # support drag&drop from/to library, from/to reader/card, enabled plugins def check_iactions_for_drag(self, event, md, func): if self.added_actions: pos = event.pos() for iac in self.gui.iactions.itervalues(): if iac.accepts_drops: aa = iac.qaction w = self.widgetForAction(aa) m = aa.menu() if (((w is not None and w.geometry().contains(pos)) or (m is not None and m.isVisible() and m.geometry().contains(pos))) and getattr(iac, func)(event, md)): return True return False def dragEnterEvent(self, event): md = event.mimeData() if md.hasFormat("application/calibre+from_library") or \ md.hasFormat("application/calibre+from_device"): event.setDropAction(Qt.CopyAction) event.accept() return if self.check_iactions_for_drag(event, md, 'accept_enter_event'): event.accept() else: event.ignore() def dragMoveEvent(self, event): allowed = False md = event.mimeData() # Drop is only allowed in the location manager widget's different from the selected one for ac in self.location_manager.available_actions: w = self.widgetForAction(ac) if w is not None: 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(): allowed = True break if allowed: event.acceptProposedAction() return if self.check_iactions_for_drag(event, md, 'accept_drag_move_event'): event.acceptProposedAction() else: event.ignore() def dropEvent(self, event): data = event.mimeData() mime = 'application/calibre+from_library' if data.hasFormat(mime): ids = list(map(int, str(data.data(mime)).split())) tgt = None for ac in self.location_manager.available_actions: w = self.widgetForAction(ac) if w is not None and w.geometry().contains(event.pos()): tgt = ac.calibre_name if tgt is not None: if tgt == 'main': tgt = None self.gui.sync_to_device(tgt, False, send_ids=ids) event.accept() return mime = 'application/calibre+from_device' if data.hasFormat(mime): paths = [unicode(u.toLocalFile()) for u in data.urls()] if paths: self.gui.iactions['Add Books'].add_books_from_device( self.gui.current_view(), paths=paths) event.accept() return # Give added_actions an opportunity to process the drag&drop event if self.check_iactions_for_drag(event, data, 'drop_event'): event.accept() else: event.ignore()
class ToolBar(QToolBar): # {{{ def __init__(self, donate, location_manager, parent): QToolBar.__init__(self, parent) self.setMovable(False) self.setFloatable(False) self.setOrientation(Qt.Horizontal) self.setAllowedAreas(Qt.TopToolBarArea|Qt.BottomToolBarArea) self.setStyleSheet('QToolButton:checked { font-weight: bold }') self.preferred_width = self.sizeHint().width() self.gui = parent self.donate_button = donate self.added_actions = [] self.location_manager = location_manager donate.setAutoRaise(True) donate.setCursor(Qt.PointingHandCursor) self.setAcceptDrops(True) self.showing_donate = False def resizeEvent(self, ev): QToolBar.resizeEvent(self, ev) style = self.get_text_style() self.setToolButtonStyle(style) if hasattr(self, 'd_widget') and hasattr(self.d_widget, 'filler'): self.d_widget.filler.setVisible(style != Qt.ToolButtonIconOnly) def get_text_style(self): style = Qt.ToolButtonTextUnderIcon s = gprefs['toolbar_icon_size'] if s != 'off': p = gprefs['toolbar_text'] if p == 'never': style = Qt.ToolButtonIconOnly elif p == 'auto' and self.preferred_width > self.width()+35: style = Qt.ToolButtonIconOnly return style def contextMenuEvent(self, ev): ac = self.actionAt(ev.pos()) if ac is None: return ch = self.widgetForAction(ac) sm = getattr(ch, 'showMenu', None) if callable(sm): ev.accept() sm() def update_lm_actions(self): for ac in self.added_actions: if ac in self.location_manager.all_actions: ac.setVisible(ac in self.location_manager.available_actions) def init_bar(self, actions): self.showing_donate = False for ac in self.added_actions: m = ac.menu() if m is not None: m.setVisible(False) self.clear() self.added_actions = [] bar = self for what in actions: if what is None: bar.addSeparator() elif what == 'Location Manager': for ac in self.location_manager.all_actions: bar.addAction(ac) bar.added_actions.append(ac) bar.setup_tool_button(bar, ac, QToolButton.MenuButtonPopup) ac.setVisible(False) elif what == 'Donate': self.d_widget = QWidget() self.d_widget.setLayout(QVBoxLayout()) self.d_widget.layout().addWidget(self.donate_button) if isosx: self.d_widget.setStyleSheet('QWidget, QToolButton {background-color: none; border: none; }') self.d_widget.layout().setContentsMargins(0,0,0,0) self.d_widget.setContentsMargins(0,0,0,0) self.d_widget.filler = QLabel(u'\u00a0') self.d_widget.layout().addWidget(self.d_widget.filler) bar.addWidget(self.d_widget) self.showing_donate = True elif what in self.gui.iactions: action = self.gui.iactions[what] bar.addAction(action.qaction) self.added_actions.append(action.qaction) self.setup_tool_button(bar, action.qaction, action.popup_type) self.preferred_width = self.sizeHint().width() def setup_tool_button(self, bar, ac, menu_mode=None): ch = bar.widgetForAction(ac) if ch is None: ch = self.child_bar.widgetForAction(ac) ch.setCursor(Qt.PointingHandCursor) ch.setAutoRaise(True) if ac.menu() is not None and menu_mode is not None: ch.setPopupMode(menu_mode) return ch # support drag&drop from/to library, from/to reader/card, enabled plugins def check_iactions_for_drag(self, event, md, func): if self.added_actions: pos = event.pos() for iac in self.gui.iactions.itervalues(): if iac.accepts_drops: aa = iac.qaction w = self.widgetForAction(aa) m = aa.menu() if (( (w is not None and w.geometry().contains(pos)) or (m is not None and m.isVisible() and m.geometry().contains(pos)) ) and getattr(iac, func)(event, md)): return True return False def dragEnterEvent(self, event): md = event.mimeData() if md.hasFormat("application/calibre+from_library") or \ md.hasFormat("application/calibre+from_device"): event.setDropAction(Qt.CopyAction) event.accept() return if self.check_iactions_for_drag(event, md, 'accept_enter_event'): event.accept() else: event.ignore() def dragMoveEvent(self, event): allowed = False md = event.mimeData() # Drop is only allowed in the location manager widget's different from the selected one for ac in self.location_manager.available_actions: w = self.widgetForAction(ac) if w is not None: 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(): allowed = True break if allowed: event.acceptProposedAction() return if self.check_iactions_for_drag(event, md, 'accept_drag_move_event'): event.acceptProposedAction() else: event.ignore() def dropEvent(self, event): data = event.mimeData() mime = 'application/calibre+from_library' if data.hasFormat(mime): ids = list(map(int, str(data.data(mime)).split())) tgt = None for ac in self.location_manager.available_actions: w = self.widgetForAction(ac) if w is not None and w.geometry().contains(event.pos()): tgt = ac.calibre_name if tgt is not None: if tgt == 'main': tgt = None self.gui.sync_to_device(tgt, False, send_ids=ids) event.accept() return mime = 'application/calibre+from_device' if data.hasFormat(mime): paths = [unicode(u.toLocalFile()) for u in data.urls()] if paths: self.gui.iactions['Add Books'].add_books_from_device( self.gui.current_view(), paths=paths) event.accept() return # Give added_actions an opportunity to process the drag&drop event if self.check_iactions_for_drag(event, data, 'drop_event'): event.accept() else: event.ignore()
class ToolBar(QToolBar): # {{{ def __init__(self, donate, location_manager, parent): QToolBar.__init__(self, parent) self.setMovable(False) self.setFloatable(False) self.setOrientation(Qt.Horizontal) self.setAllowedAreas(Qt.TopToolBarArea | Qt.BottomToolBarArea) self.setStyleSheet("QToolButton:checked { font-weight: bold }") self.preferred_width = self.sizeHint().width() self.gui = parent self.donate_button = donate self.added_actions = [] self.location_manager = location_manager donate.setAutoRaise(True) donate.setCursor(Qt.PointingHandCursor) self.setAcceptDrops(True) self.showing_donate = False def resizeEvent(self, ev): QToolBar.resizeEvent(self, ev) style = self.get_text_style() self.setToolButtonStyle(style) if hasattr(self, "d_widget") and hasattr(self.d_widget, "filler"): self.d_widget.filler.setVisible(style != Qt.ToolButtonIconOnly) def get_text_style(self): style = Qt.ToolButtonTextUnderIcon s = gprefs["toolbar_icon_size"] if s != "off": p = gprefs["toolbar_text"] if p == "never": style = Qt.ToolButtonIconOnly elif p == "auto" and self.preferred_width > self.width() + 35: style = Qt.ToolButtonIconOnly return style def contextMenuEvent(self, ev): ac = self.actionAt(ev.pos()) if ac is None: return ch = self.widgetForAction(ac) sm = getattr(ch, "showMenu", None) if callable(sm): ev.accept() sm() def update_lm_actions(self): for ac in self.added_actions: if ac in self.location_manager.all_actions: ac.setVisible(ac in self.location_manager.available_actions) def init_bar(self, actions): self.showing_donate = False for ac in self.added_actions: m = ac.menu() if m is not None: m.setVisible(False) self.clear() self.added_actions = [] bar = self for what in actions: if what is None: bar.addSeparator() elif what == "Location Manager": for ac in self.location_manager.all_actions: bar.addAction(ac) bar.added_actions.append(ac) bar.setup_tool_button(bar, ac, QToolButton.MenuButtonPopup) ac.setVisible(False) elif what == "Donate": self.d_widget = QWidget() self.d_widget.setLayout(QVBoxLayout()) self.d_widget.layout().addWidget(self.donate_button) if isosx: self.d_widget.setStyleSheet("QWidget, QToolButton {background-color: none; border: none; }") self.d_widget.layout().setContentsMargins(0, 0, 0, 0) self.d_widget.setContentsMargins(0, 0, 0, 0) self.d_widget.filler = QLabel("\u00a0") self.d_widget.layout().addWidget(self.d_widget.filler) bar.addWidget(self.d_widget) self.showing_donate = True elif what in self.gui.iactions: action = self.gui.iactions[what] bar.addAction(action.qaction) self.added_actions.append(action.qaction) self.setup_tool_button(bar, action.qaction, action.popup_type) self.preferred_width = self.sizeHint().width() def setup_tool_button(self, bar, ac, menu_mode=None): ch = bar.widgetForAction(ac) if ch is None: ch = self.child_bar.widgetForAction(ac) ch.setCursor(Qt.PointingHandCursor) ch.setAutoRaise(True) if ac.menu() is not None and menu_mode is not None: ch.setPopupMode(menu_mode) return ch # support drag&drop from/to library from/to reader/card def dragEnterEvent(self, event): md = event.mimeData() if md.hasFormat("application/calibre+from_library") or md.hasFormat("application/calibre+from_device"): event.setDropAction(Qt.CopyAction) event.accept() else: event.ignore() def dragMoveEvent(self, event): allowed = False md = event.mimeData() # Drop is only allowed in the location manager widget's different from the selected one for ac in self.location_manager.available_actions: w = self.widgetForAction(ac) if w is not None: 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() ): allowed = True break if allowed: event.acceptProposedAction() else: event.ignore() def dropEvent(self, event): data = event.mimeData() mime = "application/calibre+from_library" if data.hasFormat(mime): ids = list(map(int, str(data.data(mime)).split())) tgt = None for ac in self.location_manager.available_actions: w = self.widgetForAction(ac) if w is not None and w.geometry().contains(event.pos()): tgt = ac.calibre_name if tgt is not None: if tgt == "main": tgt = None self.gui.sync_to_device(tgt, False, send_ids=ids) event.accept() mime = "application/calibre+from_device" if data.hasFormat(mime): paths = [unicode(u.toLocalFile()) for u in data.urls()] if paths: self.gui.iactions["Add Books"].add_books_from_device(self.gui.current_view(), paths=paths) event.accept()