10
10
QDialog ,
11
11
QDialogButtonBox ,
12
12
QLineEdit ,
13
+ QMessageBox ,
13
14
QScrollArea ,
14
15
QSizePolicy ,
15
16
QSpacerItem ,
@@ -39,7 +40,11 @@ class PlanFeatureForm(QDialog, FormClass): # type: ignore
39
40
"""Parent class for feature forms for adding and modifying feature attribute data."""
40
41
41
42
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 ,
43
48
):
44
49
super ().__init__ ()
45
50
self .setupUi (self )
@@ -57,7 +62,9 @@ def __init__(
57
62
self .regulation_groups_tree_layout : QVBoxLayout
58
63
59
64
# 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 ]
61
68
self .plan_regulation_group_libraries_combobox .addItems (
62
69
library .name for library in self .regulation_group_libraries
63
70
)
@@ -97,6 +104,44 @@ def _remove_spacer(self):
97
104
self .plan_regulation_group_scrollarea_contents .layout ().removeItem (self .scroll_area_spacer )
98
105
self .scroll_area_spacer = None
99
106
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
+
100
145
def add_selected_plan_regulation_group (self , item : QTreeWidgetItem , column : int ):
101
146
if not item .parent ():
102
147
return
@@ -113,7 +158,9 @@ def add_plan_regulation_group(self, definition: RegulationGroup):
113
158
self ._add_spacer ()
114
159
115
160
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
+ )
117
164
if group_as_form .exec_ ():
118
165
regulation_group_widget .from_model (group_as_form .model )
119
166
@@ -146,5 +193,6 @@ def into_model(self) -> PlanFeature:
146
193
)
147
194
148
195
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 ()
0 commit comments