Skip to content

Commit

Permalink
E-book viewer: When using the tap and hold gesture on a touchscreen, …
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
kovidgoyal committed May 11, 2014
1 parent 99f2fc6 commit 94c324b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
Binary file modified resources/compiled_coffeescript.zip
Binary file not shown.
16 changes: 16 additions & 0 deletions src/calibre/ebooks/oeb/display/utils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 12 additions & 6 deletions src/calibre/gui2/viewer/documentview.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,7 @@ def javascript(self, string, typ=None):
return ans

def javaScriptConsoleMessage(self, msg, lineno, msgid):
if self.debug_javascript:
prints(msg)
else:
return QWebPage.javaScriptConsoleMessage(self, msg, lineno, msgid)
prints(msg)

def javaScriptAlert(self, frame, msg):
if self.debug_javascript:
Expand Down Expand Up @@ -681,6 +678,12 @@ def contextMenuEvent(self, ev):
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():
Expand Down Expand Up @@ -748,8 +751,11 @@ def search_next(self):
def search_online(self):
t = unicode(self.selectedText()).strip()
if t:
url = 'https://www.google.com/search?q=' + QUrl().toPercentEncoding(t)
open_url(QUrl.fromEncoded(url))
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):
self.manager = manager
Expand Down

0 comments on commit 94c324b

Please sign in to comment.