Skip to content

Commit 27268d2

Browse files
committed
Merge branch 'main' into create-new-land-use-plan
2 parents 0a86854 + ae4c791 commit 27268d2

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

arho_feature_template/core/feature_template_library.py

+33-24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from qgis.core import QgsFeature, QgsProject, QgsVectorLayer
66
from qgis.gui import QgsMapToolDigitizeFeature
7-
from qgis.PyQt.QtCore import Qt
7+
from qgis.PyQt.QtCore import QItemSelectionModel
88
from qgis.PyQt.QtGui import QStandardItem, QStandardItemModel
99
from qgis.utils import iface
1010

@@ -56,8 +56,6 @@ def __init__(self, template_config: FeatureTemplate) -> None:
5656
self.config = template_config
5757
super().__init__(template_config.name)
5858

59-
self.setCheckable(True)
60-
6159
def is_valid(self) -> bool:
6260
"""Check if the template is valid agains current QGIS project
6361
@@ -91,34 +89,50 @@ def __init__(self) -> None:
9189
self.template_dock.library_selection.currentIndexChanged.connect(
9290
lambda: self.set_active_library(self.template_dock.library_selection.currentText())
9391
)
92+
# Update template list when search text changes
93+
self.template_dock.search_box.valueChanged.connect(self.on_template_search_text_changed)
9494

95-
# Activate map tool when template selection changes
96-
self.template_model.itemChanged.connect(self.on_item_changed)
95+
# Activate map tool when template is selected
96+
self.template_dock.template_list.clicked.connect(self.on_template_item_clicked)
9797

9898
self.digitize_map_tool = TemplateGeometryDigitizeMapTool(iface.mapCanvas(), iface.cadDockWidget())
9999
self.digitize_map_tool.digitizingCompleted.connect(self.ask_for_feature_attributes)
100+
self.digitize_map_tool.deactivated.connect(self.template_dock.template_list.clearSelection)
100101

101-
def on_item_changed(self, item: TemplateItem) -> None:
102-
if item.checkState() == Qt.Checked:
103-
self._uncheck_others(item)
102+
def on_template_item_clicked(self, index):
103+
item = self.template_model.itemFromIndex(index)
104+
try:
105+
layer = get_layer_from_project(item.config.feature.layer)
106+
except (LayerNotFoundError, LayerNotVectorTypeError):
107+
logger.exception("Failed to activate template")
108+
return
109+
self.active_template = item
110+
self.start_digitizing_for_layer(layer)
104111

105-
try:
106-
layer = get_layer_from_project(item.config.feature.layer)
107-
except (LayerNotFoundError, LayerNotVectorTypeError):
108-
logger.exception("Failed to activate template")
109-
return
110-
self.active_template = item
111-
self.start_digitizing_for_layer(layer)
112+
# Reselect as a workaround for first selection visual clarity
113+
self.template_dock.template_list.selectionModel().select(
114+
index, QItemSelectionModel.Select | QItemSelectionModel.Rows
115+
)
112116

113-
def _uncheck_others(self, item: QStandardItem) -> None:
117+
def on_template_search_text_changed(self, search_text: str):
114118
for row in range(self.template_model.rowCount()):
115-
other_item = self.template_model.item(row)
116-
if other_item != item and other_item.checkState() == Qt.Checked:
117-
other_item.setCheckState(Qt.Unchecked)
119+
item = self.template_model.item(row)
120+
121+
# If the search text is in the item's text, show the row
122+
if search_text in item.text().lower():
123+
self.template_dock.template_list.setRowHidden(row, False)
124+
else:
125+
# Otherwise, hide the row
126+
self.template_dock.template_list.setRowHidden(row, True)
118127

119128
def start_digitizing_for_layer(self, layer: QgsVectorLayer) -> None:
120129
self.digitize_map_tool.clean()
121130
self.digitize_map_tool.setLayer(layer)
131+
if not layer.isEditable():
132+
succeeded = layer.startEditing()
133+
if not succeeded:
134+
logger.warning("Failed to start editing layer %s", layer.name())
135+
return
122136
iface.mapCanvas().setMapTool(self.digitize_map_tool)
123137

124138
def ask_for_feature_attributes(self, feature: QgsFeature) -> None:
@@ -138,11 +152,6 @@ def ask_for_feature_attributes(self, feature: QgsFeature) -> None:
138152
attribute,
139153
widget.text(),
140154
)
141-
if not layer.isEditable():
142-
succeeded = layer.startEditing()
143-
if not succeeded:
144-
logger.warning("Failed to start editing layer %s", layer.name())
145-
return
146155

147156
layer.beginEditCommand("Create feature from template")
148157
layer.addFeature(feature)

0 commit comments

Comments
 (0)