Migrate toolbar and context menus to new plugin system

This commit is contained in:
Kovid Goyal 2010-08-13 19:14:06 -06:00
parent 0ad974110a
commit d81e4c655b
6 changed files with 120 additions and 109 deletions

View File

@ -622,7 +622,7 @@ class ActionRestart(InterfaceActionBase):
actual_plugin = 'calibre.gui2.actions.restart:RestartAction'
class ActionOpenFolder(InterfaceActionBase):
name = 'OpenFolder'
name = 'Open Folder'
actual_plugin = 'calibre.gui2.actions.open:OpenFolderAction'
class ActionSendToDevice(InterfaceActionBase):
@ -649,9 +649,17 @@ class ActionChooseLibrary(InterfaceActionBase):
name = 'Choose Library'
actual_plugin = 'calibre.gui2.actions.choose_library:ChooseLibraryAction'
class ActionAddToLibrary(InterfaceActionBase):
name = 'Add To Library'
actual_plugin = 'calibre.gui2.actions.add_to_library:AddToLibraryAction'
class ActionEditCollections(InterfaceActionBase):
name = 'Edit Collections'
actual_plugin = 'calibre.gui2.actions.edit_collections:EditCollectionsAction'
plugins += [ActionAdd, ActionFetchAnnotations, ActionGenerateCatalog,
ActionConvert, ActionDelete, ActionEditMetadata, ActionView,
ActionFetchNews, ActionSaveToDisk, ActionShowBookDetails,
ActionRestart, ActionOpenFolder, ActionConnectShare,
ActionSendToDevice, ActionHelp, ActionPreferences, ActionSimilarBooks]
ActionSendToDevice, ActionHelp, ActionPreferences, ActionSimilarBooks,
ActionAddToLibrary, ActionEditCollections]

View File

@ -0,0 +1,22 @@
#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
__license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
from calibre.gui2.actions import InterfaceAction
class AddToLibraryAction(InterfaceAction):
name = 'Add To Library'
action_spec = (_('Add books to library'), 'add_book.svg', None, None)
def location_selected(self, loc):
enabled = loc != 'library'
self.qaction.setEnabled(enabled)
self.qaction.triggered.connect(self.add_books_to_library)
def add_books_to_library(self, *args):
self.gui.iactions['Add Books'].add_books_from_device(
self.gui.current_view())

View File

@ -0,0 +1,29 @@
#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
__license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
from calibre.gui2.actions import InterfaceAction
class EditCollectionsAction(InterfaceAction):
name = 'Edit Collections'
action_spec = (_('Manage collections'), None, None, None)
def location_selected(self, loc):
enabled = loc != 'library'
self.qaction.setEnabled(enabled)
self.qaction.triggered.connect(self.edit_collections)
def edit_collections(self, *args):
oncard = None
cv = self.gui.current_view()
if cv is self.gui.card_a_view:
oncard = 'carda'
if cv is self.gui.card_b_view:
oncard = 'cardb'
self.gui.iactions['Edit Metadata'].edit_device_collections(cv,
oncard=oncard)

View File

