mirror of
https://github.com/kovidgoyal/calibre.git
synced 2026-03-12 12:51:07 -04:00
Bookshelf: Move group by context menu to button on search bar
More discoverable and does not modify the context menu which is under user control.
This commit is contained in:
parent
e8849a0ec1
commit
400a8df596
@ -247,6 +247,7 @@ class AlternateViewsButtons(LayoutButton): # {{{
|
||||
|
||||
buttons = set()
|
||||
ignore_toggles = False
|
||||
needs_group_by = False
|
||||
|
||||
def __init__(self, name: str, icon: str, label: str, view_name: str, gui: CentralContainer, shortcut=None, config_key=None):
|
||||
super().__init__(name, icon, label, gui, shortcut=shortcut)
|
||||
@ -298,6 +299,7 @@ class AlternateViewsButtons(LayoutButton): # {{{
|
||||
btn.update_state(False)
|
||||
self.gui.library_view.alternate_views.show_view(self.view_name if show else None)
|
||||
self.gui.sort_button.setVisible(show)
|
||||
self.gui.group_by_button.setVisible(self.needs_group_by and show)
|
||||
AlternateViewsButtons.ignore_toggles = False
|
||||
# }}}
|
||||
|
||||
@ -317,6 +319,7 @@ class GridViewButton(AlternateViewsButtons): # {{{
|
||||
|
||||
|
||||
class BookshelfViewButton(AlternateViewsButtons): # {{{
|
||||
needs_group_by = True
|
||||
def __init__(self, gui):
|
||||
super().__init__(
|
||||
'bookshelf_view',
|
||||
|
||||
@ -239,6 +239,19 @@ class SearchBar(QFrame): # {{{
|
||||
sb.setVisible(False)
|
||||
l.addWidget(sb)
|
||||
|
||||
parent.group_by_button = self.group_by_button = gb = QToolButton(self)
|
||||
gb.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
|
||||
gb.setToolTip(_('Change how the displayed books are grouped'))
|
||||
gb.setCursor(Qt.CursorShape.PointingHandCursor)
|
||||
gb.setPopupMode(QToolButton.ToolButtonPopupMode.InstantPopup)
|
||||
gb.setAutoRaise(True)
|
||||
gb.setText(_('Group by'))
|
||||
gb.setIcon(QIcon.ic('bookshelf.png'))
|
||||
gb.setMenu(QMenu(gb))
|
||||
gb.menu().aboutToShow.connect(self.populate_group_by_menu)
|
||||
gb.setVisible(False)
|
||||
l.addWidget(gb)
|
||||
|
||||
x = parent.search = SearchBox2(self, as_url=search_as_url)
|
||||
x.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)
|
||||
x.setObjectName('search')
|
||||
@ -292,6 +305,10 @@ class SearchBar(QFrame): # {{{
|
||||
from calibre.gui2.ui import get_gui
|
||||
get_gui().iactions['Sort By'].update_menu(self.sort_button.menu())
|
||||
|
||||
def populate_group_by_menu(self):
|
||||
from calibre.gui2.ui import get_gui
|
||||
get_gui().bookshelf_view.populate_group_by_menu(self.group_by_button.menu())
|
||||
|
||||
def do_fts(self):
|
||||
from calibre.gui2.ui import get_gui
|
||||
get_gui().iactions['Full Text Search'].show_fts()
|
||||
|
||||
@ -1997,12 +1997,8 @@ class BookshelfView(MomentumScrollMixin, QAbstractScrollArea):
|
||||
def set_context_menu(self, menu: QMenu):
|
||||
self.context_menu = menu
|
||||
|
||||
def contextMenuEvent(self, ev: QContextMenuEvent):
|
||||
# Create menu with grouping options
|
||||
menu = QMenu(self)
|
||||
|
||||
# Add grouping submenu
|
||||
grouping_menu = menu.addMenu(QIcon.ic('bookshelf.png'), _('Group by'))
|
||||
def populate_group_by_menu(self, grouping_menu: QMenu) -> None:
|
||||
grouping_menu.clear()
|
||||
fm = self.gui.current_db.new_api.field_metadata
|
||||
|
||||
def add(field: str, name: str) -> None:
|
||||
@ -2020,14 +2016,11 @@ class BookshelfView(MomentumScrollMixin, QAbstractScrollArea):
|
||||
cf[k] = numeric_sort_key(fm[k])
|
||||
for k in sorted(cf, key=cf.get):
|
||||
add(k, fm[k]['name'])
|
||||
# Add standard context menu items if available
|
||||
if cm := self.context_menu:
|
||||
menu.addSeparator()
|
||||
for action in cm.actions():
|
||||
menu.addAction(action)
|
||||
|
||||
menu.popup(ev.globalPos())
|
||||
ev.accept()
|
||||
def contextMenuEvent(self, ev: QContextMenuEvent):
|
||||
if self.context_menu:
|
||||
self.context_menu.popup(ev.globalPos())
|
||||
ev.accept()
|
||||
|
||||
def set_grouping_mode(self, mode: str):
|
||||
'''Set the grouping mode and refresh display.'''
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user