Skip to content

Commit 0b394ca

Browse files
committed
Adding JS mixin support for QtWebEngine and QtWebKit (so either one will be found and will work)
1 parent 3464d1e commit 0b394ca

File tree

6 files changed

+157
-34
lines changed

6 files changed

+157
-34
lines changed

src/timeline/app.js

+5-16
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
*/
2828

2929
// Initialize Angular application
30-
/*global App, timeline, angular*/
31-
var timeline = null;
30+
/*global App, angular*/
3231
var App = angular.module("openshot-timeline", ["ui.bootstrap", "ngAnimate"]);
3332

3433

@@ -37,18 +36,8 @@ $(document).ready(function () {
3736

3837
var body_object = $("body");
3938

40-
// Check for Qt Integration
41-
new QWebChannel(qt.webChannelTransport, function (channel) {
42-
timeline = channel.objects.timeline;
43-
timeline.qt_log("INFO", "Qt Ready");
44-
45-
// Only enable Qt once Angular as initialized
46-
angular.element(document).ready(function () {
47-
timeline.qt_log("INFO", "Angular Ready");
48-
body_object.scope().enableQt();
49-
});
50-
51-
});
39+
// Initialize Qt Mixin (WebEngine or WebKit)
40+
init_mixin();
5241

5342
/// Capture window resize event, and resize scrollable divs (i.e. track container)
5443
$(window).resize(function () {
@@ -64,8 +53,8 @@ $(document).ready(function () {
6453

6554
track_controls.height(new_track_height);
6655
$("#scrolling_tracks").height(new_track_height);
67-
body_object.scope().playhead_height = $("#track-container").height();
68-
$(".playhead-line").height(body_object.scope().playhead_height);
56+
body_object.scope().playhead_height = $("#track-container").height();
57+
$(".playhead-line").height(body_object.scope().playhead_height);
6958
});
7059

7160
// Bind to keydown event (to detect SHIFT)

src/timeline/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<script type="text/javascript" src="media/js/angular-animate.min.js"></script>
1818

1919
<!-- OpenShot JavaScript Sources -->
20-
<script type="text/javascript" src="js/qwebchannel.js"></script>
20+
{{MIXIN_JS_INCLUDE}}
2121
<script type="text/javascript" src="app.js"></script>
2222
<script type="text/javascript" src="js/functions.js"></script>
2323
<script type="text/javascript" src="js/controllers.js"></script>

src/timeline/js/mixin_webengine.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @file
3+
* @brief JavaScript file to initialize QtWebEngine JS mixin
4+
* @author Jonathan Thomas <[email protected]>
5+
*
6+
* @section LICENSE
7+
*
8+
* Copyright (c) 2008-2018 OpenShot Studios, LLC
9+
* <http://www.openshotstudios.com/>. This file is part of
10+
* OpenShot Video Editor, an open-source project dedicated to
11+
* delivering high quality video editing and animation solutions to the
12+
* world. For more information visit <http://www.openshot.org/>.
13+
*
14+
* OpenShot Video Editor is free software: you can redistribute it
15+
* and/or modify it under the terms of the GNU General Public License
16+
* as published by the Free Software Foundation, either version 3 of the
17+
* License, or (at your option) any later version.
18+
*
19+
* OpenShot Video Editor is distributed in the hope that it will be
20+
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
* GNU General Public License for more details.
23+
*
24+
* You should have received a copy of the GNU General Public License
25+
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26+
*/
27+
28+
/*global timeline*/
29+
var timeline = null;
30+
31+
function init_mixin() {
32+
33+
// Check for Qt Integration
34+
new QWebChannel(qt.webChannelTransport, function (channel) {
35+
timeline = channel.objects.timeline;
36+
timeline.qt_log("INFO", "Qt Ready");
37+
38+
// Only enable Qt once Angular as initialized
39+
angular.element(document).ready(function () {
40+
timeline.qt_log("INFO", "Angular Ready");
41+
$("body").scope().enableQt();
42+
});
43+
44+
});
45+
46+
}

src/timeline/js/mixin_webkit.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @file
3+
* @brief JavaScript file to initialize QtWebKit JS mixin
4+
* @author Jonathan Thomas <[email protected]>
5+
*
6+
* @section LICENSE
7+
*
8+
* Copyright (c) 2008-2018 OpenShot Studios, LLC
9+
* <http://www.openshotstudios.com/>. This file is part of
10+
* OpenShot Video Editor, an open-source project dedicated to
11+
* delivering high quality video editing and animation solutions to the
12+
* world. For more information visit <http://www.openshot.org/>.
13+
*
14+
* OpenShot Video Editor is free software: you can redistribute it
15+
* and/or modify it under the terms of the GNU General Public License
16+
* as published by the Free Software Foundation, either version 3 of the
17+
* License, or (at your option) any later version.
18+
*
19+
* OpenShot Video Editor is distributed in the hope that it will be
20+
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
* GNU General Public License for more details.
23+
*
24+
* You should have received a copy of the GNU General Public License
25+
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26+
*/
27+
28+
// undef global timeline var
29+
//timeline = undefined;
30+
31+
function init_mixin() {
32+
33+
// Only enable Qt once Angular as initialized
34+
angular.element(document).ready(function () {
35+
if (typeof timeline !== "undefined") {
36+
timeline.qt_log("INFO", "Qt Ready");
37+
$("body").scope().enableQt();
38+
}
39+
timeline.qt_log("INFO", "Angular Ready");
40+
});
41+
42+
}

src/windows/views/timeline_mixins.py

+63-5
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@
3636

3737
try:
3838
# Attempt to import QtWebEngine
39-
from PyQt5.QtWebEngineWidgets import QWebEngineView
4039
from PyQt5.QtWebChannel import QWebChannel
40+
from PyQt5.QtWebEngineWidgets import QWebEngineView
4141
IS_WEBENGINE_VALID = True
4242
except ImportError:
4343
QWebEngineView = object # Prevent inheritance errors
4444
IS_WEBENGINE_VALID = False
4545

4646
try:
4747
# Attempt to import QtWebKit
48-
from PyQt5.QtWebKitWidgets import QWebView
48+
from PyQt5.QtWebKitWidgets import QWebView, QWebPage
4949
IS_WEBKIT_VALID = True
5050
except ImportError:
5151
QWebView = object # Prevent inheritance errors
@@ -63,6 +63,10 @@ def run_js(self, code, callback=None, retries=0):
6363
"""Run javascript code snippet"""
6464
raise Exception("run_js not implemented")
6565

66+
def get_html(self):
67+
"""Get HTML for Timeline, adjusted for mixin"""
68+
raise Exception("get_html not implemented")
69+
6670

6771
class TimelineQtWebEngineMixin(TimelineBaseMixin, QWebEngineView):
6872
"""QtWebEngine Timeline Widget"""
@@ -83,7 +87,7 @@ def __init__(self):
8387

8488
# Set url from configuration (QUrl takes absolute paths for file system paths, create from QFileInfo)
8589
self.webchannel = QWebChannel(self.page())
86-
self.setUrl(QUrl.fromLocalFile(QFileInfo(self.html_path).absoluteFilePath()))
90+
self.setHtml(self.get_html(), QUrl.fromLocalFile(QFileInfo(self.html_path).absoluteFilePath()))
8791
self.page().setWebChannel(self.webchannel)
8892

8993
# Connect signal of javascript initialization to our javascript reference init function
@@ -114,6 +118,34 @@ def setup_js_data(self):
114118
# Export self as a javascript object in webview
115119
self.webchannel.registerObject('timeline', self)
116120

121+
def get_html(self):
122+
"""Get HTML for Timeline, adjusted for mixin"""
123+
html = open(self.html_path, 'r', encoding='utf-8').read()
124+
html = html.replace('{{MIXIN_JS_INCLUDE}}',
125+
'''
126+
<script type="text/javascript" src="js/qwebchannel.js"></script>
127+
<script type="text/javascript" src="js/mixin_webengine.js"></script>
128+
129+
''')
130+
return html
131+
132+
def keyPressEvent(self, event):
133+
""" Keypress callback for timeline """
134+
key_value = event.key()
135+
if (key_value == Qt.Key_Shift or key_value == Qt.Key_Control):
136+
137+
# Only pass a few keystrokes to the webview (CTRL and SHIFT)
138+
return QWebEngineView.keyPressEvent(self, event)
139+
140+
else:
141+
# Ignore most keypresses
142+
event.ignore()
143+
144+
145+
class LoggingWebPage(QWebPage):
146+
"""Override console.log message to display messages"""
147+
def javaScriptConsoleMessage(self, msg, line, source):
148+
log.warning('JS: %s line %d: %s' % (source, line, msg))
117149

118150
class TimelineQtWebKitMixin(TimelineBaseMixin, QWebView):
119151
"""QtWebKit Timeline Widget"""
@@ -126,11 +158,15 @@ def __init__(self):
126158
# Delete the webview when closed
127159
self.setAttribute(Qt.WA_DeleteOnClose)
128160

161+
# Connect logging web page (for console.log)
162+
page = LoggingWebPage()
163+
self.setPage(page)
164+
129165
# Disable image caching on timeline
130166
self.settings().setObjectCacheCapacities(0, 0, 0)
131167

132168
# Set url from configuration (QUrl takes absolute paths for file system paths, create from QFileInfo)
133-
self.setUrl(QUrl.fromLocalFile(QFileInfo(self.html_path).absoluteFilePath()))
169+
self.setHtml(self.get_html(), QUrl.fromLocalFile(QFileInfo(self.html_path).absoluteFilePath()))
134170

135171
# Connect signal of javascript initialization to our javascript reference init function
136172
self.page().mainFrame().javaScriptWindowObjectCleared.connect(self.setup_js_data)
@@ -152,7 +188,8 @@ def run_js(self, code, callback=None, retries=0):
152188
else:
153189
# Execute JS code
154190
if callback:
155-
return self.page().mainFrame().evaluateJavaScript(code, callback)
191+
# Pass output to callback
192+
callback(self.page().mainFrame().evaluateJavaScript(code))
156193
else:
157194
return self.page().mainFrame().evaluateJavaScript(code)
158195

@@ -161,6 +198,27 @@ def setup_js_data(self):
161198
self.page().mainFrame().addToJavaScriptWindowObject('timeline', self)
162199
self.page().mainFrame().addToJavaScriptWindowObject('mainWindow', self.window)
163200

201+
def get_html(self):
202+
"""Get HTML for Timeline, adjusted for mixin"""
203+
html = open(self.html_path, 'r', encoding='utf-8').read()
204+
html = html.replace('{{MIXIN_JS_INCLUDE}}',
205+
'''
206+
<script type="text/javascript" src="js/mixin_webkit.js"></script>
207+
208+
''')
209+
return html
210+
211+
def keyPressEvent(self, event):
212+
""" Keypress callback for timeline """
213+
key_value = event.key()
214+
if (key_value == Qt.Key_Shift or key_value == Qt.Key_Control):
215+
216+
# Only pass a few keystrokes to the webview (CTRL and SHIFT)
217+
return QWebView.keyPressEvent(self, event)
218+
219+
else:
220+
# Ignore most keypresses
221+
event.ignore()
164222

165223
# Set correct Mixin (with QtWebEngine preference)
166224
if IS_WEBENGINE_VALID:

src/windows/views/timeline_webview.py

-12
Original file line numberDiff line numberDiff line change
@@ -2748,18 +2748,6 @@ def update_zoom(self, newValue):
27482748
get_app().updates.update(["scale"], newScale)
27492749
get_app().updates.ignore_history = False
27502750

2751-
def keyPressEvent(self, event):
2752-
""" Keypress callback for timeline """
2753-
key_value = event.key()
2754-
if (key_value == Qt.Key_Shift or key_value == Qt.Key_Control):
2755-
2756-
# Only pass a few keystrokes to the webview (CTRL and SHIFT)
2757-
return QWebEngineView.keyPressEvent(self, event)
2758-
2759-
else:
2760-
# Ignore most keypresses
2761-
event.ignore()
2762-
27632751
# Capture wheel event to alter zoom slider control
27642752
def wheelEvent(self, event):
27652753
if int(QCoreApplication.instance().keyboardModifiers() & Qt.ControlModifier) > 0:

0 commit comments

Comments
 (0)