Save single format to disk: Only show the format available in the selected books. Fixes #1007287 (Save single format to disc menu)

This commit is contained in:
Kovid Goyal 2012-06-01 17:37:13 +05:30
parent b689b6facd
commit 2fe4e54ce4
3 changed files with 24 additions and 26 deletions

View File

@ -116,7 +116,7 @@ class DeleteAction(InterfaceAction):
for action in list(self.delete_menu.actions())[1:]:
action.setEnabled(enabled)
def _get_selected_formats(self, msg, ids, exclude=False):
def _get_selected_formats(self, msg, ids, exclude=False, single=False):
from calibre.gui2.dialogs.select_formats import SelectFormats
c = Counter()
db = self.gui.library_view.model().db
@ -125,7 +125,8 @@ class DeleteAction(InterfaceAction):
if fmts_:
for x in frozenset([x.lower() for x in fmts_.split(',')]):
c[x] += 1
d = SelectFormats(c, msg, parent=self.gui, exclude=exclude)
d = SelectFormats(c, msg, parent=self.gui, exclude=exclude,
single=single)
if d.exec_() != d.Accepted:
return None
return d.selected_formats

View File

@ -8,30 +8,11 @@ __docformat__ = 'restructuredtext en'
import os
from functools import partial
from PyQt4.Qt import QMenu, pyqtSignal
from calibre.utils.config import prefs
from calibre.gui2 import (error_dialog, Dispatcher, gprefs,
choose_dir, warning_dialog, open_local_file)
from calibre.gui2.actions import InterfaceAction
from calibre.ebooks import BOOK_EXTENSIONS
class SaveMenu(QMenu): # {{{
save_fmt = pyqtSignal(object)
def __init__(self, parent):
QMenu.__init__(self, _('Save single format to disk...'), parent)
for ext in sorted(BOOK_EXTENSIONS):
action = self.addAction(ext.upper())
setattr(self, 'do_'+ext, partial(self.do, ext))
action.triggered.connect(
getattr(self, 'do_'+ext))
def do(self, ext, *args):
self.save_fmt.emit(ext)
# }}}
class SaveToDiskAction(InterfaceAction):
@ -54,9 +35,8 @@ class SaveToDiskAction(InterfaceAction):
_('Save only %s format to disk in a single directory')%
prefs['output_format'].upper(),
triggered=partial(self.save_single_fmt_to_single_dir, False))
self.save_sub_menu = SaveMenu(self.gui)
self.save_sub_menu_action = self.save_menu.addMenu(self.save_sub_menu)
self.save_sub_menu.save_fmt.connect(self.save_specific_format_disk)
cm('specific format', _('Save single format to disk...'),
triggered=self.save_specific_format_disk)
def location_selected(self, loc):
enabled = loc == 'library'
@ -74,8 +54,17 @@ class SaveToDiskAction(InterfaceAction):
def save_single_format_to_disk(self, checked):
self.save_to_disk(checked, False, prefs['output_format'])
def save_specific_format_disk(self, fmt):
self.save_to_disk(False, False, fmt)
def save_specific_format_disk(self):
rb = self.gui.iactions['Remove Books']
ids = rb._get_selected_ids(err_title=
_('Cannot save to disk'))
if not ids: return
fmts = rb._get_selected_formats(
_('Choose format to save to disk'), ids,
single=True)
if not fmts:
return
self.save_to_disk(False, False, list(fmts)[0])
def save_to_single_dir(self, checked):
self.save_to_disk(checked, True)

View File

@ -50,6 +50,7 @@ class SelectFormats(QDialog):
def __init__(self, fmt_count, msg, single=False, parent=None, exclude=False):
QDialog.__init__(self, parent)
self._l = QVBoxLayout(self)
self.single_fmt = single
self.setLayout(self._l)
self.setWindowTitle(_('Choose formats'))
self._m = QLabel(msg)
@ -57,6 +58,8 @@ class SelectFormats(QDialog):
self._l.addWidget(self._m)
self.formats = Formats(fmt_count)
self.fview = QListView(self)
self.fview.doubleClicked.connect(self.double_clicked,
type=Qt.QueuedConnection)
if exclude:
self.fview.setStyleSheet('''
QListView { background-color: #FAE7B5}
@ -82,6 +85,11 @@ class SelectFormats(QDialog):
self.selected_formats.add(self.formats.fmt(idx))
QDialog.accept(self, *args)
def double_clicked(self, index):
if self.single_fmt:
self.accept()
if __name__ == '__main__':
from PyQt4.Qt import QApplication
app = QApplication([])