Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update plan regulation widget UI #79

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 115 additions & 68 deletions arho_feature_template/gui/plan_regulation_widget.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from collections import defaultdict
from importlib import resources
from typing import TYPE_CHECKING, cast

Expand All @@ -10,21 +9,19 @@
from qgis.PyQt.QtCore import Qt, pyqtSignal
from qgis.PyQt.QtWidgets import (
QFormLayout,
QHBoxLayout,
QLabel,
QLineEdit,
QMenu,
QSizePolicy,
QTextEdit,
QToolButton,
QWidget,
)

from arho_feature_template.core.plan_regulation_config import PlanRegulationConfig, Unit, ValueType
from arho_feature_template.utils.misc_utils import get_additional_information_name
from arho_feature_template.utils.misc_utils import get_additional_information_name, get_layer_by_name

if TYPE_CHECKING:
from numbers import Number

from qgis.PyQt.QtWidgets import QPushButton

from arho_feature_template.core.plan_regulation_group_config import PlanRegulationDefinition
Expand Down Expand Up @@ -56,21 +53,23 @@ def __init__(self, config: PlanRegulationConfig, parent=None):
self.setupUi(self)

# TYPES
# self.spacer_layout: QBoxLayout
self.plan_regulation_name: QLineEdit
self.form_layout: QFormLayout

self.add_additional_information_btn: QPushButton
self.add_regulation_number_btn: QPushButton
self.add_file_btn: QPushButton
self.add_field_btn: QPushButton
self.del_btn: QPushButton
self.expand_hide_btn: QToolButton

self.code_label: QLabel
self.code: QLineEdit

# INIT
self.config = config
self.regulation_number_added = False
# Key is related field name, value is 1) widget, 2) tuple of widget and default value
# NOTE: Maybe this is not something needed? Instead, when user clicks Ok, write into a YAML
# in a separate script?
self.attribute_widgets: dict[str, QWidget | tuple[QWidget, str | Number]] = defaultdict(dict)
self.expanded = True
self.widgets: list[tuple[QLabel, QWidget]] = []
self.plan_regulation_name.setText(config.name)
self.plan_regulation_name.setReadOnly(True)
self.init_value_fields()
Expand All @@ -80,44 +79,35 @@ def populate_from_definition(self, definition: PlanRegulationDefinition):
# Additional information
for info in definition.additional_information:
info_type: str = cast("str", info["type"])
layout = self.add_additional_info(info_type)
if info_type in ADDITIONAL_INFORMATION_TYPES_WITH_INPUT:
self.add_additional_info(info_type)
if info_type == "kayttotarkoituskohdistus":
info_value_widget = QLineEdit()
layout.addWidget(info_value_widget)
label = QLabel(get_additional_information_name(info_type))
value = info.get("value")
if value:
info_value_widget.setText(value)
self.form_layout.addRow(label, info_value_widget)

# TODO: Other saved information from PlanRegulationDefinition

def init_value_fields(self):
layout = QHBoxLayout()
value_type = self.config.value_type
if value_type:
self._add_value_input(value_type, layout)

unit = self.config.unit
if unit:
self._add_value_unit(unit, layout)
self._add_value_input(value_type, self.config.unit)

def _add_value_input(self, value_type: ValueType, layout: QHBoxLayout):
def _add_value_input(self, value_type: ValueType, unit: Unit | None):
if value_type in [ValueType.DECIMAL, ValueType.POSITIVE_DECIMAL]:
self.add_decimal_input(layout, value_type)
self.add_decimal_input(value_type, unit)
elif value_type == ValueType.POSITIVE_INTEGER:
self.add_positive_integer_input(layout)
self.add_positive_integer_input(unit)
elif value_type == ValueType.POSITIVE_INTEGER_RANGE:
self.add_positive_integer_range_input(layout)
self.add_positive_integer_range_input(unit)
elif value_type == ValueType.VERSIONED_TEXT:
self.add_versioned_text_input(layout)
self.add_versioned_text_input()
else:
msg = f"Invalid input value type for plan regulation: {value_type}"
raise ValueError(msg)

def _add_value_unit(self, unit: Unit, layout: QHBoxLayout):
# NOTE: Unit could also be suffix in the QgsSpinBox, could be clearer?
unit_label = QLabel(unit.value)
layout.addWidget(unit_label)

def init_buttons(self):
# DEL
self.del_btn.setIcon(QgsApplication.getThemeIcon("mActionDeleteSelected.svg"))
Expand Down Expand Up @@ -157,65 +147,122 @@ def init_buttons(self):
type_main_menu.addMenu(type_menu)
type_main_menu.addMenu(signifigance_menu)
self.add_additional_information_btn.setMenu(type_main_menu)

# REGULATION NUMBER
self.add_regulation_number_btn.clicked.connect(self.add_regulation_number)

# ADD FILE
self.add_file_btn.clicked.connect(self.add_file)

def add_decimal_input(self, layout: QHBoxLayout, value_type: ValueType):
self.add_additional_information_btn.setIcon(QgsApplication.getThemeIcon("mActionPropertiesWidget.svg"))

# OTHER INFO / ADD FIELD
add_field_menu = QMenu(self)
add_field_menu.addAction("Määräysnumero").triggered.connect(self.add_regulation_number)
add_field_menu.addAction("Liiteasiakirja").triggered.connect(self.add_file)
add_field_menu.addAction("Aihetunniste").triggered.connect(self.add_topic_tag)

