Skip to content

Commit 45533a2

Browse files
committed
refactor existing assocation check, add comments to HierarchicalCodeComboBox
1 parent 60aba95 commit 45533a2

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

arho_feature_template/core/plan_manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,9 @@ def save_regulation_group_as_config(regulation_group: RegulationGroup):
493493

494494

495495
def save_regulation_group_association(regulation_group_id: str, layer_name: str, feature_id: str):
496-
feature = RegulationGroupAssociationLayer.feature_from(regulation_group_id, layer_name, feature_id)
497-
if not feature:
496+
if RegulationGroupAssociationLayer.association_exists(regulation_group_id, layer_name, feature_id):
498497
return
498+
feature = RegulationGroupAssociationLayer.feature_from(regulation_group_id, layer_name, feature_id)
499499
layer = RegulationGroupAssociationLayer.get_from_project()
500500

501501
_save_feature(feature=feature, layer=layer, id_=None, edit_text="Kaavamääräysryhmän assosiaation lisäys")

arho_feature_template/gui/components/code_combobox.py

+24-14
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def set_value(self, value: str | None) -> None:
4848
if index != -1:
4949
self.setCurrentIndex(index)
5050
else:
51-
self.setCurrentIndex(0) # Set NULL if not found
51+
self.setCurrentIndex(0) # Set selection to NULL if item with `value` was not found
5252

5353

5454
class HierarchicalCodeComboBox(QComboBox):
@@ -102,29 +102,39 @@ def value(self) -> str:
102102
item = self.tree_widget.selectedItems()[0]
103103
return item.data(0, Qt.UserRole)
104104

105-
def set_value(self, value: str | None) -> None:
106-
# NOTE: Does not work fully currently
105+
def _find_item_recursive(self, item: QTreeWidgetItem, value: str) -> QTreeWidgetItem:
106+
"""Recursively try to find item with given value and return the item if found."""
107+
# Found item, return it
108+
if item.data(0, Qt.UserRole) == value:
109+
return item
110+
111+
# Loop children
112+
for i in range(item.childCount()):
113+
found_item = self._find_item_recursive(item.child(i), value)
114+
if found_item:
115+
return found_item
107116

108-
def find_item_recursive(item: QTreeWidgetItem, value: str) -> QTreeWidgetItem:
109-
if item.data(0, Qt.UserRole) == value:
110-
return item
111-
for i in range(item.childCount()):
112-
found_item = find_item_recursive(item.child(i), value)
113-
if found_item:
114-
return found_item
115-
return None
117+
return None
116118

119+
def set_value(self, value: str | None) -> None:
120+
# Set selection to NULL if `value` is None
117121
if value is None:
118-
self.setCurrentIndex(0)
122+
self.setCurrentIndex(self.null_index)
119123
return
120124

125+
# Loop top level tree items
121126
for i in range(self.count()):
122-
found_item = find_item_recursive(self.tree_widget.topLevelItem(i), value)
127+
# Handle child items recursively
128+
found_item = self._find_item_recursive(self.tree_widget.topLevelItem(i), value)
129+
130+
# If matching item was found, set it as selected. Because of the hybrid TreeWidget + ComboBox
131+
# nature of the widget, value setting is unintuitive and tricky
123132
if found_item:
133+
self.tree_widget.setCurrentItem(found_item)
124134
idx = self.tree_widget.indexFromItem(found_item)
125135
self.setRootModelIndex(idx.parent())
126136
self.setCurrentIndex(idx.row())
127137
self.setRootModelIndex(self.null_index.parent())
128138
return
129139

130-
self.setCurrentIndex(0) # Set combobox index to NULL
140+
self.setCurrentIndex(self.null_index) # Set selection to NULL if item with `value` was not found

arho_feature_template/project/layers/plan_layers.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,6 @@ def feature_from(cls, regulation_group_id: str, layer_name: str, feature_id: str
235235
layer = cls.get_from_project()
236236
attribute = cls.layer_name_to_attribute_map.get(layer_name)
237237

238-
# Check if association exists to avoid duplicate assocations
239-
for feature in cls.get_features_by_attribute_value("plan_regulation_group_id", regulation_group_id):
240-
if feature[attribute] == feature_id:
241-
return None
242-
# for feature in layer.getFeatures():
243-
# if feature["plan_regulation_group_id"] == regulation_group_id and feature[attribute] == feature_id:
244-
# return None
245-
246238
feature = QgsVectorLayerUtils.createFeature(layer)
247239
feature["plan_regulation_group_id"] = regulation_group_id
248240

@@ -253,6 +245,14 @@ def feature_from(cls, regulation_group_id: str, layer_name: str, feature_id: str
253245

254246
return feature
255247

248+
@classmethod
249+
def association_exists(cls, regulation_group_id: str, layer_name: str, feature_id: str):
250+
attribute = cls.layer_name_to_attribute_map.get(layer_name)
251+
for feature in cls.get_features_by_attribute_value("plan_regulation_group_id", regulation_group_id):
252+
if feature[attribute] == feature_id:
253+
return True
254+
return False
255+
256256
@classmethod
257257
def get_associations_for_feature(cls, feature_id: str, layer_name: str) -> Generator[QgsFeature]:
258258
attribute = cls.layer_name_to_attribute_map.get(layer_name)

0 commit comments

Comments
 (0)