@ -7,7 +7,7 @@ __docformat__ = 'restructuredtext en'
import functools, sys, os
from PyQt4.Qt import Qt, QStackedWidget, \
from PyQt4.Qt import Qt, QStackedWidget, QMenu, \
QSize, QSizePolicy, QStatusBar, QLabel, QFont
from calibre.utils.config import prefs
@ -27,47 +27,32 @@ def partial(*args, **kwargs):
_keep_refs.append(ans)
return ans
LIBRARY_CONTEXT_MENU = (
'Edit Metadata', 'Send To Device', 'Save To Disk', 'Connect Share', None,
'Convert Books', 'View', 'Open Folder', 'Show Book Details', None,
'Remove Books',
)
DEVICE_CONTEXT_MENU = ('View', 'Save To Disk', None, 'Remove Books', None,
'Add To Library', 'Edit Collections',
)
class LibraryViewMixin(object): # {{{
def __init__(self, db):
self.library_view.set_context_menu(self.action_edit, self.action_sync,
self.action_convert, self.action_view,
self.action_save,
self.action_open_containing_folder,
self.action_show_book_details,
self.action_del,
self.action_conn_share,
add_to_library = None,
edit_device_collections=None,
similar_menu=similar_menu)
add_to_library = (_('Add books to library'),
self.iactions['Add Books'].add_books_from_device)
edc = self.iactions['Edit Metadata'].edit_device_collections
edit_device_collections = (_('Manage collections'),
partial(edc, oncard=None))
self.memory_view.set_context_menu(None, None, None,
self.action_view, self.action_save, None, None,
self.action_del, None,
add_to_library=add_to_library,
edit_device_collections=edit_device_collections)
edit_device_collections = (_('Manage collections'),
partial(edc, oncard='carda'))
self.card_a_view.set_context_menu(None, None, None,
self.action_view, self.action_save, None, None,
self.action_del, None,
add_to_library=add_to_library,
edit_device_collections=edit_device_collections)
edit_device_collections = (_('Manage collections'),
partial(edc, oncard='cardb'))
self.card_b_view.set_context_menu(None, None, None,
self.action_view, self.action_save, None, None,
self.action_del, None,
add_to_library=add_to_library,
edit_device_collections=edit_device_collections)
lm = QMenu(self)
def populate_menu(m, items):
for what in items:
if what is None:
lm.addSeparator()
elif what in self.iactions:
lm.addAction(self.iactions[what].qaction)
populate_menu(lm, LIBRARY_CONTEXT_MENU)
dm = QMenu(self)
populate_menu(dm, DEVICE_CONTEXT_MENU)
self.library_view.set_context_menu(lm)
for v in (self.memory_view, self.card_a_view, self.card_b_view):
v.set_context_menu(dm)
self.library_view.files_dropped.connect(self.iactions['Add Books'].files_dropped, type=Qt.QueuedConnection)
for func, args in [

View File

@ -5,7 +5,6 @@ __license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
from operator import attrgetter
from functools import partial
from PyQt4.Qt import QIcon, Qt, QWidget, QToolBar, QSize, \
@ -20,7 +19,18 @@ from calibre.gui2 import config, gprefs
from calibre.gui2.widgets import ComboBoxWithHelp
from calibre import human_readable
TOOLBAR_NO_DEVICE = (
'Add Books', 'Edit Metadata', None, 'Convert Books', 'View', None,
'Choose Library', 'Donate', None, 'Fetch News', 'Save To Disk',
'Connect Share', None, 'Remove Books', None, 'Help', 'Preferences',
)
TOOLBAR_DEVICE = (
'Add Books', 'Edit Metadata', None, 'Convert Books', 'View',
'Send To Device', None, None, 'Location Manager', None, None,
'Fetch News', 'Save To Disk', 'Connect Share', None,
'Remove Books', None, 'Help', 'Preferences',
)
class LocationManager(QObject): # {{{
@ -203,6 +213,7 @@ class ToolBar(QToolBar): # {{{
def __init__(self, donate, location_manager, parent):
QToolBar.__init__(self, parent)
self.gui = parent
self.setContextMenuPolicy(Qt.PreventContextMenu)
self.setMovable(False)
self.setFloatable(False)
@ -237,46 +248,30 @@ class ToolBar(QToolBar): # {{{
def build_bar(self):
showing_device = self.location_manager.has_device
order_field = 'device' if showing_device else 'normal'
o = attrgetter(order_field+'_order')
sepvals = [2] if showing_device else [1]
sepvals += [3]
actions = [x for x in self.all_actions if o(x) > -1]
actions.sort(cmp=lambda x,y : cmp(o(x), o(y)))
actions = TOOLBAR_DEVICE if showing_device else TOOLBAR_NO_DEVICE
self.clear()
for what in actions:
if what is None:
self.addSeparator()
elif what == 'Location Manager':
for ac in self.location_manager.available_actions:
self.addAction(ac)
self.setup_tool_button(ac, QToolButton.MenuPopup)
elif what == 'Donate' and config['show_donate_button']:
self.addWidget(self.d_widget)
elif what in self.gui.iactions:
action = self.gui.iactions[what]
self.addAction(action.qaction)
self.setup_tool_button(action.qaction, action.popup_type)
def setup_tool_button(ac):
def setup_tool_button(self, ac, menu_mode=None):
ch = self.widgetForAction(ac)
ch.setCursor(Qt.PointingHandCursor)
ch.setAutoRaise(True)
if ac.menu() is not None:
name = getattr(ac, 'action_name', None)
ch.setPopupMode(ch.InstantPopup if name == 'conn_share'
else ch.MenuButtonPopup)
for x in actions:
self.addAction(x)
setup_tool_button(x)
if x.action_name == 'choose_library':
self.choose_action = x
if showing_device:
self.addSeparator()
for ac in self.location_manager.available_actions:
self.addAction(ac)
setup_tool_button(ac)
self.addSeparator()
self.location_manager.location_library.trigger()
elif config['show_donate_button']:
self.addWidget(self.d_widget)
for x in actions:
if x.separator_before in sepvals:
self.insertSeparator(x)
self.choose_action.setVisible(not showing_device)
if ac.menu() is not None and menu_mode is not None:
ch.setPopupMode(menu_mode)
def resizeEvent(self, ev):
QToolBar.resizeEvent(self, ev)

View File

@ -389,37 +389,9 @@ class BooksView(QTableView): # {{{
#}}}
# Context Menu {{{
def set_context_menu(self, edit_metadata, send_to_device, convert, view,
save, open_folder, book_details, delete, conn_share,
similar_menu=None, add_to_library=None,
edit_device_collections=None):
def set_context_menu(self, menu):
self.setContextMenuPolicy(Qt.DefaultContextMenu)
self.context_menu = QMenu(self)
if edit_metadata is not None:
self.context_menu.addAction(edit_metadata)
if send_to_device is not None:
self.context_menu.addAction(send_to_device)
if convert is not None:
self.context_menu.addAction(convert)
if conn_share is not None:
self.context_menu.addAction(conn_share)
self.context_menu.addAction(view)
self.context_menu.addAction(save)
if open_folder is not None:
self.context_menu.addAction(open_folder)
if delete is not None:
self.context_menu.addAction(delete)
if book_details is not None:
self.context_menu.addAction(book_details)
if similar_menu is not None:
self.context_menu.addMenu(similar_menu)
if add_to_library is not None:
func = partial(add_to_library[1], view=self)
self.context_menu.addAction(add_to_library[0], func)
if edit_device_collections is not None:
func = partial(edit_device_collections[1], view=self)
self.edit_collections_menu = \
self.context_menu.addAction(edit_device_collections[0], func)
self.context_menu = menu
def contextMenuEvent(self, event):
self.context_menu.popup(event.globalPos())