9
9
from qgis .PyQt .QtCore import Qt , pyqtSignal
10
10
from qgis .PyQt .QtWidgets import (
11
11
QFormLayout ,
12
- QHBoxLayout ,
13
12
QLabel ,
14
13
QLineEdit ,
15
14
QMenu ,
20
19
)
21
20
22
21
from arho_feature_template .core .plan_regulation_config import PlanRegulationConfig , Unit , ValueType
23
- from arho_feature_template .utils .misc_utils import get_additional_information_name
22
+ from arho_feature_template .utils .misc_utils import get_additional_information_name , get_layer_by_name
24
23
25
24
if TYPE_CHECKING :
26
25
from qgis .PyQt .QtWidgets import QPushButton
@@ -70,7 +69,7 @@ def __init__(self, config: PlanRegulationConfig, parent=None):
70
69
self .config = config
71
70
self .regulation_number_added = False
72
71
self .expanded = True
73
- self .widgets : list [tuple ] = []
72
+ self .widgets : list [tuple [ QLabel , QWidget ] ] = []
74
73
self .plan_regulation_name .setText (config .name )
75
74
self .plan_regulation_name .setReadOnly (True )
76
75
self .init_value_fields ()
@@ -80,44 +79,35 @@ def populate_from_definition(self, definition: PlanRegulationDefinition):
80
79
# Additional information
81
80
for info in definition .additional_information :
82
81
info_type : str = cast ("str" , info ["type" ])
83
- layout = self .add_additional_info (info_type )
84
- if info_type in ADDITIONAL_INFORMATION_TYPES_WITH_INPUT :
82
+ self .add_additional_info (info_type )
83
+ if info_type == "kayttotarkoituskohdistus" :
85
84
info_value_widget = QLineEdit ()
86
- layout . addWidget ( info_value_widget )
85
+ label = QLabel ( get_additional_information_name ( info_type ) )
87
86
value = info .get ("value" )
88
87
if value :
89
88
info_value_widget .setText (value )
89
+ self .form_layout .addRow (label , info_value_widget )
90
90
91
91
# TODO: Other saved information from PlanRegulationDefinition
92
92
93
93
def init_value_fields (self ):
94
- layout = QHBoxLayout ()
95
94
value_type = self .config .value_type
96
95
if value_type :
97
- self ._add_value_input (value_type , layout )
96
+ self ._add_value_input (value_type , self . config . unit )
98
97
99
- unit = self .config .unit
100
- if unit :
101
- self ._add_value_unit (unit , layout )
102
-
103
- def _add_value_input (self , value_type : ValueType , layout : QHBoxLayout ):
98
+ def _add_value_input (self , value_type : ValueType , unit : Unit | None ):
104
99
if value_type in [ValueType .DECIMAL , ValueType .POSITIVE_DECIMAL ]:
105
- self .add_decimal_input (layout , value_type )
100
+ self .add_decimal_input (value_type , unit )
106
101
elif value_type == ValueType .POSITIVE_INTEGER :
107
- self .add_positive_integer_input (layout )
102
+ self .add_positive_integer_input (unit )
108
103
elif value_type == ValueType .POSITIVE_INTEGER_RANGE :
109
- self .add_positive_integer_range_input (layout )
104
+ self .add_positive_integer_range_input (unit )
110
105
elif value_type == ValueType .VERSIONED_TEXT :
111
- self .add_versioned_text_input (layout )
106
+ self .add_versioned_text_input ()
112
107
else :
113
108
msg = f"Invalid input value type for plan regulation: { value_type } "
114
109
raise ValueError (msg )
115
110
116
- def _add_value_unit (self , unit : Unit , layout : QHBoxLayout ):
117
- # NOTE: Unit could also be suffix in the QgsSpinBox, could be clearer?
118
- unit_label = QLabel (unit .value )
119
- layout .addWidget (unit_label )
120
-
121
111
def init_buttons (self ):
122
112
# DEL
123
113
self .del_btn .setIcon (QgsApplication .getThemeIcon ("mActionDeleteSelected.svg" ))
@@ -163,6 +153,16 @@ def init_buttons(self):
163
153
add_field_menu = QMenu (self )
164
154
add_field_menu .addAction ("Määräysnumero" ).triggered .connect (self .add_regulation_number )
165
155
add_field_menu .addAction ("Liiteasiakirja" ).triggered .connect (self .add_file )
156
+ add_field_menu .addAction ("Aihetunniste" ).triggered .connect (self .add_topic_tag )
157
+
158
+ theme_menu = QMenu ("Kaavoitusteema" , self )
159
+ language = "fin"
160
+ for feature in get_layer_by_name ("Kaavoitusteemat" ).getFeatures ():
161
+ name = feature ["name" ][language ]
162
+ action = theme_menu .addAction (name )
163
+ action .triggered .connect (lambda _ , name = name : self .add_theme (name ))
164
+ add_field_menu .addMenu (theme_menu )
165
+
166
166
self .add_field_btn .setMenu (add_field_menu )
167
167
self .add_field_btn .setIcon (QgsApplication .getThemeIcon ("mActionAdd.svg" ))
168
168
@@ -171,22 +171,28 @@ def init_buttons(self):
171
171
172
172
def _on_expand_hide_btn_clicked (self ):
173
173
if self .expanded :
174
- for label , widget in self .widgets :
174
+ for label , value_widget in self .widgets :
175
175
self .form_layout .removeWidget (label )
176
176
label .hide ()
177
- self .form_layout .removeWidget (widget )
178
- widget .hide ()
177
+ self .form_layout .removeWidget (value_widget )
178
+ value_widget .hide ()
179
179
self .expand_hide_btn .setArrowType (Qt .ArrowType .DownArrow )
180
180
self .expanded = False
181
181
else :
182
- for label , widget in self .widgets :
182
+ for label , value_widget in self .widgets :
183
+ self .form_layout .addRow (label , value_widget )
183
184
label .show ()
184
- widget .show ()
185
- self .form_layout .addRow (label , widget )
185
+ value_widget .show ()
186
186
self .expand_hide_btn .setArrowType (Qt .ArrowType .UpArrow )
187
187
self .expanded = True
188
188
189
- def add_decimal_input (self , layout : QHBoxLayout , value_type : ValueType ):
189
+ def _add_widgets_to_form (self , label : QLabel , widget : QWidget ):
190
+ self .form_layout .addRow (label , widget )
191
+ self .widgets .append ((label , widget ))
192
+ if not self .expanded :
193
+ self ._on_expand_hide_btn_clicked ()
194
+
195
+ def add_decimal_input (self , value_type : ValueType , unit : Unit | None ):
190
196
value_widget = QgsDoubleSpinBox ()
191
197
label = QLabel ("Arvo" )
192
198
if value_type == ValueType .POSITIVE_DECIMAL :
@@ -196,65 +202,67 @@ def add_decimal_input(self, layout: QHBoxLayout, value_type: ValueType):
196
202
value_widget .setMinimum (- 9999.9 )
197
203
label .setToolTip ("Tyyppi: desimaali" )
198
204
value_widget .setSizePolicy (QSizePolicy .Expanding , QSizePolicy .Fixed )
199
- layout . addWidget ( value_widget )
200
- self . form_layout . addRow ( label , layout )
201
- self .widgets . append (( label , value_widget ) )
205
+ if unit :
206
+ value_widget . setSuffix ( f" { unit . value } " )
207
+ self ._add_widgets_to_form ( label , value_widget )
202
208
203
- def add_positive_integer_input (self , layout : QHBoxLayout ):
209
+ def add_positive_integer_input (self , unit : Unit | None ):
204
210
value_widget = QgsSpinBox ()
205
211
value_widget .setMinimum (0 )
206
212
value_widget .setSizePolicy (QSizePolicy .Expanding , QSizePolicy .Fixed )
207
- layout .addWidget (value_widget )
213
+ if unit :
214
+ value_widget .setSuffix (f" { unit .value } " )
208
215
label = QLabel ("Arvo" )
209
216
label .setToolTip ("Tyyppi: kokonaisluku (positiivinen)" )
210
- self .form_layout .addRow (label , layout )
211
- self .widgets .append ((label , value_widget ))
217
+ self ._add_widgets_to_form (label , value_widget )
212
218
213
- def add_positive_integer_range_input (self , layout : QHBoxLayout ):
219
+ def add_positive_integer_range_input (self , unit : Unit | None ):
214
220
min_widget = QgsSpinBox ()
215
221
min_widget .setMinimum (0 )
222
+ min_label = QLabel ("Arvo minimi" )
223
+ min_label .setToolTip ("Tyyppi: kokonaisluku arvoväli (positiivinen)" )
224
+
216
225
max_widget = QgsSpinBox ()
217
226
max_widget .setMinimum (0 )
218
- layout .addWidget (min_widget )
219
- dash_label = QLabel (" — " )
220
- dash_label .setAlignment (Qt .AlignCenter )
221
- dash_label .setSizePolicy (QSizePolicy .Maximum , QSizePolicy .Fixed )
222
- layout .addWidget (dash_label )
223
- layout .addWidget (max_widget )
224
- label = QLabel ("Arvo" )
225
- label .setToolTip ("Tyyppi: kokonaisluku arvoväli (positiivinen)" )
226
- self .form_layout .addRow (label , layout )
227
- self .widgets .append ((label , layout ))
227
+ max_label = QLabel ("Arvo maksimi" )
228
+ max_label .setToolTip ("Tyyppi: kokonaisluku arvoväli (positiivinen)" )
229
+ if unit :
230
+ min_widget .setSuffix (f" { unit .value } " )
231
+ max_widget .setSuffix (f" { unit .value } " )
232
+ self ._add_widgets_to_form (min_label , min_widget )
233
+ self ._add_widgets_to_form (max_label , max_widget )
228
234
229
- def add_versioned_text_input (self , layout : QHBoxLayout ):
235
+ def add_versioned_text_input (self ):
230
236
text_widget = QTextEdit ()
231
- layout .addWidget (text_widget )
232
237
label = QLabel ("Arvo" )
233
238
label .setToolTip ("Tyyppi: kieliversioitu teksti" )
234
- self .form_layout .addRow (label , layout )
235
- self .widgets .append ((label , text_widget ))
236
-
237
- def add_additional_info (self , info_type : str ) -> QHBoxLayout :
238
- layout = QHBoxLayout ()
239
- info_name = get_additional_information_name (info_type )
240
- info_type_label = QLineEdit (info_name )
241
- info_type_label .setReadOnly (True )
242
- layout .addWidget (info_type_label )
239
+ self ._add_widgets_to_form (label , text_widget )
240
+
241
+ def add_additional_info (self , info_type : str ):
242
+ info_type_line = QLineEdit (get_additional_information_name (info_type ))
243
+ info_type_line .setReadOnly (True )
243
244
label = QLabel ("Lisätiedonlaji" )
244
- self .form_layout .addRow (label , layout )
245
- self .widgets .append ((label , info_type_label ))
246
- return layout
245
+ self ._add_widgets_to_form (label , info_type_line )
247
246
248
247
def add_regulation_number (self ):
249
248
if not self .regulation_number_added :
250
249
number_widget = QgsSpinBox ()
251
250
label = QLabel ("Määräysnumero" )
252
- self .form_layout .addRow (label , number_widget )
253
- self .widgets .append ((label , number_widget ))
251
+ self ._add_widgets_to_form (label , number_widget )
254
252
self .regulation_number_added = True
255
253
256
254
def add_file (self ):
257
255
file_input = QgsFileWidget ()
258
256
label = QLabel ("Liiteasiakirja" )
259
- self .form_layout .addRow (label , file_input )
260
- self .widgets .append ((label , file_input ))
257
+ self ._add_widgets_to_form (label , file_input )
258
+
259
+ def add_topic_tag (self ):
260
+ text_input = QLineEdit ()
261
+ label = QLabel ("Aihetunniste" )
262
+ self ._add_widgets_to_form (label , text_input )
263
+
264
+ def add_theme (self , theme_name : str ):
265
+ theme_type_line = QLineEdit (theme_name )
266
+ theme_type_line .setReadOnly (True )
267
+ label = QLabel ("Kaavoitusteema" )
268
+ self ._add_widgets_to_form (label , theme_type_line )
0 commit comments