def show_context_menu(self, point): item = self.folders.itemAt(point) if item is None: return m = QMenu(self) m.addAction(QIcon(I('mimetypes/dir.png')), _('Create new folder'), partial(self.create_folder, item)) m.popup(self.folders.mapToGlobal(point))
def contextMenuEvent(self, ev): m = QMenu(self) m.addAction(_('Set to undefined') + '\t' + QKeySequence(Qt.Key_Space).toString(QKeySequence.NativeText), self.clear_to_undefined) m.addSeparator() populate_standard_spinbox_context_menu(self, m) m.popup(ev.globalPos())
def show_context_menu(self, point): item = self.itemAt(point) if item is None or item in set(self.categories.itervalues()): return m = QMenu(self) sel = self.selectedItems() num = len(sel) container = current_container() ci = self.currentItem() if ci is not None: cn = unicode(ci.data(0, NAME_ROLE) or '') mt = unicode(ci.data(0, MIME_ROLE) or '') cat = unicode(ci.data(0, CATEGORY_ROLE) or '') n = elided_text(cn.rpartition('/')[-1]) m.addAction(QIcon(I('save.png')), _('Export %s') % n, partial(self.export, cn)) if cn not in container.names_that_must_not_be_changed and cn not in container.names_that_must_not_be_removed and mt not in OEB_FONTS: m.addAction(_('Replace %s with file...') % n, partial(self.replace, cn)) if num > 1: m.addAction(QIcon(I('save.png')), _('Export all %d selected files') % num, self.export_selected) m.addSeparator() m.addAction(QIcon(I('modified.png')), _('&Rename %s') % n, self.edit_current_item) if is_raster_image(mt): m.addAction(QIcon(I('default_cover.png')), _('Mark %s as cover image') % n, partial(self.mark_as_cover, cn)) elif current_container().SUPPORTS_TITLEPAGES and mt in OEB_DOCS and cat == 'text': m.addAction(QIcon(I('default_cover.png')), _('Mark %s as cover page') % n, partial(self.mark_as_titlepage, cn)) m.addSeparator() if num > 0: m.addSeparator() if num > 1: m.addAction(QIcon(I('modified.png')), _('&Bulk rename the selected files'), self.request_bulk_rename) m.addAction(QIcon(I('modified.png')), _('Change the file extension for the selected files'), self.request_change_ext) m.addAction(QIcon(I('trash.png')), ngettext( '&Delete the selected file', '&Delete the {} selected files', num).format(num), self.request_delete) m.addAction(QIcon(I('edit-copy.png')), ngettext( '&Copy the selected file to another editor instance', '&Copy the {} selected files to another editor instance', num).format(num), self.copy_selected_files) m.addSeparator() selected_map = defaultdict(list) for item in sel: selected_map[unicode(item.data(0, CATEGORY_ROLE) or '')].append(unicode(item.data(0, NAME_ROLE) or '')) for items in selected_map.itervalues(): items.sort(key=self.index_of_name) if selected_map['text']: m.addAction(QIcon(I('format-text-color.png')), _('Link &stylesheets...'), partial(self.link_stylesheets, selected_map['text'])) if len(selected_map['text']) > 1: m.addAction(QIcon(I('merge.png')), _('&Merge selected text files'), partial(self.start_merge, 'text', selected_map['text'])) if len(selected_map['styles']) > 1: m.addAction(QIcon(I('merge.png')), _('&Merge selected style files'), partial(self.start_merge, 'styles', selected_map['styles'])) if len(list(m.actions())) > 0: m.popup(self.mapToGlobal(point))
def contextMenuEvent(self, ev): m = QMenu(self) m.addAction(_('Set date to undefined') + '\t' + QKeySequence(Qt.Key_Minus).toString(QKeySequence.NativeText), self.clear_date) m.addAction(_('Set date to today') + '\t' + QKeySequence(Qt.Key_Equal).toString(QKeySequence.NativeText), self.today_date) m.addSeparator() populate_standard_spinbox_context_menu(self, m) m.popup(ev.globalPos())
def contextMenuEvent(self, ev): m = QMenu(self) m.addAction( _('Set date to undefined') + '\t' + QKeySequence(Qt.Key_Minus).toString(QKeySequence.NativeText), self.clear_date) m.addSeparator() populate_standard_spinbox_context_menu(self, m) m.popup(ev.globalPos())
def show_context_menu(self, point): item = self.itemAt(point) if item is None or item in set(self.categories.itervalues()): return m = QMenu(self) sel = self.selectedItems() num = len(sel) container = current_container() ci = self.currentItem() if ci is not None: cn = unicode(ci.data(0, NAME_ROLE) or '') mt = unicode(ci.data(0, MIME_ROLE) or '') cat = unicode(ci.data(0, CATEGORY_ROLE) or '') n = elided_text(cn.rpartition('/')[-1]) m.addAction(QIcon(I('save.png')), _('Export %s') % n, partial(self.export, cn)) if cn not in container.names_that_must_not_be_changed and cn not in container.names_that_must_not_be_removed and mt not in OEB_FONTS: m.addAction(_('Replace %s with file...') % n, partial(self.replace, cn)) if num > 1: m.addAction(QIcon(I('save.png')), _('Export all %d selected files') % num, self.export_selected) m.addSeparator() m.addAction(QIcon(I('modified.png')), _('&Rename %s') % n, self.edit_current_item) if is_raster_image(mt): m.addAction(QIcon(I('default_cover.png')), _('Mark %s as cover image') % n, partial(self.mark_as_cover, cn)) elif current_container().SUPPORTS_TITLEPAGES and mt in OEB_DOCS and cat == 'text': m.addAction(QIcon(I('default_cover.png')), _('Mark %s as cover page') % n, partial(self.mark_as_titlepage, cn)) m.addSeparator() if num > 0: m.addSeparator() if num > 1: m.addAction(QIcon(I('modified.png')), _('&Bulk rename the selected files'), self.request_bulk_rename) m.addAction(QIcon(I('modified.png')), _('Change the file extension for the selected files'), self.request_change_ext) m.addAction(QIcon(I('trash.png')), ngettext( '&Delete the selected file', '&Delete the {} selected files', num).format(num), self.request_delete) m.addSeparator() selected_map = defaultdict(list) for item in sel: selected_map[unicode(item.data(0, CATEGORY_ROLE) or '')].append(unicode(item.data(0, NAME_ROLE) or '')) for items in selected_map.itervalues(): items.sort(key=self.index_of_name) if selected_map['text']: m.addAction(QIcon(I('format-text-color.png')), _('Link &stylesheets...'), partial(self.link_stylesheets, selected_map['text'])) if len(selected_map['text']) > 1: m.addAction(QIcon(I('merge.png')), _('&Merge selected text files'), partial(self.start_merge, 'text', selected_map['text'])) if len(selected_map['styles']) > 1: m.addAction(QIcon(I('merge.png')), _('&Merge selected style files'), partial(self.start_merge, 'styles', selected_map['styles'])) if len(list(m.actions())) > 0: m.popup(self.mapToGlobal(point))
class SearchRestrictionMixin(object): no_restriction = _('<None>') def __init__(self, *args, **kwargs): pass def init_search_restriction_mixin(self): self.checked = QIcon(I('ok.png')) self.empty = QIcon(I('blank.png')) self.current_search_action = QAction(self.empty, _('*current search'), self) self.current_search_action.triggered.connect( partial(self.apply_virtual_library, library='*')) self.addAction(self.current_search_action) self.keyboard.register_shortcut( 'vl-from-current-search', _('Virtual library from current search'), description=_( 'Create a temporary Virtual library from the current search'), group=_('Miscellaneous'), default_keys=('Ctrl+*', ), action=self.current_search_action) self.search_based_vl_name = None self.search_based_vl = None self.virtual_library_menu = QMenu() self.virtual_library.clicked.connect(self.virtual_library_clicked) self.clear_vl.clicked.connect(lambda x: (self.apply_virtual_library( ), self.clear_additional_restriction())) self.virtual_library_tooltip = \ _('Use a "virtual library" to show only a subset of the books present in this library') self.virtual_library.setToolTip(self.virtual_library_tooltip) self.search_restriction = ComboBoxWithHelp(self) self.search_restriction.setVisible(False) self.clear_vl.setText(_("(all books)")) self.ar_menu = QMenu(_('Additional restriction')) self.edit_menu = QMenu(_('Edit Virtual library')) self.rm_menu = QMenu(_('Remove Virtual library')) self.search_restriction_list_built = False def add_virtual_library(self, db, name, search): virt_libs = db.prefs.get('virtual_libraries', {}) virt_libs[name] = search db.new_api.set_pref('virtual_libraries', virt_libs) def do_create_edit(self, name=None): db = self.library_view.model().db virt_libs = db.prefs.get('virtual_libraries', {}) cd = CreateVirtualLibrary(self, virt_libs.keys(), editing=name) if cd.exec_() == cd.Accepted: if name: self._remove_vl(name, reapply=False) self.add_virtual_library(db, cd.library_name, cd.library_search) if not name or name == db.data.get_base_restriction_name(): self.apply_virtual_library(cd.library_name) self.rebuild_vl_tabs() def virtual_library_clicked(self): m = self.virtual_library_menu m.clear() a = m.addAction(_('Create Virtual library')) a.triggered.connect(partial(self.do_create_edit, name=None)) a = self.edit_menu self.build_virtual_library_list(a, self.do_create_edit) m.addMenu(a) a = self.rm_menu self.build_virtual_library_list(a, self.remove_vl_triggered) m.addMenu(a) if gprefs['show_vl_tabs']: m.addAction(_('Hide virtual library tabs'), self.vl_tabs.disable_bar) else: m.addAction(_('Show virtual libraries as tabs'), self.vl_tabs.enable_bar) m.addSeparator() db = self.library_view.model().db a = self.ar_menu a.clear() a.setIcon(self.checked if db.data.get_search_restriction_name( ) else self.empty) self.build_search_restriction_list() m.addMenu(a) m.addSeparator() current_lib = db.data.get_base_restriction_name() if current_lib == '': a = m.addAction(self.checked, self.no_restriction) else: a = m.addAction(self.empty, self.no_restriction) a.triggered.connect(partial(self.apply_virtual_library, library='')) a = m.addAction(self.current_search_action) if self.search_based_vl_name: a = m.addAction( self.checked if db.data.get_base_restriction_name().startswith('*') else self.empty, self.search_based_vl_name) a.triggered.connect( partial(self.apply_virtual_library, library=self.search_based_vl_name)) m.addSeparator() virt_libs = db.prefs.get('virtual_libraries', {}) for vl in sorted(virt_libs.keys(), key=sort_key): a = m.addAction(self.checked if vl == current_lib else self.empty, vl.replace('&', '&&')) a.triggered.connect(partial(self.apply_virtual_library, library=vl)) p = QPoint(0, self.virtual_library.height()) self.virtual_library_menu.popup(self.virtual_library.mapToGlobal(p)) def rebuild_vl_tabs(self): self.vl_tabs.rebuild() def apply_virtual_library(self, library=None, update_tabs=True): db = self.library_view.model().db virt_libs = db.prefs.get('virtual_libraries', {}) if not library: db.data.set_base_restriction('') db.data.set_base_restriction_name('') elif library == '*': if not self.search.current_text: error_dialog(self, _('No search'), _('There is no current search to use'), show=True) return txt = _build_full_search_string(self) try: db.data.search_getting_ids('', txt, use_virtual_library=False) except ParseException as e: error_dialog(self, _('Invalid search'), _('The search in the search box is not valid'), det_msg=e.msg, show=True) return self.search_based_vl = txt db.data.set_base_restriction(txt) self.search_based_vl_name = self._trim_restriction_name('*' + txt) db.data.set_base_restriction_name(self.search_based_vl_name) elif library == self.search_based_vl_name: db.data.set_base_restriction(self.search_based_vl) db.data.set_base_restriction_name(self.search_based_vl_name) elif library in virt_libs: db.data.set_base_restriction(virt_libs[library]) db.data.set_base_restriction_name(library) self.virtual_library.setToolTip(self.virtual_library_tooltip + '\n' + db.data.get_base_restriction()) self._apply_search_restriction(db.data.get_search_restriction(), db.data.get_search_restriction_name()) if update_tabs: self.vl_tabs.update_current() def build_virtual_library_list(self, menu, handler): db = self.library_view.model().db virt_libs = db.prefs.get('virtual_libraries', {}) menu.clear() menu.setIcon(self.empty) def add_action(name, search): a = menu.addAction(name.replace('&', '&&')) a.triggered.connect(partial(handler, name=name)) a.setIcon(self.empty) libs = sorted(virt_libs.keys(), key=sort_key) if libs: menu.setEnabled(True) for n in libs: add_action(n, virt_libs[n]) else: menu.setEnabled(False) def remove_vl_triggered(self, name=None): if not confirm(_( 'Are you sure you want to remove the virtual library <b>{0}</b>?' ).format(name), 'confirm_vl_removal', parent=self): return self._remove_vl(name, reapply=True) def _remove_vl(self, name, reapply=True): db = self.library_view.model().db virt_libs = db.prefs.get('virtual_libraries', {}) virt_libs.pop(name, None) db.new_api.set_pref('virtual_libraries', virt_libs) if reapply and db.data.get_base_restriction_name() == name: self.apply_virtual_library('') self.rebuild_vl_tabs() def _trim_restriction_name(self, name): return name[0:MAX_VIRTUAL_LIBRARY_NAME_LENGTH].strip() def build_search_restriction_list(self): self.search_restriction_list_built = True from calibre.gui2.ui import get_gui m = self.ar_menu m.clear() current_restriction_text = None if self.search_restriction.count() > 1: txt = unicode(self.search_restriction.itemText(2)) if txt.startswith('*'): current_restriction_text = txt self.search_restriction.clear() current_restriction = self.library_view.model( ).db.data.get_search_restriction_name() m.setIcon(self.checked if current_restriction else self.empty) def add_action(txt, index): self.search_restriction.addItem(txt) txt = self._trim_restriction_name(txt) if txt == current_restriction: a = m.addAction(self.checked, txt if txt else self.no_restriction) else: a = m.addAction(self.empty, txt if txt else self.no_restriction) a.triggered.connect( partial(self.search_restriction_triggered, action=a, index=index)) add_action('', 0) add_action(_('*current search'), 1) dex = 2 if current_restriction_text: add_action(current_restriction_text, 2) dex += 1 for n in sorted(get_gui().current_db.saved_search_names(), key=sort_key): add_action(n, dex) dex += 1 def search_restriction_triggered(self, action=None, index=None): self.search_restriction.setCurrentIndex(index) self.apply_search_restriction(index) def apply_named_search_restriction(self, name): if not self.search_restriction_list_built: self.build_search_restriction_list() if not name: r = 0 else: r = self.search_restriction.findText(name) if r < 0: r = 0 self.search_restriction.setCurrentIndex(r) self.apply_search_restriction(r) def apply_text_search_restriction(self, search): if not self.search_restriction_list_built: self.build_search_restriction_list() search = unicode(search) if not search: self.search_restriction.setCurrentIndex(0) self._apply_search_restriction('', '') else: s = '*' + search if self.search_restriction.count() > 1: txt = unicode(self.search_restriction.itemText(2)) if txt.startswith('*'): self.search_restriction.setItemText(2, s) else: self.search_restriction.insertItem(2, s) else: self.search_restriction.insertItem(2, s) self.search_restriction.setCurrentIndex(2) self._apply_search_restriction(search, self._trim_restriction_name(s)) def apply_search_restriction(self, i): if not self.search_restriction_list_built: self.build_search_restriction_list() if i == 1: self.apply_text_search_restriction( unicode(self.search.currentText())) elif i == 2 and unicode( self.search_restriction.currentText()).startswith('*'): self.apply_text_search_restriction( unicode(self.search_restriction.currentText())[1:]) else: r = unicode(self.search_restriction.currentText()) if r is not None and r != '': restriction = 'search:"%s"' % (r) else: restriction = '' self._apply_search_restriction(restriction, r) def clear_additional_restriction(self): self.search_restriction.setCurrentIndex(0) self._apply_search_restriction('', '') def _apply_search_restriction(self, restriction, name): self.saved_search.clear() # The order below is important. Set the restriction, force a '' search # to apply it, reset the tag browser to take it into account, then set # the book count. self.library_view.model().db.data.set_search_restriction(restriction) self.library_view.model().db.data.set_search_restriction_name(name) self.search.clear(emit_search=True) self.tags_view.recount() self.set_number_of_books_shown() self.current_view().setFocus(Qt.OtherFocusReason) self.set_window_title() v = self.current_view() if not v.currentIndex().isValid(): v.set_current_row() if not v.refresh_book_details(): self.book_details.reset_info() def set_number_of_books_shown(self): db = self.library_view.model().db if self.current_view() == self.library_view and db is not None and \ db.data.search_restriction_applied(): restrictions = [ x for x in (db.data.get_base_restriction_name(), db.data.get_search_restriction_name()) if x ] t = ' :: '.join(restrictions) if len(t) > 20: t = t[:19] + u'…' self.clear_vl.setVisible(True) self.clear_vl.setVisible(not gprefs['show_vl_tabs']) else: # No restriction or not library view t = '' self.clear_vl.setVisible(False) self.clear_vl.setText(t.replace('&', '&&'))
class TagsView(QTreeView): # {{{ refresh_required = pyqtSignal() tags_marked = pyqtSignal(object) edit_user_category = pyqtSignal(object) delete_user_category = pyqtSignal(object) del_item_from_user_cat = pyqtSignal(object, object, object) add_item_to_user_cat = pyqtSignal(object, object, object) add_subcategory = pyqtSignal(object) tags_list_edit = pyqtSignal(object, object, object) saved_search_edit = pyqtSignal(object) rebuild_saved_searches = pyqtSignal() author_sort_edit = pyqtSignal(object, object, object, object, object) tag_item_renamed = pyqtSignal() search_item_renamed = pyqtSignal() drag_drop_finished = pyqtSignal(object) restriction_error = pyqtSignal() tag_item_delete = pyqtSignal(object, object, object, object, object) apply_tag_to_selected = pyqtSignal(object, object, object) def __init__(self, parent=None): QTreeView.__init__(self, parent=None) self.setMouseTracking(True) self.alter_tb = None self.disable_recounting = False self.setUniformRowHeights(True) self.setIconSize(QSize(20, 20)) self.setTabKeyNavigation(True) self.setAnimated(True) self.setHeaderHidden(True) self.setItemDelegate(TagDelegate(self)) self.made_connections = False self.setAcceptDrops(True) self.setDragEnabled(True) self.setDragDropMode(self.DragDrop) self.setDropIndicatorShown(True) self.in_drag_drop = False self.setAutoExpandDelay(500) self.pane_is_visible = False self.search_icon = QIcon(I('search.png')) self.search_copy_icon = QIcon(I("search_copy_saved.png")) self.user_category_icon = QIcon(I('tb_folder.png')) self.edit_metadata_icon = QIcon(I('edit_input.png')) self.delete_icon = QIcon(I('list_remove.png')) self.rename_icon = QIcon(I('edit-undo.png')) self._model = TagsModel(self) self._model.search_item_renamed.connect(self.search_item_renamed) self._model.refresh_required.connect(self.refresh_required, type=Qt.QueuedConnection) self._model.tag_item_renamed.connect(self.tag_item_renamed) self._model.restriction_error.connect(self.restriction_error) self._model.user_categories_edited.connect(self.user_categories_edited, type=Qt.QueuedConnection) self._model.drag_drop_finished.connect(self.drag_drop_finished) self.set_look_and_feel() # Allowing keyboard focus looks bad in the Qt Fusion style and is useless # anyway since the enter/spacebar keys do nothing self.setFocusPolicy(Qt.NoFocus) QApplication.instance().palette_changed.connect(self.set_style_sheet, type=Qt.QueuedConnection) def set_style_sheet(self): stylish_tb = ''' QTreeView { background-color: palette(window); color: palette(window-text); border: none; } ''' self.setStyleSheet(''' QTreeView::item { border: 1px solid transparent; padding-top:PADex; padding-bottom:PADex; } QTreeView::item:hover { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1); border: 1px solid #bfcde4; border-radius: 6px; } '''.replace('PAD', unicode_type(gprefs['tag_browser_item_padding'])) + ( '' if gprefs['tag_browser_old_look'] else stylish_tb)) def set_look_and_feel(self): self.set_style_sheet() self.setAlternatingRowColors(gprefs['tag_browser_old_look']) self.itemDelegate().old_look = gprefs['tag_browser_old_look'] @property def hidden_categories(self): return self._model.hidden_categories @property def db(self): return self._model.db @property def collapse_model(self): return self._model.collapse_model def set_pane_is_visible(self, to_what): pv = self.pane_is_visible self.pane_is_visible = to_what if to_what and not pv: self.recount() def get_state(self): state_map = {} expanded_categories = [] hide_empty_categories = self.model().prefs['tag_browser_hide_empty_categories'] crmap = self._model.category_row_map() for category in self._model.category_nodes: if (category.category_key in self.hidden_categories or ( hide_empty_categories and len(category.child_tags()) == 0)): continue row = crmap.get(category.category_key) if row is not None: index = self._model.index(row, 0, QModelIndex()) if self.isExpanded(index): expanded_categories.append(category.category_key) states = [c.tag.state for c in category.child_tags()] names = [(c.tag.name, c.tag.category) for c in category.child_tags()] state_map[category.category_key] = dict(zip(names, states)) return expanded_categories, state_map def reread_collapse_parameters(self): self._model.reread_collapse_model(self.get_state()[1]) def set_database(self, db, alter_tb): self._model.set_database(db) self.alter_tb = alter_tb self.pane_is_visible = True # because TagsModel.set_database did a recount self.setModel(self._model) self.setContextMenuPolicy(Qt.CustomContextMenu) pop = self.db.CATEGORY_SORTS.index(config['sort_tags_by']) self.alter_tb.sort_menu.actions()[pop].setChecked(True) try: match_pop = self.db.MATCH_TYPE.index(config['match_tags_type']) except ValueError: match_pop = 0 self.alter_tb.match_menu.actions()[match_pop].setChecked(True) if not self.made_connections: self.clicked.connect(self.toggle) self.customContextMenuRequested.connect(self.show_context_menu) self.refresh_required.connect(self.recount, type=Qt.QueuedConnection) self.alter_tb.sort_menu.triggered.connect(self.sort_changed) self.alter_tb.match_menu.triggered.connect(self.match_changed) self.made_connections = True self.refresh_signal_processed = True db.add_listener(self.database_changed) self.expanded.connect(self.item_expanded) def database_changed(self, event, ids): if self.refresh_signal_processed: self.refresh_signal_processed = False self.refresh_required.emit() def user_categories_edited(self, user_cats, nkey): state_map = self.get_state()[1] self.db.new_api.set_pref('user_categories', user_cats) self._model.rebuild_node_tree(state_map=state_map) p = self._model.find_category_node('@'+nkey) self.show_item_at_path(p) @property def match_all(self): return (self.alter_tb and self.alter_tb.match_menu.actions()[1].isChecked()) def sort_changed(self, action): for i, ac in enumerate(self.alter_tb.sort_menu.actions()): if ac is action: config.set('sort_tags_by', self.db.CATEGORY_SORTS[i]) self.recount() break def match_changed(self, action): try: for i, ac in enumerate(self.alter_tb.match_menu.actions()): if ac is action: config.set('match_tags_type', self.db.MATCH_TYPE[i]) except: pass def mousePressEvent(self, event): if event.buttons() & Qt.LeftButton: self.possible_drag_start = event.pos() return QTreeView.mousePressEvent(self, event) def mouseMoveEvent(self, event): dex = self.indexAt(event.pos()) if dex.isValid(): self.setCursor(Qt.PointingHandCursor) else: self.unsetCursor() if not event.buttons() & Qt.LeftButton: return if self.in_drag_drop or not dex.isValid(): QTreeView.mouseMoveEvent(self, event) return # don't start drag/drop until the mouse has moved a bit. if ((event.pos() - self.possible_drag_start).manhattanLength() < QApplication.startDragDistance()): QTreeView.mouseMoveEvent(self, event) return # Must deal with odd case where the node being dragged is 'virtual', # created to form a hierarchy. We can't really drag this node, but in # addition we can't allow drag recognition to notice going over some # other node and grabbing that one. So we set in_drag_drop to prevent # this from happening, turning it off when the user lifts the button. self.in_drag_drop = True if not self._model.flags(dex) & Qt.ItemIsDragEnabled: QTreeView.mouseMoveEvent(self, event) return md = self._model.mimeData([dex]) pixmap = dex.data(DRAG_IMAGE_ROLE).pixmap(self.iconSize()) drag = QDrag(self) drag.setPixmap(pixmap) drag.setMimeData(md) if (self._model.is_in_user_category(dex) or self._model.is_index_on_a_hierarchical_category(dex)): ''' Things break if we specify MoveAction as the default, which is what we want for drag on hierarchical categories. Dragging user categories stops working. Don't know why. To avoid the problem we fix the action in dragMoveEvent. ''' drag.exec_(Qt.CopyAction|Qt.MoveAction, Qt.CopyAction) else: drag.exec_(Qt.CopyAction) def mouseReleaseEvent(self, event): # Swallow everything except leftButton so context menus work correctly if event.button() == Qt.LeftButton or self.in_drag_drop: QTreeView.mouseReleaseEvent(self, event) self.in_drag_drop = False def mouseDoubleClickEvent(self, event): # swallow these to avoid toggling and editing at the same time pass @property def search_string(self): tokens = self._model.tokens() joiner = ' and ' if self.match_all else ' or ' return joiner.join(tokens) def toggle_current_index(self): ci = self.currentIndex() if ci.isValid(): self.toggle(ci) def toggle(self, index): self._toggle(index, None) def _toggle(self, index, set_to): ''' set_to: if None, advance the state. Otherwise must be one of the values in TAG_SEARCH_STATES ''' modifiers = int(QApplication.keyboardModifiers()) exclusive = modifiers not in (Qt.CTRL, Qt.SHIFT) if self._model.toggle(index, exclusive, set_to=set_to): self.tags_marked.emit(self.search_string) def conditional_clear(self, search_string): if search_string != self.search_string: self.clear() def context_menu_handler(self, action=None, category=None, key=None, index=None, search_state=None, use_vl=None, is_first_letter=False): if not action: return try: if action == 'set_icon': try: path = choose_files(self, 'choose_category_icon', _('Change icon for: %s')%key, filters=[ ('Images', ['png', 'gif', 'jpg', 'jpeg'])], all_files=False, select_only_single_file=True) if path: path = path[0] p = QIcon(path).pixmap(QSize(128, 128)) d = os.path.join(config_dir, 'tb_icons') if not os.path.exists(d): os.makedirs(d) with open(os.path.join(d, 'icon_' + sanitize_file_name(key)+'.png'), 'wb') as f: f.write(pixmap_to_data(p, format='PNG')) path = os.path.basename(f.name) self._model.set_custom_category_icon(key, unicode_type(path)) self.recount() except: import traceback traceback.print_exc() return if action == 'clear_icon': self._model.set_custom_category_icon(key, None) self.recount() return def set_completion_data(category): try: completion_data = self.db.new_api.all_field_names(category) except: completion_data = None self.itemDelegate().set_completion_data(completion_data) if action == 'edit_item_no_vl': item = self.model().get_node(index) item.use_vl = False set_completion_data(category) self.edit(index) return if action == 'edit_item_in_vl': item = self.model().get_node(index) item.use_vl = True set_completion_data(category) self.edit(index) return if action == 'delete_item_in_vl': tag = index.tag children = index.child_tags() self.tag_item_delete.emit(key, tag.id, tag.original_name, self.model().get_book_ids_to_use(), children) return if action == 'delete_item_no_vl': tag = index.tag children = index.child_tags() self.tag_item_delete.emit(key, tag.id, tag.original_name, None, children) return if action == 'open_editor': self.tags_list_edit.emit(category, key, is_first_letter) return if action == 'manage_categories': self.edit_user_category.emit(category) return if action == 'search': self._toggle(index, set_to=search_state) return if action == "raw_search": from calibre.gui2.ui import get_gui get_gui().get_saved_search_text(search_name='search:' + key) return if action == 'add_to_category': tag = index.tag if len(index.children) > 0: for c in index.all_children(): self.add_item_to_user_cat.emit(category, c.tag.original_name, c.tag.category) self.add_item_to_user_cat.emit(category, tag.original_name, tag.category) return if action == 'add_subcategory': self.add_subcategory.emit(key) return if action == 'search_category': self._toggle(index, set_to=search_state) return if action == 'delete_user_category': self.delete_user_category.emit(key) return if action == 'delete_search': self.model().db.saved_search_delete(key) self.rebuild_saved_searches.emit() return if action == 'delete_item_from_user_category': tag = index.tag if len(index.children) > 0: for c in index.children: self.del_item_from_user_cat.emit(key, c.tag.original_name, c.tag.category) self.del_item_from_user_cat.emit(key, tag.original_name, tag.category) return if action == 'manage_searches': self.saved_search_edit.emit(category) return if action == 'edit_authors': self.author_sort_edit.emit(self, index, False, False, is_first_letter) return if action == 'edit_author_sort': self.author_sort_edit.emit(self, index, True, False, is_first_letter) return if action == 'edit_author_link': self.author_sort_edit.emit(self, index, False, True, False) return reset_filter_categories = True if action == 'hide': self.hidden_categories.add(category) elif action == 'show': self.hidden_categories.discard(category) elif action == 'categorization': changed = self.collapse_model != category self._model.collapse_model = category if changed: reset_filter_categories = False gprefs['tags_browser_partition_method'] = category elif action == 'defaults': self.hidden_categories.clear() elif action == 'add_tag': item = self.model().get_node(index) if item is not None: self.apply_to_selected_books(item) return elif action == 'remove_tag': item = self.model().get_node(index) if item is not None: self.apply_to_selected_books(item, True) return self.db.new_api.set_pref('tag_browser_hidden_categories', list(self.hidden_categories)) if reset_filter_categories: self._model.set_categories_filter(None) self._model.rebuild_node_tree() except Exception: import traceback traceback.print_exc() return def apply_to_selected_books(self, item, remove=False): if item.type != item.TAG: return tag = item.tag if not tag.category or not tag.original_name: return self.apply_tag_to_selected.emit(tag.category, tag.original_name, remove) def show_context_menu(self, point): def display_name(tag): ans = tag.name if tag.category == 'search': n = tag.name if len(n) > 45: n = n[:45] + '...' ans = "'" + n + "'" elif tag.is_hierarchical and not tag.is_editable: ans = tag.original_name if ans: ans = ans.replace('&', '&&') return ans index = self.indexAt(point) self.context_menu = QMenu(self) if index.isValid(): item = index.data(Qt.UserRole) tag = None tag_item = item if item.type == TagTreeItem.TAG: tag = item.tag while item.type != TagTreeItem.CATEGORY: item = item.parent if item.type == TagTreeItem.CATEGORY: if not item.category_key.startswith('@'): while item.parent != self._model.root_item: item = item.parent category = unicode_type(item.name or '') key = item.category_key # Verify that we are working with a field that we know something about if key not in self.db.field_metadata: return True fm = self.db.field_metadata[key] # Did the user click on a leaf node? if tag: # If the user right-clicked on an editable item, then offer # the possibility of renaming that item. if tag.is_editable or tag.is_hierarchical: # Add the 'rename' items to both interior and leaf nodes if self.model().get_in_vl(): self.context_menu.addAction(self.rename_icon, _('Rename %s in Virtual library')%display_name(tag), partial(self.context_menu_handler, action='edit_item_in_vl', index=index, category=key)) self.context_menu.addAction(self.rename_icon, _('Rename %s')%display_name(tag), partial(self.context_menu_handler, action='edit_item_no_vl', index=index, category=key)) if tag.is_editable: if key in ('tags', 'series', 'publisher') or \ self._model.db.field_metadata.is_custom_field(key): if self.model().get_in_vl(): self.context_menu.addAction(self.delete_icon, _('Delete %s in Virtual library')%display_name(tag), partial(self.context_menu_handler, action='delete_item_in_vl', key=key, index=tag_item)) self.context_menu.addAction(self.delete_icon, _('Delete %s')%display_name(tag), partial(self.context_menu_handler, action='delete_item_no_vl', key=key, index=tag_item)) if key == 'authors': self.context_menu.addAction(_('Edit sort for %s')%display_name(tag), partial(self.context_menu_handler, action='edit_author_sort', index=tag.id)) self.context_menu.addAction(_('Edit link for %s')%display_name(tag), partial(self.context_menu_handler, action='edit_author_link', index=tag.id)) # is_editable is also overloaded to mean 'can be added # to a User category' m = QMenu(_('Add %s to User category')%display_name(tag), self.context_menu) m.setIcon(self.user_category_icon) added = [False] def add_node_tree(tree_dict, m, path): p = path[:] for k in sorted(tree_dict.keys(), key=sort_key): p.append(k) n = k[1:] if k.startswith('@') else k m.addAction(self.user_category_icon, n, partial(self.context_menu_handler, 'add_to_category', category='.'.join(p), index=tag_item)) added[0] = True if len(tree_dict[k]): tm = m.addMenu(self.user_category_icon, _('Children of %s')%n) add_node_tree(tree_dict[k], tm, p) p.pop() add_node_tree(self.model().user_category_node_tree, m, []) if added[0]: self.context_menu.addMenu(m) # is_editable also means the tag can be applied/removed # from selected books if fm['datatype'] != 'rating': m = self.context_menu.addMenu(self.edit_metadata_icon, _('Apply %s to selected books')%display_name(tag)) m.addAction(QIcon(I('plus.png')), _('Add %s to selected books') % display_name(tag), partial(self.context_menu_handler, action='add_tag', index=index)) m.addAction(QIcon(I('minus.png')), _('Remove %s from selected books') % display_name(tag), partial(self.context_menu_handler, action='remove_tag', index=index)) elif key == 'search' and tag.is_searchable: self.context_menu.addAction(self.rename_icon, _('Rename %s')%display_name(tag), partial(self.context_menu_handler, action='edit_item_no_vl', index=index)) self.context_menu.addAction(self.delete_icon, _('Delete search %s')%display_name(tag), partial(self.context_menu_handler, action='delete_search', key=tag.original_name)) if key.startswith('@') and not item.is_gst: self.context_menu.addAction(self.user_category_icon, _('Remove %(item)s from category %(cat)s')% dict(item=display_name(tag), cat=item.py_name), partial(self.context_menu_handler, action='delete_item_from_user_category', key=key, index=tag_item)) if tag.is_searchable: # Add the search for value items. All leaf nodes are searchable self.context_menu.addAction(self.search_icon, _('Search for %s')%display_name(tag), partial(self.context_menu_handler, action='search', search_state=TAG_SEARCH_STATES['mark_plus'], index=index)) self.context_menu.addAction(self.search_icon, _('Search for everything but %s')%display_name(tag), partial(self.context_menu_handler, action='search', search_state=TAG_SEARCH_STATES['mark_minus'], index=index)) self.context_menu.addAction(self.search_copy_icon, _('Search using saved search expression'), partial(self.context_menu_handler, action='raw_search', key=tag.name)) self.context_menu.addSeparator() elif key.startswith('@') and not item.is_gst: if item.can_be_edited: self.context_menu.addAction(self.rename_icon, _('Rename %s')%item.py_name, partial(self.context_menu_handler, action='edit_item_no_vl', index=index)) self.context_menu.addAction(self.user_category_icon, _('Add sub-category to %s')%item.py_name, partial(self.context_menu_handler, action='add_subcategory', key=key)) self.context_menu.addAction(self.delete_icon, _('Delete User category %s')%item.py_name, partial(self.context_menu_handler, action='delete_user_category', key=key)) self.context_menu.addSeparator() # Hide/Show/Restore categories self.context_menu.addAction(_('Hide category %s') % category, partial(self.context_menu_handler, action='hide', category=key)) if self.hidden_categories: m = self.context_menu.addMenu(_('Show category')) for col in sorted(self.hidden_categories, key=lambda x: sort_key(self.db.field_metadata[x]['name'])): m.addAction(self.db.field_metadata[col]['name'], partial(self.context_menu_handler, action='show', category=col)) # search by category. Some categories are not searchable, such # as search and news if item.tag.is_searchable: self.context_menu.addAction(self.search_icon, _('Search for books in category %s')%category, partial(self.context_menu_handler, action='search_category', index=self._model.createIndex(item.row(), 0, item), search_state=TAG_SEARCH_STATES['mark_plus'])) self.context_menu.addAction(self.search_icon, _('Search for books not in category %s')%category, partial(self.context_menu_handler, action='search_category', index=self._model.createIndex(item.row(), 0, item), search_state=TAG_SEARCH_STATES['mark_minus'])) # Offer specific editors for tags/series/publishers/saved searches self.context_menu.addSeparator() if key in ['tags', 'publisher', 'series'] or ( self.db.field_metadata[key]['is_custom'] and self.db.field_metadata[key]['datatype'] != 'composite'): if tag_item.type == TagTreeItem.CATEGORY and tag_item.temporary: self.context_menu.addAction(_('Manage %s')%category, partial(self.context_menu_handler, action='open_editor', category=tag_item.name, key=key, is_first_letter=True)) else: self.context_menu.addAction(_('Manage %s')%category, partial(self.context_menu_handler, action='open_editor', category=tag.original_name if tag else None, key=key)) elif key == 'authors': if tag_item.type == TagTreeItem.CATEGORY: if tag_item.temporary: self.context_menu.addAction(_('Manage %s')%category, partial(self.context_menu_handler, action='edit_authors', index=tag_item.name, is_first_letter=True)) else: self.context_menu.addAction(_('Manage %s')%category, partial(self.context_menu_handler, action='edit_authors')) else: self.context_menu.addAction(_('Manage %s')%category, partial(self.context_menu_handler, action='edit_authors', index=tag.id)) elif key == 'search': self.context_menu.addAction(_('Manage Saved searches'), partial(self.context_menu_handler, action='manage_searches', category=tag.name if tag else None)) if tag is None: self.context_menu.addSeparator() self.context_menu.addAction(_('Change category icon'), partial(self.context_menu_handler, action='set_icon', key=key)) self.context_menu.addAction(_('Restore default icon'), partial(self.context_menu_handler, action='clear_icon', key=key)) # Always show the User categories editor self.context_menu.addSeparator() if key.startswith('@') and \ key[1:] in self.db.prefs.get('user_categories', {}).keys(): self.context_menu.addAction(_('Manage User categories'), partial(self.context_menu_handler, action='manage_categories', category=key[1:])) else: self.context_menu.addAction(_('Manage User categories'), partial(self.context_menu_handler, action='manage_categories', category=None)) if self.hidden_categories: if not self.context_menu.isEmpty(): self.context_menu.addSeparator() self.context_menu.addAction(_('Show all categories'), partial(self.context_menu_handler, action='defaults')) m = self.context_menu.addMenu(_('Change sub-categorization scheme')) da = m.addAction(_('Disable'), partial(self.context_menu_handler, action='categorization', category='disable')) fla = m.addAction(_('By first letter'), partial(self.context_menu_handler, action='categorization', category='first letter')) pa = m.addAction(_('Partition'), partial(self.context_menu_handler, action='categorization', category='partition')) if self.collapse_model == 'disable': da.setCheckable(True) da.setChecked(True) elif self.collapse_model == 'first letter': fla.setCheckable(True) fla.setChecked(True) else: pa.setCheckable(True) pa.setChecked(True) if config['sort_tags_by'] != "name": fla.setEnabled(False) m.hovered.connect(self.collapse_menu_hovered) fla.setToolTip(_('First letter is usable only when sorting by name')) # Apparently one cannot set a tooltip to empty, so use a star and # deal with it in the hover method da.setToolTip('*') pa.setToolTip('*') if index.isValid() and self.model().rowCount(index) > 0: self.context_menu.addSeparator() self.context_menu.addAction(_('E&xpand all children'), partial(self.expand_node_and_descendants, index)) self.context_menu.addAction(_('Collapse all levels'), self.collapseAll) if not self.context_menu.isEmpty(): self.context_menu.popup(self.mapToGlobal(point)) return True def expand_node_and_descendants(self, index): if not index.isValid(): return self.expand(index) for r in range(self.model().rowCount(index)): self.expand_node_and_descendants(index.child(r, 0)) def collapse_menu_hovered(self, action): tip = action.toolTip() if tip == '*': tip = '' QToolTip.showText(QCursor.pos(), tip) def dragMoveEvent(self, event): QTreeView.dragMoveEvent(self, event) self.setDropIndicatorShown(False) index = self.indexAt(event.pos()) if not index.isValid(): return src_is_tb = event.mimeData().hasFormat('application/calibre+from_tag_browser') item = index.data(Qt.UserRole) if item.type == TagTreeItem.ROOT: return if src_is_tb: src = json_loads(bytes(event.mimeData().data('application/calibre+from_tag_browser'))) if len(src) == 1: src_item = self._model.get_node(self._model.index_for_path(src[0][5])) if (src_item.type == TagTreeItem.TAG and src_item.tag.category == item.tag.category and not item.temporary and self._model.is_key_a_hierarchical_category(src_item.tag.category)): event.setDropAction(Qt.MoveAction) self.setDropIndicatorShown(True) return if item.type == TagTreeItem.TAG and self._model.flags(index) & Qt.ItemIsDropEnabled: event.setDropAction(Qt.CopyAction) self.setDropIndicatorShown(not src_is_tb) return if item.type == TagTreeItem.CATEGORY and not item.is_gst: fm_dest = self.db.metadata_for_field(item.category_key) if fm_dest['kind'] == 'user': if src_is_tb: if event.dropAction() == Qt.MoveAction: # src is initialized above for s in src: if s[0] == TagTreeItem.TAG and \ (not s[1].startswith('@') or s[2]): return self.setDropIndicatorShown(True) return md = event.mimeData() if hasattr(md, 'column_name'): fm_src = self.db.metadata_for_field(md.column_name) if md.column_name in ['authors', 'publisher', 'series'] or \ (fm_src['is_custom'] and ( (fm_src['datatype'] in ['series', 'text', 'enumeration'] and not fm_src['is_multiple']) or ( fm_src['datatype'] == 'composite' and fm_src['display'].get('make_category', False)))): self.setDropIndicatorShown(True) def clear(self): if self.model(): self.model().clear_state() def is_visible(self, idx): item = idx.data(Qt.UserRole) if getattr(item, 'type', None) == TagTreeItem.TAG: idx = idx.parent() return self.isExpanded(idx) def recount_with_position_based_index(self): self._model.use_position_based_index_on_next_recount = True self.recount() def recount(self, *args): ''' Rebuild the category tree, expand any categories that were expanded, reset the search states, and reselect the current node. ''' if self.disable_recounting or not self.pane_is_visible: return self.refresh_signal_processed = True ci = self.currentIndex() if not ci.isValid(): ci = self.indexAt(QPoint(10, 10)) use_pos = self._model.use_position_based_index_on_next_recount self._model.use_position_based_index_on_next_recount = False if use_pos: path = self._model.path_for_index(ci) if self.is_visible(ci) else None else: path = self._model.named_path_for_index(ci) if self.is_visible(ci) else None expanded_categories, state_map = self.get_state() self._model.rebuild_node_tree(state_map=state_map) self.blockSignals(True) for category in expanded_categories: idx = self._model.index_for_category(category) if idx is not None and idx.isValid(): self.expand(idx) if path is not None: if use_pos: self.show_item_at_path(path) else: index = self._model.index_for_named_path(path) if index.isValid(): self.show_item_at_index(index) self.blockSignals(False) def show_item_at_path(self, path, box=False, position=QTreeView.PositionAtCenter): ''' Scroll the browser and open categories to show the item referenced by path. If possible, the item is placed in the center. If box=True, a box is drawn around the item. ''' if path: self.show_item_at_index(self._model.index_for_path(path), box=box, position=position) def expand_parent(self, idx): # Needed otherwise Qt sometimes segfaults if the node is buried in a # collapsed, off screen hierarchy. To be safe, we expand from the # outermost in p = self._model.parent(idx) if p.isValid(): self.expand_parent(p) self.expand(idx) def show_item_at_index(self, idx, box=False, position=QTreeView.PositionAtCenter): if idx.isValid() and idx.data(Qt.UserRole) is not self._model.root_item: self.expand_parent(idx) self.setCurrentIndex(idx) self.scrollTo(idx, position) if box: self._model.set_boxed(idx) def item_expanded(self, idx): ''' Called by the expanded signal ''' self.setCurrentIndex(idx)
class ConfigWidget(ConfigWidgetBase, Ui_Form): def genesis(self, gui): self.gui = gui self.delegate = Delegate(self.tweaks_view) self.tweaks_view.setItemDelegate(self.delegate) self.tweaks_view.currentChanged = self.current_changed self.view = self.tweaks_view self.highlighter = PythonHighlighter(self.edit_tweak.document()) self.restore_default_button.clicked.connect(self.restore_to_default) self.apply_button.clicked.connect(self.apply_tweak) self.plugin_tweaks_button.clicked.connect(self.plugin_tweaks) self.splitter.setStretchFactor(0, 1) self.splitter.setStretchFactor(1, 100) self.next_button.clicked.connect(self.find_next) self.previous_button.clicked.connect(self.find_previous) self.search.initialize('tweaks_search_history', help_text=_('Search for tweak')) self.search.search.connect(self.find) self.view.setContextMenuPolicy(Qt.CustomContextMenu) self.view.customContextMenuRequested.connect(self.show_context_menu) self.copy_icon = QIcon(I('edit-copy.png')) def show_context_menu(self, point): idx = self.tweaks_view.currentIndex() if not idx.isValid(): return True tweak = self.tweaks.data(idx, Qt.UserRole) self.context_menu = QMenu(self) self.context_menu.addAction( self.copy_icon, _('Copy to clipboard'), partial(self.copy_item_to_clipboard, val=u"%s (%s: %s)" % (tweak.name, _('ID'), tweak.var_names[0]))) self.context_menu.popup(self.mapToGlobal(point)) return True def copy_item_to_clipboard(self, val): cb = QApplication.clipboard() cb.clear() cb.setText(val) def plugin_tweaks(self): raw = self.tweaks.plugin_tweaks_string d = PluginTweaks(raw, self) if d.exec_() == d.Accepted: g, l = {}, {} try: exec(unicode(d.edit.toPlainText()), g, l) except: import traceback return error_dialog( self, _('Failed'), _('There was a syntax error in your tweak. Click ' 'the show details button for details.'), show=True, det_msg=traceback.format_exc()) self.tweaks.set_plugin_tweaks(l) self.changed() def current_changed(self, current, previous): self.tweaks_view.scrollTo(current) tweak = self.tweaks.data(current, Qt.UserRole) self.help.setPlainText(tweak.doc) self.edit_tweak.setPlainText(tweak.edit_text) def changed(self, *args): self.changed_signal.emit() def initialize(self): self.tweaks = self._model = Tweaks() self.tweaks_view.setModel(self.tweaks) def restore_to_default(self, *args): idx = self.tweaks_view.currentIndex() if idx.isValid(): self.tweaks.restore_to_default(idx) tweak = self.tweaks.data(idx, Qt.UserRole) self.edit_tweak.setPlainText(tweak.edit_text) self.changed() def restore_defaults(self): ConfigWidgetBase.restore_defaults(self) self.tweaks.restore_to_defaults() self.changed() def apply_tweak(self): idx = self.tweaks_view.currentIndex() if idx.isValid(): l, g = {}, {} try: exec(unicode(self.edit_tweak.toPlainText()), g, l) except: import traceback error_dialog(self.gui, _('Failed'), _('There was a syntax error in your tweak. Click ' 'the show details button for details.'), det_msg=traceback.format_exc(), show=True) return self.tweaks.update_tweak(idx, l) self.changed() def commit(self): raw = self.tweaks.to_string() try: exec(raw) except: import traceback error_dialog( self, _('Invalid tweaks'), _('The tweaks you entered are invalid, try resetting the' ' tweaks to default and changing them one by one until' ' you find the invalid setting.'), det_msg=traceback.format_exc(), show=True) raise AbortCommit('abort') write_tweaks(raw) ConfigWidgetBase.commit(self) return True def find(self, query): if not query: return try: idx = self._model.find(query) except ParseException: self.search.search_done(False) return self.search.search_done(True) if not idx.isValid(): info_dialog(self, _('No matches'), _('Could not find any shortcuts matching %s') % query, show=True, show_copy_button=False) return self.highlight_index(idx) def highlight_index(self, idx): if not idx.isValid(): return self.view.scrollTo(idx) self.view.selectionModel().select( idx, self.view.selectionModel().ClearAndSelect) self.view.setCurrentIndex(idx) def find_next(self, *args): idx = self.view.currentIndex() if not idx.isValid(): idx = self._model.index(0) idx = self._model.find_next(idx, unicode(self.search.currentText())) self.highlight_index(idx) def find_previous(self, *args): idx = self.view.currentIndex() if not idx.isValid(): idx = self._model.index(0) idx = self._model.find_next(idx, unicode(self.search.currentText()), backwards=True) self.highlight_index(idx)
class Quickview(QDialog, Ui_Quickview): reopen_after_dock_change = pyqtSignal() tab_pressed_signal = pyqtSignal(object, object) quickview_closed = pyqtSignal() def __init__(self, gui, row): self.is_pane = gprefs.get('quickview_is_pane', False) if not self.is_pane: QDialog.__init__(self, gui, flags=Qt.Widget) else: QDialog.__init__(self, gui) Ui_Quickview.__init__(self) self.setupUi(self) self.isClosed = False self.current_book = None self.closed_by_button = False if self.is_pane: self.main_grid_layout.setContentsMargins(0, 0, 0, 0) self.books_table_column_widths = None try: self.books_table_column_widths = \ gprefs.get('quickview_dialog_books_table_widths', None) if not self.is_pane: geom = gprefs.get('quickview_dialog_geometry', None) if geom: QApplication.instance().safe_restore_geometry( self, QByteArray(geom)) except: pass if not self.is_pane: # Remove the help button from the window title bar icon = self.windowIcon() self.setWindowFlags(self.windowFlags() & (~Qt.WindowContextHelpButtonHint)) self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint) self.setWindowIcon(icon) self.view = gui.library_view self.db = self.view.model().db self.gui = gui self.is_closed = False self.current_book_id = None # the db id of the book used to fill the lh pane self.current_column = None # current logical column in books list self.current_key = None # current lookup key in books list self.last_search = None self.no_valid_items = False self.fm = self.db.field_metadata self.items.setSelectionMode(QAbstractItemView.SingleSelection) self.items.currentTextChanged.connect(self.item_selected) self.items.setProperty('highlight_current_item', 150) focus_filter = WidgetFocusFilter(self.items) focus_filter.focus_entered_signal.connect(self.focus_entered) self.items.installEventFilter(focus_filter) self.tab_pressed_signal.connect(self.tab_pressed) return_filter = BooksTableFilter(self.books_table) return_filter.return_pressed_signal.connect(self.return_pressed) self.books_table.installEventFilter(return_filter) focus_filter = WidgetFocusFilter(self.books_table) focus_filter.focus_entered_signal.connect(self.focus_entered) self.books_table.installEventFilter(focus_filter) self.close_button.clicked.connect(self.close_button_clicked) self.refresh_button.clicked.connect(self.refill) self.tab_order_widgets = [ self.items, self.books_table, self.lock_qv, self.dock_button, self.search_button, self.refresh_button, self.close_button ] for idx, widget in enumerate(self.tab_order_widgets): widget.installEventFilter( WidgetTabFilter(widget, idx, self.tab_pressed_signal)) self.books_table.setSelectionBehavior(QAbstractItemView.SelectRows) self.books_table.setSelectionMode(QAbstractItemView.SingleSelection) self.books_table.setProperty('highlight_current_item', 150) # Set up the books table columns self.add_columns_to_widget() self.books_table_header_height = self.books_table.height() self.books_table.cellDoubleClicked.connect(self.book_doubleclicked) self.books_table.currentCellChanged.connect( self.books_table_cell_changed) self.books_table.cellClicked.connect( self.books_table_set_search_string) self.books_table.cellActivated.connect( self.books_table_set_search_string) self.books_table.sortByColumn(0, Qt.AscendingOrder) # get the standard table row height. Do this here because calling # resizeRowsToContents can word wrap long cell contents, creating # double-high rows self.books_table.setRowCount(1) self.books_table.setItem(0, 0, TableItem('A', '')) self.books_table.resizeRowsToContents() self.books_table_row_height = self.books_table.rowHeight(0) self.books_table.setRowCount(0) # Add the data self.refresh(row) self.view.clicked.connect(self.slave) self.view.selectionModel().currentColumnChanged.connect( self.column_slave) QCoreApplication.instance().aboutToQuit.connect(self.save_state) self.search_button.clicked.connect(self.do_search) self.view.model().new_bookdisplay_data.connect(self.book_was_changed) self.close_button.setDefault(False) self.close_button_tooltip = _( 'The Quickview shortcut ({0}) shows/hides the Quickview panel') self.search_button_tooltip = _( 'Search in the library view for the currently highlighted selection' ) self.search_button.setToolTip(self.search_button_tooltip) if self.is_pane: self.dock_button.setText(_('Undock')) self.dock_button.setToolTip( _('Pop up the quickview panel into its own floating window')) self.dock_button.setIcon(QIcon(I('arrow-up.png'))) # Remove the ampersands from the buttons because shortcuts exist. self.lock_qv.setText(_('Lock Quickview contents')) self.search_button.setText(_('Search')) self.refresh_button.setText(_('Refresh')) self.gui.quickview_splitter.add_quickview_dialog(self) self.close_button.setVisible(False) else: self.dock_button.setToolTip( _('Embed the quickview panel into the main calibre window')) self.dock_button.setIcon(QIcon(I('arrow-down.png'))) self.set_focus() self.books_table.horizontalHeader().sectionResized.connect( self.section_resized) self.dock_button.clicked.connect(self.show_as_pane_changed) self.gui.search.cleared.connect(self.indicate_no_items) # Enable the refresh button only when QV is locked self.refresh_button.setEnabled(False) self.lock_qv.stateChanged.connect(self.lock_qv_changed) self.view_icon = QIcon(I('view.png')) self.view_plugin = self.gui.iactions['View'] self.books_table.setContextMenuPolicy(Qt.CustomContextMenu) self.books_table.customContextMenuRequested.connect( self.show_context_menu) def show_context_menu(self, point): index = self.books_table.indexAt(point) item = self.books_table.item(index.row(), 0) if item is None: return False book_id = int(item.data(Qt.UserRole)) self.context_menu = QMenu(self) self.context_menu.addAction( self.view_icon, _('View'), partial(self.view_plugin._view_calibre_books, [book_id])) self.context_menu.popup(self.books_table.mapToGlobal(point)) return True def lock_qv_changed(self, state): self.refresh_button.setEnabled(state) def add_columns_to_widget(self): ''' Get the list of columns from the preferences. Clear the current table and add the current column set ''' self.column_order = [x[0] for x in get_qv_field_list(self.fm) if x[1]] self.books_table.clear() self.books_table.setRowCount(0) self.books_table.setColumnCount(len(self.column_order)) for idx, col in enumerate(self.column_order): t = QTableWidgetItem(self.fm[col]['name']) self.books_table.setHorizontalHeaderItem(idx, t) def refill(self): ''' Refill the table in case the columns displayed changes ''' self.add_columns_to_widget() self._refresh(self.current_book_id, self.current_key) def set_search_text(self, txt): if txt: self.search_button.setEnabled(True) else: self.search_button.setEnabled(False) self.last_search = txt def set_shortcuts(self, search_sc, qv_sc): if self.is_pane: self.search_button.setToolTip(self.search_button_tooltip + ' (' + search_sc + ')') self.close_button.setToolTip( self.close_button_tooltip.format(qv_sc)) def focus_entered(self, obj): if obj == self.books_table: self.books_table_set_search_string( self.books_table.currentRow(), self.books_table.currentColumn()) elif obj.currentItem(): self.item_selected(obj.currentItem().text()) def books_table_cell_changed(self, cur_row, cur_col, prev_row, prev_col): self.books_table_set_search_string(cur_row, cur_col) def books_table_set_search_string(self, current_row, current_col): ''' Given the contents of a cell, compute a search string that will find that book and any others with identical contents in the cell. ''' current = self.books_table.item(current_row, current_col) if current is None: return book_id = current.data(Qt.UserRole) if current is None: return col = self.column_order[current.column()] if col == 'title': self.set_search_text('title:="' + current.text().replace('"', '\\"') + '"') elif col == 'authors': authors = [] for aut in [t.strip() for t in current.text().split('&')]: authors.append('authors:="' + aut.replace('"', '\\"') + '"') self.set_search_text(' and '.join(authors)) elif self.fm[col]['datatype'] == 'series': mi = self.db.get_metadata(book_id, index_is_id=True, get_user_categories=False) t = mi.get(col) if t: self.set_search_text(col + ':="' + t + '"') else: self.set_search_text(None) else: if self.fm[col]['is_multiple']: items = [(col + ':"=' + v.strip() + '"') for v in current.text().split( self.fm[col]['is_multiple']['ui_to_list'])] self.set_search_text(' and '.join(items)) else: self.set_search_text(col + ':"=' + current.text() + '"') def tab_pressed(self, in_widget, isForward): if isForward: in_widget += 1 if in_widget >= len(self.tab_order_widgets): in_widget = 0 else: in_widget -= 1 if in_widget < 0: in_widget = len(self.tab_order_widgets) - 1 self.tab_order_widgets[in_widget].setFocus(Qt.TabFocusReason) def show(self): QDialog.show(self) if self.is_pane: self.gui.quickview_splitter.show_quickview_widget() def show_as_pane_changed(self): gprefs['quickview_is_pane'] = not gprefs.get('quickview_is_pane', False) self.reopen_after_dock_change.emit() # search button def do_search(self): if self.no_valid_items: return if self.last_search is not None: self.gui.search.set_search_string(self.last_search) def book_was_changed(self, mi): ''' Called when book information is changed in the library view. Make that book info current. This means that prev and next in edit metadata will move the current book and change quickview ''' if self.is_closed or self.current_column is None: return # There is an ordering problem when libraries are changed. The library # view is changed, triggering a book_was_changed signal. Unfortunately # this happens before the library_changed actions are run, meaning we # still have the old database. To avoid the problem we just ignore the # operation if we get an exception. The "close" will come # eventually. try: self.refresh(self.view.model().index(self.db.row(mi.id), self.current_column)) except: pass # clicks on the items listWidget def item_selected(self, txt): if self.no_valid_items: return self.fill_in_books_box(unicode_type(txt)) self.set_search_text(self.current_key + ':"=' + txt.replace('"', '\\"') + '"') def refresh(self, idx): ''' Given a cell in the library view, display the information. This method converts the index into the lookup key ''' if self.lock_qv.isChecked(): return try: self.current_column = (self.view.column_map.index('authors') if self.current_column is None and self.view.column_map[idx.column()] == 'title' else idx.column()) key = self.view.column_map[self.current_column] book_id = self.view.model().id(idx.row()) if self.current_book_id == book_id and self.current_key == key: return self._refresh(book_id, key) except: traceback.print_exc() self.indicate_no_items() def _refresh(self, book_id, key): ''' Actually fill in the left-hand pane from the information in the selected column of the selected book ''' # Only show items for categories if key is None or not self.fm[key]['is_category']: if self.current_key is None: self.indicate_no_items() return key = self.current_key label_text = _('&Item: {0} ({1})') if self.is_pane: label_text = label_text.replace('&', '') self.items.blockSignals(True) self.items.clear() self.books_table.setRowCount(0) mi = self.db.get_metadata(book_id, index_is_id=True, get_user_categories=False) vals = mi.get(key, None) try: # Check if we are in the GridView and there are no values for the # selected column. In this case switch the column to 'authors' # because there isn't an easy way to switch columns in GridView # when the QV box is empty. if not vals: is_grid_view = ( self.gui.current_view().alternate_views.current_view != self.gui.current_view().alternate_views.main_view) if is_grid_view: key = 'authors' vals = mi.get(key, None) except: traceback.print_exc() self.current_book_id = book_id self.current_key = key self.items_label.setText(label_text.format(self.fm[key]['name'], key)) if vals: self.no_valid_items = False if self.fm[key]['datatype'] == 'rating': if self.fm[key]['display'].get('allow_half_stars', False): vals = unicode_type(vals / 2.0) else: vals = unicode_type(vals // 2) if not isinstance(vals, list): vals = [vals] vals.sort(key=sort_key) for v in vals: a = QListWidgetItem(v) self.items.addItem(a) self.items.setCurrentRow(0) self.fill_in_books_box(vals[0]) else: self.indicate_no_items() self.items.blockSignals(False) def indicate_no_items(self): self.no_valid_items = True self.items.clear() self.add_columns_to_widget() self.items.addItem(QListWidgetItem(_('**No items found**'))) self.books_label.setText( _('Click in a column in the library view ' 'to see the information for that book')) def fill_in_books_box(self, selected_item): ''' Given the selected row in the left-hand box, fill in the grid with the books that contain that data. ''' # Do a bit of fix-up on the items so that the search works. if selected_item.startswith('.'): sv = '.' + selected_item else: sv = selected_item sv = self.current_key + ':"=' + sv.replace('"', r'\"') + '"' if gprefs['qv_respects_vls']: books = self.db.search(sv, return_matches=True, sort_results=False) else: books = self.db.new_api.search(sv) self.books_table.setRowCount(len(books)) label_text = _('&Books with selected item "{0}": {1}') if self.is_pane: label_text = label_text.replace('&', '') self.books_label.setText(label_text.format(selected_item, len(books))) select_item = None self.books_table.setSortingEnabled(False) self.books_table.blockSignals(True) tt = ('<p>' + _( 'Double click on a book to change the selection in the library view or ' 'change the column shown in the left-hand pane. ' 'Shift- or Control- double click to edit the metadata of a book, ' 'which also changes the selected book.') + '</p>') for row, b in enumerate(books): mi = self.db.new_api.get_proxy_metadata(b) for col in self.column_order: try: if col == 'title': a = TableItem(mi.title, mi.title_sort) if b == self.current_book_id: select_item = a elif col == 'authors': a = TableItem(' & '.join(mi.authors), mi.author_sort) elif col == 'series': series = mi.format_field('series')[1] if series is None: a = TableItem('', '', 0) else: a = TableItem(series, mi.series, mi.series_index) elif col == 'size': v = mi.get('book_size') if v is not None: a = TableItem('{:n}'.format(v), v) a.setTextAlignment(Qt.AlignRight) else: a = TableItem(' ', None) elif self.fm[col]['datatype'] == 'series': v = mi.format_field(col)[1] a = TableItem(v, mi.get(col), mi.get(col + '_index')) elif self.fm[col]['datatype'] == 'datetime': v = mi.format_field(col)[1] d = mi.get(col) if d is None: d = UNDEFINED_DATE a = TableItem(v, timestampfromdt(d)) elif self.fm[col]['datatype'] in ('float', 'int'): v = mi.format_field(col)[1] sv = mi.get(col) a = TableItem(v, sv) else: v = mi.format_field(col)[1] a = TableItem(v, v) except: traceback.print_exc() a = TableItem( _('Something went wrong while filling in the table'), '') a.setData(Qt.UserRole, b) a.setToolTip(tt) self.books_table.setItem(row, self.key_to_table_widget_column(col), a) self.books_table.setRowHeight(row, self.books_table_row_height) self.books_table.blockSignals(False) self.books_table.setSortingEnabled(True) if select_item is not None: self.books_table.setCurrentItem(select_item) self.books_table.scrollToItem(select_item, QAbstractItemView.PositionAtCenter) self.set_search_text(sv) # Deal with sizing the table columns. Done here because the numbers are not # correct until the first paint. def resizeEvent(self, *args): QDialog.resizeEvent(self, *args) # Do this if we are resizing for the first time to reset state. if self.is_pane and self.height() == 0: self.gui.quickview_splitter.set_sizes() if self.books_table_column_widths is not None: for c, w in enumerate(self.books_table_column_widths): self.books_table.setColumnWidth(c, w) else: # the vertical scroll bar might not be rendered, so might not yet # have a width. Assume 25. Not a problem because user-changed column # widths will be remembered w = self.books_table.width( ) - 25 - self.books_table.verticalHeader().width() w //= self.books_table.columnCount() for c in range(0, self.books_table.columnCount()): self.books_table.setColumnWidth(c, w) self.save_state() def key_to_table_widget_column(self, key): return self.column_order.index(key) def return_pressed(self): row = self.books_table.currentRow() if gprefs['qv_retkey_changes_column']: self.select_book(row, self.books_table.currentColumn()) else: self.select_book(row, self.key_to_table_widget_column(self.current_key)) def book_doubleclicked(self, row, column): if self.no_valid_items: return try: if gprefs['qv_dclick_changes_column']: self.select_book(row, column) else: self.select_book( row, self.key_to_table_widget_column(self.current_key)) except: from calibre.gui2 import error_dialog error_dialog( self, _('Quickview: Book not in library view'), _('The book you selected is not currently displayed in ' 'the library view, perhaps because of a search, so ' 'Quickview cannot select it.'), show=True, show_copy_button=False) def select_book(self, row, column): ''' row and column both refer the qv table. In particular, column is not the logical column in the book list. ''' item = self.books_table.item(row, column) if item is None: return book_id = int(self.books_table.item(row, column).data(Qt.UserRole)) key = self.column_order[column] modifiers = int(QApplication.keyboardModifiers()) if modifiers in (Qt.CTRL, Qt.SHIFT): self.view.select_rows([book_id]) em = find_plugin('Edit Metadata') if em and em.actual_plugin_: em.actual_plugin_.edit_metadata(None) else: self.view.select_cell(self.db.data.id_to_index(book_id), self.view.column_map.index(key)) def set_focus(self): self.activateWindow() self.books_table.setFocus() def column_slave(self, current): ''' called when the column is changed on the booklist ''' if gprefs['qv_follows_column']: self.slave(current) def slave(self, current): ''' called when a book is clicked on the library view ''' if self.is_closed: return self.refresh(current) self.view.activateWindow() def section_resized(self, logicalIndex, oldSize, newSize): self.save_state() def save_state(self): if self.is_closed: return self.books_table_column_widths = [] for c in range(0, self.books_table.columnCount()): self.books_table_column_widths.append( self.books_table.columnWidth(c)) gprefs[ 'quickview_dialog_books_table_widths'] = self.books_table_column_widths if not self.is_pane: gprefs['quickview_dialog_geometry'] = bytearray( self.saveGeometry()) def _close(self): self.save_state() # clean up to prevent memory leaks self.db = self.view = self.gui = None self.is_closed = True def close_button_clicked(self): self.closed_by_button = True self.quickview_closed.emit() def reject(self): if not self.closed_by_button: self.close_button_clicked() else: self._reject() def _reject(self): if self.is_pane: self.gui.quickview_splitter.hide_quickview_widget() self.gui.library_view.setFocus(Qt.ActiveWindowFocusReason) self._close() QDialog.reject(self)
class BooksView(QTableView): # {{{ files_dropped = pyqtSignal(object) add_column_signal = pyqtSignal() is_library_view = True def viewportEvent(self, event): if (event.type() == event.ToolTip and not gprefs['book_list_tooltips']): return False return QTableView.viewportEvent(self, event) def __init__(self, parent, modelcls=BooksModel, use_edit_metadata_dialog=True): QTableView.__init__(self, parent) self.default_row_height = self.verticalHeader().defaultSectionSize() self.gui = parent self.setProperty('highlight_current_item', 150) self.row_sizing_done = False self.alternate_views = AlternateViews(self) if not tweaks['horizontal_scrolling_per_column']: self.setHorizontalScrollMode(self.ScrollPerPixel) self.setEditTriggers(self.EditKeyPressed) if tweaks['doubleclick_on_library_view'] == 'edit_cell': self.setEditTriggers(self.DoubleClicked|self.editTriggers()) elif tweaks['doubleclick_on_library_view'] == 'open_viewer': self.setEditTriggers(self.SelectedClicked|self.editTriggers()) self.doubleClicked.connect(parent.iactions['View'].view_triggered) elif tweaks['doubleclick_on_library_view'] == 'edit_metadata': # Must not enable single-click to edit, or the field will remain # open in edit mode underneath the edit metadata dialog if use_edit_metadata_dialog: self.doubleClicked.connect( partial(parent.iactions['Edit Metadata'].edit_metadata, checked=False)) else: self.setEditTriggers(self.DoubleClicked|self.editTriggers()) setup_dnd_interface(self) self.setAlternatingRowColors(True) self.setShowGrid(False) self.setWordWrap(False) self.rating_delegate = RatingDelegate(self) self.timestamp_delegate = DateDelegate(self) self.pubdate_delegate = PubDateDelegate(self) self.last_modified_delegate = DateDelegate(self, tweak_name='gui_last_modified_display_format') self.languages_delegate = LanguagesDelegate(self) self.tags_delegate = CompleteDelegate(self, ',', 'all_tag_names') self.authors_delegate = CompleteDelegate(self, '&', 'all_author_names', True) self.cc_names_delegate = CompleteDelegate(self, '&', 'all_custom', True) self.series_delegate = TextDelegate(self) self.publisher_delegate = TextDelegate(self) self.text_delegate = TextDelegate(self) self.cc_text_delegate = CcTextDelegate(self) self.cc_enum_delegate = CcEnumDelegate(self) self.cc_bool_delegate = CcBoolDelegate(self) self.cc_comments_delegate = CcCommentsDelegate(self) self.cc_template_delegate = CcTemplateDelegate(self) self.cc_number_delegate = CcNumberDelegate(self) self.display_parent = parent self._model = modelcls(self) self.setModel(self._model) self._model.count_changed_signal.connect(self.do_row_sizing, type=Qt.QueuedConnection) self.setSelectionBehavior(QAbstractItemView.SelectRows) self.setSortingEnabled(True) self.selectionModel().currentRowChanged.connect(self._model.current_changed) self.preserve_state = partial(PreserveViewState, self) self.marked_changed_listener = FunctionDispatcher(self.marked_changed) # {{{ Column Header setup self.can_add_columns = True self.was_restored = False self.column_header = HeaderView(Qt.Horizontal, self) self.setHorizontalHeader(self.column_header) self.column_header.sortIndicatorChanged.disconnect() self.column_header.sortIndicatorChanged.connect(self.user_sort_requested) self.column_header.setSectionsMovable(True) self.column_header.setSectionsClickable(True) self.column_header.sectionMoved.connect(self.save_state) self.column_header.setContextMenuPolicy(Qt.CustomContextMenu) self.column_header.customContextMenuRequested.connect(self.show_column_header_context_menu) self.column_header.sectionResized.connect(self.column_resized, Qt.QueuedConnection) self.row_header = HeaderView(Qt.Vertical, self) self.row_header.setSectionResizeMode(self.row_header.Fixed) self.setVerticalHeader(self.row_header) # }}} self._model.database_changed.connect(self.database_changed) hv = self.verticalHeader() hv.setSectionsClickable(True) hv.setCursor(Qt.PointingHandCursor) self.selected_ids = [] self._model.about_to_be_sorted.connect(self.about_to_be_sorted) self._model.sorting_done.connect(self.sorting_done, type=Qt.QueuedConnection) # Column Header Context Menu {{{ def column_header_context_handler(self, action=None, column=None): if not action or not column: return try: idx = self.column_map.index(column) except: return h = self.column_header if action == 'hide': if h.hiddenSectionCount() >= h.count(): return error_dialog(self, _('Cannot hide all columns'), _( 'You must not hide all columns'), show=True) h.setSectionHidden(idx, True) elif action == 'show': h.setSectionHidden(idx, False) if h.sectionSize(idx) < 3: sz = h.sectionSizeHint(idx) h.resizeSection(idx, sz) elif action == 'ascending': self.sort_by_column_and_order(idx, True) elif action == 'descending': self.sort_by_column_and_order(idx, False) elif action == 'defaults': self.apply_state(self.get_default_state()) elif action == 'addcustcol': self.add_column_signal.emit() elif action.startswith('align_'): alignment = action.partition('_')[-1] self._model.change_alignment(column, alignment) elif action == 'quickview': from calibre.customize.ui import find_plugin qv = find_plugin('Show Quickview') if qv: rows = self.selectionModel().selectedRows() if len(rows) > 0: current_row = rows[0].row() current_col = self.column_map.index(column) index = self.model().index(current_row, current_col) qv.actual_plugin_.change_quickview_column(index) self.save_state() def show_column_header_context_menu(self, pos): idx = self.column_header.logicalIndexAt(pos) if idx > -1 and idx < len(self.column_map): col = self.column_map[idx] name = unicode(self.model().headerData(idx, Qt.Horizontal, Qt.DisplayRole) or '') self.column_header_context_menu = QMenu(self) if col != 'ondevice': self.column_header_context_menu.addAction(_('Hide column %s') % name, partial(self.column_header_context_handler, action='hide', column=col)) m = self.column_header_context_menu.addMenu( _('Sort on %s') % name) a = m.addAction(_('Ascending'), partial(self.column_header_context_handler, action='ascending', column=col)) d = m.addAction(_('Descending'), partial(self.column_header_context_handler, action='descending', column=col)) if self._model.sorted_on[0] == col: ac = a if self._model.sorted_on[1] else d ac.setCheckable(True) ac.setChecked(True) if col not in ('ondevice', 'inlibrary') and \ (not self.model().is_custom_column(col) or self.model().custom_columns[col]['datatype'] not in ('bool', )): m = self.column_header_context_menu.addMenu( _('Change text alignment for %s') % name) al = self._model.alignment_map.get(col, 'left') for x, t in (('left', _('Left')), ('right', _('Right')), ('center', _('Center'))): a = m.addAction(t, partial(self.column_header_context_handler, action='align_'+x, column=col)) if al == x: a.setCheckable(True) a.setChecked(True) if not isinstance(self, DeviceBooksView): if self._model.db.field_metadata[col]['is_category']: act = self.column_header_context_menu.addAction(_('Quickview column %s') % name, partial(self.column_header_context_handler, action='quickview', column=col)) rows = self.selectionModel().selectedRows() if len(rows) > 1: act.setEnabled(False) hidden_cols = [self.column_map[i] for i in range(self.column_header.count()) if self.column_header.isSectionHidden(i)] try: hidden_cols.remove('ondevice') except: pass if hidden_cols: self.column_header_context_menu.addSeparator() m = self.column_header_context_menu.addMenu(_('Show column')) for col in hidden_cols: hidx = self.column_map.index(col) name = unicode(self.model().headerData(hidx, Qt.Horizontal, Qt.DisplayRole) or '') m.addAction(name, partial(self.column_header_context_handler, action='show', column=col)) self.column_header_context_menu.addSeparator() self.column_header_context_menu.addAction( _('Shrink column if it is too wide to fit'), partial(self.resize_column_to_fit, column=self.column_map[idx])) self.column_header_context_menu.addAction( _('Restore default layout'), partial(self.column_header_context_handler, action='defaults', column=col)) if self.can_add_columns: self.column_header_context_menu.addAction( QIcon(I('column.png')), _('Add your own columns'), partial(self.column_header_context_handler, action='addcustcol', column=col)) self.column_header_context_menu.popup(self.column_header.mapToGlobal(pos)) # }}} # Sorting {{{ def sort_by_column_and_order(self, col, ascending): self.column_header.blockSignals(True) self.sortByColumn(col, Qt.AscendingOrder if ascending else Qt.DescendingOrder) self.column_header.blockSignals(False) def user_sort_requested(self, col, order=Qt.AscendingOrder): if col >= len(self.column_map) or col < 0: return QTableView.sortByColumn(self, col) field = self.column_map[col] self.intelligent_sort(field, order == Qt.AscendingOrder) def intelligent_sort(self, field, ascending): m = self.model() pname = 'previous_sort_order_' + self.__class__.__name__ previous = gprefs.get(pname, {}) if field == m.sorted_on[0] or field not in previous: self.sort_by_named_field(field, ascending) previous[field] = ascending gprefs[pname] = previous return previous[m.sorted_on[0]] = m.sorted_on[1] gprefs[pname] = previous self.sort_by_named_field(field, previous[field]) def about_to_be_sorted(self, idc): selected_rows = [r.row() for r in self.selectionModel().selectedRows()] self.selected_ids = [idc(r) for r in selected_rows] def sorting_done(self, indexc): pos = self.horizontalScrollBar().value() self.select_rows(self.selected_ids, using_ids=True, change_current=True, scroll=True) self.selected_ids = [] self.horizontalScrollBar().setValue(pos) def sort_by_named_field(self, field, order, reset=True): if field in self.column_map: idx = self.column_map.index(field) self.sort_by_column_and_order(idx, order) else: self._model.sort_by_named_field(field, order, reset) self.column_header.blockSignals(True) self.column_header.setSortIndicator(-1, Qt.AscendingOrder) self.column_header.blockSignals(False) def multisort(self, fields, reset=True, only_if_different=False): if len(fields) == 0: return sh = self.cleanup_sort_history(self._model.sort_history, ignore_column_map=True) if only_if_different and len(sh) >= len(fields): ret=True for i,t in enumerate(fields): if t[0] != sh[i][0]: ret = False break if ret: return for n,d in reversed(fields): if n in self._model.db.field_metadata.keys(): sh.insert(0, (n, d)) sh = self.cleanup_sort_history(sh, ignore_column_map=True) self._model.sort_history = [tuple(x) for x in sh] self._model.resort(reset=reset) col = fields[0][0] dir = Qt.AscendingOrder if fields[0][1] else Qt.DescendingOrder if col in self.column_map: col = self.column_map.index(col) self.column_header.blockSignals(True) try: self.column_header.setSortIndicator(col, dir) finally: self.column_header.blockSignals(False) # }}} # Ondevice column {{{ def set_ondevice_column_visibility(self): col, h = self._model.column_map.index('ondevice'), self.column_header h.setSectionHidden(col, not self._model.device_connected) def set_device_connected(self, is_connected): self._model.set_device_connected(is_connected) self.set_ondevice_column_visibility() # }}} # Save/Restore State {{{ def get_state(self): h = self.column_header cm = self.column_map state = {} state['hidden_columns'] = [cm[i] for i in range(h.count()) if h.isSectionHidden(i) and cm[i] != 'ondevice'] state['last_modified_injected'] = True state['languages_injected'] = True state['sort_history'] = \ self.cleanup_sort_history(self.model().sort_history, ignore_column_map=self.is_library_view) state['column_positions'] = {} state['column_sizes'] = {} state['column_alignment'] = self._model.alignment_map for i in range(h.count()): name = cm[i] state['column_positions'][name] = h.visualIndex(i) if name != 'ondevice': state['column_sizes'][name] = h.sectionSize(i) return state def write_state(self, state): db = getattr(self.model(), 'db', None) name = unicode(self.objectName()) if name and db is not None: db.new_api.set_pref(name + ' books view state', state) def save_state(self): # Only save if we have been initialized (set_database called) if len(self.column_map) > 0 and self.was_restored: state = self.get_state() self.write_state(state) def cleanup_sort_history(self, sort_history, ignore_column_map=False): history = [] for col, order in sort_history: if not isinstance(order, bool): continue col = {'date':'timestamp', 'sort':'title'}.get(col, col) if ignore_column_map or col in self.column_map: if (not history or history[-1][0] != col): history.append([col, order]) return history def apply_sort_history(self, saved_history, max_sort_levels=3): if not saved_history: return if self.is_library_view: for col, order in reversed(self.cleanup_sort_history( saved_history, ignore_column_map=True)[:max_sort_levels]): self.sort_by_named_field(col, order) else: for col, order in reversed(self.cleanup_sort_history( saved_history)[:max_sort_levels]): self.sort_by_column_and_order(self.column_map.index(col), order) def apply_state(self, state, max_sort_levels=3): h = self.column_header cmap = {} hidden = state.get('hidden_columns', []) for i, c in enumerate(self.column_map): cmap[c] = i if c != 'ondevice': h.setSectionHidden(i, c in hidden) positions = state.get('column_positions', {}) pmap = {} for col, pos in positions.items(): if col in cmap: pmap[pos] = col for pos in sorted(pmap.keys()): col = pmap[pos] idx = cmap[col] current_pos = h.visualIndex(idx) if current_pos != pos: h.moveSection(current_pos, pos) # Because of a bug in Qt 5 we have to ensure that the header is actually # relaid out by changing this value, without this sometimes ghost # columns remain visible when changing libraries for i in xrange(h.count()): val = h.isSectionHidden(i) h.setSectionHidden(i, not val) h.setSectionHidden(i, val) sizes = state.get('column_sizes', {}) for col, size in sizes.items(): if col in cmap: sz = sizes[col] if sz < 3: sz = h.sectionSizeHint(cmap[col]) h.resizeSection(cmap[col], sz) self.apply_sort_history(state.get('sort_history', None), max_sort_levels=max_sort_levels) for col, alignment in state.get('column_alignment', {}).items(): self._model.change_alignment(col, alignment) for i in range(h.count()): if not h.isSectionHidden(i) and h.sectionSize(i) < 3: sz = h.sectionSizeHint(i) h.resizeSection(i, sz) def get_default_state(self): old_state = { 'hidden_columns': ['last_modified', 'languages'], 'sort_history':[DEFAULT_SORT], 'column_positions': {}, 'column_sizes': {}, 'column_alignment': { 'size':'center', 'timestamp':'center', 'pubdate':'center'}, 'last_modified_injected': True, 'languages_injected': True, } h = self.column_header cm = self.column_map for i in range(h.count()): name = cm[i] old_state['column_positions'][name] = i if name != 'ondevice': old_state['column_sizes'][name] = \ min(350, max(self.sizeHintForColumn(i), h.sectionSizeHint(i))) if name in ('timestamp', 'last_modified'): old_state['column_sizes'][name] += 12 return old_state def get_old_state(self): ans = None name = unicode(self.objectName()) if name: name += ' books view state' db = getattr(self.model(), 'db', None) if db is not None: ans = db.prefs.get(name, None) if ans is None: ans = gprefs.get(name, None) try: del gprefs[name] except: pass if ans is not None: db.new_api.set_pref(name, ans) else: injected = False if not ans.get('last_modified_injected', False): injected = True ans['last_modified_injected'] = True hc = ans.get('hidden_columns', []) if 'last_modified' not in hc: hc.append('last_modified') if not ans.get('languages_injected', False): injected = True ans['languages_injected'] = True hc = ans.get('hidden_columns', []) if 'languages' not in hc: hc.append('languages') if injected: db.new_api.set_pref(name, ans) return ans def restore_state(self): old_state = self.get_old_state() if old_state is None: old_state = self.get_default_state() max_levels = 3 if tweaks['sort_columns_at_startup'] is not None: sh = [] try: for c,d in tweaks['sort_columns_at_startup']: if not isinstance(d, bool): d = True if d == 0 else False sh.append((c, d)) except: # Ignore invalid tweak values as users seem to often get them # wrong print('Ignoring invalid sort_columns_at_startup tweak, with error:') import traceback traceback.print_exc() old_state['sort_history'] = sh max_levels = max(3, len(sh)) self.column_header.blockSignals(True) self.apply_state(old_state, max_sort_levels=max_levels) self.column_header.blockSignals(False) self.do_row_sizing() self.was_restored = True def refresh_row_sizing(self): self.row_sizing_done = False self.do_row_sizing() def do_row_sizing(self): # Resize all rows to have the correct height if not self.row_sizing_done and self.model().rowCount(QModelIndex()) > 0: vh = self.verticalHeader() vh.setDefaultSectionSize(max(vh.minimumSectionSize(), self.default_row_height + gprefs['book_list_extra_row_spacing'])) self._model.set_row_height(self.rowHeight(0)) self.row_sizing_done = True def resize_column_to_fit(self, column): col = self.column_map.index(column) self.column_resized(col, self.columnWidth(col), self.columnWidth(col)) def column_resized(self, col, old_size, new_size): # arbitrary: scroll bar + header + some max_width = self.width() - (self.verticalScrollBar().width() + self.verticalHeader().width() + 10) if max_width < 200: max_width = 200 if new_size > max_width: self.column_header.blockSignals(True) self.setColumnWidth(col, max_width) self.column_header.blockSignals(False) # }}} # Initialization/Delegate Setup {{{ def set_database(self, db): self.alternate_views.set_database(db) self.save_state() self._model.set_database(db) self.tags_delegate.set_database(db) self.cc_names_delegate.set_database(db) self.authors_delegate.set_database(db) self.series_delegate.set_auto_complete_function(db.all_series) self.publisher_delegate.set_auto_complete_function(db.all_publishers) self.alternate_views.set_database(db, stage=1) def marked_changed(self, old_marked, current_marked): self.alternate_views.marked_changed(old_marked, current_marked) if bool(old_marked) == bool(current_marked): changed = old_marked | current_marked i = self.model().db.data.id_to_index def f(x): try: return i(x) except ValueError: pass sections = tuple(x for x in map(f, changed) if x is not None) if sections: self.row_header.headerDataChanged(Qt.Vertical, min(sections), max(sections)) # This is needed otherwise Qt does not always update the # viewport correctly. See https://bugs.launchpad.net/bugs/1404697 self.row_header.viewport().update() else: # Marked items have either appeared or all been removed self.model().set_row_decoration(current_marked) self.row_header.headerDataChanged(Qt.Vertical, 0, self.row_header.count()-1) self.row_header.geometriesChanged.emit() def database_changed(self, db): db.data.add_marked_listener(self.marked_changed_listener) for i in range(self.model().columnCount(None)): if self.itemDelegateForColumn(i) in (self.rating_delegate, self.timestamp_delegate, self.pubdate_delegate, self.last_modified_delegate, self.languages_delegate): self.setItemDelegateForColumn(i, self.itemDelegate()) cm = self.column_map for colhead in cm: if self._model.is_custom_column(colhead): cc = self._model.custom_columns[colhead] if cc['datatype'] == 'datetime': delegate = CcDateDelegate(self) delegate.set_format(cc['display'].get('date_format','')) self.setItemDelegateForColumn(cm.index(colhead), delegate) elif cc['datatype'] == 'comments': self.setItemDelegateForColumn(cm.index(colhead), self.cc_comments_delegate) elif cc['datatype'] == 'text': if cc['is_multiple']: if cc['display'].get('is_names', False): self.setItemDelegateForColumn(cm.index(colhead), self.cc_names_delegate) else: self.setItemDelegateForColumn(cm.index(colhead), self.tags_delegate) else: self.setItemDelegateForColumn(cm.index(colhead), self.cc_text_delegate) elif cc['datatype'] == 'series': self.setItemDelegateForColumn(cm.index(colhead), self.cc_text_delegate) elif cc['datatype'] in ('int', 'float'): self.setItemDelegateForColumn(cm.index(colhead), self.cc_number_delegate) elif cc['datatype'] == 'bool': self.setItemDelegateForColumn(cm.index(colhead), self.cc_bool_delegate) elif cc['datatype'] == 'rating': self.setItemDelegateForColumn(cm.index(colhead), self.rating_delegate) elif cc['datatype'] == 'composite': self.setItemDelegateForColumn(cm.index(colhead), self.cc_template_delegate) elif cc['datatype'] == 'enumeration': self.setItemDelegateForColumn(cm.index(colhead), self.cc_enum_delegate) else: dattr = colhead+'_delegate' delegate = colhead if hasattr(self, dattr) else 'text' self.setItemDelegateForColumn(cm.index(colhead), getattr(self, delegate+'_delegate')) self.restore_state() self.set_ondevice_column_visibility() # incase there were marked books self.model().set_row_decoration(set()) self.row_header.headerDataChanged(Qt.Vertical, 0, self.row_header.count()-1) self.row_header.geometriesChanged.emit() # }}} # Context Menu {{{ def set_context_menu(self, menu, edit_collections_action): self.setContextMenuPolicy(Qt.DefaultContextMenu) self.context_menu = menu self.alternate_views.set_context_menu(menu) self.edit_collections_action = edit_collections_action def contextMenuEvent(self, event): sac = self.gui.iactions['Sort By'] sort_added = tuple(ac for ac in self.context_menu.actions() if ac is sac.qaction) if sort_added: sac.update_menu() self.context_menu.popup(event.globalPos()) event.accept() # }}} @property def column_map(self): return self._model.column_map @property def visible_columns(self): h = self.horizontalHeader() logical_indices = (x for x in xrange(h.count()) if not h.isSectionHidden(x)) rmap = {i:x for i, x in enumerate(self.column_map)} return (rmap[h.visualIndex(x)] for x in logical_indices if h.visualIndex(x) > -1) def refresh_book_details(self): idx = self.currentIndex() if idx.isValid(): self._model.current_changed(idx, idx) return True return False def scrollContentsBy(self, dx, dy): # Needed as Qt bug causes headerview to not always update when scrolling QTableView.scrollContentsBy(self, dx, dy) if dy != 0: self.column_header.update() def scroll_to_row(self, row): if row > -1: h = self.horizontalHeader() for i in range(h.count()): if not h.isSectionHidden(i) and h.sectionViewportPosition(i) >= 0: self.scrollTo(self.model().index(row, i), self.PositionAtCenter) break @property def current_book(self): ci = self.currentIndex() if ci.isValid(): try: return self.model().db.data.index_to_id(ci.row()) except (IndexError, ValueError, KeyError, TypeError, AttributeError): pass def current_book_state(self): return self.current_book, self.horizontalScrollBar().value() def restore_current_book_state(self, state): book_id, hpos = state try: row = self.model().db.data.id_to_index(book_id) except (IndexError, ValueError, KeyError, TypeError, AttributeError): return self.set_current_row(row) self.scroll_to_row(row) self.horizontalScrollBar().setValue(hpos) def set_current_row(self, row=0, select=True, for_sync=False): if row > -1 and row < self.model().rowCount(QModelIndex()): h = self.horizontalHeader() logical_indices = list(range(h.count())) logical_indices = [x for x in logical_indices if not h.isSectionHidden(x)] pairs = [(x, h.visualIndex(x)) for x in logical_indices if h.visualIndex(x) > -1] if not pairs: pairs = [(0, 0)] pairs.sort(cmp=lambda x,y:cmp(x[1], y[1])) i = pairs[0][0] index = self.model().index(row, i) if for_sync: sm = self.selectionModel() sm.setCurrentIndex(index, sm.NoUpdate) else: self.setCurrentIndex(index) if select: sm = self.selectionModel() sm.select(index, sm.ClearAndSelect|sm.Rows) def row_at_top(self): pos = 0 while pos < 100: ans = self.rowAt(pos) if ans > -1: return ans pos += 5 def row_at_bottom(self): pos = self.viewport().height() limit = pos - 100 while pos > limit: ans = self.rowAt(pos) if ans > -1: return ans pos -= 5 def moveCursor(self, action, modifiers): orig = self.currentIndex() index = QTableView.moveCursor(self, action, modifiers) if action == QTableView.MovePageDown: moved = index.row() - orig.row() try: rows = self.row_at_bottom() - self.row_at_top() except TypeError: rows = moved if moved > rows: index = self.model().index(orig.row() + rows, index.column()) elif action == QTableView.MovePageUp: moved = orig.row() - index.row() try: rows = self.row_at_bottom() - self.row_at_top() except TypeError: rows = moved if moved > rows: index = self.model().index(orig.row() - rows, index.column()) elif action == QTableView.MoveHome and modifiers & Qt.ControlModifier: return self.model().index(0, orig.column()) elif action == QTableView.MoveEnd and modifiers & Qt.ControlModifier: return self.model().index(self.model().rowCount(QModelIndex()) - 1, orig.column()) return index def selectionCommand(self, index, event): if event and event.type() == event.KeyPress and event.key() in (Qt.Key_Home, Qt.Key_End) and event.modifiers() & Qt.CTRL: return QItemSelectionModel.ClearAndSelect | QItemSelectionModel.Rows return super(BooksView, self).selectionCommand(index, event) def ids_to_rows(self, ids): row_map = OrderedDict() ids = frozenset(ids) m = self.model() for row in xrange(m.rowCount(QModelIndex())): if len(row_map) >= len(ids): break c = m.id(row) if c in ids: row_map[c] = row return row_map def select_rows(self, identifiers, using_ids=True, change_current=True, scroll=True): ''' Select rows identified by identifiers. identifiers can be a set of ids, row numbers or QModelIndexes. ''' rows = set([x.row() if hasattr(x, 'row') else x for x in identifiers]) if using_ids: rows = set([]) identifiers = set(identifiers) m = self.model() for row in xrange(m.rowCount(QModelIndex())): if m.id(row) in identifiers: rows.add(row) rows = list(sorted(rows)) if rows: row = rows[0] if change_current: self.set_current_row(row, select=False) if scroll: self.scroll_to_row(row) sm = self.selectionModel() sel = QItemSelection() m = self.model() max_col = m.columnCount(QModelIndex()) - 1 # Create a range based selector for each set of contiguous rows # as supplying selectors for each individual row causes very poor # performance if a large number of rows has to be selected. for k, g in itertools.groupby(enumerate(rows), lambda (i,x):i-x): group = list(map(operator.itemgetter(1), g)) sel.merge(QItemSelection(m.index(min(group), 0), m.index(max(group), max_col)), sm.Select) sm.select(sel, sm.ClearAndSelect)
def show_context_menu(self, point): item = self.itemAt(point) if item is None or item in set(self.categories.itervalues()): return m = QMenu(self) sel = self.selectedItems() num = len(sel) container = current_container() ci = self.currentItem() if ci is not None: cn = unicode(ci.data(0, NAME_ROLE) or "") mt = unicode(ci.data(0, MIME_ROLE) or "") cat = unicode(ci.data(0, CATEGORY_ROLE) or "") n = elided_text(cn.rpartition("/")[-1]) m.addAction(QIcon(I("save.png")), _("Export %s") % n, partial(self.export, cn)) if ( cn not in container.names_that_must_not_be_changed and cn not in container.names_that_must_not_be_removed and mt not in OEB_FONTS ): m.addAction(_("Replace %s with file...") % n, partial(self.replace, cn)) m.addSeparator() m.addAction(QIcon(I("modified.png")), _("&Rename %s") % n, self.edit_current_item) if is_raster_image(mt): m.addAction( QIcon(I("default_cover.png")), _("Mark %s as cover image") % n, partial(self.mark_as_cover, cn) ) elif current_container().SUPPORTS_TITLEPAGES and mt in OEB_DOCS and cat == "text": m.addAction( QIcon(I("default_cover.png")), _("Mark %s as cover page") % n, partial(self.mark_as_titlepage, cn) ) m.addSeparator() if num > 0: m.addSeparator() if num > 1: m.addAction(QIcon(I("modified.png")), _("&Bulk rename selected files"), self.request_bulk_rename) m.addAction(QIcon(I("trash.png")), _("&Delete the %d selected file(s)") % num, self.request_delete) m.addSeparator() selected_map = defaultdict(list) for item in sel: selected_map[unicode(item.data(0, CATEGORY_ROLE) or "")].append(unicode(item.data(0, NAME_ROLE) or "")) for items in selected_map.itervalues(): items.sort(key=self.index_of_name) if selected_map["text"]: m.addAction( QIcon(I("format-text-color.png")), _("Link &stylesheets..."), partial(self.link_stylesheets, selected_map["text"]), ) if len(selected_map["text"]) > 1: m.addAction( QIcon(I("merge.png")), _("&Merge selected text files"), partial(self.start_merge, "text", selected_map["text"]), ) if len(selected_map["styles"]) > 1: m.addAction( QIcon(I("merge.png")), _("&Merge selected style files"), partial(self.start_merge, "styles", selected_map["styles"]), ) if len(list(m.actions())) > 0: m.popup(self.mapToGlobal(point))
class ConfigWidget(ConfigWidgetBase): def setupUi(self, x): self.l = l = QVBoxLayout(self) self.la1 = la = QLabel( _("Values for the tweaks are shown below. Edit them to change the behavior of calibre." " Your changes will only take effect <b>after a restart</b> of calibre.")) l.addWidget(la), la.setWordWrap(True) self.splitter = s = QSplitter(self) s.setChildrenCollapsible(False) l.addWidget(s, 10) self.lv = lv = QWidget(self) lv.l = l2 = QVBoxLayout(lv) l2.setContentsMargins(0, 0, 0, 0) self.tweaks_view = tv = TweaksView(self) l2.addWidget(tv) self.plugin_tweaks_button = b = QPushButton(self) b.setToolTip(_("Edit tweaks for any custom plugins you have installed")) b.setText(_("&Plugin tweaks")) l2.addWidget(b) s.addWidget(lv) self.lv1 = lv = QWidget(self) s.addWidget(lv) lv.g = g = QGridLayout(lv) g.setContentsMargins(0, 0, 0, 0) self.search = sb = SearchBox2(self) sb.sizePolicy().setHorizontalStretch(10) sb.setSizeAdjustPolicy(sb.AdjustToMinimumContentsLength) sb.setMinimumContentsLength(10) g.addWidget(self.search, 0, 0, 1, 1) self.next_button = b = QPushButton(self) b.setIcon(QIcon(I("arrow-down.png"))) b.setText(_("&Next")) g.addWidget(self.next_button, 0, 1, 1, 1) self.previous_button = b = QPushButton(self) b.setIcon(QIcon(I("arrow-up.png"))) b.setText(_("&Previous")) g.addWidget(self.previous_button, 0, 2, 1, 1) self.hb = hb = QGroupBox(self) hb.setTitle(_("Help")) hb.l = l2 = QVBoxLayout(hb) self.help = h = QPlainTextEdit(self) l2.addWidget(h) h.setLineWrapMode(QPlainTextEdit.NoWrap) h.setReadOnly(True) g.addWidget(hb, 1, 0, 1, 3) self.eb = eb = QGroupBox(self) g.addWidget(eb, 2, 0, 1, 3) eb.setTitle(_("Edit tweak")) eb.g = ebg = QGridLayout(eb) self.edit_tweak = et = QPlainTextEdit(self) et.setMinimumWidth(400) et.setLineWrapMode(QPlainTextEdit.NoWrap) ebg.addWidget(et, 0, 0, 1, 2) self.restore_default_button = b = QPushButton(self) b.setToolTip(_("Restore this tweak to its default value")) b.setText(_("&Reset this tweak")) ebg.addWidget(b, 1, 0, 1, 1) self.apply_button = ab = QPushButton(self) ab.setToolTip(_("Apply any changes you made to this tweak")) ab.setText(_("&Apply changes to this tweak")) ebg.addWidget(ab, 1, 1, 1, 1) def genesis(self, gui): self.gui = gui self.delegate = Delegate(self.tweaks_view) self.tweaks_view.setItemDelegate(self.delegate) self.tweaks_view.current_changed.connect(self.current_changed) self.view = self.tweaks_view self.highlighter = PythonHighlighter(self.edit_tweak.document()) self.restore_default_button.clicked.connect(self.restore_to_default) self.apply_button.clicked.connect(self.apply_tweak) self.plugin_tweaks_button.clicked.connect(self.plugin_tweaks) self.splitter.setStretchFactor(0, 1) self.splitter.setStretchFactor(1, 100) self.next_button.clicked.connect(self.find_next) self.previous_button.clicked.connect(self.find_previous) self.search.initialize('tweaks_search_history', help_text=_('Search for tweak')) self.search.search.connect(self.find) self.view.setContextMenuPolicy(Qt.CustomContextMenu) self.view.customContextMenuRequested.connect(self.show_context_menu) self.copy_icon = QIcon(I('edit-copy.png')) def show_context_menu(self, point): idx = self.tweaks_view.currentIndex() if not idx.isValid(): return True tweak = self.tweaks.data(idx, Qt.UserRole) self.context_menu = QMenu(self) self.context_menu.addAction(self.copy_icon, _('Copy to clipboard'), partial(self.copy_item_to_clipboard, val=u"%s (%s: %s)"%(tweak.name, _('ID'), tweak.var_names[0]))) self.context_menu.popup(self.mapToGlobal(point)) return True def copy_item_to_clipboard(self, val): cb = QApplication.clipboard() cb.clear() cb.setText(val) def plugin_tweaks(self): raw = self.tweaks.plugin_tweaks_string d = PluginTweaks(raw, self) if d.exec_() == d.Accepted: g, l = {}, {} try: exec(unicode(d.edit.toPlainText()), g, l) except: import traceback return error_dialog(self, _('Failed'), _('There was a syntax error in your tweak. Click ' 'the show details button for details.'), show=True, det_msg=traceback.format_exc()) self.tweaks.set_plugin_tweaks(l) self.changed() def current_changed(self, current, previous): self.tweaks_view.scrollTo(current) tweak = self.tweaks.data(current, Qt.UserRole) self.help.setPlainText(tweak.doc) self.edit_tweak.setPlainText(tweak.edit_text) def changed(self, *args): self.changed_signal.emit() def initialize(self): self.tweaks = self._model = Tweaks() self.tweaks_view.setModel(self.tweaks) def restore_to_default(self, *args): idx = self.tweaks_view.currentIndex() if idx.isValid(): self.tweaks.restore_to_default(idx) tweak = self.tweaks.data(idx, Qt.UserRole) self.edit_tweak.setPlainText(tweak.edit_text) self.changed() def restore_defaults(self): ConfigWidgetBase.restore_defaults(self) self.tweaks.restore_to_defaults() self.changed() def apply_tweak(self): idx = self.tweaks_view.currentIndex() if idx.isValid(): l, g = {}, {} try: exec(unicode(self.edit_tweak.toPlainText()), g, l) except: import traceback error_dialog(self.gui, _('Failed'), _('There was a syntax error in your tweak. Click ' 'the "Show details" button for details.'), det_msg=traceback.format_exc(), show=True) return self.tweaks.update_tweak(idx, l) self.changed() def commit(self): raw = self.tweaks.to_string() try: exec(raw) except: import traceback error_dialog(self, _('Invalid tweaks'), _('The tweaks you entered are invalid, try resetting the' ' tweaks to default and changing them one by one until' ' you find the invalid setting.'), det_msg=traceback.format_exc(), show=True) raise AbortCommit('abort') write_tweaks(raw) ConfigWidgetBase.commit(self) return True def find(self, query): if not query: return try: idx = self._model.find(query) except ParseException: self.search.search_done(False) return self.search.search_done(True) if not idx.isValid(): info_dialog(self, _('No matches'), _('Could not find any shortcuts matching %s')%query, show=True, show_copy_button=False) return self.highlight_index(idx) def highlight_index(self, idx): if not idx.isValid(): return self.view.scrollTo(idx) self.view.selectionModel().select(idx, self.view.selectionModel().ClearAndSelect) self.view.setCurrentIndex(idx) def find_next(self, *args): idx = self.view.currentIndex() if not idx.isValid(): idx = self._model.index(0) idx = self._model.find_next(idx, unicode(self.search.currentText())) self.highlight_index(idx) def find_previous(self, *args): idx = self.view.currentIndex() if not idx.isValid(): idx = self._model.index(0) idx = self._model.find_next(idx, unicode(self.search.currentText()), backwards=True) self.highlight_index(idx)
class ConfigWidget(ConfigWidgetBase): def setupUi(self, x): self.l = l = QVBoxLayout(self) self.la1 = la = QLabel( _("Values for the tweaks are shown below. Edit them to change the behavior of calibre." " Your changes will only take effect <b>after a restart</b> of calibre." )) l.addWidget(la), la.setWordWrap(True) self.splitter = s = QSplitter(self) s.setChildrenCollapsible(False) l.addWidget(s, 10) self.lv = lv = QWidget(self) lv.l = l2 = QVBoxLayout(lv) l2.setContentsMargins(0, 0, 0, 0) self.tweaks_view = tv = TweaksView(self) l2.addWidget(tv) self.plugin_tweaks_button = b = QPushButton(self) b.setToolTip( _("Edit tweaks for any custom plugins you have installed")) b.setText(_("&Plugin tweaks")) l2.addWidget(b) s.addWidget(lv) self.lv1 = lv = QWidget(self) s.addWidget(lv) lv.g = g = QGridLayout(lv) g.setContentsMargins(0, 0, 0, 0) self.search = sb = SearchBox2(self) sb.sizePolicy().setHorizontalStretch(10) sb.setSizeAdjustPolicy(sb.AdjustToMinimumContentsLength) sb.setMinimumContentsLength(10) g.addWidget(self.search, 0, 0, 1, 1) self.next_button = b = QPushButton(self) b.setIcon(QIcon(I("arrow-down.png"))) b.setText(_("&Next")) g.addWidget(self.next_button, 0, 1, 1, 1) self.previous_button = b = QPushButton(self) b.setIcon(QIcon(I("arrow-up.png"))) b.setText(_("&Previous")) g.addWidget(self.previous_button, 0, 2, 1, 1) self.hb = hb = QGroupBox(self) hb.setTitle(_("Help")) hb.l = l2 = QVBoxLayout(hb) self.help = h = QPlainTextEdit(self) l2.addWidget(h) h.setLineWrapMode(QPlainTextEdit.NoWrap) h.setReadOnly(True) g.addWidget(hb, 1, 0, 1, 3) self.eb = eb = QGroupBox(self) g.addWidget(eb, 2, 0, 1, 3) eb.setTitle(_("Edit tweak")) eb.g = ebg = QGridLayout(eb) self.edit_tweak = et = QPlainTextEdit(self) et.setMinimumWidth(400) et.setLineWrapMode(QPlainTextEdit.NoWrap) ebg.addWidget(et, 0, 0, 1, 2) self.restore_default_button = b = QPushButton(self) b.setToolTip(_("Restore this tweak to its default value")) b.setText(_("&Reset this tweak")) ebg.addWidget(b, 1, 0, 1, 1) self.apply_button = ab = QPushButton(self) ab.setToolTip(_("Apply any changes you made to this tweak")) ab.setText(_("&Apply changes to this tweak")) ebg.addWidget(ab, 1, 1, 1, 1) def genesis(self, gui): self.gui = gui self.delegate = Delegate(self.tweaks_view) self.tweaks_view.setItemDelegate(self.delegate) self.tweaks_view.current_changed.connect(self.current_changed) self.view = self.tweaks_view self.highlighter = PythonHighlighter(self.edit_tweak.document()) self.restore_default_button.clicked.connect(self.restore_to_default) self.apply_button.clicked.connect(self.apply_tweak) self.plugin_tweaks_button.clicked.connect(self.plugin_tweaks) self.splitter.setStretchFactor(0, 1) self.splitter.setStretchFactor(1, 100) self.next_button.clicked.connect(self.find_next) self.previous_button.clicked.connect(self.find_previous) self.search.initialize('tweaks_search_history', help_text=_('Search for tweak')) self.search.search.connect(self.find) self.view.setContextMenuPolicy(Qt.CustomContextMenu) self.view.customContextMenuRequested.connect(self.show_context_menu) self.copy_icon = QIcon(I('edit-copy.png')) def show_context_menu(self, point): idx = self.tweaks_view.currentIndex() if not idx.isValid(): return True tweak = self.tweaks.data(idx, Qt.UserRole) self.context_menu = QMenu(self) self.context_menu.addAction( self.copy_icon, _('Copy to clipboard'), partial(self.copy_item_to_clipboard, val=u"%s (%s: %s)" % (tweak.name, _('ID'), tweak.var_names[0]))) self.context_menu.popup(self.mapToGlobal(point)) return True def copy_item_to_clipboard(self, val): cb = QApplication.clipboard() cb.clear() cb.setText(val) def plugin_tweaks(self): raw = self.tweaks.plugin_tweaks_string d = PluginTweaks(raw, self) if d.exec_() == d.Accepted: g, l = {}, {} try: exec(unicode(d.edit.toPlainText()), g, l) except: import traceback return error_dialog( self, _('Failed'), _('There was a syntax error in your tweak. Click ' 'the show details button for details.'), show=True, det_msg=traceback.format_exc()) self.tweaks.set_plugin_tweaks(l) self.changed() def current_changed(self, current, previous): self.tweaks_view.scrollTo(current) tweak = self.tweaks.data(current, Qt.UserRole) self.help.setPlainText(tweak.doc) self.edit_tweak.setPlainText(tweak.edit_text) def changed(self, *args): self.changed_signal.emit() def initialize(self): self.tweaks = self._model = Tweaks() self.tweaks_view.setModel(self.tweaks) def restore_to_default(self, *args): idx = self.tweaks_view.currentIndex() if idx.isValid(): self.tweaks.restore_to_default(idx) tweak = self.tweaks.data(idx, Qt.UserRole) self.edit_tweak.setPlainText(tweak.edit_text) self.changed() def restore_defaults(self): ConfigWidgetBase.restore_defaults(self) self.tweaks.restore_to_defaults() self.changed() def apply_tweak(self): idx = self.tweaks_view.currentIndex() if idx.isValid(): l, g = {}, {} try: exec(unicode(self.edit_tweak.toPlainText()), g, l) except: import traceback error_dialog(self.gui, _('Failed'), _('There was a syntax error in your tweak. Click ' 'the show details button for details.'), det_msg=traceback.format_exc(), show=True) return self.tweaks.update_tweak(idx, l) self.changed() def commit(self): raw = self.tweaks.to_string() try: exec(raw) except: import traceback error_dialog( self, _('Invalid tweaks'), _('The tweaks you entered are invalid, try resetting the' ' tweaks to default and changing them one by one until' ' you find the invalid setting.'), det_msg=traceback.format_exc(), show=True) raise AbortCommit('abort') write_tweaks(raw) ConfigWidgetBase.commit(self) return True def find(self, query): if not query: return try: idx = self._model.find(query) except ParseException: self.search.search_done(False) return self.search.search_done(True) if not idx.isValid(): info_dialog(self, _('No matches'), _('Could not find any shortcuts matching %s') % query, show=True, show_copy_button=False) return self.highlight_index(idx) def highlight_index(self, idx): if not idx.isValid(): return self.view.scrollTo(idx) self.view.selectionModel().select( idx, self.view.selectionModel().ClearAndSelect) self.view.setCurrentIndex(idx) def find_next(self, *args): idx = self.view.currentIndex() if not idx.isValid(): idx = self._model.index(0) idx = self._model.find_next(idx, unicode(self.search.currentText())) self.highlight_index(idx) def find_previous(self, *args): idx = self.view.currentIndex() if not idx.isValid(): idx = self._model.index(0) idx = self._model.find_next(idx, unicode(self.search.currentText()), backwards=True) self.highlight_index(idx)
class TagsView(QTreeView): # {{{ refresh_required = pyqtSignal() tags_marked = pyqtSignal(object) edit_user_category = pyqtSignal(object) delete_user_category = pyqtSignal(object) del_item_from_user_cat = pyqtSignal(object, object, object) add_item_to_user_cat = pyqtSignal(object, object, object) add_subcategory = pyqtSignal(object) tags_list_edit = pyqtSignal(object, object) saved_search_edit = pyqtSignal(object) rebuild_saved_searches = pyqtSignal() author_sort_edit = pyqtSignal(object, object, object, object) tag_item_renamed = pyqtSignal() search_item_renamed = pyqtSignal() drag_drop_finished = pyqtSignal(object) restriction_error = pyqtSignal() tag_item_delete = pyqtSignal(object, object, object, object) def __init__(self, parent=None): QTreeView.__init__(self, parent=None) self.setMouseTracking(True) self.alter_tb = None self.disable_recounting = False self.setUniformRowHeights(True) self.setIconSize(QSize(20, 20)) self.setTabKeyNavigation(True) self.setAnimated(True) self.setHeaderHidden(True) self.setItemDelegate(TagDelegate(self)) self.made_connections = False self.setAcceptDrops(True) self.setDragEnabled(True) self.setDragDropMode(self.DragDrop) self.setDropIndicatorShown(True) self.in_drag_drop = False self.setAutoExpandDelay(500) self.pane_is_visible = False self.search_icon = QIcon(I('search.png')) self.user_category_icon = QIcon(I('tb_folder.png')) self.delete_icon = QIcon(I('list_remove.png')) self.rename_icon = QIcon(I('edit-undo.png')) self._model = TagsModel(self) self._model.search_item_renamed.connect(self.search_item_renamed) self._model.refresh_required.connect(self.refresh_required, type=Qt.QueuedConnection) self._model.tag_item_renamed.connect(self.tag_item_renamed) self._model.restriction_error.connect(self.restriction_error) self._model.user_categories_edited.connect(self.user_categories_edited, type=Qt.QueuedConnection) self._model.drag_drop_finished.connect(self.drag_drop_finished) stylish_tb = ''' QTreeView { background-color: palette(window); color: palette(window-text); border: none; } ''' self.setStyleSheet(''' QTreeView::item { border: 1px solid transparent; padding-top:0.8ex; padding-bottom:0.8ex; } QTreeView::item:hover { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1); border: 1px solid #bfcde4; border-radius: 6px; } ''' + ('' if gprefs['tag_browser_old_look'] else stylish_tb)) if gprefs['tag_browser_old_look']: self.setAlternatingRowColors(True) # Allowing keyboard focus looks bad in the Qt Fusion style and is useless # anyway since the enter/spacebar keys do nothing self.setFocusPolicy(Qt.NoFocus) @property def hidden_categories(self): return self._model.hidden_categories @property def db(self): return self._model.db @property def collapse_model(self): return self._model.collapse_model def set_pane_is_visible(self, to_what): pv = self.pane_is_visible self.pane_is_visible = to_what if to_what and not pv: self.recount() def get_state(self): state_map = {} expanded_categories = [] for row, category in enumerate(self._model.category_nodes): if self.isExpanded(self._model.index(row, 0, QModelIndex())): expanded_categories.append(category.category_key) states = [c.tag.state for c in category.child_tags()] names = [(c.tag.name, c.tag.category) for c in category.child_tags()] state_map[category.category_key] = dict(izip(names, states)) return expanded_categories, state_map def reread_collapse_parameters(self): self._model.reread_collapse_model(self.get_state()[1]) def set_database(self, db, alter_tb): self._model.set_database(db) self.alter_tb = alter_tb self.pane_is_visible = True # because TagsModel.set_database did a recount self.setModel(self._model) self.setContextMenuPolicy(Qt.CustomContextMenu) pop = self.db.CATEGORY_SORTS.index(config['sort_tags_by']) self.alter_tb.sort_menu.actions()[pop].setChecked(True) try: match_pop = self.db.MATCH_TYPE.index(config['match_tags_type']) except ValueError: match_pop = 0 self.alter_tb.match_menu.actions()[match_pop].setChecked(True) if not self.made_connections: self.clicked.connect(self.toggle) self.customContextMenuRequested.connect(self.show_context_menu) self.refresh_required.connect(self.recount, type=Qt.QueuedConnection) self.alter_tb.sort_menu.triggered.connect(self.sort_changed) self.alter_tb.match_menu.triggered.connect(self.match_changed) self.made_connections = True self.refresh_signal_processed = True db.add_listener(self.database_changed) self.expanded.connect(self.item_expanded) def database_changed(self, event, ids): if self.refresh_signal_processed: self.refresh_signal_processed = False self.refresh_required.emit() def user_categories_edited(self, user_cats, nkey): state_map = self.get_state()[1] self.db.new_api.set_pref('user_categories', user_cats) self._model.rebuild_node_tree(state_map=state_map) p = self._model.find_category_node('@'+nkey) self.show_item_at_path(p) @property def match_all(self): return (self.alter_tb and self.alter_tb.match_menu.actions()[1].isChecked()) def sort_changed(self, action): for i, ac in enumerate(self.alter_tb.sort_menu.actions()): if ac is action: config.set('sort_tags_by', self.db.CATEGORY_SORTS[i]) self.recount() break def match_changed(self, action): try: for i, ac in enumerate(self.alter_tb.match_menu.actions()): if ac is action: config.set('match_tags_type', self.db.MATCH_TYPE[i]) except: pass def mouseMoveEvent(self, event): dex = self.indexAt(event.pos()) if dex.isValid(): self.setCursor(Qt.PointingHandCursor) else: self.unsetCursor() if not event.buttons() & Qt.LeftButton: return if self.in_drag_drop or not dex.isValid(): QTreeView.mouseMoveEvent(self, event) return # Must deal with odd case where the node being dragged is 'virtual', # created to form a hierarchy. We can't really drag this node, but in # addition we can't allow drag recognition to notice going over some # other node and grabbing that one. So we set in_drag_drop to prevent # this from happening, turning it off when the user lifts the button. self.in_drag_drop = True if not self._model.flags(dex) & Qt.ItemIsDragEnabled: QTreeView.mouseMoveEvent(self, event) return md = self._model.mimeData([dex]) pixmap = dex.data(DRAG_IMAGE_ROLE).pixmap(self.iconSize()) drag = QDrag(self) drag.setPixmap(pixmap) drag.setMimeData(md) if self._model.is_in_user_category(dex): drag.exec_(Qt.CopyAction|Qt.MoveAction, Qt.CopyAction) else: drag.exec_(Qt.CopyAction) def mouseReleaseEvent(self, event): # Swallow everything except leftButton so context menus work correctly if event.button() == Qt.LeftButton or self.in_drag_drop: QTreeView.mouseReleaseEvent(self, event) self.in_drag_drop = False def mouseDoubleClickEvent(self, event): # swallow these to avoid toggling and editing at the same time pass @property def search_string(self): tokens = self._model.tokens() joiner = ' and ' if self.match_all else ' or ' return joiner.join(tokens) def toggle(self, index): self._toggle(index, None) def _toggle(self, index, set_to): ''' set_to: if None, advance the state. Otherwise must be one of the values in TAG_SEARCH_STATES ''' modifiers = int(QApplication.keyboardModifiers()) exclusive = modifiers not in (Qt.CTRL, Qt.SHIFT) if self._model.toggle(index, exclusive, set_to=set_to): self.tags_marked.emit(self.search_string) def conditional_clear(self, search_string): if search_string != self.search_string: self.clear() def context_menu_handler(self, action=None, category=None, key=None, index=None, search_state=None, use_vl=None): if not action: return try: if action == 'set_icon': try: path = choose_files(self, 'choose_category_icon', _('Change Icon for: %s')%key, filters=[ ('Images', ['png', 'gif', 'jpg', 'jpeg'])], all_files=False, select_only_single_file=True) if path: path = path[0] p = QIcon(path).pixmap(QSize(128, 128)) d = os.path.join(config_dir, 'tb_icons') if not os.path.exists(d): os.makedirs(d) with open(os.path.join(d, 'icon_'+ sanitize_file_name_unicode(key)+'.png'), 'wb') as f: f.write(pixmap_to_data(p, format='PNG')) path = os.path.basename(f.name) self._model.set_custom_category_icon(key, unicode(path)) self.recount() except: import traceback traceback.print_exc() return if action == 'clear_icon': self._model.set_custom_category_icon(key, None) self.recount() return if action == 'edit_item_no_vl': item = self.model().get_node(index) item.use_vl = False self.edit(index) return if action == 'edit_item_in_vl': item = self.model().get_node(index) item.use_vl = True self.edit(index) return if action == 'delete_item_in_vl': self.tag_item_delete.emit(key, index.id, index.original_name, self.model().get_book_ids_to_use()) return if action == 'delete_item_no_vl': self.tag_item_delete.emit(key, index.id, index.original_name, None) return if action == 'open_editor': self.tags_list_edit.emit(category, key) return if action == 'manage_categories': self.edit_user_category.emit(category) return if action == 'search': self._toggle(index, set_to=search_state) return if action == 'add_to_category': tag = index.tag if len(index.children) > 0: for c in index.all_children(): self.add_item_to_user_cat.emit(category, c.tag.original_name, c.tag.category) self.add_item_to_user_cat.emit(category, tag.original_name, tag.category) return if action == 'add_subcategory': self.add_subcategory.emit(key) return if action == 'search_category': self._toggle(index, set_to=search_state) return if action == 'delete_user_category': self.delete_user_category.emit(key) return if action == 'delete_search': self.model().db.saved_search_delete(key) self.rebuild_saved_searches.emit() return if action == 'delete_item_from_user_category': tag = index.tag if len(index.children) > 0: for c in index.children: self.del_item_from_user_cat.emit(key, c.tag.original_name, c.tag.category) self.del_item_from_user_cat.emit(key, tag.original_name, tag.category) return if action == 'manage_searches': self.saved_search_edit.emit(category) return if action == 'edit_author_sort': self.author_sort_edit.emit(self, index, True, False) return if action == 'edit_author_link': self.author_sort_edit.emit(self, index, False, True) return reset_filter_categories = True if action == 'hide': self.hidden_categories.add(category) elif action == 'show': self.hidden_categories.discard(category) elif action == 'categorization': changed = self.collapse_model != category self._model.collapse_model = category if changed: reset_filter_categories = False gprefs['tags_browser_partition_method'] = category elif action == 'defaults': self.hidden_categories.clear() self.db.new_api.set_pref('tag_browser_hidden_categories', list(self.hidden_categories)) if reset_filter_categories: self._model.set_categories_filter(None) self._model.rebuild_node_tree() except: return def show_context_menu(self, point): def display_name(tag): if tag.category == 'search': n = tag.name if len(n) > 45: n = n[:45] + '...' return "'" + n + "'" return tag.name index = self.indexAt(point) self.context_menu = QMenu(self) if index.isValid(): item = index.data(Qt.UserRole) tag = None if item.type == TagTreeItem.TAG: tag_item = item tag = item.tag while item.type != TagTreeItem.CATEGORY: item = item.parent if item.type == TagTreeItem.CATEGORY: if not item.category_key.startswith('@'): while item.parent != self._model.root_item: item = item.parent category = unicode(item.name or '') key = item.category_key # Verify that we are working with a field that we know something about if key not in self.db.field_metadata: return True # Did the user click on a leaf node? if tag: # If the user right-clicked on an editable item, then offer # the possibility of renaming that item. if tag.is_editable: # Add the 'rename' items if self.model().get_in_vl(): self.context_menu.addAction(self.rename_icon, _('Rename %s in virtual library')%display_name(tag), partial(self.context_menu_handler, action='edit_item_in_vl', index=index)) self.context_menu.addAction(self.rename_icon, _('Rename %s')%display_name(tag), partial(self.context_menu_handler, action='edit_item_no_vl', index=index)) if key in ('tags', 'series', 'publisher') or \ self._model.db.field_metadata.is_custom_field(key): if self.model().get_in_vl(): self.context_menu.addAction(self.delete_icon, _('Delete %s in virtual library')%display_name(tag), partial(self.context_menu_handler, action='delete_item_in_vl', key=key, index=tag)) self.context_menu.addAction(self.delete_icon, _('Delete %s')%display_name(tag), partial(self.context_menu_handler, action='delete_item_no_vl', key=key, index=tag)) if key == 'authors': self.context_menu.addAction(_('Edit sort for %s')%display_name(tag), partial(self.context_menu_handler, action='edit_author_sort', index=tag.id)) self.context_menu.addAction(_('Edit link for %s')%display_name(tag), partial(self.context_menu_handler, action='edit_author_link', index=tag.id)) # is_editable is also overloaded to mean 'can be added # to a user category' m = self.context_menu.addMenu(self.user_category_icon, _('Add %s to user category')%display_name(tag)) nt = self.model().category_node_tree def add_node_tree(tree_dict, m, path): p = path[:] for k in sorted(tree_dict.keys(), key=sort_key): p.append(k) n = k[1:] if k.startswith('@') else k m.addAction(self.user_category_icon, n, partial(self.context_menu_handler, 'add_to_category', category='.'.join(p), index=tag_item)) if len(tree_dict[k]): tm = m.addMenu(self.user_category_icon, _('Children of %s')%n) add_node_tree(tree_dict[k], tm, p) p.pop() add_node_tree(nt, m, []) elif key == 'search' and tag.is_searchable: self.context_menu.addAction(self.rename_icon, _('Rename %s')%display_name(tag), partial(self.context_menu_handler, action='edit_item_no_vl', index=index)) self.context_menu.addAction(self.delete_icon, _('Delete search %s')%display_name(tag), partial(self.context_menu_handler, action='delete_search', key=tag.original_name)) if key.startswith('@') and not item.is_gst: self.context_menu.addAction(self.user_category_icon, _('Remove %(item)s from category %(cat)s')% dict(item=display_name(tag), cat=item.py_name), partial(self.context_menu_handler, action='delete_item_from_user_category', key=key, index=tag_item)) if tag.is_searchable: # Add the search for value items. All leaf nodes are searchable self.context_menu.addAction(self.search_icon, _('Search for %s')%display_name(tag), partial(self.context_menu_handler, action='search', search_state=TAG_SEARCH_STATES['mark_plus'], index=index)) self.context_menu.addAction(self.search_icon, _('Search for everything but %s')%display_name(tag), partial(self.context_menu_handler, action='search', search_state=TAG_SEARCH_STATES['mark_minus'], index=index)) self.context_menu.addSeparator() elif key.startswith('@') and not item.is_gst: if item.can_be_edited: self.context_menu.addAction(self.rename_icon, _('Rename %s')%item.py_name, partial(self.context_menu_handler, action='edit_item_no_vl', index=index)) self.context_menu.addAction(self.user_category_icon, _('Add sub-category to %s')%item.py_name, partial(self.context_menu_handler, action='add_subcategory', key=key)) self.context_menu.addAction(self.delete_icon, _('Delete user category %s')%item.py_name, partial(self.context_menu_handler, action='delete_user_category', key=key)) self.context_menu.addSeparator() # Hide/Show/Restore categories self.context_menu.addAction(_('Hide category %s') % category, partial(self.context_menu_handler, action='hide', category=key)) if self.hidden_categories: m = self.context_menu.addMenu(_('Show category')) for col in sorted(self.hidden_categories, key=lambda x: sort_key(self.db.field_metadata[x]['name'])): m.addAction(self.db.field_metadata[col]['name'], partial(self.context_menu_handler, action='show', category=col)) # search by category. Some categories are not searchable, such # as search and news if item.tag.is_searchable: self.context_menu.addAction(self.search_icon, _('Search for books in category %s')%category, partial(self.context_menu_handler, action='search_category', index=self._model.createIndex(item.row(), 0, item), search_state=TAG_SEARCH_STATES['mark_plus'])) self.context_menu.addAction(self.search_icon, _('Search for books not in category %s')%category, partial(self.context_menu_handler, action='search_category', index=self._model.createIndex(item.row(), 0, item), search_state=TAG_SEARCH_STATES['mark_minus'])) # Offer specific editors for tags/series/publishers/saved searches self.context_menu.addSeparator() if key in ['tags', 'publisher', 'series'] or \ (self.db.field_metadata[key]['is_custom'] and self.db.field_metadata[key]['datatype'] != 'composite'): self.context_menu.addAction(_('Manage %s')%category, partial(self.context_menu_handler, action='open_editor', category=tag.original_name if tag else None, key=key)) elif key == 'authors': self.context_menu.addAction(_('Manage %s')%category, partial(self.context_menu_handler, action='edit_author_sort')) elif key == 'search': self.context_menu.addAction(_('Manage Saved Searches'), partial(self.context_menu_handler, action='manage_searches', category=tag.name if tag else None)) self.context_menu.addSeparator() self.context_menu.addAction(_('Change category icon'), partial(self.context_menu_handler, action='set_icon', key=key)) self.context_menu.addAction(_('Restore default icon'), partial(self.context_menu_handler, action='clear_icon', key=key)) # Always show the user categories editor self.context_menu.addSeparator() if key.startswith('@') and \ key[1:] in self.db.prefs.get('user_categories', {}).keys(): self.context_menu.addAction(_('Manage User Categories'), partial(self.context_menu_handler, action='manage_categories', category=key[1:])) else: self.context_menu.addAction(_('Manage User Categories'), partial(self.context_menu_handler, action='manage_categories', category=None)) if self.hidden_categories: if not self.context_menu.isEmpty(): self.context_menu.addSeparator() self.context_menu.addAction(_('Show all categories'), partial(self.context_menu_handler, action='defaults')) m = self.context_menu.addMenu(_('Change sub-categorization scheme')) da = m.addAction(_('Disable'), partial(self.context_menu_handler, action='categorization', category='disable')) fla = m.addAction(_('By first letter'), partial(self.context_menu_handler, action='categorization', category='first letter')) pa = m.addAction(_('Partition'), partial(self.context_menu_handler, action='categorization', category='partition')) if self.collapse_model == 'disable': da.setCheckable(True) da.setChecked(True) elif self.collapse_model == 'first letter': fla.setCheckable(True) fla.setChecked(True) else: pa.setCheckable(True) pa.setChecked(True) if config['sort_tags_by'] != "name": fla.setEnabled(False) m.hovered.connect(self.collapse_menu_hovered) fla.setToolTip(_('First letter is usable only when sorting by name')) # Apparently one cannot set a tooltip to empty, so use a star and # deal with it in the hover method da.setToolTip('*') pa.setToolTip('*') if index.isValid() and self.model().rowCount(index) > 0: self.context_menu.addSeparator() self.context_menu.addAction(_('E&xpand all children'), partial(self.expand_node_and_descendants, index)) if not self.context_menu.isEmpty(): self.context_menu.popup(self.mapToGlobal(point)) return True def expand_node_and_descendants(self, index): if not index.isValid(): return self.expand(index) for r in xrange(self.model().rowCount(index)): self.expand_node_and_descendants(index.child(r, 0)) def collapse_menu_hovered(self, action): tip = action.toolTip() if tip == '*': tip = '' QToolTip.showText(QCursor.pos(), tip) def dragMoveEvent(self, event): QTreeView.dragMoveEvent(self, event) self.setDropIndicatorShown(False) index = self.indexAt(event.pos()) if not index.isValid(): return src_is_tb = event.mimeData().hasFormat('application/calibre+from_tag_browser') item = index.data(Qt.UserRole) if item.type == TagTreeItem.ROOT: return flags = self._model.flags(index) if item.type == TagTreeItem.TAG and flags & Qt.ItemIsDropEnabled: self.setDropIndicatorShown(not src_is_tb) return if item.type == TagTreeItem.CATEGORY and not item.is_gst: fm_dest = self.db.metadata_for_field(item.category_key) if fm_dest['kind'] == 'user': if src_is_tb: if event.dropAction() == Qt.MoveAction: data = str(event.mimeData().data('application/calibre+from_tag_browser')) src = cPickle.loads(data) for s in src: if s[0] == TagTreeItem.TAG and \ (not s[1].startswith('@') or s[2]): return self.setDropIndicatorShown(True) return md = event.mimeData() if hasattr(md, 'column_name'): fm_src = self.db.metadata_for_field(md.column_name) if md.column_name in ['authors', 'publisher', 'series'] or \ (fm_src['is_custom'] and ( (fm_src['datatype'] in ['series', 'text', 'enumeration'] and not fm_src['is_multiple']) or (fm_src['datatype'] == 'composite' and fm_src['display'].get('make_category', False)))): self.setDropIndicatorShown(True) def clear(self): if self.model(): self.model().clear_state() def is_visible(self, idx): item = idx.data(Qt.UserRole) if getattr(item, 'type', None) == TagTreeItem.TAG: idx = idx.parent() return self.isExpanded(idx) def recount(self, *args): ''' Rebuild the category tree, expand any categories that were expanded, reset the search states, and reselect the current node. ''' if self.disable_recounting or not self.pane_is_visible: return self.refresh_signal_processed = True ci = self.currentIndex() if not ci.isValid(): ci = self.indexAt(QPoint(10, 10)) path = self.model().path_for_index(ci) if self.is_visible(ci) else None expanded_categories, state_map = self.get_state() self._model.rebuild_node_tree(state_map=state_map) self.blockSignals(True) for category in expanded_categories: idx = self._model.index_for_category(category) if idx is not None and idx.isValid(): self.expand(idx) self.show_item_at_path(path) self.blockSignals(False) def show_item_at_path(self, path, box=False, position=QTreeView.PositionAtCenter): ''' Scroll the browser and open categories to show the item referenced by path. If possible, the item is placed in the center. If box=True, a box is drawn around the item. ''' if path: self.show_item_at_index(self._model.index_for_path(path), box=box, position=position) def expand_parent(self, idx): # Needed otherwise Qt sometimes segfaults if the node is buried in a # collapsed, off screen hierarchy. To be safe, we expand from the # outermost in p = self._model.parent(idx) if p.isValid(): self.expand_parent(p) self.expand(idx) def show_item_at_index(self, idx, box=False, position=QTreeView.PositionAtCenter): if idx.isValid() and idx.data(Qt.UserRole) is not self._model.root_item: self.expand_parent(idx) self.setCurrentIndex(idx) self.scrollTo(idx, position) if box: self._model.set_boxed(idx) def item_expanded(self, idx): ''' Called by the expanded signal ''' self.setCurrentIndex(idx)
class Quickview(QDialog, Ui_Quickview): reopen_after_dock_change = pyqtSignal() tab_pressed_signal = pyqtSignal(object, object) quickview_closed = pyqtSignal() def __init__(self, gui, row): self.is_pane = gprefs.get('quickview_is_pane', False) if not self.is_pane: QDialog.__init__(self, gui, flags=Qt.Widget) else: QDialog.__init__(self, gui) Ui_Quickview.__init__(self) self.setupUi(self) self.isClosed = False self.current_book = None self.closed_by_button = False if self.is_pane: self.main_grid_layout.setContentsMargins(0, 0, 0, 0) self.books_table_column_widths = None try: self.books_table_column_widths = \ gprefs.get('quickview_dialog_books_table_widths', None) if not self.is_pane: geom = gprefs.get('quickview_dialog_geometry', bytearray('')) self.restoreGeometry(QByteArray(geom)) except: pass if not self.is_pane: # Remove the help button from the window title bar icon = self.windowIcon() self.setWindowFlags(self.windowFlags()&(~Qt.WindowContextHelpButtonHint)) self.setWindowFlags(self.windowFlags()|Qt.WindowStaysOnTopHint) self.setWindowIcon(icon) self.view = gui.library_view self.db = self.view.model().db self.gui = gui self.is_closed = False self.current_book_id = None # the db id of the book used to fill the lh pane self.current_column = None # current logical column in books list self.current_key = None # current lookup key in books list self.last_search = None self.no_valid_items = False self.fm = self.db.field_metadata self.items.setSelectionMode(QAbstractItemView.SingleSelection) self.items.currentTextChanged.connect(self.item_selected) self.items.setProperty('highlight_current_item', 150) focus_filter = WidgetFocusFilter(self.items) focus_filter.focus_entered_signal.connect(self.focus_entered) self.items.installEventFilter(focus_filter) self.tab_pressed_signal.connect(self.tab_pressed) return_filter = BooksTableFilter(self.books_table) return_filter.return_pressed_signal.connect(self.return_pressed) self.books_table.installEventFilter(return_filter) focus_filter = WidgetFocusFilter(self.books_table) focus_filter.focus_entered_signal.connect(self.focus_entered) self.books_table.installEventFilter(focus_filter) self.close_button.clicked.connect(self.close_button_clicked) self.refresh_button.clicked.connect(self.refill) self.tab_order_widgets = [self.items, self.books_table, self.lock_qv, self.dock_button, self.search_button, self.refresh_button, self.close_button] for idx,widget in enumerate(self.tab_order_widgets): widget.installEventFilter(WidgetTabFilter(widget, idx, self.tab_pressed_signal)) self.books_table.setSelectionBehavior(QAbstractItemView.SelectRows) self.books_table.setSelectionMode(QAbstractItemView.SingleSelection) self.books_table.setProperty('highlight_current_item', 150) # Set up the books table columns self.add_columns_to_widget() self.books_table_header_height = self.books_table.height() self.books_table.cellDoubleClicked.connect(self.book_doubleclicked) self.books_table.currentCellChanged.connect(self.books_table_cell_changed) self.books_table.cellClicked.connect(self.books_table_set_search_string) self.books_table.cellActivated.connect(self.books_table_set_search_string) self.books_table.sortByColumn(0, Qt.AscendingOrder) # get the standard table row height. Do this here because calling # resizeRowsToContents can word wrap long cell contents, creating # double-high rows self.books_table.setRowCount(1) self.books_table.setItem(0, 0, TableItem('A', '')) self.books_table.resizeRowsToContents() self.books_table_row_height = self.books_table.rowHeight(0) self.books_table.setRowCount(0) # Add the data self.refresh(row) self.view.clicked.connect(self.slave) self.view.selectionModel().currentColumnChanged.connect(self.column_slave) QCoreApplication.instance().aboutToQuit.connect(self.save_state) self.search_button.clicked.connect(self.do_search) self.view.model().new_bookdisplay_data.connect(self.book_was_changed) self.close_button.setDefault(False) self.close_button_tooltip = _('The Quickview shortcut ({0}) shows/hides the Quickview panel') self.search_button_tooltip = _('Search in the library view for the currently highlighted selection') self.search_button.setToolTip(self.search_button_tooltip) if self.is_pane: self.dock_button.setText(_('Undock')) self.dock_button.setToolTip(_('Pop up the quickview panel into its own floating window')) self.dock_button.setIcon(QIcon(I('arrow-up.png'))) # Remove the ampersands from the buttons because shortcuts exist. self.lock_qv.setText(_('Lock Quickview contents')) self.search_button.setText(_('Search')) self.refresh_button.setText(_('Refresh')) self.gui.quickview_splitter.add_quickview_dialog(self) self.close_button.setVisible(False) else: self.dock_button.setToolTip(_('Embed the quickview panel into the main calibre window')) self.dock_button.setIcon(QIcon(I('arrow-down.png'))) self.set_focus() self.books_table.horizontalHeader().sectionResized.connect(self.section_resized) self.dock_button.clicked.connect(self.show_as_pane_changed) self.gui.search.cleared.connect(self.indicate_no_items) # Enable the refresh button only when QV is locked self.refresh_button.setEnabled(False) self.lock_qv.stateChanged.connect(self.lock_qv_changed) self.view_icon = QIcon(I('view.png')) self.view_plugin = self.gui.iactions['View'] self.books_table.setContextMenuPolicy(Qt.CustomContextMenu) self.books_table.customContextMenuRequested.connect(self.show_context_menu) def show_context_menu(self, point): index = self.books_table.indexAt(point) item = self.books_table.item(index.row(), 0) book_id = int(item.data(Qt.UserRole)) self.context_menu = QMenu(self) self.context_menu.addAction(self.view_icon, _('View'), partial(self.view_plugin._view_calibre_books, [book_id])) self.context_menu.popup(self.books_table.mapToGlobal(point)) return True def lock_qv_changed(self, state): self.refresh_button.setEnabled(state) def add_columns_to_widget(self): ''' Get the list of columns from the preferences. Clear the current table and add the current column set ''' self.column_order = [x[0] for x in get_qv_field_list(self.fm) if x[1]] self.books_table.clear() self.books_table.setRowCount(0) self.books_table.setColumnCount(len(self.column_order)) for idx,col in enumerate(self.column_order): t = QTableWidgetItem(self.fm[col]['name']) self.books_table.setHorizontalHeaderItem(idx, t) def refill(self): ''' Refill the table in case the columns displayed changes ''' self.add_columns_to_widget() self._refresh(self.current_book_id, self.current_key) def set_search_text(self, txt): if txt: self.search_button.setEnabled(True) else: self.search_button.setEnabled(False) self.last_search = txt def set_shortcuts(self, search_sc, qv_sc): if self.is_pane: self.search_button.setToolTip(self.search_button_tooltip + ' (' + search_sc + ')') self.close_button.setToolTip(self.close_button_tooltip.format(qv_sc)) def focus_entered(self, obj): if obj == self.books_table: self.books_table_set_search_string(self.books_table.currentRow(), self.books_table.currentColumn()) elif obj.currentItem(): self.item_selected(obj.currentItem().text()) def books_table_cell_changed(self, cur_row, cur_col, prev_row, prev_col): self.books_table_set_search_string(cur_row, cur_col) def books_table_set_search_string(self, current_row, current_col): ''' Given the contents of a cell, compute a search string that will find that book and any others with identical contents in the cell. ''' current = self.books_table.item(current_row, current_col) if current is None: return book_id = current.data(Qt.UserRole) if current is None: return col = self.column_order[current.column()] if col == 'title': self.set_search_text('title:="' + current.text().replace('"', '\\"') + '"') elif col == 'authors': authors = [] for aut in [t.strip() for t in current.text().split('&')]: authors.append('authors:="' + aut.replace('"', '\\"') + '"') self.set_search_text(' and '.join(authors)) elif self.fm[col]['datatype'] == 'series': mi = self.db.get_metadata(book_id, index_is_id=True, get_user_categories=False) t = mi.get(col) if t: self.set_search_text(col+ ':="' + t + '"') else: self.set_search_text(None) else: if self.fm[col]['is_multiple']: items = [(col + ':"=' + v.strip() + '"') for v in current.text().split(self.fm[col]['is_multiple']['ui_to_list'])] self.set_search_text(' and '.join(items)) else: self.set_search_text(col + ':"=' + current.text() + '"') def tab_pressed(self, in_widget, isForward): if isForward: in_widget += 1 if in_widget >= len(self.tab_order_widgets): in_widget = 0 else: in_widget -= 1 if in_widget < 0: in_widget = len(self.tab_order_widgets) - 1 self.tab_order_widgets[in_widget].setFocus(Qt.TabFocusReason) def show(self): QDialog.show(self) if self.is_pane: self.gui.quickview_splitter.show_quickview_widget() def show_as_pane_changed(self): gprefs['quickview_is_pane'] = not gprefs.get('quickview_is_pane', False) self.reopen_after_dock_change.emit() # search button def do_search(self): if self.no_valid_items: return if self.last_search is not None: self.gui.search.set_search_string(self.last_search) def book_was_changed(self, mi): ''' Called when book information is changed in the library view. Make that book info current. This means that prev and next in edit metadata will move the current book and change quickview ''' if self.is_closed or self.current_column is None: return # There is an ordering problem when libraries are changed. The library # view is changed, triggering a book_was_changed signal. Unfortunately # this happens before the library_changed actions are run, meaning we # still have the old database. To avoid the problem we just ignore the # operation if we get an exception. The "close" will come # eventually. try: self.refresh(self.view.model().index(self.db.row(mi.id), self.current_column)) except: pass # clicks on the items listWidget def item_selected(self, txt): if self.no_valid_items: return self.fill_in_books_box(unicode(txt)) self.set_search_text(self.current_key + ':"=' + txt.replace('"', '\\"') + '"') def refresh(self, idx): ''' Given a cell in the library view, display the information. This method converts the index into the lookup ken ''' if self.lock_qv.isChecked(): return try: bv_row = idx.row() self.current_column = idx.column() key = self.view.column_map[self.current_column] book_id = self.view.model().id(bv_row) if self.current_book_id == book_id and self.current_key == key: return self._refresh(book_id, key) except: self.indicate_no_items() def _refresh(self, book_id, key): ''' Actually fill in the left-hand pane from the information in the selected column of the selected book ''' # Only show items for categories if key is None or not self.fm[key]['is_category']: if self.current_key is None: self.indicate_no_items() return key = self.current_key label_text = _('&Item: {0} ({1})') if self.is_pane: label_text = label_text.replace('&', '') self.items_label.setText(label_text.format(self.fm[key]['name'], key)) self.items.blockSignals(True) self.items.clear() self.books_table.setRowCount(0) mi = self.db.get_metadata(book_id, index_is_id=True, get_user_categories=False) vals = mi.get(key, None) if vals: self.no_valid_items = False if self.fm[key]['datatype'] == 'rating': if self.fm[key]['display'].get('allow_half_stars', False): vals = unicode(vals/2.0) else: vals = unicode(vals/2) if not isinstance(vals, list): vals = [vals] vals.sort(key=sort_key) for v in vals: a = QListWidgetItem(v) self.items.addItem(a) self.items.setCurrentRow(0) self.current_book_id = book_id self.current_key = key self.fill_in_books_box(vals[0]) else: self.indicate_no_items() self.items.blockSignals(False) def indicate_no_items(self): self.no_valid_items = True self.items.clear() self.add_columns_to_widget() self.items.addItem(QListWidgetItem(_('**No items found**'))) self.books_label.setText(_('Click in a column in the library view ' 'to see the information for that book')) def fill_in_books_box(self, selected_item): ''' Given the selected row in the left-hand box, fill in the grid with the books that contain that data. ''' # Do a bit of fix-up on the items so that the search works. if selected_item.startswith('.'): sv = '.' + selected_item else: sv = selected_item sv = self.current_key + ':"=' + sv.replace('"', r'\"') + '"' if gprefs['qv_respects_vls']: books = self.db.search(sv, return_matches=True, sort_results=False) else: books = self.db.new_api.search(sv) self.books_table.setRowCount(len(books)) label_text = _('&Books with selected item "{0}": {1}') if self.is_pane: label_text = label_text.replace('&', '') self.books_label.setText(label_text.format(selected_item, len(books))) select_item = None self.books_table.setSortingEnabled(False) self.books_table.blockSignals(True) tt = ('<p>' + _( 'Double click on a book to change the selection in the library view or ' 'change the column shown in the left-hand pane. ' 'Shift- or Control- double click to edit the metadata of a book, ' 'which also changes the selected book.' ) + '</p>') for row, b in enumerate(books): mi = self.db.new_api.get_proxy_metadata(b) for col in self.column_order: try: if col == 'title': a = TableItem(mi.title, mi.title_sort) if b == self.current_book_id: select_item = a elif col == 'authors': a = TableItem(' & '.join(mi.authors), mi.author_sort) elif col == 'series': series = mi.format_field('series')[1] if series is None: a = TableItem('', '', 0) else: a = TableItem(series, mi.series, mi.series_index) elif col == 'size': v = mi.get('book_size') if v is not None: a = TableItem('{:n}'.format(v), v) a.setTextAlignment(Qt.AlignRight) else: a = TableItem(' ', None) elif self.fm[col]['datatype'] == 'series': v = mi.format_field(col)[1] a = TableItem(v, mi.get(col), mi.get(col+'_index')) elif self.fm[col]['datatype'] == 'datetime': v = mi.format_field(col)[1] d = mi.get(col) if d is None: d = UNDEFINED_DATE a = TableItem(v, timestampfromdt(d)) elif self.fm[col]['datatype'] in ('float', 'int'): v = mi.format_field(col)[1] sv = mi.get(col) a = TableItem(v, sv) else: v = mi.format_field(col)[1] a = TableItem(v, v) except: traceback.print_exc() a = TableItem(_('Something went wrong while filling in the table'), '') a.setData(Qt.UserRole, b) a.setToolTip(tt) self.books_table.setItem(row, self.key_to_table_widget_column(col), a) self.books_table.setRowHeight(row, self.books_table_row_height) self.books_table.blockSignals(False) self.books_table.setSortingEnabled(True) if select_item is not None: self.books_table.setCurrentItem(select_item) self.books_table.scrollToItem(select_item, QAbstractItemView.PositionAtCenter) self.set_search_text(sv) # Deal with sizing the table columns. Done here because the numbers are not # correct until the first paint. def resizeEvent(self, *args): QDialog.resizeEvent(self, *args) # Do this if we are resizing for the first time to reset state. if self.is_pane and self.height() == 0: self.gui.quickview_splitter.set_sizes() if self.books_table_column_widths is not None: for c,w in enumerate(self.books_table_column_widths): self.books_table.setColumnWidth(c, w) else: # the vertical scroll bar might not be rendered, so might not yet # have a width. Assume 25. Not a problem because user-changed column # widths will be remembered w = self.books_table.width() - 25 - self.books_table.verticalHeader().width() w /= self.books_table.columnCount() for c in range(0, self.books_table.columnCount()): self.books_table.setColumnWidth(c, w) self.save_state() def key_to_table_widget_column(self, key): return self.column_order.index(key) def return_pressed(self): row = self.books_table.currentRow() if gprefs['qv_retkey_changes_column']: self.select_book(row, self.books_table.currentColumn()) else: self.select_book(row, self.key_to_table_widget_column(self.current_key)) def book_doubleclicked(self, row, column): if self.no_valid_items: return try: if gprefs['qv_dclick_changes_column']: self.select_book(row, column) else: self.select_book(row, self.key_to_table_widget_column(self.current_key)) except: from calibre.gui2 import error_dialog error_dialog(self, _('Quickview: Book not in library view'), _('The book you selected is not currently displayed in ' 'the library view, perhaps because of a search, so ' 'Quickview cannot select it.'), show=True, show_copy_button=False) def select_book(self, row, column): ''' row and column both refer the qv table. In particular, column is not the logical column in the book list. ''' item = self.books_table.item(row, column) if item is None: return book_id = int(self.books_table.item(row, column).data(Qt.UserRole)) key = self.column_order[column] modifiers = int(QApplication.keyboardModifiers()) if modifiers in (Qt.CTRL, Qt.SHIFT): self.view.select_rows([book_id]) em = find_plugin('Edit Metadata') if em and em.actual_plugin_: em.actual_plugin_.edit_metadata(None) else: self.view.select_cell(self.db.data.id_to_index(book_id), self.view.column_map.index(key)) def set_focus(self): self.activateWindow() self.books_table.setFocus() def column_slave(self, current): ''' called when the column is changed on the booklist ''' if gprefs['qv_follows_column']: self.slave(current) def slave(self, current): ''' called when a book is clicked on the library view ''' if self.is_closed: return self.refresh(current) self.view.activateWindow() def section_resized(self, logicalIndex, oldSize, newSize): self.save_state() def save_state(self): if self.is_closed: return self.books_table_column_widths = [] for c in range(0, self.books_table.columnCount()): self.books_table_column_widths.append(self.books_table.columnWidth(c)) gprefs['quickview_dialog_books_table_widths'] = self.books_table_column_widths if not self.is_pane: gprefs['quickview_dialog_geometry'] = bytearray(self.saveGeometry()) def _close(self): self.save_state() # clean up to prevent memory leaks self.db = self.view = self.gui = None self.is_closed = True def close_button_clicked(self): self.closed_by_button = True self.quickview_closed.emit() def reject(self): if not self.closed_by_button: self.close_button_clicked() else: self._reject() def _reject(self): if self.is_pane: self.gui.quickview_splitter.hide_quickview_widget() self.gui.library_view.setFocus(Qt.ActiveWindowFocusReason) self._close() QDialog.reject(self)
class ConfigWidget(ConfigWidgetBase, Ui_Form): def genesis(self, gui): self.gui = gui self.delegate = Delegate(self.tweaks_view) self.tweaks_view.setItemDelegate(self.delegate) self.tweaks_view.currentChanged = self.current_changed self.view = self.tweaks_view self.highlighter = PythonHighlighter(self.edit_tweak.document()) self.restore_default_button.clicked.connect(self.restore_to_default) self.apply_button.clicked.connect(self.apply_tweak) self.plugin_tweaks_button.clicked.connect(self.plugin_tweaks) self.splitter.setStretchFactor(0, 1) self.splitter.setStretchFactor(1, 100) self.next_button.clicked.connect(self.find_next) self.previous_button.clicked.connect(self.find_previous) self.search.initialize('tweaks_search_history', help_text=_('Search for tweak')) self.search.search.connect(self.find) self.view.setContextMenuPolicy(Qt.CustomContextMenu) self.view.customContextMenuRequested.connect(self.show_context_menu) self.copy_icon = QIcon(I('edit-copy.png')) def show_context_menu(self, point): idx = self.tweaks_view.currentIndex() if not idx.isValid(): return True tweak = self.tweaks.data(idx, Qt.UserRole) self.context_menu = QMenu(self) self.context_menu.addAction(self.copy_icon, _('Copy to clipboard'), partial(self.copy_item_to_clipboard, val=u"%s (%s: %s)"%(tweak.name, _('ID'), tweak.var_names[0]))) self.context_menu.popup(self.mapToGlobal(point)) return True def copy_item_to_clipboard(self, val): cb = QApplication.clipboard() cb.clear() cb.setText(val) def plugin_tweaks(self): raw = self.tweaks.plugin_tweaks_string d = PluginTweaks(raw, self) if d.exec_() == d.Accepted: g, l = {}, {} try: exec(unicode(d.edit.toPlainText()), g, l) except: import traceback return error_dialog(self, _('Failed'), _('There was a syntax error in your tweak. Click ' 'the show details button for details.'), show=True, det_msg=traceback.format_exc()) self.tweaks.set_plugin_tweaks(l) self.changed() def current_changed(self, current, previous): self.tweaks_view.scrollTo(current) tweak = self.tweaks.data(current, Qt.UserRole) self.help.setPlainText(tweak.doc) self.edit_tweak.setPlainText(tweak.edit_text) def changed(self, *args): self.changed_signal.emit() def initialize(self): self.tweaks = self._model = Tweaks() self.tweaks_view.setModel(self.tweaks) def restore_to_default(self, *args): idx = self.tweaks_view.currentIndex() if idx.isValid(): self.tweaks.restore_to_default(idx) tweak = self.tweaks.data(idx, Qt.UserRole) self.edit_tweak.setPlainText(tweak.edit_text) self.changed() def restore_defaults(self): ConfigWidgetBase.restore_defaults(self) self.tweaks.restore_to_defaults() self.changed() def apply_tweak(self): idx = self.tweaks_view.currentIndex() if idx.isValid(): l, g = {}, {} try: exec(unicode(self.edit_tweak.toPlainText()), g, l) except: import traceback error_dialog(self.gui, _('Failed'), _('There was a syntax error in your tweak. Click ' 'the show details button for details.'), det_msg=traceback.format_exc(), show=True) return self.tweaks.update_tweak(idx, l) self.changed() def commit(self): raw = self.tweaks.to_string() try: exec(raw) except: import traceback error_dialog(self, _('Invalid tweaks'), _('The tweaks you entered are invalid, try resetting the' ' tweaks to default and changing them one by one until' ' you find the invalid setting.'), det_msg=traceback.format_exc(), show=True) raise AbortCommit('abort') write_tweaks(raw) ConfigWidgetBase.commit(self) return True def find(self, query): if not query: return try: idx = self._model.find(query) except ParseException: self.search.search_done(False) return self.search.search_done(True) if not idx.isValid(): info_dialog(self, _('No matches'), _('Could not find any shortcuts matching %s')%query, show=True, show_copy_button=False) return self.highlight_index(idx) def highlight_index(self, idx): if not idx.isValid(): return self.view.scrollTo(idx) self.view.selectionModel().select(idx, self.view.selectionModel().ClearAndSelect) self.view.setCurrentIndex(idx) def find_next(self, *args): idx = self.view.currentIndex() if not idx.isValid(): idx = self._model.index(0) idx = self._model.find_next(idx, unicode(self.search.currentText())) self.highlight_index(idx) def find_previous(self, *args): idx = self.view.currentIndex() if not idx.isValid(): idx = self._model.index(0) idx = self._model.find_next(idx, unicode(self.search.currentText()), backwards=True) self.highlight_index(idx)
class SearchRestrictionMixin(object): no_restriction = _('<None>') def __init__(self, *args, **kwargs): pass def init_search_restirction_mixin(self): self.checked = QIcon(I('ok.png')) self.empty = QIcon(I('blank.png')) self.current_search_action = QAction(self.empty, _('*current search'), self) self.current_search_action.triggered.connect(partial(self.apply_virtual_library, library='*')) self.addAction(self.current_search_action) self.keyboard.register_shortcut( 'vl-from-current-search', _('Virtual library from current search'), description=_( 'Create a temporary Virtual library from the current search'), group=_('Miscellaneous'), default_keys=('Ctrl+*',), action=self.current_search_action) self.search_based_vl_name = None self.search_based_vl = None self.virtual_library_menu = QMenu() self.virtual_library.clicked.connect(self.virtual_library_clicked) self.clear_vl.clicked.connect(lambda x: (self.apply_virtual_library(), self.clear_additional_restriction())) self.virtual_library_tooltip = \ _('Use a "virtual library" to show only a subset of the books present in this library') self.virtual_library.setToolTip(self.virtual_library_tooltip) self.search_restriction = ComboBoxWithHelp(self) self.search_restriction.setVisible(False) self.search_count.setText(_("(all books)")) self.ar_menu = QMenu(_('Additional restriction')) self.edit_menu = QMenu(_('Edit Virtual Library')) self.rm_menu = QMenu(_('Remove Virtual Library')) self.search_restriction_list_built = False def add_virtual_library(self, db, name, search): virt_libs = db.prefs.get('virtual_libraries', {}) virt_libs[name] = search db.prefs.set('virtual_libraries', virt_libs) def do_create_edit(self, name=None): db = self.library_view.model().db virt_libs = db.prefs.get('virtual_libraries', {}) cd = CreateVirtualLibrary(self, virt_libs.keys(), editing=name) if cd.exec_() == cd.Accepted: if name: self._remove_vl(name, reapply=False) self.add_virtual_library(db, cd.library_name, cd.library_search) if not name or name == db.data.get_base_restriction_name(): self.apply_virtual_library(cd.library_name) self.rebuild_vl_tabs() def virtual_library_clicked(self): m = self.virtual_library_menu m.clear() a = m.addAction(_('Create Virtual Library')) a.triggered.connect(partial(self.do_create_edit, name=None)) a = self.edit_menu self.build_virtual_library_list(a, self.do_create_edit) m.addMenu(a) a = self.rm_menu self.build_virtual_library_list(a, self.remove_vl_triggered) m.addMenu(a) if gprefs['show_vl_tabs']: m.addAction(_('Hide virtual library tabs'), self.vl_tabs.disable_bar) else: m.addAction(_('Show virtual libraries as tabs'), self.vl_tabs.enable_bar) m.addSeparator() db = self.library_view.model().db a = self.ar_menu a.clear() a.setIcon(self.checked if db.data.get_search_restriction_name() else self.empty) self.build_search_restriction_list() m.addMenu(a) m.addSeparator() current_lib = db.data.get_base_restriction_name() if current_lib == '': a = m.addAction(self.checked, self.no_restriction) else: a = m.addAction(self.empty, self.no_restriction) a.triggered.connect(partial(self.apply_virtual_library, library='')) a = m.addAction(self.current_search_action) if self.search_based_vl_name: a = m.addAction( self.checked if db.data.get_base_restriction_name().startswith('*') else self.empty, self.search_based_vl_name) a.triggered.connect(partial(self.apply_virtual_library, library=self.search_based_vl_name)) m.addSeparator() virt_libs = db.prefs.get('virtual_libraries', {}) for vl in sorted(virt_libs.keys(), key=sort_key): a = m.addAction(self.checked if vl == current_lib else self.empty, vl.replace('&', '&&')) a.triggered.connect(partial(self.apply_virtual_library, library=vl)) p = QPoint(0, self.virtual_library.height()) self.virtual_library_menu.popup(self.virtual_library.mapToGlobal(p)) def rebuild_vl_tabs(self): self.vl_tabs.rebuild() def apply_virtual_library(self, library=None, update_tabs=True): db = self.library_view.model().db virt_libs = db.prefs.get('virtual_libraries', {}) if not library: db.data.set_base_restriction('') db.data.set_base_restriction_name('') elif library == '*': if not self.search.current_text: error_dialog(self, _('No search'), _('There is no current search to use'), show=True) return txt = _build_full_search_string(self) try: db.data.search_getting_ids('', txt, use_virtual_library=False) except ParseException as e: error_dialog(self, _('Invalid search'), _('The search in the search box is not valid'), det_msg=e.msg, show=True) return self.search_based_vl = txt db.data.set_base_restriction(txt) self.search_based_vl_name = self._trim_restriction_name('*' + txt) db.data.set_base_restriction_name(self.search_based_vl_name) elif library == self.search_based_vl_name: db.data.set_base_restriction(self.search_based_vl) db.data.set_base_restriction_name(self.search_based_vl_name) elif library in virt_libs: db.data.set_base_restriction(virt_libs[library]) db.data.set_base_restriction_name(library) self.virtual_library.setToolTip(self.virtual_library_tooltip + '\n' + db.data.get_base_restriction()) self._apply_search_restriction(db.data.get_search_restriction(), db.data.get_search_restriction_name()) if update_tabs: self.vl_tabs.update_current() def build_virtual_library_list(self, menu, handler): db = self.library_view.model().db virt_libs = db.prefs.get('virtual_libraries', {}) menu.clear() menu.setIcon(self.empty) def add_action(name, search): a = menu.addAction(name.replace('&', '&&')) a.triggered.connect(partial(handler, name=name)) a.setIcon(self.empty) libs = sorted(virt_libs.keys(), key=sort_key) if libs: menu.setEnabled(True) for n in libs: add_action(n, virt_libs[n]) else: menu.setEnabled(False) def remove_vl_triggered(self, name=None): if not confirm( _('Are you sure you want to remove the virtual library <b>{0}</b>?').format(name), 'confirm_vl_removal', parent=self): return self._remove_vl(name, reapply=True) def _remove_vl(self, name, reapply=True): db = self.library_view.model().db virt_libs = db.prefs.get('virtual_libraries', {}) virt_libs.pop(name, None) db.prefs.set('virtual_libraries', virt_libs) if reapply and db.data.get_base_restriction_name() == name: self.apply_virtual_library('') self.rebuild_vl_tabs() def _trim_restriction_name(self, name): return name[0:MAX_VIRTUAL_LIBRARY_NAME_LENGTH].strip() def build_search_restriction_list(self): self.search_restriction_list_built = True from calibre.gui2.ui import get_gui m = self.ar_menu m.clear() current_restriction_text = None if self.search_restriction.count() > 1: txt = unicode(self.search_restriction.itemText(2)) if txt.startswith('*'): current_restriction_text = txt self.search_restriction.clear() current_restriction = self.library_view.model().db.data.get_search_restriction_name() m.setIcon(self.checked if current_restriction else self.empty) def add_action(txt, index): self.search_restriction.addItem(txt) txt = self._trim_restriction_name(txt) if txt == current_restriction: a = m.addAction(self.checked, txt if txt else self.no_restriction) else: a = m.addAction(self.empty, txt if txt else self.no_restriction) a.triggered.connect(partial(self.search_restriction_triggered, action=a, index=index)) add_action('', 0) add_action(_('*current search'), 1) dex = 2 if current_restriction_text: add_action(current_restriction_text, 2) dex += 1 for n in sorted(get_gui().current_db.saved_search_names(), key=sort_key): add_action(n, dex) dex += 1 def search_restriction_triggered(self, action=None, index=None): self.search_restriction.setCurrentIndex(index) self.apply_search_restriction(index) def apply_named_search_restriction(self, name): if not self.search_restriction_list_built: self.build_search_restriction_list() if not name: r = 0 else: r = self.search_restriction.findText(name) if r < 0: r = 0 self.search_restriction.setCurrentIndex(r) self.apply_search_restriction(r) def apply_text_search_restriction(self, search): if not self.search_restriction_list_built: self.build_search_restriction_list() search = unicode(search) if not search: self.search_restriction.setCurrentIndex(0) self._apply_search_restriction('', '') else: s = '*' + search if self.search_restriction.count() > 1: txt = unicode(self.search_restriction.itemText(2)) if txt.startswith('*'): self.search_restriction.setItemText(2, s) else: self.search_restriction.insertItem(2, s) else: self.search_restriction.insertItem(2, s) self.search_restriction.setCurrentIndex(2) self._apply_search_restriction(search, self._trim_restriction_name(s)) def apply_search_restriction(self, i): if not self.search_restriction_list_built: self.build_search_restriction_list() if i == 1: self.apply_text_search_restriction(unicode(self.search.currentText())) elif i == 2 and unicode(self.search_restriction.currentText()).startswith('*'): self.apply_text_search_restriction( unicode(self.search_restriction.currentText())[1:]) else: r = unicode(self.search_restriction.currentText()) if r is not None and r != '': restriction = 'search:"%s"'%(r) else: restriction = '' self._apply_search_restriction(restriction, r) def clear_additional_restriction(self): self._apply_search_restriction('', '') def _apply_search_restriction(self, restriction, name): self.saved_search.clear() # The order below is important. Set the restriction, force a '' search # to apply it, reset the tag browser to take it into account, then set # the book count. self.library_view.model().db.data.set_search_restriction(restriction) self.library_view.model().db.data.set_search_restriction_name(name) self.search.clear(emit_search=True) self.tags_view.recount() self.set_number_of_books_shown() self.current_view().setFocus(Qt.OtherFocusReason) self.set_window_title() v = self.current_view() if not v.currentIndex().isValid(): v.set_current_row() if not v.refresh_book_details(): self.book_details.reset_info() def set_number_of_books_shown(self): db = self.library_view.model().db if self.current_view() == self.library_view and db is not None and \ db.data.search_restriction_applied(): restrictions = [x for x in (db.data.get_base_restriction_name(), db.data.get_search_restriction_name()) if x] t = ' :: '.join(restrictions) if len(t) > 20: t = t[:19] + u'…' self.search_count.setStyleSheet( 'QLabel { border-radius: 6px; background-color: %s }' % tweaks['highlight_virtual_library']) self.clear_vl.setVisible(True) self.search_count.setVisible(not gprefs['show_vl_tabs']) else: # No restriction or not library view t = '' self.search_count.setStyleSheet( 'QLabel { background-color: transparent; }') self.clear_vl.setVisible(False) self.search_count.setVisible(False) self.search_count.setText(t)