4
4
5
5
from qgis .core import QgsFeature , QgsProject , QgsVectorLayer
6
6
from qgis .gui import QgsMapToolDigitizeFeature
7
- from qgis .PyQt .QtCore import Qt
7
+ from qgis .PyQt .QtCore import QItemSelectionModel
8
8
from qgis .PyQt .QtGui import QStandardItem , QStandardItemModel
9
9
from qgis .utils import iface
10
10
@@ -56,8 +56,6 @@ def __init__(self, template_config: FeatureTemplate) -> None:
56
56
self .config = template_config
57
57
super ().__init__ (template_config .name )
58
58
59
- self .setCheckable (True )
60
-
61
59
def is_valid (self ) -> bool :
62
60
"""Check if the template is valid agains current QGIS project
63
61
@@ -94,11 +92,27 @@ def __init__(self) -> None:
94
92
# Update template list when search text changes
95
93
self .template_dock .search_box .valueChanged .connect (self .on_template_search_text_changed )
96
94
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 )
99
97
100
98
self .digitize_map_tool = TemplateGeometryDigitizeMapTool (iface .mapCanvas (), iface .cadDockWidget ())
101
99
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
+ )
102
116
103
117
def on_template_search_text_changed (self , search_text : str ):
104
118
for row in range (self .template_model .rowCount ()):
@@ -111,24 +125,6 @@ def on_template_search_text_changed(self, search_text: str):
111
125
# Otherwise, hide the row
112
126
self .template_dock .template_list .setRowHidden (row , True )
113
127
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
-
132
128
def start_digitizing_for_layer (self , layer : QgsVectorLayer ) -> None :
133
129
self .digitize_map_tool .clean ()
134
130
self .digitize_map_tool .setLayer (layer )
0 commit comments