In the comments editor, permit insertion of references to images on the 'net (<img ...>) as well as links (<a ...>).

This commit is contained in:
Charles Haley 2013-09-13 19:28:18 +02:00
parent c6c17ef976
commit 6f4b23a447

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'))
@ -242,14 +244,18 @@ class EditorWidget(QWebView): # {{{
d.url.setText(files[0]) d.url.setText(files[0])
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 '
'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. If the selected file is an image, it will be ' '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 on ' 'inserted as an image reference, otherwise it will be a link. '
'your computer, it will stop working if the file is moved.')) '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)
@ -263,6 +269,8 @@ class EditorWidget(QWebView): # {{{
with lopen(link, 'rb') as f: with lopen(link, 'rb') as f:
q = what(f) q = what(f)
is_image = q in {'jpeg', 'png', 'gif'} is_image = q in {'jpeg', 'png', 'gif'}
else:
is_image = d.treat_as_image.isChecked()
return link, name, is_image return link, name, is_image
def parse_link(self, link): def parse_link(self, link):