From a64c1623ae720c81586f29242e91102470367f74 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Sun, 24 Apr 2022 11:46:17 +0100 Subject: [PATCH] Set & clear marks on the row label context menu --- src/calibre/gui2/actions/mark_books.py | 24 +++++++++++++++++++++--- src/calibre/gui2/library/views.py | 24 +++++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/calibre/gui2/actions/mark_books.py b/src/calibre/gui2/actions/mark_books.py index d804127bde..5c81b5eb9b 100644 --- a/src/calibre/gui2/actions/mark_books.py +++ b/src/calibre/gui2/actions/mark_books.py @@ -76,6 +76,9 @@ class MarkWithTextDialog(QDialog): super().accept() +mark_books_with_text = None + + class MarkBooksAction(InterfaceAction): name = 'Mark Books' @@ -119,12 +122,16 @@ class MarkBooksAction(InterfaceAction): m.aboutToShow.connect(self.about_to_show_menu) ma = partial(self.create_menu_action, m) self.show_marked_action = a = ma('mark_with_text', _('Mark books with text label'), icon='marked.png') - a.triggered.connect(self.mark_with_text) + a.triggered.connect(partial(self.mark_with_text, book_ids = None)) + global mark_books_with_text + mark_books_with_text = self.mark_with_text self.show_marked_action = a = ma('show-marked', _('Show marked books'), icon='search.png', shortcut='Shift+Ctrl+M') a.triggered.connect(self.show_marked) self.show_marked_with_text = QMenu(_('Show marked books with text label')) self.show_marked_with_text.setIcon(self.search_icon) m.addMenu(self.show_marked_with_text) + self.clear_selected_marked_action = a = ma('clear-marks-on-selected', _('Clear marks for selected books'), icon='clear_left.png') + a.triggered.connect(self.clear_marks_on_selected_books) self.clear_marked_action = a = ma('clear-all-marked', _('Clear all marked books'), icon='clear_left.png') a.triggered.connect(self.clear_all_marked) m.addSeparator() @@ -226,8 +233,9 @@ class MarkBooksAction(InterfaceAction): mids.pop(book_id, None) db.data.set_marked_ids(mids) - def mark_with_text(self): - book_ids = self._get_selected_ids() + def mark_with_text(self, book_ids=None): + if book_ids is None: + book_ids = self._get_selected_ids() if not book_ids: return dialog = MarkWithTextDialog(self.gui) @@ -240,3 +248,13 @@ class MarkBooksAction(InterfaceAction): for book_id in book_ids: mids[book_id] = txt db.data.set_marked_ids(mids) + + def clear_marks_on_selected_books(self): + book_ids = self._get_selected_ids() + if not book_ids: + return + db = self.gui.current_db + items = db.data.marked_ids.copy() + for book_id in book_ids: + items.pop(book_id, None) + self.gui.current_db.data.set_marked_ids(items) diff --git a/src/calibre/gui2/library/views.py b/src/calibre/gui2/library/views.py index c1dd1cd2ee..ccf059abad 100644 --- a/src/calibre/gui2/library/views.py +++ b/src/calibre/gui2/library/views.py @@ -516,11 +516,29 @@ class BooksView(QTableView): # {{{ def show_row_header_context_menu(self, pos): menu = QMenu(self) - menu.addAction(_('Hide row numbers'), self.hide_row_numbers) + # Even when hidden, row numbers show if any marks show, which is why it makes + # sense to offer "show row numbers" here. Saves having to go to Preferences + # Look & feel, assuming you know the trick. + if gprefs['row_numbers_in_book_list']: + menu.addAction(_('Hide row numbers'), partial(self.hide_row_numbers, show=False)) + else: + menu.addAction(_('Show row numbers'), partial(self.hide_row_numbers, show=True)) + db = self._model.db + row = self.row_header.logicalIndexAt(pos) + if row >= 0 and row < len(db.data): + book_id_col = db.field_metadata['id']['rec_index'] + book_id = db.data[row][book_id_col] + m = menu.addAction(_('Toggle mark for book'), lambda: db.data.toggle_marked_ids({book_id,})) + ic = QIcon.ic('marked.png') + m.setIcon(ic) + from calibre.gui2.actions.mark_books import mark_books_with_text + m = menu.addAction(_('Mark book with text label'), partial(mark_books_with_text, {book_id,})) + m.setIcon(ic) menu.popup(self.mapToGlobal(pos)) - def hide_row_numbers(self): - gprefs['row_numbers_in_book_list'] = False + # Probably should change the method name, but leave it for compatibility + def hide_row_numbers(self, show=False): + gprefs['row_numbers_in_book_list'] = show self.set_row_header_visibility() def show_column_header_context_menu(self, pos, view=None):