Skip to content

Commit 22e2223

Browse files
committed
fix expand button by not nesting layouts inside form layout of plan regulation, add topic tag (name) and theme
1 parent 90a3f2f commit 22e2223

File tree

1 file changed

+73
-65
lines changed

1 file changed

+73
-65
lines changed

arho_feature_template/gui/plan_regulation_widget.py

+73-65
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from qgis.PyQt.QtCore import Qt, pyqtSignal
1010
from qgis.PyQt.QtWidgets import (
1111
QFormLayout,
12-
QHBoxLayout,
1312
QLabel,
1413
QLineEdit,
1514
QMenu,
@@ -20,7 +19,7 @@
2019
)
2120

2221
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
2423

2524
if TYPE_CHECKING:
2625
from qgis.PyQt.QtWidgets import QPushButton
@@ -70,7 +69,7 @@ def __init__(self, config: PlanRegulationConfig, parent=None):
7069
self.config = config
7170
self.regulation_number_added = False
7271
self.expanded = True
73-
self.widgets: list[tuple] = []
72+
self.widgets: list[tuple[QLabel, QWidget]] = []
7473
self.plan_regulation_name.setText(config.name)
7574
self.plan_regulation_name.setReadOnly(True)
7675
self.init_value_fields()
@@ -80,44 +79,35 @@ def populate_from_definition(self, definition: PlanRegulationDefinition):
8079
# Additional information
8180
for info in definition.additional_information:
8281
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":
8584
info_value_widget = QLineEdit()
86-
layout.addWidget(info_value_widget)
85+
label = QLabel(get_additional_information_name(info_type))
8786
value = info.get("value")
8887
if value:
8988
info_value_widget.setText(value)
89+
self.form_layout.addRow(label, info_value_widget)
9090

9191
# TODO: Other saved information from PlanRegulationDefinition
9292

9393
def init_value_fields(self):
94-
layout = QHBoxLayout()
9594
value_type = self.config.value_type
9695
if value_type:
97-
self._add_value_input(value_type, layout)
96+
self._add_value_input(value_type, self.config.unit)
9897

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):
10499
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)
106101
elif value_type == ValueType.POSITIVE_INTEGER:
107-
self.add_positive_integer_input(layout)
102+
self.add_positive_integer_input(unit)
108103
elif value_type == ValueType.POSITIVE_INTEGER_RANGE:
109-
self.add_positive_integer_range_input(layout)
104+
self.add_positive_integer_range_input(unit)
110105
elif value_type == ValueType.VERSIONED_TEXT:
111-
self.add_versioned_text_input(layout)
106+
self.add_versioned_text_input()
112107
else:
113108
msg = f"Invalid input value type for plan regulation: {value_type}"
114109
raise ValueError(msg)
115110

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-
121111
def init_buttons(self):
122112
# DEL
123113
self.del_btn.setIcon(QgsApplication.getThemeIcon("mActionDeleteSelected.svg"))
@@ -163,6 +153,16 @@ def init_buttons(self):
163153
add_field_menu = QMenu(self)
164154
add_field_menu.addAction("Määräysnumero").triggered.connect(self.add_regulation_number)
165155
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+
166166
self.add_field_btn.setMenu(add_field_menu)
167167
self.add_field_btn.setIcon(QgsApplication.getThemeIcon("mActionAdd.svg"))
168168

@@ -171,22 +171,28 @@ def init_buttons(self):
171171

172172
def _on_expand_hide_btn_clicked(self):
173173
if self.expanded:
174-
for label, widget in self.widgets:
174+
for label, value_widget in self.widgets:
175175
self.form_layout.removeWidget(label)
176176
label.hide()
177-
self.form_layout.removeWidget(widget)
178-
widget.hide()
177+
self.form_layout.removeWidget(value_widget)
178+
value_widget.hide()
179179
self.expand_hide_btn.setArrowType(Qt.ArrowType.DownArrow)
180180
self.expanded = False
181181
else:
182-
for label, widget in self.widgets:
182+
for label, value_widget in self.widgets:
183+
self.form_layout.addRow(label, value_widget)
183184
label.show()
184-
widget.show()
185-
self.form_layout.addRow(label, widget)
185+
value_widget.show()
186186
self.expand_hide_btn.setArrowType(Qt.ArrowType.UpArrow)
187187
self.expanded = True
188188

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):
190196
value_widget = QgsDoubleSpinBox()
191197
label = QLabel("Arvo")
192198
if value_type == ValueType.POSITIVE_DECIMAL:
@@ -196,65 +202,67 @@ def add_decimal_input(self, layout: QHBoxLayout, value_type: ValueType):
196202
value_widget.setMinimum(-9999.9)
197203
label.setToolTip("Tyyppi: desimaali")
198204
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)
202208

203-
def add_positive_integer_input(self, layout: QHBoxLayout):
209+
def add_positive_integer_input(self, unit: Unit | None):
204210
value_widget = QgsSpinBox()
205211
value_widget.setMinimum(0)
206212
value_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
207-
layout.addWidget(value_widget)
213+
if unit:
214+
value_widget.setSuffix(f" {unit.value}")
208215
label = QLabel("Arvo")
209216
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)
212218

213-
def add_positive_integer_range_input(self, layout: QHBoxLayout):
219+
def add_positive_integer_range_input(self, unit: Unit | None):
214220
min_widget = QgsSpinBox()
215221
min_widget.setMinimum(0)
222+
min_label = QLabel("Arvo minimi")
223+
min_label.setToolTip("Tyyppi: kokonaisluku arvoväli (positiivinen)")
224+
216225
max_widget = QgsSpinBox()
217226
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)
228234

229-
def add_versioned_text_input(self, layout: QHBoxLayout):
235+
def add_versioned_text_input(self):
230236
text_widget = QTextEdit()
231-
layout.addWidget(text_widget)
232237
label = QLabel("Arvo")
233238
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)
243244
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)
247246

248247
def add_regulation_number(self):
249248
if not self.regulation_number_added:
250249
number_widget = QgsSpinBox()
251250
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)
254252
self.regulation_number_added = True
255253

256254
def add_file(self):
257255
file_input = QgsFileWidget()
258256
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

Comments
 (0)