|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from importlib import resources |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +from qgis.core import QgsApplication |
| 7 | +from qgis.PyQt import uic |
| 8 | +from qgis.PyQt.QtCore import pyqtSignal |
| 9 | +from qgis.PyQt.QtWidgets import QMenu, QWidget |
| 10 | + |
| 11 | +from arho_feature_template.core.models import Proposition, Regulation, RegulationGroup, RegulationLibrary |
| 12 | +from arho_feature_template.gui.components.plan_proposition_widget import PropositionWidget |
| 13 | +from arho_feature_template.gui.components.plan_regulation_widget import RegulationWidget |
| 14 | +from arho_feature_template.project.layers.code_layers import PlanRegulationGroupTypeLayer |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + from qgis.PyQt.QtWidgets import QFormLayout, QFrame, QLineEdit, QPushButton |
| 18 | + |
| 19 | +ui_path = resources.files(__package__) / "general_regulation_group_widget.ui" |
| 20 | +FormClass, _ = uic.loadUiType(ui_path) |
| 21 | + |
| 22 | + |
| 23 | +class GeneralRegulationGroupWidget(QWidget, FormClass): # type: ignore |
| 24 | + """A widget representation of a general regulation group.""" |
| 25 | + |
| 26 | + # open_as_form_signal = pyqtSignal(QWidget) |
| 27 | + delete_signal = pyqtSignal(QWidget) |
| 28 | + |
| 29 | + def __init__(self, regulation_group: RegulationGroup, layer_name: str): |
| 30 | + super().__init__() |
| 31 | + self.setupUi(self) |
| 32 | + |
| 33 | + # TYPES |
| 34 | + self.frame: QFrame |
| 35 | + self.name: QLineEdit |
| 36 | + # self.edit_btn: QPushButton |
| 37 | + self.add_field_btn: QPushButton |
| 38 | + self.del_btn: QPushButton |
| 39 | + self.regulation_group_details_layout: QFormLayout |
| 40 | + |
| 41 | + # INIT |
| 42 | + self.regulation_widgets: list[RegulationWidget] = [] |
| 43 | + self.proposition_widgets: list[PropositionWidget] = [] |
| 44 | + |
| 45 | + regulation_group.type_code_id = PlanRegulationGroupTypeLayer.get_id_by_feature_layer_name(layer_name) |
| 46 | + self.from_model(regulation_group) |
| 47 | + |
| 48 | + self.verbal_regulation_config = RegulationLibrary.get_regulation_by_code("sanallinenMaarays") |
| 49 | + |
| 50 | + # self.edit_btn.setIcon(QIcon(resources_path("icons", "settings.svg"))) |
| 51 | + # self.edit_btn.clicked.connect(lambda: self.open_as_form_signal.emit(self)) |
| 52 | + add_field_menu = QMenu() |
| 53 | + add_field_menu.addAction("Lisää kaavamääräys").triggered.connect(self.add_new_regulation) |
| 54 | + add_field_menu.addAction("Lisää kaavasuositus").triggered.connect(self.add_new_proposition) |
| 55 | + self.add_field_btn.setMenu(add_field_menu) |
| 56 | + self.add_field_btn.setIcon(QgsApplication.getThemeIcon("mActionAdd.svg")) |
| 57 | + |
| 58 | + self.del_btn.setIcon(QgsApplication.getThemeIcon("mActionDeleteSelected.svg")) |
| 59 | + self.del_btn.clicked.connect(lambda: self.delete_signal.emit(self)) |
| 60 | + |
| 61 | + def from_model(self, regulation_group: RegulationGroup): |
| 62 | + self.regulation_group = regulation_group |
| 63 | + |
| 64 | + self.name.setText(regulation_group.name if regulation_group.name else "") |
| 65 | + |
| 66 | + # Remove existing child widgets if reinitializing |
| 67 | + for widget in self.regulation_widgets: |
| 68 | + self.delete_regulation_widget(widget) |
| 69 | + for widget in self.proposition_widgets: |
| 70 | + self.delete_proposition_widget(widget) |
| 71 | + for regulation in regulation_group.regulations: |
| 72 | + self.add_regulation_widget(regulation) |
| 73 | + for proposition in regulation_group.propositions: |
| 74 | + self.add_proposition_widget(proposition) |
| 75 | + |
| 76 | + def add_new_regulation(self): |
| 77 | + regulation = Regulation(self.verbal_regulation_config) |
| 78 | + self.add_regulation_widget(regulation) |
| 79 | + |
| 80 | + def add_regulation_widget(self, regulation: Regulation) -> RegulationWidget: |
| 81 | + widget = RegulationWidget(regulation=regulation, parent=self.frame) |
| 82 | + widget.delete_signal.connect(self.delete_regulation_widget) |
| 83 | + self.frame.layout().addWidget(widget) |
| 84 | + self.regulation_widgets.append(widget) |
| 85 | + return widget |
| 86 | + |
| 87 | + def delete_regulation_widget(self, regulation_widget: RegulationWidget): |
| 88 | + self.frame.layout().removeWidget(regulation_widget) |
| 89 | + self.regulation_widgets.remove(regulation_widget) |
| 90 | + regulation_widget.deleteLater() |
| 91 | + |
| 92 | + def add_new_proposition(self): |
| 93 | + proposition = Proposition(value="") |
| 94 | + self.add_proposition_widget(proposition) |
| 95 | + |
| 96 | + def add_proposition_widget(self, proposition: Proposition) -> PropositionWidget: |
| 97 | + widget = PropositionWidget(proposition=proposition, parent=self.frame) |
| 98 | + widget.delete_signal.connect(self.delete_proposition_widget) |
| 99 | + self.frame.layout().addWidget(widget) |
| 100 | + self.proposition_widgets.append(widget) |
| 101 | + return widget |
| 102 | + |
| 103 | + def delete_proposition_widget(self, proposition_widget: RegulationWidget): |
| 104 | + self.frame.layout().removeWidget(proposition_widget) |
| 105 | + self.proposition_widgets.remove(proposition_widget) |
| 106 | + proposition_widget.deleteLater() |
| 107 | + |
| 108 | + def into_model(self) -> RegulationGroup: |
| 109 | + return RegulationGroup( |
| 110 | + type_code_id=self.regulation_group.type_code_id, |
| 111 | + name=self.name.text(), |
| 112 | + short_name=None, |
| 113 | + color_code=None, |
| 114 | + regulations=[widget.into_model() for widget in self.regulation_widgets], |
| 115 | + propositions=[widget.into_model() for widget in self.proposition_widgets], |
| 116 | + id_=self.regulation_group.id_, |
| 117 | + ) |
0 commit comments