Edit Book: When inserting images, add an option to insert the image as a full page image

This commit is contained in:
Kovid Goyal 2016-01-19 12:44:53 +05:30
parent b090f18d81
commit 12df3ac3c4
5 changed files with 33 additions and 12 deletions

View File

@ -71,6 +71,8 @@ d['auto_close_tags'] = True
d['restore_book_state'] = True
d['editor_accepts_drops'] = True
d['toolbar_icon_size'] = 24
d['insert_full_screen_image'] = False
d['preserve_aspect_ratio_when_inserting_image'] = False
del d
ucase_map = {l:string.ascii_uppercase[i] for i, l in enumerate(string.ascii_lowercase)}

View File

@ -776,14 +776,14 @@ class Boss(QObject):
if rdata is None:
return
if rtype == 'image':
chosen_name, chosen_image_is_external = rdata
chosen_name, chosen_image_is_external, fullpage, preserve_ar = rdata
if chosen_image_is_external:
with open(chosen_image_is_external[1], 'rb') as f:
current_container().add_file(chosen_image_is_external[0], f.read())
self.refresh_file_list()
chosen_name = chosen_image_is_external[0]
href = current_container().name_to_href(chosen_name, edname)
ed.insert_image(href)
ed.insert_image(href, fullpage=fullpage, preserve_aspect_ratio=preserve_ar)
elif action[0] == 'insert_hyperlink':
self.commit_all_editors_to_container()
d = InsertLink(current_container(), edname, initial_text=ed.get_smart_selection(), parent=self.gui)

View File

@ -11,10 +11,10 @@ from functools import partial
from PyQt5.Qt import (
QGridLayout, QSize, QListView, QStyledItemDelegate, QLabel, QPixmap,
QApplication, QSizePolicy, QAbstractListModel, Qt, QRect,
QApplication, QSizePolicy, QAbstractListModel, Qt, QRect, QCheckBox,
QPainter, QModelIndex, QSortFilterProxyModel, QLineEdit, QToolButton,
QIcon, QFormLayout, pyqtSignal, QTreeWidget, QTreeWidgetItem, QVBoxLayout,
QMenu, QInputDialog)
QMenu, QInputDialog, QHBoxLayout)
from calibre import fit_image
from calibre.constants import plugins
@ -226,7 +226,6 @@ class InsertImage(Dialog):
l.addWidget(b, 2, 1)
f.textChanged.connect(self.filter_changed)
l.addWidget(self.bb, 3, 0, 1, 2)
if self.for_browsing:
self.bb.clear()
self.bb.addButton(self.bb.Close)
@ -244,6 +243,19 @@ class InsertImage(Dialog):
b.clicked.connect(self.paste_image)
b.setIcon(QIcon(I('edit-paste.png')))
b.setToolTip(_('Paste an image from the clipboard'))
self.fullpage = f = QCheckBox(_('Full page image'), self)
f.setToolTip(_('Insert the image so that it takes up an entire page when viewed in a reader'))
f.setChecked(tprefs['insert_full_screen_image'])
self.preserve_aspect_ratio = a = QCheckBox(_('Preserve aspect ratio'))
a.setToolTip(_('Preserve the aspect ratio of the inserted image when rendering it full paged'))
a.setChecked(tprefs['preserve_aspect_ratio_when_inserting_image'])
f.toggled.connect(lambda : (tprefs.set('insert_full_screen_image', f.isChecked()), a.setVisible(f.isChecked())))
a.toggled.connect(lambda : tprefs.set('preserve_aspect_ratio_when_inserting_image', a.isChecked()))
a.setVisible(f.isChecked())
h = QHBoxLayout()
l.addLayout(h, 3, 0, 1, -1)
h.addWidget(f), h.addStretch(10), h.addWidget(a)
l.addWidget(self.bb, 4, 0, 1, 2)
def refresh(self):
self.d.cover_cache.clear()
@ -305,7 +317,7 @@ def get_resource_data(rtype, parent):
if rtype == 'image':
d = InsertImage(parent)
if d.exec_() == d.Accepted:
return d.chosen_image, d.chosen_image_is_external
return d.chosen_image, d.chosen_image_is_external, d.fullpage.isChecked(), d.preserve_aspect_ratio.isChecked()
def create_folder_tree(container):
root = {}

View File

@ -682,7 +682,7 @@ class TextEdit(PlainTextEdit):
c.setPosition(c.position() - len(suffix))
self.setTextCursor(c)
def insert_image(self, href):
def insert_image(self, href, fullpage=False, preserve_aspect_ratio=False):
c = self.textCursor()
template, alt = 'url(%s)', ''
left = min(c.position(), c.anchor)
@ -690,12 +690,19 @@ class TextEdit(PlainTextEdit):
left, right = self.get_range_inside_tag()
c.setPosition(left)
c.setPosition(right, c.KeepAnchor)
href = prepare_string_for_xml(href, True)
if fullpage:
template = '''\
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" \
version="1.1" width="100%%" height="100%%" viewBox="0 0 1200 1600" preserveAspectRatio="{}">\
<image width="1200" height="1600" xlink:href="%s"/>\
</svg>'''.format('xMidYMid meet' if preserve_aspect_ratio else 'none')
else:
alt = _('Image')
template = '<img alt="{0}" src="%s" />'.format(alt)
href = prepare_string_for_xml(href, True)
text = template % href
c.insertText(text)
if self.syntax == 'html':
if self.syntax == 'html' and not fullpage:
c.setPosition(left + 10)
c.setPosition(c.position() + len(alt), c.KeepAnchor)
else:

View File

@ -221,8 +221,8 @@ class Editor(QMainWindow):
func = getattr(self.editor, action)
func(*args)
def insert_image(self, href):
self.editor.insert_image(href)
def insert_image(self, href, fullpage=False, preserve_aspect_ratio=False):
self.editor.insert_image(href, fullpage=fullpage, preserve_aspect_ratio=preserve_aspect_ratio)
def insert_hyperlink(self, href, text):
self.editor.insert_hyperlink(href, text)