diff --git a/src/calibre/gui2/init.py b/src/calibre/gui2/init.py index 6687197bf3..f648cf6066 100644 --- a/src/calibre/gui2/init.py +++ b/src/calibre/gui2/init.py @@ -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', diff --git a/src/calibre/gui2/layout.py b/src/calibre/gui2/layout.py index 1334da9465..460a3d6062 100644 --- a/src/calibre/gui2/layout.py +++ b/src/calibre/gui2/layout.py @@ -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() diff --git a/src/calibre/gui2/library/bookshelf_view.py b/src/calibre/gui2/library/bookshelf_view.py index aaa4272bc7..2ef4974af6 100644 --- a/src/calibre/gui2/library/bookshelf_view.py +++ b/src/calibre/gui2/library/bookshelf_view.py @@ -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.'''