mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
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:
parent
99f2fc6879
commit
94c324b317
Binary file not shown.
@ -103,6 +103,22 @@ class CalibreUtils
|
|||||||
return this.viewport_to_document(r.top, 0, elem.ownerDocument)[0]
|
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?
|
if window?
|
||||||
window.calibre_utils = new CalibreUtils()
|
window.calibre_utils = new CalibreUtils()
|
||||||
|
@ -364,10 +364,7 @@ class Document(QWebPage): # {{{
|
|||||||
return ans
|
return ans
|
||||||
|
|
||||||
def javaScriptConsoleMessage(self, msg, lineno, msgid):
|
def javaScriptConsoleMessage(self, msg, lineno, msgid):
|
||||||
if self.debug_javascript:
|
prints(msg)
|
||||||
prints(msg)
|
|
||||||
else:
|
|
||||||
return QWebPage.javaScriptConsoleMessage(self, msg, lineno, msgid)
|
|
||||||
|
|
||||||
def javaScriptAlert(self, frame, msg):
|
def javaScriptAlert(self, frame, msg):
|
||||||
if self.debug_javascript:
|
if self.debug_javascript:
|
||||||
@ -681,6 +678,12 @@ class DocumentView(QWebView): # {{{
|
|||||||
ac = getattr(self, '%s_action' % x)
|
ac = getattr(self, '%s_action' % x)
|
||||||
menu.addAction(ac.icon(), '%s [%s]' % (unicode(ac.text()), ','.join(self.shortcuts.get_shortcuts(sc))), ac.trigger)
|
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():
|
if not text and img.isNull():
|
||||||
menu.addSeparator()
|
menu.addSeparator()
|
||||||
if self.manager.action_back.isEnabled():
|
if self.manager.action_back.isEnabled():
|
||||||
@ -748,8 +751,11 @@ class DocumentView(QWebView): # {{{
|
|||||||
def search_online(self):
|
def search_online(self):
|
||||||
t = unicode(self.selectedText()).strip()
|
t = unicode(self.selectedText()).strip()
|
||||||
if t:
|
if t:
|
||||||
url = 'https://www.google.com/search?q=' + QUrl().toPercentEncoding(t)
|
self.do_search_online(t)
|
||||||
open_url(QUrl.fromEncoded(url))
|
|
||||||
|
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):
|
def set_manager(self, manager):
|
||||||
self.manager = manager
|
self.manager = manager
|
||||||
|
Loading…
x
Reference in New Issue
Block a user