-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
q3dcontroller.py
509 lines (387 loc) · 17.3 KB
/
q3dcontroller.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# -*- coding: utf-8 -*-
# (C) 2016 Minoru Akagi
# SPDX-License-Identifier: GPL-2.0-or-later
# begin: 2016-02-10
import time
from PyQt5.QtCore import QObject, QTimer, pyqtSignal, pyqtSlot, qDebug
from qgis.core import QgsApplication
from .conf import DEBUG_MODE
from .build import ThreeJSBuilder
from .exportsettings import ExportSettings
from .q3dcore import Layer
from .q3dconst import LayerType, Script
from .utils import hex_color, js_bool, logMessage
class Q3DControllerInterface(QObject):
# signals - controller iface to viewer iface
dataSent = pyqtSignal(dict) # data
scriptSent = pyqtSignal(str, object, str) # script, data, msg_shown_in_log_panel
statusMessage = pyqtSignal(str, int) # message, timeout_ms
progressUpdated = pyqtSignal(int, str) # percentage, msg
loadScriptsRequest = pyqtSignal(list, bool) # list of script ID, force (if False, do not load a script that is already loaded)
readyToQuit = pyqtSignal()
def __init__(self, controller=None):
super().__init__(parent=controller)
self.controller = controller
self.iface = None
def connectToIface(self, iface):
"""iface: web view side interface (Q3DInterface or its subclass)"""
self.iface = iface
self.dataSent.connect(iface.sendJSONObject)
self.scriptSent.connect(iface.runScript)
self.loadScriptsRequest.connect(iface.loadScriptFiles)
self.statusMessage.connect(iface.statusMessage)
self.progressUpdated.connect(iface.progressUpdated)
if hasattr(iface, "abortRequest"):
iface.abortRequest.connect(self.controller.abort)
iface.buildSceneRequest.connect(self.controller.requestBuildScene)
iface.buildLayerRequest.connect(self.controller.requestBuildLayer)
iface.updateWidgetRequest.connect(self.controller.requestUpdateWidget)
iface.runScriptRequest.connect(self.controller.requestRunScript)
iface.updateExportSettingsRequest.connect(self.controller.updateExportSettings)
iface.cameraChanged.connect(self.controller.switchCamera)
iface.navStateChanged.connect(self.controller.setNavigationEnabled)
iface.previewStateChanged.connect(self.controller.setPreviewEnabled)
iface.layerAdded.connect(self.controller.addLayer)
iface.layerRemoved.connect(self.controller.removeLayer)
def disconnectFromIface(self):
iface = self.iface
self.dataSent.disconnect(iface.sendJSONObject)
self.scriptSent.disconnect(iface.runScript)
self.loadScriptsRequest.disconnect(iface.loadScriptFiles)
self.statusMessage.disconnect(iface.statusMessage)
self.progressUpdated.disconnect(iface.progressUpdated)
if hasattr(iface, "abortRequest"):
iface.abortRequest.disconnect(self.controller.abort)
iface.buildSceneRequest.disconnect(self.controller.requestBuildScene)
iface.buildLayerRequest.disconnect(self.controller.requestBuildLayer)
iface.updateWidgetRequest.disconnect(self.controller.requestUpdateWidget)
iface.runScriptRequest.disconnect(self.controller.requestRunScript)
iface.updateExportSettingsRequest.disconnect(self.controller.updateExportSettings)
iface.cameraChanged.disconnect(self.controller.switchCamera)
iface.navStateChanged.disconnect(self.controller.setNavigationEnabled)
iface.previewStateChanged.disconnect(self.controller.setPreviewEnabled)
iface.layerAdded.disconnect(self.controller.addLayer)
iface.layerRemoved.disconnect(self.controller.removeLayer)
self.iface = None
def sendJSONObject(self, obj):
self.dataSent.emit(obj)
def runScript(self, string, data=None, msg=""):
self.scriptSent.emit(string, data, msg)
def showStatusMessage(self, msg, timeout_ms=0):
"""show message in status bar"""
self.statusMessage.emit(msg, timeout_ms)
def clearStatusMessage(self):
"""clear message in status bar"""
self.statusMessage.emit("", 0)
def showMessageBar(self, msg, timeout_ms=0, warning=False):
"""show message bar at top of web view"""
self.runScript("showMessageBar(pyData(), {}, {})".format(timeout_ms, js_bool(warning)), data=msg)
def progress(self, percentage=100, msg=""):
self.progressUpdated.emit(int(percentage), msg)
def loadScriptFile(self, id, force=False):
self.loadScriptsRequest.emit([id], force)
def loadScriptFiles(self, ids, force=False):
self.loadScriptsRequest.emit(ids, force)
class Q3DController(QObject):
# requests
BUILD_SCENE_ALL = 1 # build scene
BUILD_SCENE = 2 # build scene, but do not update background color, coordinates display mode and so on
RELOAD_PAGE = 3
def __init__(self, settings=None, thread=None, parent=None):
super().__init__(parent)
if settings is None:
defaultSettings = {}
settings = ExportSettings()
settings.loadSettings(defaultSettings)
err_msg = settings.checkValidity()
if err_msg:
logMessage("Invalid settings: " + err_msg, warning=True)
self.settings = settings
self.builder = ThreeJSBuilder(settings)
self.iface = Q3DControllerInterface(self)
self.iface.setObjectName("controllerInterface")
self.enabled = True
self.aborted = False # layer export aborted
self.buildingLayer = None
self.mapCanvas = None
self.requestQueue = []
self.timer = QTimer(self)
self.timer.setInterval(1)
self.timer.setSingleShot(True)
# move to worker thread
if thread:
self.moveToThread(thread)
self.timer.timeout.connect(self._processRequests)
def teardown(self):
self.timer.stop()
self.timer.timeout.disconnect(self._processRequests)
self.iface.deleteLater()
self.iface = None
def connectToIface(self, iface):
"""iface: Q3DInterface or its subclass"""
self.iface.connectToIface(iface)
def disconnectFromIface(self):
self.iface.disconnectFromIface()
# self.iface = Mock()
def connectToMapCanvas(self, canvas):
self.mapCanvas = canvas
self.mapCanvas.renderComplete.connect(self._requestBuildScene)
# self.mapCanvas.extentsChanged.connect(self.updateExtent)
def disconnectFromMapCanvas(self):
if self.mapCanvas:
self.mapCanvas.renderComplete.disconnect(self._requestBuildScene)
# self.mapCanvas.extentsChanged.disconnect(self.updateExtent)
self.mapCanvas = None
def buildScene(self, update_scene_opts=True, build_layers=True, update_extent=True):
if self.buildingLayer:
logMessage("Previous building is still in progress. Cannot start to build scene.")
return False
self.aborted = False
self.iface.progress(0, "Building scene")
if update_extent and self.mapCanvas:
self.builder.settings.setMapSettings(self.mapCanvas.mapSettings())
self.iface.sendJSONObject(self.builder.buildScene(False))
if update_scene_opts:
sp = self.settings.sceneProperties()
# outline effect
self.iface.runScript("setOutlineEffectEnabled({})".format(js_bool(sp.get("checkBox_Outline"))))
# update background color
params = "{0}, 1".format(hex_color(sp.get("colorButton_Color", 0), prefix="0x")) if sp.get("radioButton_Color") else "0, 0"
self.iface.runScript("setBackgroundColor({0})".format(params))
# coordinate display
self.iface.runScript("Q3D.Config.coord.visible = {};".format(js_bool(self.settings.coordDisplay())))
latlon = self.settings.isCoordLatLon()
self.iface.runScript("Q3D.Config.coord.latlon = {};".format(js_bool(latlon)))
if latlon:
self.iface.loadScriptFile(Script.PROJ4)
if build_layers:
self.buildLayers()
self.iface.progress()
self.iface.clearStatusMessage()
return not self.aborted
def buildLayers(self):
self.aborted = False
self.iface.runScript('loadStart("LYRS", true)')
ret = True
layers = self.settings.layers()
for layer in sorted(layers, key=lambda lyr: lyr.type):
if layer.visible:
if not self._buildLayer(layer) or self.aborted:
ret = False
break
self.iface.runScript('loadEnd("LYRS")')
return ret
def buildLayer(self, layer):
self.aborted = False
if isinstance(layer, dict):
layer = Layer.fromDict(layer)
if self.buildingLayer:
logMessage('Previous building is still in progress. Cannot start building layer "{}".'.format(layer.name))
return False
ret = self._buildLayer(layer)
self.iface.progress()
self.iface.clearStatusMessage()
if ret and len(self.settings.layersToExport()) == 1:
self.iface.runScript("adjustCameraPos()")
return ret
def _buildLayer(self, layer):
self.buildingLayer = layer
pmsg = "Building {0}...".format(layer.name)
self.iface.progress(0, pmsg)
if layer.type == LayerType.POINT and layer.properties.get("comboBox_ObjectType") == "3D Model":
self.iface.loadScriptFiles([Script.COLLADALOADER,
Script.GLTFLOADER])
elif layer.type == LayerType.LINESTRING and layer.properties.get("comboBox_ObjectType") == "Thick Line":
self.iface.loadScriptFiles([Script.MESHLINE])
elif layer.type == LayerType.POINTCLOUD:
self.iface.loadScriptFiles([Script.FETCH,
Script.POTREE,
Script.PCLAYER])
t0 = t4 = time.time()
dlist = []
i = 0
for builder in self.builder.layerBuilders(layer):
self.iface.progress(i / (i + 4) * 100, pmsg)
if self.aborted:
logMessage("***** layer building aborted *****")
self.buildingLayer = None
return False
t1 = time.time()
obj = builder.build()
t2 = time.time()
if obj:
self.iface.sendJSONObject(obj)
QgsApplication.processEvents() # NOTE: process events only for the calling thread
i += 1
t3 = time.time()
dlist.append([t1 - t4, t2 - t1, t3 - t2])
t4 = t3
if DEBUG_MODE:
dlist = "\n".join([" {:.3f} {:.3f} {:.3f}".format(d[0], d[1], d[2]) for d in dlist])
qDebug("{0} layer updated: {1:.3f}s\n{2}\n".format(layer.name,
time.time() - t0,
dlist).encode("utf-8"))
self.buildingLayer = None
return True
def hideLayer(self, layer):
"""hide layer and remove all objects from the layer"""
self.iface.runScript('hideLayer("{}", true)'.format(layer.jsLayerId))
def hideAllLayers(self):
"""hide all layers and remove all objects from the layers"""
self.iface.runScript("hideAllLayers(true)")
def processRequests(self):
self.timer.stop()
if self.requestQueue:
self.timer.start()
def _processRequests(self):
if not self.enabled or self.buildingLayer or not self.requestQueue:
return
try:
if self.BUILD_SCENE_ALL in self.requestQueue:
self.requestQueue.clear()
self.buildScene()
elif self.BUILD_SCENE in self.requestQueue:
self.requestQueue.clear()
self.buildScene(update_scene_opts=False)
elif self.RELOAD_PAGE in self.requestQueue:
self.requestQueue.clear()
self.iface.runScript("location.reload()")
else:
item = self.requestQueue.pop(0)
if isinstance(item, Layer):
if item.visible:
self.buildLayer(item)
else:
self.hideLayer(item)
else:
self.iface.runScript(item.get("string"), item.get("data"))
except Exception as e:
import traceback
logMessage(traceback.format_exc(), warning=True)
self.iface.showMessageBar("One or more errors occurred. See log messages panel in QGIS main window for details.", warning=True)
self.processRequests()
@pyqtSlot(bool)
def abort(self, clear_queue=True):
if clear_queue:
self.requestQueue.clear()
if not self.aborted:
self.aborted = True
self.iface.showStatusMessage("Aborting processing...")
@pyqtSlot()
def quit(self):
self.abort()
self.iface.readyToQuit.emit()
self.teardown()
@pyqtSlot(object, bool, bool)
def requestBuildScene(self, properties=None, update_all=True, reload=False):
if DEBUG_MODE:
logMessage("Scene update requested: {}".format(properties))
if properties:
self.settings.setSceneProperties(properties)
if reload:
r = self.RELOAD_PAGE
elif update_all:
r = self.BUILD_SCENE_ALL
else:
r = self.BUILD_SCENE
self.requestQueue.append(r)
if self.buildingLayer:
self.abort(clear_queue=False)
else:
self.processRequests()
@pyqtSlot(Layer)
def requestBuildLayer(self, layer):
if DEBUG_MODE:
logMessage("Layer update for {} requested ({}).".format(layer.layerId, "visible" if layer.visible else "hidden"))
# update layer properties and layer state in worker side export settings
lyr = self.settings.getLayer(layer.layerId)
if not lyr:
return
layer.copyTo(lyr)
q = []
for i in self.requestQueue:
if isinstance(i, Layer) and i.layerId == layer.layerId:
if not i.opt.onlyMaterial:
layer.opt.onlyMaterial = False
else:
q.append(i)
self.requestQueue = q
if self.buildingLayer and self.buildingLayer.layerId == layer.layerId:
self.abort(clear_queue=False)
if not self.buildingLayer.opt.onlyMaterial:
layer.opt.onlyMaterial = False
if layer.visible:
self.requestQueue.append(layer)
if not self.buildingLayer:
self.processRequests()
else:
# immediately hide layer without adding layer to queue
self.hideLayer(layer)
@pyqtSlot(str, dict)
def requestUpdateWidget(self, name, properties):
if name == "NorthArrow":
self.iface.runScript("setNorthArrowColor({0})".format(properties.get("color", 0)))
self.iface.runScript("setNorthArrowVisible({0})".format(js_bool(properties.get("visible"))))
elif name == "Label":
self.iface.runScript('setHFLabel(pyData());', data=properties)
else:
return
self.settings.setWidgetProperties(name, properties)
@pyqtSlot(str, object)
def requestRunScript(self, string, data=None):
self.requestQueue.append({"string": string, "data": data})
if not self.buildingLayer:
self.processRequests()
@pyqtSlot(ExportSettings)
def updateExportSettings(self, settings):
if self.buildingLayer:
self.abort()
self.hideAllLayers()
settings.copyTo(self.settings)
# reload page
self.iface.runScript("location.reload()")
@pyqtSlot(bool)
def switchCamera(self, is_ortho=False):
self.settings.setCamera(is_ortho)
self.iface.runScript("switchCamera({0})".format(js_bool(is_ortho)))
@pyqtSlot(bool)
def setNavigationEnabled(self, enabled):
self.settings.setNavigationEnabled(enabled)
self.iface.runScript("setNavigationEnabled({0})".format(js_bool(enabled)))
@pyqtSlot(bool)
def setPreviewEnabled(self, enabled):
self.enabled = enabled
self.iface.runScript("setPreviewEnabled({})".format(js_bool(enabled)))
if enabled:
self.buildScene()
else:
self.abort()
@pyqtSlot(Layer)
def addLayer(self, layer):
layer = self.settings.addLayer(layer)
self.buildLayer(layer)
@pyqtSlot(str)
def removeLayer(self, layerId):
layer = self.settings.getLayer(layerId)
if layer:
self.hideLayer(layer)
self.settings.removeLayer(layerId)
# @pyqtSlot(QPainter)
def _requestBuildScene(self, _=None):
self.requestBuildScene(update_all=False)
# @pyqtSlot()
# def updateExtent(self):
# if self.settings.sceneProperties().get("radioButton_FixedExtent"):
# return
# self.requestQueue.clear()
# if self.buildingLayer:
# self.abort(clear_queue=False)
class Mock:
def __init__(self, *args, **kwargs):
pass
def __getattr__(self, attr):
if DEBUG_MODE:
logMessage("Mock: {}".format(attr))
return Mock
def __bool__(self):
return False