Skip to content

Commit 8a54839

Browse files
committed
add possibility to open regulation group as form/dialog from plan feature form dialog
1 parent 9559ca0 commit 8a54839

File tree

4 files changed

+75
-15
lines changed

4 files changed

+75
-15
lines changed

arho_feature_template/gui/components/plan_regulation_group_widget.py

+30-15
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
from qgis.core import QgsApplication
77
from qgis.PyQt import uic
88
from qgis.PyQt.QtCore import pyqtSignal
9+
from qgis.PyQt.QtGui import QIcon
910
from qgis.PyQt.QtWidgets import QWidget
1011

1112
from arho_feature_template.core.models import Proposition, Regulation, RegulationGroup
1213
from arho_feature_template.gui.components.plan_proposition_widget import PropositionWidget
1314
from arho_feature_template.gui.components.plan_regulation_widget import RegulationWidget
1415
from arho_feature_template.project.layers.code_layers import PlanRegulationGroupTypeLayer
16+
from arho_feature_template.qgis_plugin_tools.tools.resources import resources_path
1517

1618
if TYPE_CHECKING:
1719
from qgis.PyQt.QtWidgets import QFormLayout, QFrame, QLabel, QLineEdit, QPushButton
@@ -23,9 +25,10 @@
2325
class RegulationGroupWidget(QWidget, FormClass): # type: ignore
2426
"""A widget representation of a plan regulation group."""
2527

28+
open_as_form_signal = pyqtSignal(QWidget)
2629
delete_signal = pyqtSignal(QWidget)
2730

28-
def __init__(self, regulation_group_data: RegulationGroup, layer_name: str):
31+
def __init__(self, regulation_group: RegulationGroup, layer_name: str):
2932
super().__init__()
3033
self.setupUi(self)
3134

@@ -34,24 +37,36 @@ def __init__(self, regulation_group_data: RegulationGroup, layer_name: str):
3437
self.name: QLineEdit
3538
self.short_name: QLineEdit
3639
self.short_name_label: QLabel
40+
self.edit_btn: QPushButton
3741
self.del_btn: QPushButton
3842
self.regulation_group_details_layout: QFormLayout
3943

4044
# INIT
41-
self.regulation_group_data = regulation_group_data
42-
self.regulation_widgets: list[RegulationWidget] = [
43-
self.add_regulation_widget(regulation) for regulation in self.regulation_group_data.regulations
44-
]
45-
self.proposition_widgets: list[PropositionWidget] = [
46-
self.add_proposition_widget(proposition) for proposition in self.regulation_group_data.propositions
47-
]
48-
49-
regulation_group_data.type_code_id = PlanRegulationGroupTypeLayer.get_id_by_feature_layer_name(layer_name)
50-
self.name.setText(self.regulation_group_data.name if self.regulation_group_data.name else "")
51-
self.short_name.setText(self.regulation_group_data.short_name if self.regulation_group_data.short_name else "")
45+
self.regulation_widgets: list[RegulationWidget] = []
46+
self.proposition_widgets: list[PropositionWidget] = []
47+
48+
regulation_group.type_code_id = PlanRegulationGroupTypeLayer.get_id_by_feature_layer_name(layer_name)
49+
self.from_model(regulation_group)
50+
51+
self.edit_btn.setIcon(QIcon(resources_path("icons", "settings.svg")))
52+
self.edit_btn.clicked.connect(lambda: self.open_as_form_signal.emit(self))
5253
self.del_btn.setIcon(QgsApplication.getThemeIcon("mActionDeleteSelected.svg"))
5354
self.del_btn.clicked.connect(lambda: self.delete_signal.emit(self))
5455

56+
def from_model(self, regulation_group: RegulationGroup):
57+
self.regulation_group = regulation_group
58+
59+
self.name.setText(regulation_group.name if regulation_group.name else "")
60+
self.short_name.setText(regulation_group.short_name if regulation_group.short_name else "")
61+
62+
# Remove existing child widgets if reinitializing
63+
for widget in self.regulation_widgets:
64+
self.delete_regulation_widget(widget)
65+
for widget in self.proposition_widgets:
66+
self.delete_proposition_widget(widget)
67+
self.regulation_widgets = [self.add_regulation_widget(reg) for reg in regulation_group.regulations]
68+
self.proposition_widgets = [self.add_proposition_widget(prop) for prop in regulation_group.propositions]
69+
5570
def add_regulation_widget(self, regulation: Regulation) -> RegulationWidget:
5671
widget = RegulationWidget(regulation=regulation, parent=self.frame)
5772
widget.delete_signal.connect(self.delete_regulation_widget)
@@ -76,11 +91,11 @@ def delete_proposition_widget(self, proposition_widget: RegulationWidget):
7691

7792
def into_model(self) -> RegulationGroup:
7893
return RegulationGroup(
79-
type_code_id=self.regulation_group_data.type_code_id,
94+
type_code_id=self.regulation_group.type_code_id,
8095
name=self.name.text(),
8196
short_name=None if not self.short_name.text() else self.short_name.text(),
82-
color_code=self.regulation_group_data.color_code,
97+
color_code=self.regulation_group.color_code,
8398
regulations=[widget.into_model() for widget in self.regulation_widgets],
8499
propositions=[widget.into_model() for widget in self.proposition_widgets],
85-
id_=self.regulation_group_data.id_,
100+
id_=self.regulation_group.id_,
86101
)

arho_feature_template/gui/components/plan_regulation_group_widget.ui

