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
@@ -91,34 +89,50 @@ def __init__(self) -> None:
91
89
self .template_dock .library_selection .currentIndexChanged .connect (
92
90
lambda : self .set_active_library (self .template_dock .library_selection .currentText ())
93
91
)
92
+ # Update template list when search text changes
93
+ self .template_dock .search_box .valueChanged .connect (self .on_template_search_text_changed )
94
94
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 )
97
97
98
98
self .digitize_map_tool = TemplateGeometryDigitizeMapTool (iface .mapCanvas (), iface .cadDockWidget ())
99
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 )
100
101
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 )
104
111
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
+ )
112
116
113
- def _uncheck_others (self , item : QStandardItem ) -> None :
117
+ def on_template_search_text_changed (self , search_text : str ) :
114
118
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 )
118
127
119
128
def start_digitizing_for_layer (self , layer : QgsVectorLayer ) -> None :
120
129
self .digitize_map_tool .clean ()
121
130
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
122
136
iface .mapCanvas ().setMapTool (self .digitize_map_tool )
123
137
124
138
def ask_for_feature_attributes (self , feature : QgsFeature ) -> None :
@@ -138,11 +152,6 @@ def ask_for_feature_attributes(self, feature: QgsFeature) -> None:
138
152
attribute ,
139
153
widget .text (),
140
154
)
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
146
155
147
156
layer .beginEditCommand ("Create feature from template" )
148
157
layer .addFeature (feature )
0 commit comments