diff --git a/src/calibre/gui2/tweak_book/__init__.py b/src/calibre/gui2/tweak_book/__init__.py index 8c78d907ab..c286bed43a 100644 --- a/src/calibre/gui2/tweak_book/__init__.py +++ b/src/calibre/gui2/tweak_book/__init__.py @@ -58,6 +58,7 @@ d['editor_html_toolbar'] = ['fix-html-current', 'pretty-current', 'insert-image' d['editor_format_toolbar'] = [('format-text-' + x) for x in ( 'bold', 'italic', 'underline', 'strikethrough', 'subscript', 'superscript', 'color', 'background-color')] d['spell_check_case_sensitive_search'] = False +d['add_cover_preserve_aspect_ratio'] = False del d ucase_map = {l:string.ascii_uppercase[i] for i, l in enumerate(string.ascii_lowercase)} diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index b5f9b6dbc0..31984eb962 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -42,7 +42,7 @@ from calibre.gui2.tweak_book.search import validate_search_request, run_search from calibre.gui2.tweak_book.spell import find_next as find_next_word, find_next_error from calibre.gui2.tweak_book.widgets import ( RationalizeFolders, MultiSplit, ImportForeign, QuickOpen, InsertLink, - InsertSemantics, BusyCursor, InsertTag, FilterCSS) + InsertSemantics, BusyCursor, InsertTag, FilterCSS, AddCover) _diff_dialogs = [] @@ -353,27 +353,29 @@ class Boss(QObject): d = NewFileDialog(self.gui) if d.exec_() != d.Accepted: return - self.add_savepoint(_('Before: Add file %s') % self.gui.elided_text(d.file_name)) + self.do_add_file(d.file_name, d.file_data, using_template=d.using_template, edit_file=True) + + def do_add_file(self, file_name, data, using_template=False, edit_file=False): + self.add_savepoint(_('Before: Add file %s') % self.gui.elided_text(file_name)) c = current_container() - data = d.file_data - if d.using_template: + if using_template: data = data.replace(b'%CURSOR%', b'') try: - c.add_file(d.file_name, data) + c.add_file(file_name, data) except: self.rewind_savepoint() raise self.gui.file_list.build(c) - self.gui.file_list.select_name(d.file_name) + self.gui.file_list.select_name(file_name) if c.opf_name in editors: editors[c.opf_name].replace_data(c.raw_data(c.opf_name)) - mt = c.mime_map[d.file_name] - syntax = syntax_from_mime(d.file_name, mt) - if syntax: - if d.using_template: - self.edit_file(d.file_name, syntax, use_template=d.file_data.decode('utf-8')) + mt = c.mime_map[file_name] + syntax = syntax_from_mime(file_name, mt) + if syntax and edit_file: + if using_template: + self.edit_file(file_name, syntax, use_template=data.decode('utf-8')) else: - self.edit_file(d.file_name, syntax) + self.edit_file(file_name, syntax) self.set_modified() def add_files(self): @@ -406,6 +408,15 @@ class Boss(QObject): editors[c.opf_name].replace_data(c.raw_data(c.opf_name)) self.set_modified() + def add_cover(self): + d = AddCover(current_container(), self.gui) + d.import_requested.connect(self.do_add_file) + try: + if d.exec_() == d.Accepted: + pass + finally: + d.import_requested.disconnect() + def edit_toc(self): if current_container() is None: return error_dialog(self.gui, _('No book opened'), _( diff --git a/src/calibre/gui2/tweak_book/file_list.py b/src/calibre/gui2/tweak_book/file_list.py index c27af04da1..e7ab9aced0 100644 --- a/src/calibre/gui2/tweak_book/file_list.py +++ b/src/calibre/gui2/tweak_book/file_list.py @@ -685,7 +685,7 @@ class FileList(QTreeWidget): class NewFileDialog(QDialog): # {{{ - def __init__(self, initial_choice='html', parent=None): + def __init__(self, parent=None): QDialog.__init__(self, parent) self.l = l = QVBoxLayout() self.setLayout(l) @@ -715,6 +715,7 @@ class NewFileDialog(QDialog): # {{{ self.file_data = b'' self.using_template = False + self.setMinimumWidth(350) def show_error(self, msg): self.err_label.setText('
' + msg)
@@ -723,15 +724,19 @@ class NewFileDialog(QDialog): # {{{
def import_file(self):
path = choose_files(self, 'tweak-book-new-resource-file', _('Choose file'), select_only_single_file=True)
if path:
- path = path[0]
- with open(path, 'rb') as f:
- self.file_data = f.read()
- name = os.path.basename(path)
- fmap = get_recommended_folders(current_container(), (name,))
- if fmap[name]:
- name = '/'.join((fmap[name], name))
- self.name.setText(name)
- self.la.setText(_('Choose a name for the imported file'))
+ self.do_import_file(path[0])
+
+ def do_import_file(self, path, hide_button=False):
+ with open(path, 'rb') as f:
+ self.file_data = f.read()
+ name = os.path.basename(path)
+ fmap = get_recommended_folders(current_container(), (name,))
+ if fmap[name]:
+ name = '/'.join((fmap[name], name))
+ self.name.setText(name)
+ self.la.setText(_('Choose a name for the imported file'))
+ if hide_button:
+ self.imp_button.setVisible(False)
@property
def name_is_ok(self):
diff --git a/src/calibre/gui2/tweak_book/ui.py b/src/calibre/gui2/tweak_book/ui.py
index 0bfeda5906..232d28f99c 100644
--- a/src/calibre/gui2/tweak_book/ui.py
+++ b/src/calibre/gui2/tweak_book/ui.py
@@ -357,6 +357,7 @@ class Main(MainWindow):
self.action_filter_css = treg('filter.png', _('&Filter style information'), self.boss.filter_css, 'filter-css', (),
_('Filter style information'))
self.action_manage_fonts = treg('font.png', _('Manage &fonts'), self.boss.manage_fonts, 'manage-fonts', (), _('Manage fonts in the book'))
+ self.action_add_cover = treg('default_cover.png', _('Add &cover'), self.boss.add_cover, 'add-cover', (), _('Add a cover to the book'))
def ereg(icon, text, target, sid, keys, description):
return reg(icon, text, partial(self.boss.editor_action, target), sid, keys, description)
@@ -496,6 +497,7 @@ class Main(MainWindow):
e.addAction(self.action_fix_html_all)
e.addAction(self.action_pretty_all)
e.addAction(self.action_rationalize_folders)
+ e.addAction(self.action_add_cover)
e.addAction(self.action_set_semantics)
e.addAction(self.action_filter_css)
e.addAction(self.action_spell_check_book)
diff --git a/src/calibre/gui2/tweak_book/widgets.py b/src/calibre/gui2/tweak_book/widgets.py
index 8d2386860c..85ca0c1002 100644
--- a/src/calibre/gui2/tweak_book/widgets.py
+++ b/src/calibre/gui2/tweak_book/widgets.py
@@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3'
__copyright__ = '2014, Kovid Goyal