Comments editor: Allow treating arbitrary URLs as images

Merge branch 'master' of https://github.com/cbhaley/calibre
This commit is contained in:
Kovid Goyal 2013-09-14 09:04:03 +05:30
commit 6fff7a0ee5

View File

@ -13,7 +13,8 @@ import sip
from PyQt4.Qt import (QApplication, QFontInfo, QSize, QWidget, QPlainTextEdit, from PyQt4.Qt import (QApplication, QFontInfo, QSize, QWidget, QPlainTextEdit,
QToolBar, QVBoxLayout, QAction, QIcon, Qt, QTabWidget, QUrl, QFormLayout, QToolBar, QVBoxLayout, QAction, QIcon, Qt, QTabWidget, QUrl, QFormLayout,
QSyntaxHighlighter, QColor, QChar, QColorDialog, QMenu, QDialog, QLabel, QSyntaxHighlighter, QColor, QChar, QColorDialog, QMenu, QDialog, QLabel,
QHBoxLayout, QKeySequence, QLineEdit, QDialogButtonBox, QPushButton) QHBoxLayout, QKeySequence, QLineEdit, QDialogButtonBox, QPushButton,
QCheckBox)
from PyQt4.QtWebKit import QWebView, QWebPage from PyQt4.QtWebKit import QWebView, QWebPage
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode
@ -232,6 +233,7 @@ class EditorWidget(QWebView): # {{{
d.setLayout(l) d.setLayout(l)
d.url = QLineEdit(d) d.url = QLineEdit(d)
d.name = QLineEdit(d) d.name = QLineEdit(d)
d.treat_as_image = QCheckBox(d)
d.setMinimumWidth(600) d.setMinimumWidth(600)
d.bb = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) d.bb = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel)
d.br = b = QPushButton(_('&Browse')) d.br = b = QPushButton(_('&Browse'))
@ -239,17 +241,27 @@ class EditorWidget(QWebView): # {{{
def cf(): def cf():
files = choose_files(d, 'select link file', _('Choose file'), select_only_single_file=True) files = choose_files(d, 'select link file', _('Choose file'), select_only_single_file=True)
if files: if files:
d.url.setText(files[0]) path = files[0]
d.url.setText(path)
if path and os.path.exists(path):
with lopen(path, 'rb') as f:
q = what(f)
is_image = q in {'jpeg', 'png', 'gif'}
d.treat_as_image.setChecked(is_image)
b.clicked.connect(cf) b.clicked.connect(cf)
d.la = la = QLabel(_( d.la = la = QLabel(_(
'Enter a URL. You can also choose to create a link to a file on ' 'Enter a URL. If you check the "Treat the URL as an image" box '
'your computer. If the selected file is an image, it will be ' 'then the URL will be added as an image reference instead of as '
'inserted as an image. Note that if you create a link to a file or image on ' 'a link. You can also choose to create a link to a file on '
'your computer, it will stop working if the file or image is moved.')) 'your computer. '
'Note that if you create a link to a file on your computer, it '
'will stop working if the file is moved.'))
la.setWordWrap(True) la.setWordWrap(True)
la.setStyleSheet('QLabel { margin-bottom: 1.5ex }') la.setStyleSheet('QLabel { margin-bottom: 1.5ex }')
l.setWidget(0, l.SpanningRole, la) l.setWidget(0, l.SpanningRole, la)
l.addRow(_('Enter &URL:'), d.url) l.addRow(_('Enter &URL:'), d.url)
l.addRow(_('Treat the URL as an &image'), d.treat_as_image)
l.addRow(_('Enter &name (optional):'), d.name) l.addRow(_('Enter &name (optional):'), d.name)
l.addRow(_('Choose a file on your computer:'), d.br) l.addRow(_('Choose a file on your computer:'), d.br)
l.addRow(d.bb) l.addRow(d.bb)
@ -259,10 +271,7 @@ class EditorWidget(QWebView): # {{{
link, name, is_image = None, None, False link, name, is_image = None, None, False
if d.exec_() == d.Accepted: if d.exec_() == d.Accepted:
link, name = unicode(d.url.text()).strip(), unicode(d.name.text()).strip() link, name = unicode(d.url.text()).strip(), unicode(d.name.text()).strip()
if link and os.path.exists(link): is_image = d.treat_as_image.isChecked()
with lopen(link, 'rb') as f:
q = what(f)
is_image = q in {'jpeg', 'png', 'gif'}
return link, name, is_image return link, name, is_image
def parse_link(self, link): def parse_link(self, link):