def contextMenuEvent(self, ev): from calibre.gui2.open_with import populate_menu, edit_programs cm = QMenu(self) paste = cm.addAction(_("Paste Cover")) copy = cm.addAction(_("Copy Cover")) remove = cm.addAction(_("Remove Cover")) gc = cm.addAction(_("Generate Cover from metadata")) if not QApplication.instance().clipboard().mimeData().hasImage(): paste.setEnabled(False) copy.triggered.connect(self.copy_to_clipboard) paste.triggered.connect(self.paste_from_clipboard) remove.triggered.connect(self.remove_cover) gc.triggered.connect(self.generate_cover) m = QMenu(_("Open cover with...")) populate_menu(m, self.open_with, "cover_image") if len(m.actions()) == 0: cm.addAction(_("Open cover with..."), self.choose_open_with) else: m.addSeparator() m.addAction(_("Add another application to open cover..."), self.choose_open_with) m.addAction(_("Edit Open With applications..."), partial(edit_programs, "cover_image", self)) cm.addMenu(m) cm.exec_(ev.globalPos())
def contextMenuEvent(self, ev): from calibre.gui2.open_with import populate_menu, edit_programs cm = QMenu(self) paste = cm.addAction(_('Paste cover')) copy = cm.addAction(_('Copy cover')) remove = cm.addAction(_('Remove cover')) gc = cm.addAction(_('Generate cover from metadata')) cm.addSeparator() if not QApplication.instance().clipboard().mimeData().hasImage(): paste.setEnabled(False) copy.triggered.connect(self.copy_to_clipboard) paste.triggered.connect(self.paste_from_clipboard) remove.triggered.connect(self.remove_cover) gc.triggered.connect(self.generate_cover) m = QMenu(_('Open cover with...')) populate_menu(m, self.open_with, 'cover_image') if len(m.actions()) == 0: cm.addAction(_('Open cover with...'), self.choose_open_with) else: m.addSeparator() m.addAction(_('Add another application to open cover...'), self.choose_open_with) m.addAction(_('Edit Open with applications...'), partial(edit_programs, 'cover_image', self)) cm.ocw = m cm.addMenu(m) cm.si = m = create_search_internet_menu(self.search_internet.emit) cm.addMenu(m) cm.exec_(ev.globalPos())
def add_open_with_actions(self, menu, file_name): from calibre.gui2.open_with import populate_menu, edit_programs fmt = file_name.rpartition('.')[-1].lower() if not fmt: return m = QMenu(_('Open %s with...') % file_name) def connect_action(ac, entry): connect_lambda(ac.triggered, self, lambda self: self.open_with(file_name, fmt, entry)) populate_menu(m, connect_action, fmt) if len(m.actions()) == 0: menu.addAction( _('Open %s with...') % file_name, partial(self.choose_open_with, file_name, fmt)) else: m.addSeparator() m.addAction( _('Add other application for %s files...') % fmt.upper(), partial(self.choose_open_with, file_name, fmt)) m.addAction(_('Edit Open With applications...'), partial(edit_programs, fmt, file_name)) menu.addMenu(m) menu.ow = m
def add_format_entries(menu, data, book_info, copy_menu, search_menu): from calibre.ebooks.oeb.polish.main import SUPPORTED from calibre.gui2.ui import get_gui book_id = int(data['book_id']) fmt = data['fmt'] init_find_in_tag_browser(search_menu, book_info.find_in_tag_browser_action, 'formats', fmt) init_find_in_grouped_search(search_menu, 'formats', fmt, book_info) db = get_gui().current_db.new_api ofmt = fmt.upper() if fmt.startswith('ORIGINAL_') else 'ORIGINAL_' + fmt nfmt = ofmt[len('ORIGINAL_'):] fmts = {x.upper() for x in db.formats(book_id)} for a, t in [ ('remove', _('Delete the %s format')), ('save', _('Save the %s format to disk')), ('restore', _('Restore the %s format')), ('compare', ''), ('set_cover', _('Set the book cover from the %s file')), ]: if a == 'restore' and not fmt.startswith('ORIGINAL_'): continue if a == 'compare': if ofmt not in fmts or nfmt not in SUPPORTED: continue t = _('Compare to the %s format') % (fmt[9:] if fmt.startswith('ORIGINAL_') else ofmt) else: t = t % fmt ac = getattr(book_info, '%s_format_action'%a) ac.current_fmt = (book_id, fmt) ac.setText(t) menu.addAction(ac) if not fmt.upper().startswith('ORIGINAL_'): from calibre.gui2.open_with import edit_programs, populate_menu m = QMenu(_('Open %s with...') % fmt.upper()) def connect_action(ac, entry): connect_lambda(ac.triggered, book_info, lambda book_info: book_info.open_with(book_id, fmt, entry)) populate_menu(m, connect_action, fmt) if len(m.actions()) == 0: menu.addAction(_('Open %s with...') % fmt.upper(), partial(book_info.choose_open_with, book_id, fmt)) else: m.addSeparator() m.addAction(_('Add other application for %s files...') % fmt.upper(), partial(book_info.choose_open_with, book_id, fmt)) m.addAction(_('Edit Open with applications...'), partial(edit_programs, fmt, book_info)) menu.addMenu(m) menu.ow = m if fmt.upper() in SUPPORTED: menu.addSeparator() menu.addAction(_('Edit %s format') % fmt.upper(), partial(book_info.edit_fmt, book_id, fmt)) path = data['path'] if path: if data.get('fname'): path = os.path.join(path, data['fname'] + '.' + data['fmt'].lower()) ac = book_info.copy_link_action ac.current_url = path ac.setText(_('Path to file')) copy_menu.addAction(ac)
def populate_open_with(self): from calibre.gui2.open_with import populate_menu, edit_programs menu = self.own menu.clear() fmt = self._formats[self.formats.currentRow()] populate_menu(menu, self.open_with, fmt) if len(menu.actions()) == 0: menu.addAction(_('Open %s with...') % fmt.upper(), self.choose_open_with) else: menu.addSeparator() menu.addAction(_('Add other application for %s files...') % fmt.upper(), self.choose_open_with) menu.addAction(_('Edit "Open with" applications...'), partial(edit_programs, fmt, self))
def populate_open_with(self): from calibre.gui2.open_with import populate_menu, edit_programs menu = self.own menu.clear() fmt = self._formats[self.formats.currentRow()] populate_menu(menu, self.open_with, fmt) if len(menu.actions()) == 0: menu.addAction(_('Open %s with...') % fmt.upper(), self.choose_open_with) else: menu.addSeparator() menu.addAction(_('Add other application for %s files...') % fmt.upper(), self.choose_open_with) menu.addAction(_('Edit Open With applications...'), partial(edit_programs, fmt, self))
def populate_open_with(self): from calibre.gui2.open_with import populate_menu, edit_programs menu = self.own menu.clear() fmt = self._formats[self.formats.currentRow()] def connect_action(ac, entry): connect_lambda(ac.triggered, self, lambda self: self.open_with(entry)) populate_menu(menu, connect_action, fmt) if len(menu.actions()) == 0: menu.addAction(_('Open %s with...') % fmt.upper(), self.choose_open_with) else: menu.addSeparator() menu.addAction(_('Add other application for %s files...') % fmt.upper(), self.choose_open_with) menu.addAction(_('Edit "Open with" applications...'), partial(edit_programs, fmt, self))
def create_open_cover_with_menu(self, parent_menu): from calibre.gui2.open_with import edit_programs, populate_menu m = QMenu(_('Open cover with...')) def connect_action(ac, entry): connect_lambda(ac.triggered, self, lambda self: self.open_with(entry)) populate_menu(m, connect_action, 'cover_image') if len(m.actions()) == 0: parent_menu.addAction(_('Open cover with...'), self.choose_open_with) else: m.addSeparator() m.addAction(_('Add another application to open cover with...'), self.choose_open_with) m.addAction(_('Edit Open with applications...'), partial(edit_programs, 'cover_image', self)) parent_menu.ocw = m parent_menu.addMenu(m) return m
def contextMenuEvent(self, ev): from calibre.gui2.open_with import populate_menu cm = QMenu(self) paste = cm.addAction(_('Paste Cover')) copy = cm.addAction(_('Copy Cover')) remove = cm.addAction(_('Remove Cover')) gc = cm.addAction(_('Generate Cover from metadata')) if not QApplication.instance().clipboard().mimeData().hasImage(): paste.setEnabled(False) copy.triggered.connect(self.copy_to_clipboard) paste.triggered.connect(self.paste_from_clipboard) remove.triggered.connect(self.remove_cover) gc.triggered.connect(self.generate_cover) m = QMenu(_('Open with...')) populate_menu(m, self.open_with, 'jpeg') if len(m.actions()) == 0: cm.addAction(_('Open with...'), self.choose_open_with) else: m.addSeparator() m.addAction(_('Choose other program...'), self.choose_open_with) cm.addMenu(m) cm.exec_(ev.globalPos())
def details_context_menu_event(view, ev, book_info): # {{{ p = view.page() mf = p.mainFrame() r = mf.hitTestContent(ev.pos()) url = unicode(r.linkUrl().toString(NO_URL_FORMATTING)).strip() menu = p.createStandardContextMenu() ca = view.pageAction(p.Copy) for action in list(menu.actions()): if action is not ca: menu.removeAction(action) menu.addAction(QIcon(I('edit-copy.png')), _('Copy &all'), partial(copy_all, book_info)) search_internet_added = False if not r.isNull(): if url.startswith('format:'): parts = url.split(':') try: book_id, fmt = int(parts[1]), parts[2].upper() except: import traceback traceback.print_exc() else: from calibre.gui2.ui import get_gui from calibre.ebooks.oeb.polish.main import SUPPORTED db = get_gui().current_db.new_api ofmt = fmt.upper() if fmt.startswith('ORIGINAL_') else 'ORIGINAL_' + fmt nfmt = ofmt[len('ORIGINAL_'):] fmts = {x.upper() for x in db.formats(book_id)} for a, t in [ ('remove', _('Delete the %s format')), ('save', _('Save the %s format to disk')), ('restore', _('Restore the %s format')), ('compare', ''), ('set_cover', _('Set the book cover from the %s file')), ]: if a == 'restore' and not fmt.startswith('ORIGINAL_'): continue if a == 'compare': if ofmt not in fmts or nfmt not in SUPPORTED: continue t = _('Compare to the %s format') % (fmt[9:] if fmt.startswith('ORIGINAL_') else ofmt) else: t = t % fmt ac = getattr(book_info, '%s_format_action'%a) ac.current_fmt = (book_id, fmt) ac.setText(t) menu.addAction(ac) if not fmt.upper().startswith('ORIGINAL_'): from calibre.gui2.open_with import populate_menu, edit_programs m = QMenu(_('Open %s with...') % fmt.upper()) populate_menu(m, partial(book_info.open_with, book_id, fmt), fmt) if len(m.actions()) == 0: menu.addAction(_('Open %s with...') % fmt.upper(), partial(book_info.choose_open_with, book_id, fmt)) else: m.addSeparator() m.addAction(_('Add other application for %s files...') % fmt.upper(), partial(book_info.choose_open_with, book_id, fmt)) m.addAction(_('Edit Open With applications...'), partial(edit_programs, fmt, book_info)) menu.addMenu(m) menu.ow = m ac = book_info.copy_link_action ac.current_url = r.linkElement().attribute('data-full-path') if ac.current_url: ac.setText(_('&Copy path to file')) menu.addAction(ac) else: el = r.linkElement() data = el.attribute('data-item') author = el.toPlainText() if unicode(el.attribute('calibre-data')) == u'authors' else None if url and not url.startswith('search:'): for a, t in [('copy', _('&Copy link')), ]: ac = getattr(book_info, '%s_link_action'%a) ac.current_url = url if url.startswith('path:'): ac.current_url = el.attribute('title') ac.setText(t) menu.addAction(ac) if author is not None: menu.addAction(init_manage_action(book_info.manage_action, 'authors', author)) if hasattr(book_info, 'search_internet'): menu.sia = sia = create_search_internet_menu(book_info.search_internet, author) menu.addMenu(sia) search_internet_added = True if hasattr(book_info, 'search_requested'): menu.addAction(_('Search calibre for %s') % author, lambda : book_info.search_requested('authors:"={}"'.format(author.replace('"', r'\"')))) if data: try: field, value, book_id = cPickle.loads(unhexlify(data)) except Exception: field = value = book_id = None if field: if author is None and ( field in ('tags', 'series', 'publisher') or is_category(field)): menu.addAction(init_manage_action(book_info.manage_action, field, value)) ac = book_info.remove_item_action ac.data = (field, value, book_id) ac.setText(_('Remove %s from this book') % value) menu.addAction(ac) if not search_internet_added and hasattr(book_info, 'search_internet'): menu.addSeparator() menu.si = create_search_internet_menu(book_info.search_internet) menu.addMenu(menu.si) if len(menu.actions()) > 0: menu.exec_(ev.globalPos())
def details_context_menu_event(view, ev, book_info): # {{{ p = view.page() mf = p.mainFrame() r = mf.hitTestContent(ev.pos()) url = unicode(r.linkUrl().toString(QUrl.None)).strip() menu = p.createStandardContextMenu() ca = view.pageAction(p.Copy) for action in list(menu.actions()): if action is not ca: menu.removeAction(action) if not r.isNull(): if url.startswith("format:"): parts = url.split(":") try: book_id, fmt = int(parts[1]), parts[2].upper() except: import traceback traceback.print_exc() else: from calibre.gui2.ui import get_gui from calibre.ebooks.oeb.polish.main import SUPPORTED db = get_gui().current_db.new_api ofmt = fmt.upper() if fmt.startswith("ORIGINAL_") else "ORIGINAL_" + fmt nfmt = ofmt[len("ORIGINAL_") :] fmts = {x.upper() for x in db.formats(book_id)} for a, t in [ ("remove", _("Delete the %s format")), ("save", _("Save the %s format to disk")), ("restore", _("Restore the %s format")), ("compare", ""), ("set_cover", _("Set the book cover from the %s file")), ]: if a == "restore" and not fmt.startswith("ORIGINAL_"): continue if a == "compare": if ofmt not in fmts or nfmt not in SUPPORTED: continue t = _("Compare to the %s format") % (fmt[9:] if fmt.startswith("ORIGINAL_") else ofmt) else: t = t % fmt ac = getattr(book_info, "%s_format_action" % a) ac.current_fmt = (book_id, fmt) ac.setText(t) menu.addAction(ac) if not fmt.upper().startswith("ORIGINAL_"): from calibre.gui2.open_with import populate_menu, edit_programs m = QMenu(_("Open %s with...") % fmt.upper()) populate_menu(m, partial(book_info.open_with, book_id, fmt), fmt) if len(m.actions()) == 0: menu.addAction( _("Open %s with...") % fmt.upper(), partial(book_info.choose_open_with, book_id, fmt) ) else: m.addSeparator() m.addAction( _("Add other application for %s files...") % fmt.upper(), partial(book_info.choose_open_with, book_id, fmt), ) m.addAction(_("Edit Open With applications..."), partial(edit_programs, fmt, book_info)) menu.addMenu(m) ac = book_info.copy_link_action ac.current_url = r.linkElement().attribute("data-full-path") if ac.current_url: ac.setText(_("&Copy path to file")) menu.addAction(ac) else: el = r.linkElement() data = el.attribute("data-item") author = el.toPlainText() if unicode(el.attribute("calibre-data")) == u"authors" else None if not url.startswith("search:"): for a, t in [("copy", _("&Copy Link"))]: ac = getattr(book_info, "%s_link_action" % a) ac.current_url = url if url.startswith("path:"): ac.current_url = el.attribute("title") ac.setText(t) menu.addAction(ac) if author is not None: ac = book_info.manage_author_action ac.current_fmt = author ac.setText(_("Manage %s") % author) menu.addAction(ac) if data: try: field, value, book_id = cPickle.loads(unhexlify(data)) except Exception: field = value = book_id = None if field: ac = book_info.remove_item_action ac.data = (field, value, book_id) ac.setText(_("Remove %s from this book") % value) menu.addAction(ac) if len(menu.actions()) > 0: menu.exec_(ev.globalPos())
def details_context_menu_event(view, ev, book_info): # {{{ p = view.page() mf = p.mainFrame() r = mf.hitTestContent(ev.pos()) url = unicode(r.linkUrl().toString(QUrl.None)).strip() menu = p.createStandardContextMenu() ca = view.pageAction(p.Copy) for action in list(menu.actions()): if action is not ca: menu.removeAction(action) if not r.isNull(): if url.startswith('format:'): parts = url.split(':') try: book_id, fmt = int(parts[1]), parts[2].upper() except: import traceback traceback.print_exc() else: from calibre.gui2.ui import get_gui from calibre.ebooks.oeb.polish.main import SUPPORTED db = get_gui().current_db.new_api ofmt = fmt.upper() if fmt.startswith('ORIGINAL_') else 'ORIGINAL_' + fmt nfmt = ofmt[len('ORIGINAL_'):] fmts = {x.upper() for x in db.formats(book_id)} for a, t in [('remove', _('Delete the %s format')), ('save', _('Save the %s format to disk')), ('restore', _('Restore the %s format')), ('compare', ''), ]: if a == 'restore' and not fmt.startswith('ORIGINAL_'): continue if a == 'compare': if ofmt not in fmts or nfmt not in SUPPORTED: continue t = _('Compare to the %s format') % (fmt[9:] if fmt.startswith('ORIGINAL_') else ofmt) else: t = t % fmt ac = getattr(book_info, '%s_format_action'%a) ac.current_fmt = (book_id, fmt) ac.setText(t) menu.addAction(ac) if not fmt.upper().startswith('ORIGINAL_'): from calibre.gui2.open_with import populate_menu, edit_programs m = QMenu(_('Open %s with...') % fmt.upper()) populate_menu(m, partial(book_info.open_with, book_id, fmt), fmt) if len(m.actions()) == 0: menu.addAction(_('Open %s with...') % fmt.upper(), partial(book_info.choose_open_with, book_id, fmt)) else: m.addSeparator() m.addAction(_('Add other application for %s files...') % fmt.upper(), partial(book_info.choose_open_with, book_id, fmt)) m.addAction(_('Edit Open With applications...'), partial(edit_programs, fmt, book_info)) menu.addMenu(m) ac = book_info.copy_link_action ac.current_url = r.linkElement().attribute('data-full-path') if ac.current_url: ac.setText(_('&Copy path to file')) menu.addAction(ac) else: el = r.linkElement() author = el.toPlainText() if unicode(el.attribute('calibre-data')) == u'authors' else None if not url.startswith('search:'): for a, t in [('copy', _('&Copy Link')), ]: ac = getattr(book_info, '%s_link_action'%a) ac.current_url = url if url.startswith('path:'): ac.current_url = el.attribute('title') ac.setText(t) menu.addAction(ac) if author is not None: ac = book_info.manage_author_action ac.current_fmt = author ac.setText(_('Manage %s') % author) menu.addAction(ac) if len(menu.actions()) > 0: menu.exec_(ev.globalPos())
def contextMenuEvent(self, ev): p = self.page() mf = p.mainFrame() r = mf.hitTestContent(ev.pos()) url = unicode(r.linkUrl().toString(QUrl.None)).strip() menu = p.createStandardContextMenu() ca = self.pageAction(p.Copy) for action in list(menu.actions()): if action is not ca: menu.removeAction(action) if not r.isNull(): if url.startswith('format:'): parts = url.split(':') try: book_id, fmt = int(parts[1]), parts[2].upper() except: import traceback traceback.print_exc() else: from calibre.gui2.ui import get_gui from calibre.ebooks.oeb.polish.main import SUPPORTED db = get_gui().current_db.new_api ofmt = fmt.upper() if fmt.startswith( 'ORIGINAL_') else 'ORIGINAL_' + fmt nfmt = ofmt[len('ORIGINAL_'):] fmts = {x.upper() for x in db.formats(book_id)} for a, t in [ ('remove', _('Delete the %s format')), ('save', _('Save the %s format to disk')), ('restore', _('Restore the %s format')), ('compare', ''), ]: if a == 'restore' and not fmt.startswith('ORIGINAL_'): continue if a == 'compare': if ofmt not in fmts or nfmt not in SUPPORTED: continue t = _('Compare to the %s format') % ( fmt[9:] if fmt.startswith('ORIGINAL_') else ofmt) else: t = t % fmt ac = getattr(self, '%s_format_action' % a) ac.current_fmt = (book_id, fmt) ac.setText(t) menu.addAction(ac) if not fmt.upper().startswith('ORIGINAL_'): from calibre.gui2.open_with import populate_menu, edit_programs m = QMenu(_('Open %s with...') % fmt.upper()) populate_menu(m, partial(self.open_with, book_id, fmt), fmt) if len(m.actions()) == 0: menu.addAction( _('Open %s with...') % fmt.upper(), partial(self.choose_open_with, book_id, fmt)) else: m.addSeparator() m.addAction( _('Add other application for %s files...') % fmt.upper(), partial(self.choose_open_with, book_id, fmt)) m.addAction(_('Edit Open With applications...'), partial(edit_programs, fmt, self)) menu.addMenu(m) ac = self.copy_link_action ac.current_url = r.linkElement().attribute( 'data-full-path') if ac.current_url: ac.setText(_('&Copy path to file')) menu.addAction(ac) else: el = r.linkElement() author = el.toPlainText() if unicode( el.attribute('calibre-data')) == u'authors' else None if not url.startswith('search:'): for a, t in [ ('copy', _('&Copy Link')), ]: ac = getattr(self, '%s_link_action' % a) ac.current_url = url if url.startswith('path:'): ac.current_url = el.attribute('title') ac.setText(t) menu.addAction(ac) if author is not None: ac = self.manage_author_action ac.current_fmt = author ac.setText(_('Manage %s') % author) menu.addAction(ac) if len(menu.actions()) > 0: menu.exec_(ev.globalPos())