E-book viewer: When using the tap and hold gesture on a touchscreen, allow lookup of the word currently under the finger in the dictionary or online. Fixes #1318242 [Enhancement: allow text select on Windows Tablet, perhaps with stylus](https://bugs.launchpad.net/calibre/+bug/1318242)

This commit is contained in:
Kovid Goyal 2014-05-11 09:47:52 +05:30
parent 99f2fc6879
commit 94c324b317
3 changed files with 28 additions and 6 deletions

Binary file not shown.

View File

@ -103,6 +103,22 @@ class CalibreUtils
return this.viewport_to_document(r.top, 0, elem.ownerDocument)[0]
# }}}
word_at_point: (x, y) -> # {{{
# Return the word at the specified point (in viewport co-ordinates)
range = if document.caretPositionFromPoint then document.caretPositionFromPoint(x, y) else document.caretRangeFromPoint(x, y)
if range == null
return null
node = range.startContainer
if node.nodeType != Node.TEXT_NODE
return null
offset = range.startOffset
range = document.createRange()
range.selectNodeContents(node)
range.setStart(node, offset)
range.setEnd(node, offset+1)
range.expand('word')
return range.toString()
# }}}
if window?
window.calibre_utils = new CalibreUtils()

View File

@ -364,10 +364,7 @@ class Document(QWebPage): # {{{
return ans
def javaScriptConsoleMessage(self, msg, lineno, msgid):
if self.debug_javascript:
prints(msg)
else:
return QWebPage.javaScriptConsoleMessage(self, msg, lineno, msgid)
def javaScriptAlert(self, frame, msg):
if self.debug_javascript:
@ -681,6 +678,12 @@ class DocumentView(QWebView): # {{{
ac = getattr(self, '%s_action' % x)
menu.addAction(ac.icon(), '%s [%s]' % (unicode(ac.text()), ','.join(self.shortcuts.get_shortcuts(sc))), ac.trigger)
if from_touch and self.manager is not None:
word = unicode(mf.evaluateJavaScript('window.calibre_utils.word_at_point(%f, %f)' % (ev.pos().x(), ev.pos().y())).toString())
if word:
menu.addAction(self.dictionary_action.icon(), _('Lookup %s in the dictionary') % word, partial(self.manager.lookup, word))
menu.addAction(self.search_online_action.icon(), _('Search for %s online') % word, partial(self.do_search_online, word))
if not text and img.isNull():
menu.addSeparator()
if self.manager.action_back.isEnabled():
@ -748,7 +751,10 @@ class DocumentView(QWebView): # {{{
def search_online(self):
t = unicode(self.selectedText()).strip()
if t:
url = 'https://www.google.com/search?q=' + QUrl().toPercentEncoding(t)
self.do_search_online(t)
def do_search_online(self, text):
url = 'https://www.google.com/search?q=' + QUrl().toPercentEncoding(text)
open_url(QUrl.fromEncoded(url))
def set_manager(self, manager):