Metadata dialog: Comments editor: Allow specifying the name of a link when using the insert link button. Fixes #1042683 ({ENHANCEMENT] Add "Label" to Insert URL)

This commit is contained in:
Kovid Goyal 2012-08-29 14:16:43 +05:30
parent 1dea118004
commit 22fad32e76

View File

@ -5,19 +5,19 @@ __license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import re, os import re, os, json
from lxml import html from lxml import html
import sip 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, QToolBar, QVBoxLayout, QAction, QIcon, Qt, QTabWidget, QUrl, QFormLayout,
QSyntaxHighlighter, QColor, QChar, QColorDialog, QMenu, QInputDialog, QSyntaxHighlighter, QColor, QChar, QColorDialog, QMenu, QDialog,
QHBoxLayout, QKeySequence) QHBoxLayout, QKeySequence, QLineEdit, QDialogButtonBox)
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
from calibre import xml_replace_entities from calibre import xml_replace_entities, prepare_string_for_xml
from calibre.gui2 import open_url from calibre.gui2 import open_url
from calibre.utils.soupparser import fromstring from calibre.utils.soupparser import fromstring
from calibre.utils.config import tweaks from calibre.utils.config import tweaks
@ -191,15 +191,37 @@ class EditorWidget(QWebView): # {{{
self.exec_command('hiliteColor', unicode(col.name())) self.exec_command('hiliteColor', unicode(col.name()))
def insert_link(self, *args): def insert_link(self, *args):
link, ok = QInputDialog.getText(self, _('Create link'), link, name = self.ask_link()
_('Enter URL')) if not link:
if not ok:
return return
url = self.parse_link(unicode(link)) url = self.parse_link(unicode(link))
if url.isValid(): if url.isValid():
url = unicode(url.toString()) url = unicode(url.toString())
if name:
self.exec_command('insertHTML',
'<a href="%s">%s</a>'%(prepare_string_for_xml(url, True),
prepare_string_for_xml(name)))
else:
self.exec_command('createLink', url) self.exec_command('createLink', url)
def ask_link(self):
d = QDialog(self)
d.setWindowTitle(_('Create link'))
l = QFormLayout()
d.setLayout(l)
d.url = QLineEdit(d)
d.name = QLineEdit(d)
d.bb = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel)
l.addRow(_('Enter &URL:'), d.url)
l.addRow(_('Enter name (optional):'), d.name)
l.addRow(d.bb)
d.bb.accepted.connect(d.accept)
d.bb.rejected.connect(d.reject)
link, name = None, None
if d.exec_() == d.Accepted:
link, name = unicode(d.url.text()).strip(), unicode(d.name.text()).strip()
return link, name
def parse_link(self, link): def parse_link(self, link):
link = link.strip() link = link.strip()
has_schema = re.match(r'^[a-zA-Z]+:', link) has_schema = re.match(r'^[a-zA-Z]+:', link)
@ -227,7 +249,8 @@ class EditorWidget(QWebView): # {{{
def exec_command(self, cmd, arg=None): def exec_command(self, cmd, arg=None):
frame = self.page().mainFrame() frame = self.page().mainFrame()
if arg is not None: if arg is not None:
js = 'document.execCommand("%s", false, "%s");' % (cmd, arg) js = 'document.execCommand("%s", false, %s);' % (cmd,
json.dumps(unicode(arg)))
else: else:
js = 'document.execCommand("%s", false, null);' % cmd js = 'document.execCommand("%s", false, null);' % cmd
frame.evaluateJavaScript(js) frame.evaluateJavaScript(js)