From 7a4afc6fb4d914d3308ed86a287b48c5c0b28396 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Mon, 24 Aug 2020 21:19:30 -0400 Subject: [PATCH 1/3] timeline-mixins: Enable WebEngine JS logging --- src/windows/views/timeline_mixins.py | 74 ++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/src/windows/views/timeline_mixins.py b/src/windows/views/timeline_mixins.py index 2a553c4028..a7151259cb 100644 --- a/src/windows/views/timeline_mixins.py +++ b/src/windows/views/timeline_mixins.py @@ -26,10 +26,10 @@ """ import os +import logging from classes import info -from PyQt5.QtCore import QFileInfo, pyqtSlot, QUrl, Qt, QCoreApplication, QTimer -from PyQt5.QtGui import QCursor, QKeySequence, QColor -from PyQt5.QtWidgets import QMenu +from PyQt5.QtCore import Qt, QObject, QFileInfo, QUrl, QTimer +from PyQt5.QtGui import QColor from classes.logger import log from functools import partial @@ -37,10 +37,10 @@ try: # Attempt to import QtWebEngine from PyQt5.QtWebChannel import QWebChannel - from PyQt5.QtWebEngineWidgets import QWebEngineView + from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage IS_WEBENGINE_VALID = True except ImportError: - QWebEngineView = object # Prevent inheritance errors + QWebEngineView = QObject # Prevent inheritance errors IS_WEBENGINE_VALID = False try: @@ -48,17 +48,45 @@ from PyQt5.QtWebKitWidgets import QWebView, QWebPage IS_WEBKIT_VALID = True except ImportError: - QWebView = object # Prevent inheritance errors - QWebPage = object + QWebView = QObject # Prevent inheritance errors IS_WEBKIT_VALID = False +if IS_WEBKIT_VALID: + class LoggingWebKitPage(QWebPage): + """Override console.log message to display messages""" + def javaScriptConsoleMessage(self, msg, line, source, *args): + log.warning('%s@L%d: %s' % (os.path.basename(source), line, msg)) + + def __init__(self, parent=None): + super().__init__(parent=parent) + self.setObjectName("LoggingWebKitPage") +else: + LoggingWebKitPage = object + + +if IS_WEBENGINE_VALID: + class LoggingWebEnginePage(QWebEnginePage): + """Override console.log message to display messages""" + def javaScriptConsoleMessage(self, level, msg, line, source): + log.log( + self.levels[level], + '%s@%d: %s' % (os.path.basename(source), line, msg)) + + def __init__(self, parent=None): + super().__init__(parent=parent) + self.setObjectName("LoggingWebEnginePage") + self.levels = [logging.INFO, logging.WARNING, logging.ERROR] + +else: + LoggingWebEnginePage = object + class TimelineBaseMixin(object): """OpenShot Timeline Base Mixin Class""" - def __init__(self): + def __init__(self, *args): """Initialization code required for parent widget""" self.document_is_ready = False - self.html_path = html_path = os.path.join(info.PATH, 'timeline', 'index.html') + self.html_path = os.path.join(info.PATH, 'timeline', 'index.html') def run_js(self, code, callback=None, retries=0): """Run javascript code snippet""" @@ -74,8 +102,14 @@ class TimelineQtWebEngineMixin(TimelineBaseMixin, QWebEngineView): def __init__(self): """Initialization code required for widget""" + super(QWebEngineView, self).__init__() TimelineBaseMixin.__init__(self) - QWebEngineView.__init__(self) + self.setObjectName("TimelineQtWebEngineMixin") + + # Connect logging web page (for console.log) + if IS_WEBENGINE_VALID: + self.new_page = LoggingWebEnginePage(self) + self.setPage(self.new_page) # Set background color of timeline self.page().setBackgroundColor(QColor("#363636")) @@ -92,6 +126,7 @@ def __init__(self): self.page().setWebChannel(self.webchannel) # Connect signal of javascript initialization to our javascript reference init function + log.info("WebEngine backend initializing") self.page().loadStarted.connect(self.setup_js_data) def run_js(self, code, callback=None, retries=0): @@ -117,6 +152,7 @@ def run_js(self, code, callback=None, retries=0): def setup_js_data(self): # Export self as a javascript object in webview + log.info("Registering WebChannel connection with WebEngine") self.webchannel.registerObject('timeline', self) def get_html(self): @@ -143,25 +179,22 @@ def keyPressEvent(self, event): event.ignore() -class LoggingWebPage(QWebPage): - """Override console.log message to display messages""" - def javaScriptConsoleMessage(self, msg, line, source): - log.warning('JS: %s line %d: %s' % (source, line, msg)) - class TimelineQtWebKitMixin(TimelineBaseMixin, QWebView): """QtWebKit Timeline Widget""" def __init__(self): """Initialization code required for widget""" + super(QWebView, self).__init__() TimelineBaseMixin.__init__(self) - QWebView.__init__(self) + self.setObjectName("TimelineQtWebKitMixin") # Delete the webview when closed self.setAttribute(Qt.WA_DeleteOnClose) # Connect logging web page (for console.log) - page = LoggingWebPage() - self.setPage(page) + if IS_WEBKIT_VALID: + self.new_page = LoggingWebKitPage(self) + self.setPage(self.new_page) # Disable image caching on timeline self.settings().setObjectCacheCapacities(0, 0, 0) @@ -170,6 +203,7 @@ def __init__(self): self.setHtml(self.get_html(), QUrl.fromLocalFile(QFileInfo(self.html_path).absoluteFilePath())) # Connect signal of javascript initialization to our javascript reference init function + log.info("WebKit backend initializing") self.page().mainFrame().javaScriptWindowObjectCleared.connect(self.setup_js_data) def run_js(self, code, callback=None, retries=0): @@ -196,6 +230,7 @@ def run_js(self, code, callback=None, retries=0): def setup_js_data(self): # Export self as a javascript object in webview + log.info("Registering objects with WebKit") self.page().mainFrame().addToJavaScriptWindowObject('timeline', self) self.page().mainFrame().addToJavaScriptWindowObject('mainWindow', self.window) @@ -213,14 +248,13 @@ def keyPressEvent(self, event): """ Keypress callback for timeline """ key_value = event.key() if (key_value == Qt.Key_Shift or key_value == Qt.Key_Control): - # Only pass a few keystrokes to the webview (CTRL and SHIFT) return QWebView.keyPressEvent(self, event) - else: # Ignore most keypresses event.ignore() + # Set correct Mixin (with QtWebEngine preference) if IS_WEBENGINE_VALID: TimelineMixin = TimelineQtWebEngineMixin From 63d18217ef862d4642170ec4cea1c413181d22d1 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Mon, 24 Aug 2020 21:19:30 -0400 Subject: [PATCH 2/3] timeline-mixins: Enable WebEngine JS logging --- src/windows/views/timeline_mixins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/windows/views/timeline_mixins.py b/src/windows/views/timeline_mixins.py index a7151259cb..6612b3a354 100644 --- a/src/windows/views/timeline_mixins.py +++ b/src/windows/views/timeline_mixins.py @@ -70,7 +70,7 @@ class LoggingWebEnginePage(QWebEnginePage): def javaScriptConsoleMessage(self, level, msg, line, source): log.log( self.levels[level], - '%s@%d: %s' % (os.path.basename(source), line, msg)) + '%s@L%d: %s' % (os.path.basename(source), line, msg)) def __init__(self, parent=None): super().__init__(parent=parent) From 72df097082c5246e8b298e0667b94204441ef9b6 Mon Sep 17 00:00:00 2001 From: Frank Dana Date: Mon, 24 Aug 2020 22:04:59 -0400 Subject: [PATCH 3/3] Update timeline_mixins.py --- src/windows/views/timeline_mixins.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/windows/views/timeline_mixins.py b/src/windows/views/timeline_mixins.py index 6612b3a354..865e0bc76c 100644 --- a/src/windows/views/timeline_mixins.py +++ b/src/windows/views/timeline_mixins.py @@ -76,7 +76,6 @@ def __init__(self, parent=None): super().__init__(parent=parent) self.setObjectName("LoggingWebEnginePage") self.levels = [logging.INFO, logging.WARNING, logging.ERROR] - else: LoggingWebEnginePage = object