Comments editor: Disable insert link until focused

Comments editor: The Insert Link button has no effect until the user
clicks inside the comments box, therefore disable it until it is ready,
to prevent confusion. Fixes #1208073 [Insert Link not working](https://bugs.launchpad.net/calibre/+bug/1208073)
This commit is contained in:
Kovid Goyal 2013-08-04 09:18:18 +05:30
parent c70b70dd76
commit db4ba9133b

View File

@ -18,7 +18,7 @@ from PyQt4.QtWebKit import QWebView, QWebPage
from calibre.ebooks.chardet import xml_to_unicode
from calibre import xml_replace_entities, prepare_string_for_xml
from calibre.gui2 import open_url
from calibre.gui2 import open_url, error_dialog
from calibre.utils.soupparser import fromstring
from calibre.utils.config import tweaks
@ -43,7 +43,8 @@ class PageAction(QAction): # {{{
self.page_action.trigger()
def update_state(self, *args):
if sip.isdeleted(self) or sip.isdeleted(self.page_action): return
if sip.isdeleted(self) or sip.isdeleted(self.page_action):
return
if self.isCheckable():
self.setChecked(self.page_action.isChecked())
self.setEnabled(self.page_action.isEnabled())
@ -157,6 +158,8 @@ class EditorWidget(QWebView): # {{{
self.action_insert_link = QAction(QIcon(I('insert-link.png')),
_('Insert link'), self)
self.action_insert_link.triggered.connect(self.insert_link)
self.pageAction(QWebPage.ToggleBold).changed.connect(self.update_link_action)
self.action_insert_link.setEnabled(False)
self.action_clear = QAction(QIcon(I('edit-clear')), _('Clear'), self)
self.action_clear.triggered.connect(self.clear_text)
@ -166,6 +169,10 @@ class EditorWidget(QWebView): # {{{
self.setHtml('')
self.set_readonly(False)
def update_link_action(self):
wac = self.pageAction(QWebPage.ToggleBold)
self.action_insert_link.setEnabled(wac.isEnabled())
def set_readonly(self, what):
self.readonly = what
self.page().setContentEditable(not self.readonly)
@ -202,12 +209,16 @@ class EditorWidget(QWebView): # {{{
url = self.parse_link(unicode(link))
if url.isValid():
url = unicode(url.toString())
self.setFocus(Qt.OtherFocusReason)
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)
else:
error_dialog(self, _('Invalid URL'),
_('The url %r is invalid') % unicode(link), show=True)
def ask_link(self):
d = QDialog(self)
@ -378,8 +389,8 @@ class Highlighter(QSyntaxHighlighter):
start = pos
while pos < len_:
if text.mid(pos, 3) == "-->":
pos += 3;
state = State_Text;
pos += 3
state = State_Text
break
else:
pos += 1
@ -422,7 +433,7 @@ class Highlighter(QSyntaxHighlighter):
if ch == QChar('>'):
state = State_Text
break
self.setFormat(start, pos - start, self.colors['tag']);
self.setFormat(start, pos - start, self.colors['tag'])
# anywhere after tag name and before tag closing ('>')
elif state == State_InsideTag:
@ -489,7 +500,7 @@ class Highlighter(QSyntaxHighlighter):
# just stop at non-space or tag delimiter
start = pos
while pos < len_:
ch = text.at(pos);
ch = text.at(pos)
if ch.isSpace():
break
if ch in (QChar('>'), QChar('/')):
@ -538,7 +549,7 @@ class Highlighter(QSyntaxHighlighter):
state = State_DocType
else:
state = State_TagStart
break;
break
elif ch == QChar('&'):
start = pos
while pos < len_ and text.at(pos) != QChar(';'):
@ -549,7 +560,6 @@ class Highlighter(QSyntaxHighlighter):
else:
pos += 1
self.setCurrentBlockState(state)
# }}}
@ -688,3 +698,4 @@ if __name__ == '__main__':
w.html = '<b>testing</b>'
app.exec_()
# print w.html