Allow multi-select in viewer highlights panel

Useful to delete more than one highlight at a time
This commit is contained in:
Kovid Goyal 2020-07-28 10:05:13 +05:30
parent ebb83d97b8
commit 4fb62e9565
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -100,6 +100,7 @@ class Highlights(QListWidget):
def __init__(self, parent=None): def __init__(self, parent=None):
QListWidget.__init__(self, parent) QListWidget.__init__(self, parent)
self.setSelectionMode(self.ExtendedSelection)
self.setSpacing(2) self.setSpacing(2)
pi = plugins['progress_indicator'][0] pi = plugins['progress_indicator'][0]
pi.set_no_activate_on_click(self) pi.set_no_activate_on_click(self)
@ -177,6 +178,11 @@ class Highlights(QListWidget):
item = self.item(i) item = self.item(i)
yield item.data(Qt.UserRole) yield item.data(Qt.UserRole)
@property
def selected_highlights(self):
for item in self.selectedItems():
yield item.data(Qt.UserRole)
def keyPressEvent(self, ev): def keyPressEvent(self, ev):
if ev.matches(QKeySequence.Delete): if ev.matches(QKeySequence.Delete):
self.delete_requested.emit() self.delete_requested.emit()
@ -281,7 +287,7 @@ class HighlightsPanel(QWidget):
self.add_button = button('plus.png', _('Add'), _('Create a new highlight'), self.add_highlight) self.add_button = button('plus.png', _('Add'), _('Create a new highlight'), self.add_highlight)
self.edit_button = button('edit_input.png', _('Edit'), _('Edit the selected highlight'), self.edit_highlight) self.edit_button = button('edit_input.png', _('Edit'), _('Edit the selected highlight'), self.edit_highlight)
self.remove_button = button('trash.png', _('Remove'), _('Remove the selected highlight'), self.remove_highlight) self.remove_button = button('trash.png', _('Remove'), _('Remove the selected highlights'), self.remove_highlight)
h.addWidget(self.add_button), h.addWidget(self.edit_button), h.addWidget(self.remove_button) h.addWidget(self.add_button), h.addWidget(self.edit_button), h.addWidget(self.remove_button)
self.export_button = button('save.png', _('Export'), _('Export all highlights'), self.export) self.export_button = button('save.png', _('Export'), _('Export all highlights'), self.export)
@ -339,13 +345,16 @@ class HighlightsPanel(QWidget):
self.request_highlight_action.emit(h['uuid'], 'edit') self.request_highlight_action.emit(h['uuid'], 'edit')
def remove_highlight(self): def remove_highlight(self):
h = self.highlights.current_highlight highlights = tuple(self.highlights.selected_highlights)
if h is None: if not highlights:
return self.no_selected_highlight() return self.no_selected_highlight()
if question_dialog(self, _('Are you sure?'), _( if question_dialog(self, _('Are you sure?'), ngettext(
'Are you sure you want to delete this highlight permanently?') 'Are you sure you want to delete this highlight permanently?',
'Are you sure you want to delete all {} highlights permanently?',
len(highlights)).format(len(highlights))
): ):
self.request_highlight_action.emit(h['uuid'], 'delete') for h in highlights:
self.request_highlight_action.emit(h['uuid'], 'delete')
def add_highlight(self): def add_highlight(self):
self.request_highlight_action.emit(None, 'create') self.request_highlight_action.emit(None, 'create')