-
Notifications
You must be signed in to change notification settings - Fork 0
/
layerselection_widget.py
111 lines (93 loc) · 3.83 KB
/
layerselection_widget.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
import os
from contextlib import contextmanager
from qgis.PyQt import uic, QtCore
from qgis.PyQt.QtWidgets import QApplication, QWidget, QGroupBox, QPushButton
from qgis.gui import QgsMapLayerComboBox, QgsFieldComboBox
from qgis.core import Qgis, QgsMapLayer, QgsMapLayerProxyModel, QgsProject, QgsVectorLayer, QgsAggregateCalculator, \
QgsVectorLayerUtils, QgsExpression
from qgis.utils import iface, QgsMessageLog
from .exprutils import ExprUtils
@contextmanager
def waitCursor():
try:
QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
yield
except Exception as ex:
raise ex
finally:
QApplication.restoreOverrideCursor()
FORM_CLASS, _ = uic.loadUiType(os.path.join(os.path.dirname(__file__), 'layerselection_widget.ui'))
class LayerSelectionWidget(QWidget, FORM_CLASS):
addFacet = QtCore.pyqtSignal(str)
removeAllFacets = QtCore.pyqtSignal()
layerChanged = QtCore.pyqtSignal(QgsVectorLayer)
mapVlayerCb: QgsMapLayerComboBox
addFacetBtn: QPushButton
layerFieldsCb: QgsFieldComboBox
def __init__(self, parent=None):
"""Constructor."""
super().__init__(parent)
self.setupUi(self)
self._initConnections()
self._layer: QgsVectorLayer = None
self._disableComponents(True)
self.layerFieldsCb.setFilters(ExprUtils.supportedTypes())
def _initConnections(self):
self.mapVlayersCb.setFilters(QgsMapLayerProxyModel.VectorLayer)
self.mapVlayersCb.setCurrentIndex(-1) # TODO bringt nix :(
self.mapVlayersCb.layerChanged.connect(self._layerChanged)
self.mapVlayersCb.activated.connect(self._layerActivated)
self.layerFieldsCb.fieldChanged.connect(self._layerFieldChanged)
self.addFacetBtn.clicked.connect(self._addFacet)
self.removeAllBtn.clicked.connect(self._removeAllFacets)
inst: QgsProject = QgsProject.instance()
inst.layerRemoved.connect(self._layerRemoved)
def layer(self):
return self._layer
def _disableComponents(self, disabled: bool):
if disabled:
self.layerFieldsCb.clear()
self.addFacetBtn.setDisabled(True)
self.layerFieldsCb.setDisabled(True)
self.removeAllBtn.setDisabled(True)
else:
# self.addFacetBtn.setDisabled(False)
self.layerFieldsCb.setDisabled(False)
self.layerFieldsCb.clear()
def _layerFieldChanged(self, field: str):
if field != "":
self.addFacetBtn.setDisabled(False)
self.removeAllBtn.setDisabled(False)
else:
self.addFacetBtn.setDisabled(True)
self.removeAllBtn.setDisabled(True)
def _layerRemoved(self, layerId):
try:
if self._layer.id() == layerId: # layer removed
self._layer = None
self.layerChanged.emit(self._layer)
self._disableComponents(True)
except: # removed
self._layer = None
self.layerChanged.emit(self._layer)
self._disableComponents(True)
def _layerActivated(self, idx):
layer: QgsVectorLayer = self.mapVlayersCb.currentLayer()
if self._layer != layer:
self._layer = layer
self.layerChanged.emit(self._layer)
iface.setActiveLayer(layer)
self._disableComponents(False)
self.layerFieldsCb.setLayer(layer)
def _layerChanged(self, layer: QgsMapLayer):
if self._layer != layer:
self._layer = layer
self.layerChanged.emit(self._layer)
self._disableComponents(False)
self.layerFieldsCb.setLayer(layer)
def _addFacet(self):
with waitCursor():
self.addFacet.emit(self.layerFieldsCb.currentField())
def _removeAllFacets(self):
with waitCursor():
self.removeAllFacets.emit()