From 6c43fa1d95ddf4978feb815c03b05091d913d9d3 Mon Sep 17 00:00:00 2001 From: Ales Erjavec <ales.erjavec@fri.uni-lj.si> Date: Mon, 30 Apr 2018 13:00:49 +0200 Subject: [PATCH 1/2] owcorpusviewer: Avoid 'synchronous' evalJS, use 'runJavaScript' instead The 'wait' loop for QWebEngineView implemented in gui.WebView seems to interfere with key press delivery. --- orangecontrib/text/widgets/owcorpusviewer.py | 32 ++++++++++++-------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/orangecontrib/text/widgets/owcorpusviewer.py b/orangecontrib/text/widgets/owcorpusviewer.py index 398ef877a..248d23b18 100644 --- a/orangecontrib/text/widgets/owcorpusviewer.py +++ b/orangecontrib/text/widgets/owcorpusviewer.py @@ -3,7 +3,11 @@ import sre_constants from itertools import chain -from AnyQt.QtCore import Qt, QUrl, QItemSelection, QItemSelectionModel, QItemSelectionRange +from AnyQt.QtCore import ( + Qt, QUrl, QItemSelection, QItemSelectionModel, QItemSelectionRange, + pyqtSlot as Slot +) + from AnyQt.QtGui import QStandardItemModel, QStandardItem from AnyQt.QtWidgets import (QListView, QSizePolicy, QTableView, QAbstractItemView, QHeaderView, QSplitter, @@ -222,6 +226,12 @@ def show_docs(self): <!doctype html> <html> <head> + <script type="text/javascript" src="resources/jquery-3.1.1.min.js"> + </script> + <script type="text/javascript" src="resources/jquery.mark.min.js"> + </script> + <script type="text/javascript" src="resources/highlighter.js"> + </script> <meta charset='utf-8'> <style> @@ -307,16 +317,13 @@ def show_docs(self): html += '</table>' - # QUrl is a workaround to allow local resources - # https://bugreports.qt.io/browse/QTBUG-55902?focusedCommentId=335945 - self.doc_webview.setHtml(HTML.format(html), QUrl("file://")) - self.load_js() - self.highlight_docs() - - def load_js(self): - resources = os.path.join(os.path.dirname(__file__), 'resources') - for script in ('jquery-3.1.1.min.js', 'jquery.mark.min.js', 'highlighter.js', ): - self.doc_webview.evalJS(open(os.path.join(resources, script), encoding='utf-8').read()) + try: + self.doc_webview.loadFinished.disconnect(self.highlight_docs) + except TypeError: # not connected + pass + base = QUrl.fromLocalFile(__file__) + self.doc_webview.setHtml(HTML.format(html), base) + self.doc_webview.loadFinished.connect(self.highlight_docs, Qt.UniqueConnection) def search_features_changed(self): self.regenerate_docs() @@ -338,11 +345,12 @@ def refresh_search(self): self.update_info() self.commit() + @Slot() def highlight_docs(self): search_keyword = self.regexp_filter.\ strip('|').replace('\\', '\\\\') # escape one \ to two for mark.js if search_keyword: - self.doc_webview.evalJS('mark("{}");'.format(search_keyword)) + self.doc_webview.runJavaScript('mark("{}");'.format(search_keyword)) def update_info(self): if self.corpus is not None: From 5589bb74defb71705c86b3c641e7422d73ca7a95 Mon Sep 17 00:00:00 2001 From: Ales Erjavec <ales.erjavec@fri.uni-lj.si> Date: Thu, 3 May 2018 15:46:45 +0200 Subject: [PATCH 2/2] owcorpusviewer: Connect to loadFinished once, handle empty view in JS Fixes highlighting when using QWebKitWidgets --- orangecontrib/text/widgets/owcorpusviewer.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/orangecontrib/text/widgets/owcorpusviewer.py b/orangecontrib/text/widgets/owcorpusviewer.py index 248d23b18..b390293f5 100644 --- a/orangecontrib/text/widgets/owcorpusviewer.py +++ b/orangecontrib/text/widgets/owcorpusviewer.py @@ -119,6 +119,7 @@ def __init__(self): # Document contents self.doc_webview = gui.WebviewWidget(self.splitter, debug=False) + self.doc_webview.loadFinished.connect(self.highlight_docs) self.mainArea.layout().addWidget(self.splitter) @@ -316,14 +317,8 @@ def show_docs(self): token) for token in tokens[row_ind])) html += '</table>' - - try: - self.doc_webview.loadFinished.disconnect(self.highlight_docs) - except TypeError: # not connected - pass base = QUrl.fromLocalFile(__file__) self.doc_webview.setHtml(HTML.format(html), base) - self.doc_webview.loadFinished.connect(self.highlight_docs, Qt.UniqueConnection) def search_features_changed(self): self.regenerate_docs() @@ -349,8 +344,17 @@ def refresh_search(self): def highlight_docs(self): search_keyword = self.regexp_filter.\ strip('|').replace('\\', '\\\\') # escape one \ to two for mark.js + if search_keyword: - self.doc_webview.runJavaScript('mark("{}");'.format(search_keyword)) + # mark is undefined when clearing the view (`setHtml('')`). Maybe + # set and template html with all the scripts, ... but no contents? + self.doc_webview.runJavaScript( + ''' + if (typeof mark !== "undefined") {{ + mark("{}"); + }} + '''.format(search_keyword) + ) def update_info(self): if self.corpus is not None: