1
1
from __future__ import annotations
2
2
3
+ import os
3
4
from importlib import resources
5
+ from pathlib import Path
4
6
from typing import TYPE_CHECKING
5
7
6
8
from qgis .PyQt import uic
7
- from qgis .PyQt .QtWidgets import QDialog , QDialogButtonBox
9
+ from qgis .PyQt .QtCore import Qt
10
+ from qgis .PyQt .QtWidgets import (
11
+ QComboBox ,
12
+ QDialog ,
13
+ QDialogButtonBox ,
14
+ QLineEdit ,
15
+ QSizePolicy ,
16
+ QSpacerItem ,
17
+ QTextEdit ,
18
+ QTreeWidget ,
19
+ QTreeWidgetItem ,
20
+ )
8
21
9
- from arho_feature_template .core .models import Plan
22
+ from arho_feature_template .core .models import Plan , RegulationGroup , RegulationGroupLibrary
23
+ from arho_feature_template .gui .plan_regulation_group_widget import RegulationGroupWidget
10
24
from arho_feature_template .project .layers .code_layers import (
11
25
LifeCycleStatusLayer ,
12
26
OrganisationLayer ,
13
27
PlanTypeLayer ,
14
28
)
29
+ from arho_feature_template .qgis_plugin_tools .tools .resources import resources_path
15
30
16
31
if TYPE_CHECKING :
17
- from qgis .PyQt .QtWidgets import QLineEdit , QTextEdit
32
+ from qgis .PyQt .QtWidgets import QComboBox , QLineEdit , QTextEdit , QTreeWidget , QWidget
18
33
19
34
from arho_feature_template .gui .code_combobox import CodeComboBox , HierarchicalCodeComboBox
20
35
@@ -33,6 +48,10 @@ class PlanAttributeForm(QDialog, FormClass): # type: ignore
33
48
producers_plan_identifier_line_edit : QLineEdit
34
49
matter_management_identifier_line_edit : QLineEdit
35
50
51
+ plan_regulation_group_scrollarea_contents : QWidget
52
+ plan_regulation_group_libraries_combobox : QComboBox
53
+ plan_regulation_groups_tree : QTreeWidget
54
+
36
55
button_box : QDialogButtonBox
37
56
38
57
def __init__ (self , parent = None ):
@@ -49,6 +68,11 @@ def __init__(self, parent=None):
49
68
self .plan_type_combo_box .currentIndexChanged .connect (self ._check_required_fields )
50
69
self .lifecycle_status_combo_box .currentIndexChanged .connect (self ._check_required_fields )
51
70
71
+ self .scroll_area_spacer = None
72
+ self .regulation_group_widgets : list [RegulationGroupWidget ] = []
73
+ self .init_plan_regulation_group_libraries ()
74
+ self .plan_regulation_groups_tree .itemDoubleClicked .connect (self .add_selected_plan_regulation_group )
75
+
52
76
self .button_box .button (QDialogButtonBox .Ok ).setEnabled (False )
53
77
54
78
def _check_required_fields (self ) -> None :
@@ -63,6 +87,55 @@ def _check_required_fields(self) -> None:
63
87
else :
64
88
ok_button .setEnabled (False )
65
89
90
+ # --- COPIED FROM TEMPLATE ATTRIBUTE FORM ---
91
+
92
+ def _add_spacer (self ):
93
+ self .scroll_area_spacer = QSpacerItem (0 , 0 , QSizePolicy .Expanding , QSizePolicy .Expanding )
94
+ self .plan_regulation_group_scrollarea_contents .layout ().addItem (self .scroll_area_spacer )
95
+
96
+ def _remove_spacer (self ):
97
+ if self .scroll_area_spacer is not None :
98
+ self .plan_regulation_group_scrollarea_contents .layout ().removeItem (self .scroll_area_spacer )
99
+ self .scroll_area_spacer = None
100
+
101
+ def add_selected_plan_regulation_group (self , item : QTreeWidgetItem , column : int ):
102
+ if not item .parent ():
103
+ return
104
+ regulation_group : RegulationGroup = item .data (column , Qt .UserRole )
105
+ self .add_plan_regulation_group (regulation_group )
106
+
107
+ def add_plan_regulation_group (self , regulation_group : RegulationGroup ):
108
+ regulation_group_widget = RegulationGroupWidget (regulation_group , general_regulation = True )
109
+ regulation_group_widget .delete_signal .connect (self .remove_plan_regulation_group )
110
+ self ._remove_spacer ()
111
+ self .plan_regulation_group_scrollarea_contents .layout ().addWidget (regulation_group_widget )
112
+ self .regulation_group_widgets .append (regulation_group_widget )
113
+ self ._add_spacer ()
114
+
115
+ def remove_plan_regulation_group (self , regulation_group_widget : RegulationGroupWidget ):
116
+ self .plan_regulation_group_scrollarea_contents .layout ().removeWidget (regulation_group_widget )
117
+ self .regulation_group_widgets .remove (regulation_group_widget )
118
+ regulation_group_widget .deleteLater ()
119
+
120
+ def init_plan_regulation_group_libraries (self ):
121
+ katja_asemakaava_path = Path (os .path .join (resources_path (), "katja_asemakaava.yaml" ))
122
+ libraries = [RegulationGroupLibrary .from_config_file (katja_asemakaava_path )]
123
+ for library in libraries :
124
+ self .init_plan_regulation_group_library (library )
125
+
126
+ def init_plan_regulation_group_library (self , library : RegulationGroupLibrary ):
127
+ self .plan_regulation_group_libraries_combobox .addItem (library .name )
128
+ for category in library .regulation_group_categories :
129
+ category_item = QTreeWidgetItem ()
130
+ category_item .setText (0 , category .name )
131
+ self .plan_regulation_groups_tree .addTopLevelItem (category_item )
132
+ for group_definition in category .regulation_groups :
133
+ regulation_group_item = QTreeWidgetItem (category_item )
134
+ regulation_group_item .setText (0 , group_definition .name )
135
+ regulation_group_item .setData (0 , Qt .UserRole , group_definition )
136
+
137
+ # ---
138
+
66
139
def get_plan_attributes (self ) -> Plan :
67
140
return Plan (
68
141
id_ = None ,
@@ -75,5 +148,5 @@ def get_plan_attributes(self) -> Plan:
75
148
producers_plan_identifier = self .producers_plan_identifier_line_edit .text () or None ,
76
149
matter_management_identifier = self .matter_management_identifier_line_edit .text () or None ,
77
150
lifecycle_status_id = self .lifecycle_status_combo_box .value (),
78
- general_regulations = [],
151
+ general_regulations = [reg_group_widget . into_model () for reg_group_widget in self . regulation_group_widgets ],
79
152
)
0 commit comments