Skip to content

Commit 4cda490

Browse files
nmaarnioLKajan
authored andcommitted
changes to visual representation of template library:
- change template activation from checking to clicking - fix selection visual when template list is not focused/active - unselect template when digitizing is cancelled
1 parent f3263d9 commit 4cda490

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

arho_feature_template/core/feature_template_library.py

+19-23
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
@@ -94,11 +92,27 @@ def __init__(self) -> None:
9492
# Update template list when search text changes
9593
self.template_dock.search_box.valueChanged.connect(self.on_template_search_text_changed)
9694

97-
# Activate map tool when template selection changes
98-
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)
9997

10098
self.digitize_map_tool = TemplateGeometryDigitizeMapTool(iface.mapCanvas(), iface.cadDockWidget())
10199
self.digitize_map_tool.digitizingCompleted.connect(self.ask_for_feature_attributes)
100+
self.digitize_map_tool.deactivated.connect(self.template_dock.template_list.clearSelection)
101+
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)
111+
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+
)
102116

103117
def on_template_search_text_changed(self, search_text: str):
104118
for row in range(self.template_model.rowCount()):
@@ -111,24 +125,6 @@ def on_template_search_text_changed(self, search_text: str):
111125
# Otherwise, hide the row
112126
self.template_dock.template_list.setRowHidden(row, True)
113127

114-
def on_item_changed(self, item: TemplateItem) -> None:
115-
if item.checkState() == Qt.Checked:
116-
self._uncheck_others(item)
117-
118-
try:
119-
layer = get_layer_from_project(item.config.feature.layer)
120-
except (LayerNotFoundError, LayerNotVectorTypeError):
121-
logger.exception("Failed to activate template")
122-
return
123-
self.active_template = item
124-
self.start_digitizing_for_layer(layer)
125-
126-
def _uncheck_others(self, item: QStandardItem) -> None:
127-
for row in range(self.template_model.rowCount()):
128-
other_item = self.template_model.item(row)
129-
if other_item != item and other_item.checkState() == Qt.Checked:
130-
other_item.setCheckState(Qt.Unchecked)
131-
132128
def start_digitizing_for_layer(self, layer: QgsVectorLayer) -> None:
133129
self.digitize_map_tool.clean()
134130
self.digitize_map_tool.setLayer(layer)

0 commit comments

Comments
 (0)