mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Migrate toolbar and context menus to new plugin system
This commit is contained in:
parent
0ad974110a
commit
d81e4c655b
@ -649,9 +649,17 @@ class ActionChooseLibrary(InterfaceActionBase):
|
|||||||
name = 'Choose Library'
|
name = 'Choose Library'
|
||||||
actual_plugin = 'calibre.gui2.actions.choose_library:ChooseLibraryAction'
|
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,
|
plugins += [ActionAdd, ActionFetchAnnotations, ActionGenerateCatalog,
|
||||||
ActionConvert, ActionDelete, ActionEditMetadata, ActionView,
|
ActionConvert, ActionDelete, ActionEditMetadata, ActionView,
|
||||||
ActionFetchNews, ActionSaveToDisk, ActionShowBookDetails,
|
ActionFetchNews, ActionSaveToDisk, ActionShowBookDetails,
|
||||||
ActionRestart, ActionOpenFolder, ActionConnectShare,
|
ActionRestart, ActionOpenFolder, ActionConnectShare,
|
||||||
ActionSendToDevice, ActionHelp, ActionPreferences, ActionSimilarBooks]
|
ActionSendToDevice, ActionHelp, ActionPreferences, ActionSimilarBooks,
|
||||||
|
ActionAddToLibrary, ActionEditCollections]
|
||||||
|
22
src/calibre/gui2/actions/add_to_library.py
Normal file
22
src/calibre/gui2/actions/add_to_library.py
Normal 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())
|
29
src/calibre/gui2/actions/edit_collections.py
Normal file
29
src/calibre/gui2/actions/edit_collections.py
Normal 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)
|
||||||
|
|
@ -7,7 +7,7 @@ __docformat__ = 'restructuredtext en'
|
|||||||
|
|
||||||
import functools, sys, os
|
import functools, sys, os
|
||||||
|
|
||||||
from PyQt4.Qt import Qt, QStackedWidget, \
|
from PyQt4.Qt import Qt, QStackedWidget, QMenu, \
|
||||||
QSize, QSizePolicy, QStatusBar, QLabel, QFont
|
QSize, QSizePolicy, QStatusBar, QLabel, QFont
|
||||||
|
|
||||||
from calibre.utils.config import prefs
|
from calibre.utils.config import prefs
|
||||||
@ -27,47 +27,32 @@ def partial(*args, **kwargs):
|
|||||||
_keep_refs.append(ans)
|
_keep_refs.append(ans)
|
||||||
return 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): # {{{
|
class LibraryViewMixin(object): # {{{
|
||||||
|
|
||||||
def __init__(self, db):
|
def __init__(self, db):
|
||||||
|
lm = QMenu(self)
|
||||||
self.library_view.set_context_menu(self.action_edit, self.action_sync,
|
def populate_menu(m, items):
|
||||||
self.action_convert, self.action_view,
|
for what in items:
|
||||||
self.action_save,
|
if what is None:
|
||||||
self.action_open_containing_folder,
|
lm.addSeparator()
|
||||||
self.action_show_book_details,
|
elif what in self.iactions:
|
||||||
self.action_del,
|
lm.addAction(self.iactions[what].qaction)
|
||||||
self.action_conn_share,
|
populate_menu(lm, LIBRARY_CONTEXT_MENU)
|
||||||
add_to_library = None,
|
dm = QMenu(self)
|
||||||
edit_device_collections=None,
|
populate_menu(dm, DEVICE_CONTEXT_MENU)
|
||||||
similar_menu=similar_menu)
|
self.library_view.set_context_menu(lm)
|
||||||
add_to_library = (_('Add books to library'),
|
for v in (self.memory_view, self.card_a_view, self.card_b_view):
|
||||||
self.iactions['Add Books'].add_books_from_device)
|
v.set_context_menu(dm)
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
self.library_view.files_dropped.connect(self.iactions['Add Books'].files_dropped, type=Qt.QueuedConnection)
|
self.library_view.files_dropped.connect(self.iactions['Add Books'].files_dropped, type=Qt.QueuedConnection)
|
||||||
for func, args in [
|
for func, args in [
|
||||||
|
@ -5,7 +5,6 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
from operator import attrgetter
|
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from PyQt4.Qt import QIcon, Qt, QWidget, QToolBar, QSize, \
|
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.gui2.widgets import ComboBoxWithHelp
|
||||||
from calibre import human_readable
|
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): # {{{
|
class LocationManager(QObject): # {{{
|
||||||
|
|
||||||
@ -203,6 +213,7 @@ class ToolBar(QToolBar): # {{{
|
|||||||
|
|
||||||
def __init__(self, donate, location_manager, parent):
|
def __init__(self, donate, location_manager, parent):
|
||||||
QToolBar.__init__(self, parent)
|
QToolBar.__init__(self, parent)
|
||||||
|
self.gui = parent
|
||||||
self.setContextMenuPolicy(Qt.PreventContextMenu)
|
self.setContextMenuPolicy(Qt.PreventContextMenu)
|
||||||
self.setMovable(False)
|
self.setMovable(False)
|
||||||
self.setFloatable(False)
|
self.setFloatable(False)
|
||||||
@ -237,46 +248,30 @@ class ToolBar(QToolBar): # {{{
|
|||||||
|
|
||||||
def build_bar(self):
|
def build_bar(self):
|
||||||
showing_device = self.location_manager.has_device
|
showing_device = self.location_manager.has_device
|
||||||
order_field = 'device' if showing_device else 'normal'
|
actions = TOOLBAR_DEVICE if showing_device else TOOLBAR_NO_DEVICE
|
||||||
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)))
|
|
||||||
self.clear()
|
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 = self.widgetForAction(ac)
|
||||||
ch.setCursor(Qt.PointingHandCursor)
|
ch.setCursor(Qt.PointingHandCursor)
|
||||||
ch.setAutoRaise(True)
|
ch.setAutoRaise(True)
|
||||||
if ac.menu() is not None:
|
if ac.menu() is not None and menu_mode is not None:
|
||||||
name = getattr(ac, 'action_name', None)
|
ch.setPopupMode(menu_mode)
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
def resizeEvent(self, ev):
|
def resizeEvent(self, ev):
|
||||||
QToolBar.resizeEvent(self, ev)
|
QToolBar.resizeEvent(self, ev)
|
||||||
|
@ -389,37 +389,9 @@ class BooksView(QTableView): # {{{
|
|||||||
#}}}
|
#}}}
|
||||||
|
|
||||||
# Context Menu {{{
|
# Context Menu {{{
|
||||||
def set_context_menu(self, edit_metadata, send_to_device, convert, view,
|
def set_context_menu(self, menu):
|
||||||
save, open_folder, book_details, delete, conn_share,
|
|
||||||
similar_menu=None, add_to_library=None,
|
|
||||||
edit_device_collections=None):
|
|
||||||
self.setContextMenuPolicy(Qt.DefaultContextMenu)
|
self.setContextMenuPolicy(Qt.DefaultContextMenu)
|
||||||
self.context_menu = QMenu(self)
|
self.context_menu = menu
|
||||||
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)
|
|
||||||
|
|
||||||
def contextMenuEvent(self, event):
|
def contextMenuEvent(self, event):
|
||||||
self.context_menu.popup(event.globalPos())
|
self.context_menu.popup(event.globalPos())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user