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,11 +18,11 @@ 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
class PageAction(QAction): # {{{
class PageAction(QAction): # {{{
def __init__(self, wac, icon, text, checkable, view):
QAction.__init__(self, QIcon(I(icon+'.png')), text, view)
@ -43,14 +43,15 @@ 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())
# }}}
class BlockStyleAction(QAction): # {{{
class BlockStyleAction(QAction): # {{{
def __init__(self, text, name, view):
QAction.__init__(self, text, view)
@ -62,7 +63,7 @@ class BlockStyleAction(QAction): # {{{
# }}}
class EditorWidget(QWebView): # {{{
class EditorWidget(QWebView): # {{{
def __init__(self, parent=None):
QWebView.__init__(self, parent)
@ -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)
@ -362,9 +373,9 @@ class Highlighter(QSyntaxHighlighter):
self.colors['doctype'] = QColor(192, 192, 192)
self.colors['entity'] = QColor(128, 128, 128)
self.colors['tag'] = QColor(136, 18, 128)
self.colors['comment'] = QColor( 35, 110, 37)
self.colors['comment'] = QColor(35, 110, 37)
self.colors['attrname'] = QColor(153, 69, 0)
self.colors['attrval'] = QColor( 36, 36, 170)
self.colors['attrval'] = QColor(36, 36, 170)
def highlightBlock(self, text):
state = self.previousBlockState()
@ -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,12 +560,11 @@ class Highlighter(QSyntaxHighlighter):
else:
pos += 1
self.setCurrentBlockState(state)
# }}}
class Editor(QWidget): # {{{
class Editor(QWidget): # {{{
def __init__(self, parent=None, one_line_toolbar=False):
QWidget.__init__(self, parent)
@ -650,13 +660,13 @@ class Editor(QWidget): # {{{
return property(fget=fget, fset=fset)
def change_tab(self, index):
#print 'reloading:', (index and self.wyswyg_dirty) or (not index and
# print 'reloading:', (index and self.wyswyg_dirty) or (not index and
# self.source_dirty)
if index == 1: # changing to code view
if index == 1: # changing to code view
if self.wyswyg_dirty:
self.code_edit.setPlainText(self.editor.html)
self.wyswyg_dirty = False
elif index == 0: #changing to wyswyg
elif index == 0: # changing to wyswyg
if self.source_dirty:
self.editor.html = unicode(self.code_edit.toPlainText())
self.source_dirty = False
@ -687,4 +697,5 @@ if __name__ == '__main__':
w.show()
w.html = '<b>testing</b>'
app.exec_()
#print w.html
# print w.html