Skip to content

Commit ab824b3

Browse files
committed
add checks and messages for existing/duplicate regulation group short names
1 parent a8f41fe commit ab824b3

File tree

4 files changed

+82
-11
lines changed

4 files changed

+82
-11
lines changed

arho_feature_template/core/models.py

+9
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ def from_config_file(cls, config_fp: Path) -> RegulationGroupLibrary:
146146
],
147147
)
148148

149+
def get_short_names(self) -> set[str]:
150+
"""Returns set of non-empty short names (letter codes) of regulation groups part of the library."""
151+
return {
152+
regulation_group.short_name
153+
for category in self.regulation_group_categories
154+
for regulation_group in category.regulation_groups
155+
if regulation_group.short_name
156+
}
157+
149158

150159
@dataclass
151160
class RegulationLibrary:

arho_feature_template/core/plan_manager.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def edit_regulation_group(self, regulation_group: RegulationGroup):
166166
self._open_regulation_group_form(regulation_group)
167167

168168
def _open_regulation_group_form(self, regulation_group: RegulationGroup):
169-
regulation_group_form = PlanRegulationGroupForm(regulation_group)
169+
regulation_group_form = PlanRegulationGroupForm(regulation_group, self.active_plan_regulation_group_library)
170170
if regulation_group_form.exec_():
171171
if regulation_group_form.save_as_config:
172172
save_regulation_group_as_config(regulation_group_form.model)
@@ -314,7 +314,7 @@ def _plan_feature_geom_digitized(self, feature: QgsFeature):
314314

315315
plan_feature.geom = feature.geometry()
316316
attribute_form = PlanFeatureForm(
317-
plan_feature, title, [*self.regulation_group_libraries, self.active_plan_regulation_group_library]
317+
plan_feature, title, self.regulation_group_libraries, self.active_plan_regulation_group_library
318318
)
319319
if attribute_form.exec_():
320320
save_plan_feature(attribute_form.model)
@@ -326,7 +326,7 @@ def edit_plan_feature(self, feature: QgsFeature, layer_name: str):
326326

327327
title = plan_feature.name if plan_feature.name else layer_name
328328
attribute_form = PlanFeatureForm(
329-
plan_feature, title, [*self.regulation_group_libraries, self.active_plan_regulation_group_library]
329+
plan_feature, title, self.regulation_group_libraries, self.active_plan_regulation_group_library
330330
)
331331
if attribute_form.exec_():
332332
save_plan_feature(attribute_form.model)

arho_feature_template/gui/dialogs/plan_feature_form.py

+53-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
QDialog,
1111
QDialogButtonBox,
1212
QLineEdit,
13+
QMessageBox,
1314
QScrollArea,
1415
QSizePolicy,
1516
QSpacerItem,
@@ -39,7 +40,11 @@ class PlanFeatureForm(QDialog, FormClass): # type: ignore
3940
"""Parent class for feature forms for adding and modifying feature attribute data."""
4041

