forked from minorua/Qgis2threejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayerselectdialog.py
82 lines (65 loc) · 2.79 KB
/
layerselectdialog.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
LayerSelectDialog
-------------------
begin : 2015-04-22
copyright : (C) 2015 Minoru Akagi
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from PyQt5.QtWidgets import QDialog
from qgis.core import QgsLayerTreeModel, QgsProject
from .ui.layerselectdialog import Ui_LayerSelectDialog
class LayerSelectDialog(QDialog):
def __init__(self, parent):
QDialog.__init__(self, parent)
self.mapSettings = None
self.canvasReady = False # whether map settings have been applied to map canvas
# Set up the user interface from Designer.
self.ui = ui = Ui_LayerSelectDialog()
ui.setupUi(self)
ui.tabWidget.setTabEnabled(1, False)
ui.tabWidget.currentChanged.connect(self.tabPageChanged)
def initTree(self, visibleLayerIds=None):
ids = visibleLayerIds or []
self.root = QgsProject.instance().layerTreeRoot().clone()
for layer in self.root.findLayers():
layer.setItemVisibilityChecked(layer.layerId() in ids)
self.model = QgsLayerTreeModel(self.root)
self.model.setFlags(QgsLayerTreeModel.AllowNodeChangeVisibility)
self.ui.treeView.setModel(self.model)
def setMapSettings(self, mapSettings):
self.mapSettings = mapSettings
self.canvasReady = False
self.ui.tabWidget.setTabEnabled(1, bool(mapSettings))
def visibleLayers(self):
layers = []
for layer in self.root.findLayers():
if layer.isVisible():
layers.append(layer.layer())
return layers
def tabPageChanged(self, index):
if index == 1:
self.updatePreview()
def updatePreview(self):
if self.mapSettings is None:
return
if not self.canvasReady:
c = self.ui.canvas
s = self.mapSettings
c.setCanvasColor(s.backgroundColor())
c.setCrsTransformEnabled(s.hasCrsTransformEnabled())
c.setDestinationCrs(s.destinationCrs())
c.setRotation(s.rotation())
c.setExtent(s.extent())
self.canvasReady = True
self.ui.canvas.setLayers(self.visibleLayers())