Document the API for plugins to add actions to the tag browser context menu

This commit is contained in:
Kovid Goyal 2021-07-18 06:27:13 +05:30
parent 88bbc4fa2d
commit 5f78eaaea5
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 17 additions and 9 deletions

View File

@ -39,8 +39,8 @@ class InterfaceAction(QObject):
priority takes precedence.
Sub-classes should implement the :meth:`genesis`, :meth:`library_changed`,
:meth:`location_selected` :meth:`shutting_down`
and :meth:`initialization_complete` methods.
:meth:`location_selected`, :meth:`shutting_down`,
:meth:`initialization_complete` and :meth:`tag_browser_context_action` methods.
Once initialized, this plugin has access to the main calibre GUI via the
:attr:`gui` member. You can access other plugins by name, for example::
@ -349,6 +349,15 @@ class InterfaceAction(QObject):
'''
pass
def tag_browser_context_action(self, index):
'''
Called when displaying the context menu in the Tag browser. ``index`` is
the QModelIndex that points to the Tag browser item that was right clicked.
Test it for validitiy with index.valid() and get the underlying TagTreeItem
object with index.data(Qt.ItemDataRole.UserRole)
'''
pass
def shutting_down(self):
'''
Called once per plugin when the main GUI is in the process of shutting

View File

@ -1004,13 +1004,12 @@ class TagsView(QTreeView): # {{{
first = True
for ac in get_gui().iactions.values():
try:
if hasattr(ac, 'tag_browser_context_action'):
context_action = ac.tag_browser_context_action(index)
if context_action:
if first:
self.context_menu.addSeparator()
first = False
self.context_menu.addAction(context_action)
context_action = ac.tag_browser_context_action(index)
if context_action is not None:
if first:
self.context_menu.addSeparator()
first = False
self.context_menu.addAction(context_action)
except Exception:
import traceback
traceback.print_exc()