-
Notifications
You must be signed in to change notification settings - Fork 2
/
layercombo.py
192 lines (171 loc) · 7.83 KB
/
layercombo.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
#-----------------------------------------------------------
#
# QGIS Combo Manager is a python module to easily manage a combo
# box with a layer list and eventually relate it with one or
# several combos with list of corresponding fields.
#
# Copyright : (C) 2013 Denis Rouzaud
# Email : denis.rouzaud@gmail.com
#
#-----------------------------------------------------------
#
# licensed under the terms of GNU GPL 2
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this progsram; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#---------------------------------------------------------------------
from PyQt4.QtCore import Qt, QObject, pyqtSignal
from qgis.core import QGis, QgsMapLayerRegistry, QgsMapLayer
import string
from optiondictionary import OptionDictionary
AvailableOptions = {"groupLayers": (False, True),
"hasGeometry": (None, False, True),
"geomType": (None, QGis.Point, QGis.Line, QGis.Polygon),
"dataProvider": None,
"finishInit": (True, False),
"legendInterface": None,
"skipLayers": list}
def remove_accents(data):
return filter(lambda char: char in string.ascii_uppercase, data.upper())
class LayerCombo(QObject):
layerChanged = pyqtSignal()
layerChangedLayer = pyqtSignal(QgsMapLayer)
def __init__(self, widget, initLayer="", options={}, layerType=None):
QObject.__init__(self, widget)
self.widget = widget
self.options = OptionDictionary(AvailableOptions, options)
if hasattr(initLayer, '__call__'):
self.initLayer = lambda: initLayer()
else:
self.initLayer = lambda: initLayer
self.layerType = layerType
self.widget.currentIndexChanged.connect(self.__currentIndexChanged)
# finish init (set to false if LayerCombo must be returned before items are completed)
if self.options.finishInit:
self.finishInit()
def __currentIndexChanged(self, dummy):
self.layerChanged.emit()
self.layerChangedLayer.emit(self.getLayer())
def finishInit(self):
# connect signal for layers and populate combobox
QgsMapLayerRegistry.instance().layersAdded.connect(self.__canvasLayersChanged)
QgsMapLayerRegistry.instance().layersRemoved.connect(self.__canvasLayersChanged)
if self.options.groupLayers:
self.options.legendInterface.groupRelationsChanged.connect(self.__canvasLayersChanged)
self.__canvasLayersChanged()
def getLayer(self):
i = self.widget.currentIndex()
if i < 0:
return None
layerId = self.widget.itemData(i)
return QgsMapLayerRegistry.instance().mapLayer(layerId)
def setLayer(self, layer):
if layer is None:
idx = -1
else:
if type(layer) == str:
layerid = layer
else:
layerid = layer.id()
idx = self.widget.findData(layerid, Qt.UserRole)
self.widget.setCurrentIndex(idx)
def __canvasLayersChanged(self, layerList=[]):
# Avoid errors when QGIS is closing
if QgsMapLayerRegistry is None:
return
self.widget.clear()
if not self.options.groupLayers:
layerList = dict()
for layerId, layer in QgsMapLayerRegistry.instance().mapLayers().iteritems():
if not self.__checkLayer(layer):
continue
layerList[remove_accents(layer.name())] = {"id": layerId, "name": layer.name()}
for key in sorted(layerList):
layerId = layerList[key]["id"]
self.widget.addItem(layerList[key]["name"], layerId)
i = self.widget.findData(self.initLayer(), Qt.UserRole)
self.widget.setCurrentIndex(i)
else:
if self.options.legendInterface is None:
raise NameError("Cannot display layers grouped if legendInterface is not given in the options.")
for layerGroup in self.options["legendInterface"].groupLayerRelationship():
groupName = layerGroup[0]
foundParent = False
insertPosition = self.widget.count()
indent = 0
for i in range(self.widget.count()):
lineData = self.widget.itemData(i) or []
if len(lineData) > 0 and lineData[0] == groupName:
foundParent = True
insertPosition = i+1
lineData[0] = "groupTaken"
self.widget.setItemData(i, lineData)
indent = lineData[1] + 1
break
if not foundParent and groupName != "":
self.__addLayerToCombo(groupName, insertPosition)
insertPosition += 1
indent += 1
for layerid in layerGroup[1]:
if self.__addLayerToCombo(layerid, insertPosition, indent):
insertPosition += 1
def __addLayerToCombo(self, layerid, position, indent=0):
layer = QgsMapLayerRegistry.instance().mapLayer(layerid)
preStr = " "*2*indent
if layer is None: # this is a group
# save in userdata a list ["group",indent]
self.widget.insertItem(position, preStr+layerid, [layerid, indent])
j = self.widget.model().index(position, 0)
self.widget.model().setData(j, 0, Qt.UserRole - 1)
else:
if not self.__checkLayer(layer):
return False
self.widget.insertItem(position, preStr+layer.name(), layer.id())
if layer.id() == self.initLayer():
self.widget.setCurrentIndex(position)
return True
def __checkLayer(self, layer):
# skip layer
for skip in self.options.skipLayers:
if hasattr(skip, '__call__'):
if layer.id() == skip():
return False
else:
if layer.id() == skip:
return False
# data provider
if self.options.dataProvider is not None and layer.dataProvider().name() != self.options.dataProvider:
return False
# vector layer
if self.layerType == QgsMapLayer.VectorLayer:
if layer.type() != QgsMapLayer.VectorLayer:
return False
# if wanted, filter on hasGeometry
if self.options.hasGeometry is not None and layer.hasGeometryType() != self.options.hasGeometry:
return False
# if wanted, filter on the geoetry type
if self.options.geomType is not None and layer.geometryType() != self.options.geomType:
return False
# raster layer
if self.layerType == QgsMapLayer.RasterLayer:
if layer.type() != QgsMapLayer.RasterLayer:
return False
return True
class VectorLayerCombo(LayerCombo):
def __init__(self, widget, initLayer="", options={}):
LayerCombo.__init__(self, widget, initLayer, options, QgsMapLayer.VectorLayer)
class RasterLayerCombo(LayerCombo):
def __init__(self, widget, initLayer="", options={}):
LayerCombo.__init__(self, widget, initLayer, options, QgsMapLayer.RasterLayer)