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