1
1
from __future__ import annotations
2
2
3
3
from importlib import resources
4
- from typing import TYPE_CHECKING
4
+ from typing import cast
5
5
6
6
from qgis .core import QgsApplication
7
7
from qgis .gui import QgsFileWidget
8
8
from qgis .PyQt import uic
9
9
from qgis .PyQt .QtCore import Qt , pyqtSignal
10
- from qgis .PyQt .QtWidgets import QFormLayout , QFrame , QLabel , QLineEdit , QMenu , QToolButton , QVBoxLayout , QWidget
10
+ from qgis .PyQt .QtWidgets import (
11
+ QFormLayout ,
12
+ QFrame ,
13
+ QLabel ,
14
+ QLineEdit ,
15
+ QMenu ,
16
+ QPushButton ,
17
+ QToolButton ,
18
+ QVBoxLayout ,
19
+ QWidget ,
20
+ )
11
21
12
22
from arho_feature_template .core .models import (
13
23
AdditionalInformation ,
14
24
AdditionalInformationConfigLibrary ,
15
25
Regulation ,
16
26
)
17
27
from arho_feature_template .gui .components .additional_information_widget import AdditionalInformationWidget
18
- from arho_feature_template .gui .components .code_combobox import HierarchicalCodeComboBox
19
28
from arho_feature_template .gui .components .required_field_label import RequiredFieldLabel
20
29
from arho_feature_template .gui .components .value_input_widgets import (
21
30
IntegerInputWidget ,
22
31
SinglelineTextInputWidget ,
32
+ TypeOfVerbalRegulationWidget ,
23
33
ValueWidgetManager ,
24
34
)
25
- from arho_feature_template .project .layers .code_layers import VerbalRegulationType
26
35
from arho_feature_template .utils .misc_utils import LANGUAGE , get_layer_by_name
27
36
28
- if TYPE_CHECKING :
29
- from qgis .PyQt .QtWidgets import QPushButton
30
-
31
37
ui_path = resources .files (__package__ ) / "plan_regulation_widget.ui"
32
38
FormClass , _ = uic .loadUiType (ui_path )
33
39
@@ -69,7 +75,7 @@ def __init__(self, regulation: Regulation, parent=None):
69
75
self .file_widgets : list [QgsFileWidget ] = []
70
76
self .theme_widget : SinglelineTextInputWidget | None = None
71
77
self .topic_tag_widget : SinglelineTextInputWidget | None = None
72
- self .type_of_verbal_regulation_widget : HierarchicalCodeComboBox | None = None
78
+ self .type_of_verbal_regulation_widgets : list [ TypeOfVerbalRegulationWidget ] = []
73
79
74
80
self .expanded = True
75
81
self .additional_information_frame .hide ()
@@ -88,11 +94,10 @@ def _init_widgets(self):
88
94
self ._add_widget (RequiredFieldLabel ("Arvo" ), self .value_widget_manager .value_widget )
89
95
90
96
if self .config .regulation_code == "sanallinenMaarays" :
91
- self .type_of_verbal_regulation_widget = HierarchicalCodeComboBox ()
92
- self .type_of_verbal_regulation_widget .populate_from_code_layer (VerbalRegulationType )
93
- self ._add_widget (RequiredFieldLabel ("Sanallisen määräyksen laji" ), self .type_of_verbal_regulation_widget )
94
- if self .regulation .verbal_regulation_type_id is not None :
95
- self .type_of_verbal_regulation_widget .set_value (self .regulation .verbal_regulation_type_id )
97
+ for type_id in self .regulation .verbal_regulation_type_ids :
98
+ self ._add_type_of_verbal_regulation (type_id )
99
+ if len (self .type_of_verbal_regulation_widgets ) == 0 :
100
+ self ._add_type_of_verbal_regulation ()
96
101
97
102
# Additional information
98
103
for info in self .regulation .additional_information :
@@ -197,7 +202,31 @@ def _add_theme(self, theme_name: str):
197
202
self .theme_widget = SinglelineTextInputWidget (theme_name , False )
198
203
self ._add_widget (QLabel ("Kaavoitusteema" ), self .theme_widget )
199
204
205
+ def _add_type_of_verbal_regulation (self , type_id : str | None = None ):
206
+ if len (self .type_of_verbal_regulation_widgets ) == 0 :
207
+ widget = TypeOfVerbalRegulationWidget (with_add_btn = True )
208
+ btn = cast (QPushButton , widget .add_btn )
209
+ btn .clicked .connect (self ._add_type_of_verbal_regulation )
210
+ else :
211
+ widget = TypeOfVerbalRegulationWidget (with_del_btn = True )
212
+ btn = cast (QPushButton , widget .del_btn )
213
+ btn .clicked .connect (lambda : self ._delete_type_of_verbal_regulation (widget ))
214
+
215
+ if type_id :
216
+ widget .set_value (type_id )
217
+
218
+ self .type_of_verbal_regulation_widgets .append (widget )
219
+ self ._add_widget (RequiredFieldLabel ("Sanallisen määräyksen laji" ), widget )
220
+
221
+ def _delete_type_of_verbal_regulation (self , widget_to_delete : TypeOfVerbalRegulationWidget ):
222
+ self .type_of_verbal_regulation_widgets .remove (widget_to_delete )
223
+ for label , widget in self .widgets :
224
+ if widget is widget_to_delete :
225
+ widget .deleteLater ()
226
+ label .deleteLater ()
227
+
200
228
def into_model (self ) -> Regulation :
229
+ verbal_regulation_type_ids = [widget .get_value () for widget in self .type_of_verbal_regulation_widgets ]
201
230
return Regulation (
202
231
config = self .config ,
203
232
value = self .value_widget_manager .into_model () if self .value_widget_manager else None ,
@@ -206,8 +235,6 @@ def into_model(self) -> Regulation:
206
235
files = [file .filePath () for file in self .file_widgets ],
207
236
theme = self .theme_widget .get_value () if self .theme_widget else None ,
208
237
topic_tag = self .topic_tag_widget .get_value () if self .topic_tag_widget else None ,
209
- verbal_regulation_type_id = self .type_of_verbal_regulation_widget .value ()
210
- if self .type_of_verbal_regulation_widget
211
- else None ,
238
+ verbal_regulation_type_ids = [value for value in verbal_regulation_type_ids if value is not None ],
212
239
id_ = self .regulation .id_ ,
213
240
)
0 commit comments