-
Notifications
You must be signed in to change notification settings - Fork 0
/
facetedsearch.py
52 lines (43 loc) · 1.73 KB
/
facetedsearch.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
from abc import ABC
from qgis.core import QgsVectorLayer, QgsExpression
from .facet_widget import FacetWidget
class FacetedSearch(ABC):
def __init__(self):
self._widgets = []
self._layer: QgsVectorLayer = None
def setLayer(self, layer: QgsVectorLayer):
self._layer = layer
self._widgets = []
def setWidgets(self, widgets: []):
self._widgets = widgets
self._disableSelectWidgets(0)
if self._layer.subsetString():
self._layer.setSubsetString("")
if len(self._widgets) > -1:
self._enableSelectWidget(0)
def _enableSelectWidget(self, idx: int):
if idx < len(self._widgets):
widget: FacetWidget = self._widgets[idx]
widget.disable(False)
widget.reset(self._layer)
def _disableSelectWidgets(self, start: int):
for idx in range(start, len(self._widgets), 1):
fw: FacetWidget = self._widgets[idx]
fw.disable(True)
fw.clear()
def search(self, idx: int):
self._disableSelectWidgets(idx + 1)
subset: str = self._subsetFor(idx)
self._layer.setSubsetString(subset)
self._enableSelectWidget(idx + 1)
def _subsetFor(self, idx: int):
subsets = []
for i in range(0, idx + 1, 1):
widget: FacetWidget = self._widgets[i]
if widget.value() is None or widget.value() == "":
break
searchExpr: QgsExpression = widget.searchExpr().expression()
newExpr = QgsExpression(searchExpr.replace("[%{}%]".format(widget.field()), widget.value()))
expr: str = "({})".format(newExpr.expression())
subsets.append(expr)
return " and ".join(subsets)