|
8 | 8 | from qgis.PyQt.QtCore import pyqtSignal
|
9 | 9 | from qgis.PyQt.QtWidgets import QWidget
|
10 | 10 |
|
11 |
| -from arho_feature_template.gui.plan_regulation_widget import PlanRegulationWidget |
| 11 | +from arho_feature_template.gui.plan_regulation_widget import RegulationWidget |
| 12 | +from arho_feature_template.core.models import Regulation, RegulationGroup |
| 13 | +from arho_feature_template.project.layers.plan_layers import RegulationGroupLayer |
12 | 14 |
|
13 | 15 | if TYPE_CHECKING:
|
14 | 16 | from qgis.PyQt.QtWidgets import QFrame, QLineEdit, QPushButton
|
15 | 17 |
|
16 |
| - from arho_feature_template.core.plan_regulation_config import PlanRegulationDefinition |
17 |
| - from arho_feature_template.core.plan_regulation_group_config import PlanRegulationGroupDefinition |
18 |
| - |
19 | 18 | ui_path = resources.files(__package__) / "plan_regulation_group_widget.ui"
|
20 | 19 | FormClass, _ = uic.loadUiType(ui_path)
|
21 | 20 |
|
22 | 21 |
|
23 |
| -class PlanRegulationGroupWidget(QWidget, FormClass): # type: ignore |
| 22 | +class RegulationGroupWidget(QWidget, FormClass): # type: ignore |
24 | 23 | """A widget representation of a plan regulation group."""
|
25 | 24 |
|
26 | 25 | delete_signal = pyqtSignal(QWidget)
|
27 | 26 |
|
28 |
| - def __init__(self, group_definition: PlanRegulationGroupDefinition): |
| 27 | + def __init__(self, regulation_group_data: RegulationGroup): |
29 | 28 | super().__init__()
|
30 | 29 | self.setupUi(self)
|
31 | 30 |
|
32 | 31 | # TYPES
|
33 | 32 | self.frame: QFrame
|
34 |
| - self.heading: QLineEdit |
| 33 | + self.name: QLineEdit # "heading" |
35 | 34 | self.del_btn: QPushButton
|
36 | 35 |
|
37 | 36 | # INIT
|
38 |
| - self.group_definition = group_definition |
39 |
| - self.layer = "plan_regulation_group" |
40 |
| - |
41 |
| - self.heading.setText(self.group_definition.name) |
42 |
| - self.init_buttons() |
43 |
| - for plan_regulation_definition in self.group_definition.plan_regulations: |
44 |
| - _ = self.add_plan_regulation_widget(plan_regulation_definition) |
45 |
| - |
46 |
| - def init_buttons(self): |
| 37 | + self.regulation_group_data = regulation_group_data |
| 38 | + self.regulation_widgets: list[RegulationWidget] = [ |
| 39 | + self.add_regulation_widget(regulation) for regulation in self.regulation_group_data.regulations |
| 40 | + ] |
| 41 | + self.name.setText(self.regulation_group_data.name) |
47 | 42 | self.del_btn.setIcon(QgsApplication.getThemeIcon("mActionDeleteSelected.svg"))
|
48 | 43 | self.del_btn.clicked.connect(lambda: self.delete_signal.emit(self))
|
49 |
| - |
50 |
| - def add_plan_regulation_widget(self, definition: PlanRegulationDefinition) -> PlanRegulationWidget: |
51 |
| - widget = PlanRegulationWidget.from_definition(definition=definition, parent=self.frame) |
52 |
| - widget.delete_signal.connect(self.delete_plan_regulation_widget) |
| 44 | + |
| 45 | + def add_regulation_widget(self, regulation: Regulation) -> RegulationWidget: |
| 46 | + widget = RegulationWidget(regulation_data=regulation, parent=self.frame) |
| 47 | + widget.delete_signal.connect(self.delete_regulation_widget) |
53 | 48 | self.frame.layout().addWidget(widget)
|
54 | 49 | return widget
|
55 | 50 |
|
56 |
| - def delete_plan_regulation_widget(self, plan_regulation_widget: PlanRegulationWidget): |
57 |
| - self.frame.layout().removeWidget(plan_regulation_widget) |
58 |
| - plan_regulation_widget.deleteLater() |
| 51 | + def delete_regulation_widget(self, regulation_widget: RegulationWidget): |
| 52 | + self.frame.layout().removeWidget(regulation_widget) |
| 53 | + self.regulation_widgets.remove(regulation_widget) |
| 54 | + regulation_widget.deleteLater() |
| 55 | + |
| 56 | + def into_model(self, id: str) -> RegulationGroup: |
| 57 | + return RegulationGroup( |
| 58 | + type_code=self.regulation_group_data.type_code, |
| 59 | + name=self.name.text(), |
| 60 | + short_name=self.regulation_group_data.short_name, |
| 61 | + color_code=self.regulation_group_data.color_code, |
| 62 | + letter_code=self.regulation_group_data.letter_code, |
| 63 | + plan_regulations=[widget.save_data(id) for widget in self.regulation_widgets], |
| 64 | + id_=id |
| 65 | + ) |
| 66 | + |
| 67 | + def save_data(self): |
| 68 | + if not self.regulation_group_data.id_: |
| 69 | + new_feature = True |
| 70 | + id_ = "new_id_here" # TODO |
| 71 | + else: |
| 72 | + new_feature = False |
| 73 | + id_ = self.regulation_group_data.id_ |
| 74 | + |
| 75 | + model = self.into_model(id_) |
| 76 | + regulation_feature = RegulationGroupLayer.feature_from_model(model) |
| 77 | + layer = RegulationGroupLayer.get_from_project() |
| 78 | + if new_feature: |
| 79 | + layer.addFeature(regulation_feature) |
| 80 | + else: |
| 81 | + layer.updateFeature(regulation_feature) |
0 commit comments