Skip to content

Commit

Permalink
webengine: runScript synchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
minorua committed Nov 16, 2023
1 parent 62275ea commit f00eec3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
4 changes: 2 additions & 2 deletions proppages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,8 +1052,8 @@ def __init__(self, parent, layer):
self.setProperties(layer.properties)

wnd = self.parent().parent()
loaded = wnd.runScript("app.scene.mapLayers[{}].loadedPointCount()".format(layer.jsLayerId))
visible = wnd.runScript("app.scene.mapLayers[{}].pcg.children[0].numVisiblePoints".format(layer.jsLayerId))
loaded = wnd.runScript("app.scene.mapLayers[{}].loadedPointCount()".format(layer.jsLayerId), forceSync=True)
visible = wnd.runScript("app.scene.mapLayers[{}].pcg.children[0].numVisiblePoints".format(layer.jsLayerId), forceSync=True)

total = bbox = None

Expand Down
39 changes: 31 additions & 8 deletions q3dwebengineview.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--ignore-gpu-blocklist --enable-gpu-rasterization"

from PyQt5.QtCore import Qt, QSize, QUrl
from PyQt5.QtCore import Qt, QEventLoop, QSize, QUrl
from PyQt5.QtGui import QImage, QPainter
from PyQt5.QtWidgets import QDialog, QVBoxLayout
from PyQt5.QtWebChannel import QWebChannel
Expand Down Expand Up @@ -46,19 +46,42 @@ def reload(self):

self.setUrl(self.myUrl)

def runScript(self, string, data=None, message="", sourceID="q3dview.py", callback=None):
Q3DWebPageCommon.runScript(self, string, data, message, sourceID)
def runScript(self, string, data=None, message="", sourceID="q3dview.py", callback=None, forceSync=False):
"""forceSync: whether to run script synchronously"""
Q3DWebPageCommon.runScript(self, string, data, message, sourceID, callback, forceSync)

if data is not None:
assert callback is None, "cannot callback"
assert callback is None, "cannot callback when data is set"
assert not forceSync, "synchronous script execution with data not supported"

self.bridge.sendScriptData.emit(string, data)
return

elif callback:
self.runJavaScript(string, callback)
if not forceSync:
if callback:
self.runJavaScript(string, callback)

else:
self.runJavaScript(string)
else:
self.runJavaScript(string)

return

loop = QEventLoop()
result = None

def runJavaScriptCallback(res):
nonlocal result
result = res
loop.quit()

self.runJavaScript(string, runJavaScriptCallback)

loop.exec_()

if callback:
callback(result)

return result

def sendData(self, data):
self.bridge.sendScriptData.emit("loadJSONObject(pyData())", data)
Expand Down
4 changes: 2 additions & 2 deletions q3dwebkitview.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def reload(self):

self.mainFrame().setUrl(self.myUrl)

def runScript(self, string, data=None, message="", sourceID="q3dview.py", callback=None):
Q3DWebPageCommon.runScript(self, string, data, message, sourceID)
def runScript(self, string, data=None, message="", sourceID="q3dview.py", callback=None, forceSync=False):
Q3DWebPageCommon.runScript(self, string, data, message, sourceID, callback, forceSync)

if data is not None:
self.bridge.setData(data)
Expand Down
10 changes: 5 additions & 5 deletions q3dwebviewcommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def initialized(self):

self.wnd.showStatusMessage("")

def runScript(self, string, data=None, message="", sourceID="q3dview.py", callback=None):
def runScript(self, string, data=None, message="", sourceID="q3dview.py", callback=None, forceSync=False):
if not DEBUG_MODE or message is None:
return

Expand Down Expand Up @@ -112,7 +112,7 @@ def loadScriptFiles(self, ids, force=False):
self.loadScriptFile(id, force)

def cameraState(self, flat=False):
return self.runScript("cameraState({})".format(1 if flat else 0))
return self.runScript("cameraState({})".format(1 if flat else 0), forceSync=True)

def setCameraState(self, state):
"""set camera position and camera target"""
Expand Down Expand Up @@ -220,11 +220,11 @@ def dropEvent(self, event):
def sendData(self, data):
self._page.sendData(data)

def runScript(self, string, data=None, message="", sourceID="q3dview.py"):
return self._page.runScript(string, data, message, sourceID)
def runScript(self, string, data=None, message="", sourceID="q3dview.py", callback=None, forceSync=False):
return self._page.runScript(string, data, message, sourceID, callback, forceSync)

def showJSInfo(self):
info = self.runScript("app.renderer.info")
info = self.runScript("app.renderer.info", forceSync=True)
QMessageBox.information(self, "three.js Renderer Info", str(info))


Expand Down
6 changes: 3 additions & 3 deletions q3dwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,11 @@ def changeEvent(self, event):
else:
self.runScript("app.resume()")

def runScript(self, string, data=None, message="", sourceID="Q3DWindow.py"):
return self.ui.webView.runScript(string, data, message, sourceID=sourceID)
def runScript(self, string, data=None, message="", sourceID="Q3DWindow.py", callback=None, forceSync=False):
return self.webPage.runScript(string, data, message, sourceID, callback, forceSync)

def showMessageBar(self, msg, duration=0, warning=False):
return self.runScript("showMessageBar(pyData(), {}, {})".format(duration, js_bool(warning)), msg)
self.runScript("showMessageBar(pyData(), {}, {})".format(duration, js_bool(warning)), msg)

def showStatusMessage(self, message, duration=0):
self.ui.statusbar.showMessage(message, duration)
Expand Down

0 comments on commit f00eec3

Please sign in to comment.