4142
def __init__(
42-
self, plan_feature: PlanFeature, form_title: str, regulation_group_libraries: list[RegulationGroupLibrary]
43+
self,
44+
plan_feature: PlanFeature,
45+
form_title: str,
46+
regulation_group_libraries: list[RegulationGroupLibrary],
47+
active_plan_regulation_groups_library: RegulationGroupLibrary,
4348
):
4449
super().__init__()
4550
self.setupUi(self)
@@ -57,7 +62,9 @@ def __init__(
5762
self.regulation_groups_tree_layout: QVBoxLayout
5863

5964
# INIT
60-
self.regulation_group_libraries = regulation_group_libraries
65+
self.existing_group_short_names = active_plan_regulation_groups_library.get_short_names()
66+
self.active_plan_regulation_groups_library = active_plan_regulation_groups_library
67+
self.regulation_group_libraries = [*regulation_group_libraries, active_plan_regulation_groups_library]
6168
self.plan_regulation_group_libraries_combobox.addItems(
6269
library.name for library in self.regulation_group_libraries
6370
)
@@ -97,6 +104,44 @@ def _remove_spacer(self):
97104
self.plan_regulation_group_scrollarea_contents.layout().removeItem(self.scroll_area_spacer)
98105
self.scroll_area_spacer = None
99106

107+
def _check_regulation_group_short_names(self) -> bool:
108+
seen_names = set()
109+
existing_names = set()
110+
duplicate_names = set()
111+
112+
for reg_group_widget in self.regulation_group_widgets:
113+
short_name = reg_group_widget.short_name.text()
114+
if not short_name:
115+
continue
116+
117+
if short_name in self.existing_group_short_names:
118+
existing_names.add(short_name)
119+
if short_name in seen_names:
120+
duplicate_names.add(short_name)
121+
seen_names.add(short_name)
122+
123+
def format_names(names, last_item_conjucation: str = "ja"):
124+
names = list(names)
125+
formatted_names = ", ".join(f"'<b>{name}</b>'" for name in names[:-1])
126+
formatted_names += f" {last_item_conjucation} " if len(names) > 1 else ""
127+
formatted_names += f"'<b>{names[-1]}</b>'" if names else ""
128+
return formatted_names
129+
130+
if existing_names:
131+
if len(existing_names) == 1:
132+
msg = f"Kaavamääräysryhmä lyhyellä nimellä {format_names(existing_names)} on jo olemassa."
133+
else:
134+
msg = f"Kaavamääräysryhmät lyhyillä nimillä {format_names(existing_names)} ovat jo olemassa."
135+
QMessageBox.critical(self, "Virhe", msg)
136+
return False
137+
138+
if duplicate_names:
139+
msg = f"Lomakkeella on useita kaavamääräysryhmiä, joilla on lyhyt nimi {format_names(duplicate_names, 'tai')}."
140+
QMessageBox.critical(self, "Virhe", msg)
141+
return False
142+
143+
return True
144+
100145
def add_selected_plan_regulation_group(self, item: QTreeWidgetItem, column: int):
101146
if not item.parent():
102147
return
@@ -113,7 +158,9 @@ def add_plan_regulation_group(self, definition: RegulationGroup):
113158
self._add_spacer()
114159

115160
def open_plan_regulation_group_form(self, regulation_group_widget: RegulationGroupWidget):
116-
group_as_form = PlanRegulationGroupForm(regulation_group_widget.into_model())
161+
group_as_form = PlanRegulationGroupForm(
162+
regulation_group_widget.into_model(), self.active_plan_regulation_groups_library
163+
)
117164
if group_as_form.exec_():
118165
regulation_group_widget.from_model(group_as_form.model)
119166

@@ -146,5 +193,6 @@ def into_model(self) -> PlanFeature:
146193
)
147194

148195
def _on_ok_clicked(self):
149-
self.model = self.into_model()
150-
self.accept()
196+
if self._check_regulation_group_short_names():
197+
self.model = self.into_model()
198+
self.accept()

arho_feature_template/gui/dialogs/plan_regulation_group_form.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
QDialogButtonBox,
1313
QHBoxLayout,
1414
QLabel,
15+
QMessageBox,
1516
QSizePolicy,
1617
QTextBrowser,
1718
QTreeWidgetItem,
@@ -23,6 +24,7 @@
2324
Regulation,
2425
RegulationConfig,
2526
RegulationGroup,
27+
RegulationGroupLibrary,
2628
RegulationLibrary,
2729
)
2830
from arho_feature_template.gui.components.plan_proposition_widget import PropositionWidget
@@ -45,7 +47,9 @@
4547
class PlanRegulationGroupForm(QDialog, FormClass): # type: ignore
4648
"""Form to create a new plan regulation group."""
4749

48-
def __init__(self, regulation_group: RegulationGroup):
50+
def __init__(
51+
self, regulation_group: RegulationGroup, active_plan_regulation_groups_library: RegulationGroupLibrary
52+
):
4953
super().__init__()
5054
self.setupUi(self)
5155

@@ -74,6 +78,7 @@ def __init__(self, regulation_group: RegulationGroup):
7478
self.regulation_widgets: list[RegulationWidget] = []
7579
self.proposition_widgets: list[PropositionWidget] = []
7680
self.save_as_config = False
81+
self.existing_group_short_names = active_plan_regulation_groups_library.get_short_names()
7782

7883
# Initialize regulation library
7984
self.regulations_selection_widget = TreeWithSearchWidget()
@@ -137,6 +142,14 @@ def _initalize_regulation_from_config(self, config: RegulationConfig, parent: QT
137142
for child_config in config.child_regulations:
138143
self._initalize_regulation_from_config(child_config, item)
139144

145+
def _check_short_name(self) -> bool:
146+
short_name = self.short_name.text()
147+
if short_name and short_name in self.existing_group_short_names:
148+
msg = f"Kaavamääräysryhmä lyhyellä nimellä '<b>{short_name}</b>' on jo olemassa."
149+
QMessageBox.critical(self, "Virhe", msg)
150+
return False
151+
return True
152+
140153
def update_selected_regulation(self, item: QTreeWidgetItem, column: int):
141154
config: RegulationConfig = item.data(column, Qt.UserRole) # Retrieve the associated config
142155
self.regulation_info.setText(config.description)
@@ -190,5 +203,6 @@ def into_model(self) -> RegulationGroup:
190203
)
191204

192205
def _on_ok_clicked(self):
193-
self.model = self.into_model()
194-
self.accept()
206+
if self._check_short_name():
207+
self.model = self.into_model()
208+
self.accept()

0 commit comments

Comments
 (0)