-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
layerselectdialog.py
97 lines (70 loc) · 3.02 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
# (C) 2015 Minoru Akagi
# SPDX-License-Identifier: GPL-2.0-or-later
# begin: 2015-04-22
from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QLabel, QVBoxLayout
from qgis.core import QgsLayerTreeModel, QgsMapLayerProxyModel, QgsProject
from qgis.gui import QgsMapLayerComboBox
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 visibleLayerIds(self):
return [layer.id() for layer in self.visibleLayers()]
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.setDestinationCrs(s.destinationCrs())
c.setRotation(s.rotation())
c.setExtent(s.extent())
self.canvasReady = True
self.ui.canvas.setLayers(self.visibleLayers())
class SingleLayerSelectDialog(QDialog):
def __init__(self, parent=None, label=""):
QDialog.__init__(self, parent)
vl = QVBoxLayout()
if label:
vl.addWidget(QLabel(label))
self.comboBox = QgsMapLayerComboBox()
self.comboBox.setFilters(QgsMapLayerProxyModel.HasGeometry | QgsMapLayerProxyModel.RasterLayer | QgsMapLayerProxyModel.MeshLayer)
vl.addWidget(self.comboBox)
self.buttonBox = QDialogButtonBox()
self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
vl.addWidget(self.buttonBox)
self.setLayout(vl)
def selectedLayer(self):
return self.comboBox.currentLayer()