-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
q3dwebengineview.py
189 lines (131 loc) · 5.57 KB
/
q3dwebengineview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -*- coding: utf-8 -*-
# (C) 2023 Minoru Akagi
# SPDX-License-Identifier: GPL-2.0-or-later
# begin: 2023-10-03
import os
from PyQt5.QtCore import Qt, QEventLoop, QTimer, QUrl, pyqtSignal
from PyQt5.QtGui import QDesktopServices, QImage, QPainter
from PyQt5.QtWidgets import QDialog, QVBoxLayout
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, QWebEngineSettings
from .q3dwebviewcommon import Q3DWebPageCommon, Q3DWebViewCommon
def setChromiumFlags():
KEY = "QTWEBENGINE_CHROMIUM_FLAGS"
OPTIONS = ["--ignore-gpu-blocklist", "--enable-gpu-rasterization"]
if KEY in os.environ:
for opt in OPTIONS:
if opt not in os.environ[KEY]:
os.environ[KEY] += " " + opt
else:
os.environ[KEY] = " ".join(OPTIONS)
class Q3DWebEnginePage(Q3DWebPageCommon, QWebEnginePage):
jsErrorWarning = pyqtSignal(bool) # bool: is_error
def __init__(self, parent=None):
QWebEnginePage.__init__(self, parent)
Q3DWebPageCommon.__init__(self)
self.isWebEnginePage = True
def setup(self, settings, wnd=None):
"""wnd: Q3DWindow or None (off-screen mode)"""
Q3DWebPageCommon.setup(self, settings, wnd)
self.channel = QWebChannel(self)
self.channel.registerObject("bridge", self.bridge)
self.setWebChannel(self.channel)
# security setting for billboard, model file and point cloud layer
self.settings().setAttribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls, True)
url = os.path.join(os.path.abspath(os.path.dirname(__file__)), "viewer", "webengine.html").replace("\\", "/")
self.myUrl = QUrl.fromLocalFile(url)
self.reload()
def reload(self):
Q3DWebPageCommon.reload(self)
self.setUrl(self.myUrl)
def runScript(self, string, data=None, message="", sourceID="q3dview.py", callback=None, wait=False):
"""wait: whether to wait until script execution has completed"""
Q3DWebPageCommon.runScript(self, string, data, message, sourceID, callback, wait)
if data is not None:
assert callback is None, "cannot callback when data is set"
assert not wait, "synchronous script execution with data not supported"
self.bridge.sendScriptData.emit(string, data)
return
if not wait:
if callback:
self.runJavaScript(string, callback)
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)
def logToConsole(self, message, level="debug"):
self.runJavaScript('console.{}("{}");'.format(level, message.replace('"', '\\"')))
def javaScriptConsoleMessage(self, level, message, lineNumber, sourceID):
if level in (QWebEnginePage.WarningMessageLevel, QWebEnginePage.ErrorMessageLevel):
self.jsErrorWarning.emit(bool(level == QWebEnginePage.ErrorMessageLevel))
Q3DWebPageCommon.javaScriptConsoleMessage(self, message, lineNumber, sourceID)
def acceptNavigationRequest(self, url, type, isMainFrame):
if type == QWebEnginePage.NavigationTypeLinkClicked:
QDesktopServices.openUrl(url)
return False
return True
class Q3DWebEngineView(Q3DWebViewCommon, QWebEngineView):
def __init__(self, parent=None):
setChromiumFlags()
QWebEngineView.__init__(self, parent)
Q3DWebViewCommon.__init__(self)
self._page = Q3DWebEnginePage(self)
self._page.setObjectName("webEnginePage")
self.setPage(self._page)
def showDevTools(self):
if self._page.devToolsPage():
self.dlg.activateWindow()
return
dlg = self.dlg = QDialog(self)
dlg.setAttribute(Qt.WA_DeleteOnClose)
dlg.resize(800, 500)
dlg.setWindowTitle("Qgis2threejs Developer Tools")
dlg.rejected.connect(self.devToolsClosed)
ins = QWebEngineView(dlg)
self._page.setDevToolsPage(ins.page())
v = QVBoxLayout()
v.setContentsMargins(0, 0, 0, 0)
v.addWidget(ins)
dlg.setLayout(v)
dlg.show()
def showGPUInfo(self):
self.load(QUrl("chrome://gpu"))
# FIXME: unstable
def renderImage(self, width, height, callback, wnd=None):
if wnd:
geom = wnd.saveGeometry()
wnd.setEnabled(False)
img = QImage(width, height, QImage.Format_ARGB32)
painter = QPainter(img)
minSize = self.minimumSize()
maxSize = self.maximumSize()
self.setMinimumSize(width, height)
self.setMaximumSize(width, height)
def restoreGeom():
wnd.restoreGeometry(geom)
def myCallback(_=None):
self.render(painter)
painter.end()
callback(img)
self.setMinimumSize(minSize)
self.setMaximumSize(maxSize)
if wnd:
wnd.setEnabled(True)
QTimer.singleShot(200, restoreGeom)
def preCallback(_=None):
QTimer.singleShot(200, myCallback)
def requestRender():
self.runScript("app.render()", callback=preCallback)
QTimer.singleShot(1000, requestRender)