1
1
from __future__ import annotations
2
2
3
3
import logging
4
+ from collections import defaultdict
4
5
5
6
from qgis .core import QgsFeature , QgsProject , QgsVectorLayer
6
7
from qgis .gui import QgsMapToolDigitizeFeature
@@ -81,15 +82,19 @@ def __init__(self) -> None:
81
82
self .template_model = QStandardItemModel ()
82
83
self .template_dock .template_list .setModel (self .template_model )
83
84
85
+ # Set the selection mode to allow single selection
86
+ self .template_dock .template_list .setSelectionMode (self .template_dock .template_list .SingleSelection )
87
+
84
88
self ._read_library_configs ()
85
89
86
90
self .template_dock .library_selection .addItems (self .get_library_names ())
87
91
88
- # Update template list when library selection changes
92
+ # Update template tree when library selection changes
89
93
self .template_dock .library_selection .currentIndexChanged .connect (
90
94
lambda : self .set_active_library (self .template_dock .library_selection .currentText ())
91
95
)
92
- # Update template list when search text changes
96
+
97
+ # Update template tree when search text changes
93
98
self .template_dock .search_box .valueChanged .connect (self .on_template_search_text_changed )
94
99
95
100
# Activate map tool when template is selected
@@ -101,11 +106,17 @@ def __init__(self) -> None:
101
106
102
107
def on_template_item_clicked (self , index ):
103
108
item = self .template_model .itemFromIndex (index )
109
+
110
+ # Do nothing if clicked item is a group
111
+ if item .hasChildren ():
112
+ return
113
+
104
114
try :
105
115
layer = get_layer_from_project (item .config .feature .layer )
106
116
except (LayerNotFoundError , LayerNotVectorTypeError ):
107
117
logger .exception ("Failed to activate template" )
108
118
return
119
+
109
120
self .active_template = item
110
121
self .start_digitizing_for_layer (layer )
111
122
@@ -114,16 +125,29 @@ def on_template_item_clicked(self, index):
114
125
index , QItemSelectionModel .Select | QItemSelectionModel .Rows
115
126
)
116
127
117
- def on_template_search_text_changed (self , search_text : str ):
128
+ def on_template_search_text_changed (self , search_text : str ) -> None :
129
+ search_text = search_text .lower ()
130
+
118
131
for row in range (self .template_model .rowCount ()):
119
- item = self .template_model .item (row )
132
+ group_item = self .template_model .item (row )
133
+ group_visible = False
134
+
135
+ for child_row in range (group_item .rowCount ()):
136
+ template_item = group_item .child (child_row )
137
+ matches = search_text in template_item .text ().lower ()
138
+ template_item .setEnabled (matches )
139
+ group_item .setChild (child_row , template_item )
120
140
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 )
141
+ if matches :
142
+ group_visible = True
143
+
144
+ # Show or hide the group based on child matches
145
+ group_item .setEnabled (group_visible )
146
+
147
+ index = self .template_model .indexFromItem (group_item )
148
+ self .template_dock .template_list .setExpanded (index , group_visible )
149
+
150
+ self .template_model .setItem (row , group_item )
127
151
128
152
def start_digitizing_for_layer (self , layer : QgsVectorLayer ) -> None :
129
153
self .digitize_map_tool .clean ()
@@ -163,10 +187,26 @@ def get_library_names(self) -> list[str]:
163
187
def set_active_library (self , library_name : str ) -> None :
164
188
self .template_model .clear ()
165
189
190
+ # Group templates by their 'group' attribute, defaulting to "Ryhmittelemättömät" for ungrouped templates
191
+ grouped_templates = defaultdict (list )
166
192
for template in self .library_configs [library_name ].templates :
167
- item = TemplateItem (template )
168
- item .setEditable (False )
169
- self .template_model .appendRow (item )
193
+ group = getattr (template , "group" , None )
194
+ if not group :
195
+ group = "Ryhmittelemättömät"
196
+ grouped_templates [group ].append (template )
197
+
198
+ for group_name , templates in grouped_templates .items ():
199
+ group_item = QStandardItem (group_name )
200
+ group_item .setEditable (False )
201
+
202
+ for template in templates :
203
+ template_item = TemplateItem (template )
204
+ template_item .setEditable (False )
205
+ group_item .appendRow (template_item )
206
+
207
+ self .template_model .appendRow (group_item )
208
+
209
+ self .template_dock .template_list .expandAll ()
170
210
171
211
def _read_library_configs (self ) -> None :
172
212
for config_file in library_config_files ():
0 commit comments