+25
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@
6868
</property>
6969
</spacer>
7070
</item>
71+
<item>
72+
<widget class="QPushButton" name="edit_btn">
73+
<property name="sizePolicy">
74+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
75+
<horstretch>0</horstretch>
76+
<verstretch>0</verstretch>
77+
</sizepolicy>
78+
</property>
79+
<property name="maximumSize">
80+
<size>
81+
<width>30</width>
82+
<height>16777215</height>
83+
</size>
84+
</property>
85+
<property name="toolTip">
86+
<string>Muokkaa kaavamääräysryhmää</string>
87+
</property>
88+
<property name="text">
89+
<string/>
90+
</property>
91+
</widget>
92+
</item>
7193
<item>
7294
<widget class="QPushButton" name="del_btn">
7395
<property name="sizePolicy">
@@ -82,6 +104,9 @@
82104
<height>16777215</height>
83105
</size>
84106
</property>
107+
<property name="toolTip">
108+
<string>Poista kaavamääräysryhmä</string>
109+
</property>
85110
<property name="text">
86111
<string/>
87112
</property>

arho_feature_template/gui/dialogs/plan_attribute_form.py

+10
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
from arho_feature_template.core.models import Plan, RegulationGroup, RegulationGroupLibrary
2020
from arho_feature_template.gui.components.plan_regulation_group_widget import RegulationGroupWidget
2121
from arho_feature_template.gui.components.tree_with_search_widget import TreeWithSearchWidget
22+
from arho_feature_template.gui.dialogs.plan_regulation_group_form import PlanRegulationGroupForm
2223
from arho_feature_template.project.layers.code_layers import (
2324
LifeCycleStatusLayer,
2425
OrganisationLayer,
2526
PlanTypeLayer,
2627
)
28+
from arho_feature_template.utils.misc_utils import disconnect_signal
2729

2830
if TYPE_CHECKING:
2931
from qgis.PyQt.QtWidgets import QComboBox, QLineEdit, QTextEdit, QVBoxLayout, QWidget
@@ -132,12 +134,20 @@ def add_selected_plan_regulation_group(self, item: QTreeWidgetItem, column: int)
132134
def add_plan_regulation_group(self, regulation_group: RegulationGroup):
133135
regulation_group_widget = RegulationGroupWidget(regulation_group, layer_name="Kaava")
134136
regulation_group_widget.delete_signal.connect(self.remove_plan_regulation_group)
137+
regulation_group_widget.open_as_form_signal.connect(self.open_plan_regulation_group_form)
135138
self._remove_spacer()
136139
self.plan_regulation_group_scrollarea_contents.layout().addWidget(regulation_group_widget)
137140
self.regulation_group_widgets.append(regulation_group_widget)
138141
self._add_spacer()
139142

143+
def open_plan_regulation_group_form(self, regulation_group_widget: RegulationGroupWidget):
144+
group_as_form = PlanRegulationGroupForm(regulation_group_widget.into_model())
145+
if group_as_form.exec_():
146+
regulation_group_widget.from_model(group_as_form.model)
147+
140148
def remove_plan_regulation_group(self, regulation_group_widget: RegulationGroupWidget):
149+
disconnect_signal(regulation_group_widget.delete_signal)
150+
disconnect_signal(regulation_group_widget.open_as_form_signal)
141151
self.plan_regulation_group_scrollarea_contents.layout().removeWidget(regulation_group_widget)
142152
self.regulation_group_widgets.remove(regulation_group_widget)
143153
regulation_group_widget.deleteLater()

arho_feature_template/gui/dialogs/plan_feature_form.py

+10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
from arho_feature_template.core.models import PlanFeature, RegulationGroup
2222
from arho_feature_template.gui.components.plan_regulation_group_widget import RegulationGroupWidget
2323
from arho_feature_template.gui.components.tree_with_search_widget import TreeWithSearchWidget
24+
from arho_feature_template.gui.dialogs.plan_regulation_group_form import PlanRegulationGroupForm
2425
from arho_feature_template.project.layers.code_layers import UndergroundTypeLayer
26+
from arho_feature_template.utils.misc_utils import disconnect_signal
2527

2628
if TYPE_CHECKING:
2729
from qgis.PyQt.QtWidgets import QVBoxLayout, QWidget
@@ -105,12 +107,20 @@ def add_selected_plan_regulation_group(self, item: QTreeWidgetItem, column: int)
105107
def add_plan_regulation_group(self, definition: RegulationGroup):
106108
regulation_group_widget = RegulationGroupWidget(definition, cast(str, self.layer_name))
107109
regulation_group_widget.delete_signal.connect(self.remove_plan_regulation_group)
110+
regulation_group_widget.open_as_form_signal.connect(self.open_plan_regulation_group_form)
108111
self._remove_spacer()
109112
self.plan_regulation_group_scrollarea_contents.layout().addWidget(regulation_group_widget)
110113
self.regulation_group_widgets.append(regulation_group_widget)
111114
self._add_spacer()
112115

116+
def open_plan_regulation_group_form(self, regulation_group_widget: RegulationGroupWidget):
117+
group_as_form = PlanRegulationGroupForm(regulation_group_widget.into_model())
118+
if group_as_form.exec_():
119+
regulation_group_widget.from_model(group_as_form.model)
120+
113121
def remove_plan_regulation_group(self, regulation_group_widget: RegulationGroupWidget):
122+
disconnect_signal(regulation_group_widget.delete_signal)
123+
disconnect_signal(regulation_group_widget.open_as_form_signal)
114124
self.plan_regulation_group_scrollarea_contents.layout().removeWidget(regulation_group_widget)
115125
self.regulation_group_widgets.remove(regulation_group_widget)
116126
regulation_group_widget.deleteLater()

0 commit comments

Comments
 (0)