theme_menu = QMenu("Kaavoitusteema", self)
language = "fin"
for feature in get_layer_by_name("Kaavoitusteemat").getFeatures():
name = feature["name"][language]
action = theme_menu.addAction(name)
action.triggered.connect(lambda _, name=name: self.add_theme(name))
add_field_menu.addMenu(theme_menu)

self.add_field_btn.setMenu(add_field_menu)
self.add_field_btn.setIcon(QgsApplication.getThemeIcon("mActionAdd.svg"))

# EXPAND BTN
self.expand_hide_btn.clicked.connect(self._on_expand_hide_btn_clicked)

def _on_expand_hide_btn_clicked(self):
if self.expanded:
for label, value_widget in self.widgets:
self.form_layout.removeWidget(label)
label.hide()
self.form_layout.removeWidget(value_widget)
value_widget.hide()
self.expand_hide_btn.setArrowType(Qt.ArrowType.DownArrow)
self.expanded = False
else:
for label, value_widget in self.widgets:
self.form_layout.addRow(label, value_widget)
label.show()
value_widget.show()
self.expand_hide_btn.setArrowType(Qt.ArrowType.UpArrow)
self.expanded = True

def _add_widgets_to_form(self, label: QLabel, widget: QWidget):
self.form_layout.addRow(label, widget)
self.widgets.append((label, widget))
if not self.expanded:
self._on_expand_hide_btn_clicked()

def add_decimal_input(self, value_type: ValueType, unit: Unit | None):
value_widget = QgsDoubleSpinBox()
label_text = "Desimaali"
label = QLabel("Arvo")
if value_type == ValueType.POSITIVE_DECIMAL:
value_widget.setMinimum(0.0)
label_text += " (positiivinen)"
label.setToolTip("Tyyppi: desimaali (positiivinen)")
else:
value_widget.setMinimum(-9999.9)
label.setToolTip("Tyyppi: desimaali")
value_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
layout.addWidget(value_widget)
self.form_layout.addRow(label_text, layout)
if unit:
value_widget.setSuffix(f" {unit.value}")
self._add_widgets_to_form(label, value_widget)

def add_positive_integer_input(self, layout: QHBoxLayout):
def add_positive_integer_input(self, unit: Unit | None):
value_widget = QgsSpinBox()
value_widget.setMinimum(0)
value_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
layout.addWidget(value_widget)
self.form_layout.addRow("Kokonaisluku (positiivinen)", layout)
if unit:
value_widget.setSuffix(f" {unit.value}")
label = QLabel("Arvo")
label.setToolTip("Tyyppi: kokonaisluku (positiivinen)")
self._add_widgets_to_form(label, value_widget)

def add_positive_integer_range_input(self, layout: QHBoxLayout):
def add_positive_integer_range_input(self, unit: Unit | None):
min_widget = QgsSpinBox()
min_widget.setMinimum(0)
min_label = QLabel("Arvo minimi")
min_label.setToolTip("Tyyppi: kokonaisluku arvoväli (positiivinen)")

max_widget = QgsSpinBox()
max_widget.setMinimum(0)
layout.addWidget(min_widget)
dash_label = QLabel(" — ")
dash_label.setAlignment(Qt.AlignCenter)
dash_label.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Fixed)
layout.addWidget(dash_label)
layout.addWidget(max_widget)
self.form_layout.addRow("Kokonaisluku arvoväli (positiivinen)", layout)

def add_versioned_text_input(self, layout: QHBoxLayout):
max_label = QLabel("Arvo maksimi")
max_label.setToolTip("Tyyppi: kokonaisluku arvoväli (positiivinen)")
if unit:
min_widget.setSuffix(f" {unit.value}")
max_widget.setSuffix(f" {unit.value}")
self._add_widgets_to_form(min_label, min_widget)
self._add_widgets_to_form(max_label, max_widget)

def add_versioned_text_input(self):
text_widget = QTextEdit()
layout.addWidget(text_widget)
self.form_layout.addRow("Kieliversioitu teksti", layout)

def add_additional_info(self, info_type: str) -> QHBoxLayout:
layout = QHBoxLayout()
info_name = get_additional_information_name(info_type)
info_type_label = QLineEdit(info_name)
info_type_label.setReadOnly(True)
layout.addWidget(info_type_label)
self.form_layout.addRow("Lisätiedonlaji", layout)
return layout
label = QLabel("Arvo")
label.setToolTip("Tyyppi: kieliversioitu teksti")
self._add_widgets_to_form(label, text_widget)

def add_additional_info(self, info_type: str):
info_type_line = QLineEdit(get_additional_information_name(info_type))
info_type_line.setReadOnly(True)
label = QLabel("Lisätiedonlaji")
self._add_widgets_to_form(label, info_type_line)

def add_regulation_number(self):
if not self.regulation_number_added:
number_widget = QgsSpinBox()
self.form_layout.addRow("Määräysnumero", number_widget)
label = QLabel("Määräysnumero")
self._add_widgets_to_form(label, number_widget)
self.regulation_number_added = True

def add_file(self):
file_input = QgsFileWidget()
self.form_layout.addRow("Liiteasiakirja", file_input)
label = QLabel("Liiteasiakirja")
self._add_widgets_to_form(label, file_input)

def add_topic_tag(self):
text_input = QLineEdit()
label = QLabel("Aihetunniste")
self._add_widgets_to_form(label, text_input)

def add_theme(self, theme_name: str):
theme_type_line = QLineEdit(theme_name)
theme_type_line.setReadOnly(True)
label = QLabel("Kaavoitusteema")
self._add_widgets_to_form(label, theme_type_line)
Loading
Loading