Skip to content

Commit 8948619

Browse files
committed
Moved editing lifecycles to its own button
Removed lifecycle tab from plan attribute form. Created new button "Muokkaa elinkaaria" which allows editing the lifecycles.
1 parent 25fab2f commit 8948619

File tree

6 files changed

+167
-35
lines changed

6 files changed

+167
-35
lines changed

arho_feature_template/core/plan_manager.py

+17
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
RegulationGroupLibrary,
2222
)
2323
from arho_feature_template.exceptions import UnsavedChangesError
24+
from arho_feature_template.gui.dialogs.lifecycle_editor import LifecycleEditor
2425
from arho_feature_template.gui.dialogs.load_plan_dialog import LoadPlanDialog
2526
from arho_feature_template.gui.dialogs.plan_attribute_form import PlanAttributeForm
2627
from arho_feature_template.gui.dialogs.plan_feature_form import PlanFeatureForm
@@ -240,6 +241,22 @@ def edit_plan(self):
240241
feature = save_plan(attribute_form.model)
241242
self.regulation_groups_dock.initialize_regulation_groups(regulation_group_library_from_active_plan())
242243

244+
def edit_lifecycles(self):
245+
plan_layer = PlanLayer.get_from_project()
246+
if not plan_layer:
247+
return
248+
249+
active_plan_id = QgsExpressionContextUtils.projectScope(QgsProject.instance()).variable("active_plan_id")
250+
feature = PlanLayer.get_feature_by_id(active_plan_id, no_geometries=False)
251+
if feature is None:
252+
return
253+
plan_model = PlanLayer.model_from_feature(feature)
254+
255+
lifecycle_editor = LifecycleEditor(plan=plan_model)
256+
257+
if lifecycle_editor.exec_():
258+
save_plan(plan_model)
259+
243260
def add_new_plan_feature(self):
244261
if not handle_unsaved_changes():
245262
return
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from __future__ import annotations
2+
3+
from importlib import resources
4+
from typing import TYPE_CHECKING
5+
6+
from qgis.PyQt import uic
7+
from qgis.PyQt.QtWidgets import QDialog, QDialogButtonBox, QPushButton
8+
9+
from arho_feature_template.gui.components.lifecycle_table_widget import LifecycleTableWidget
10+
11+
if TYPE_CHECKING:
12+
from arho_feature_template.core.models import Plan
13+
14+
ui_path = resources.files(__package__) / "lifecycle_editor.ui"
15+
FormClass, _ = uic.loadUiType(ui_path)
16+
17+
18+
class LifecycleEditor(QDialog, FormClass): # type: ignore
19+
add_lifecycle_button: QPushButton
20+
button_box: QDialogButtonBox
21+
22+
def __init__(self, plan: Plan, parent=None):
23+
super().__init__(parent)
24+
self.setupUi(self)
25+
26+
self.plan = plan
27+
if not self.plan:
28+
self.reject()
29+
return
30+
31+
self.lifecycle_table = LifecycleTableWidget(self.plan.lifecycles)
32+
self.layout().insertWidget(1, self.lifecycle_table)
33+
34+
self.add_lifecycle_button.clicked.connect(self.lifecycle_table.add_new_lifecycle_row)
35+
self.lifecycle_table.table_edited.connect(self._check_required_fields)
36+
37+
self.button_box.accepted.connect(self._on_ok_clicked)
38+
self.button_box.rejected.connect(self.reject)
39+
self.button_box.button(QDialogButtonBox.Ok).setEnabled(False)
40+
41+
self._check_required_fields()
42+
43+
def _check_required_fields(self) -> None:
44+
"""Enable/disable the OK button based on the validation of the lifecycle table."""
45+
ok_button = self.button_box.button(QDialogButtonBox.Ok)
46+
ok_button.setEnabled(self.lifecycle_table.is_ok())
47+
48+
def _on_ok_clicked(self):
49+
"""Replace existing lifecycles with updated data and save the plan."""
50+
if not self.plan:
51+
return
52+
53+
self.plan.lifecycles = self.lifecycle_table.into_model()
54+
55+
self.accept()
56+
57+
def reject(self):
58+
super().reject()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>Dialog</class>
4+
<widget class="QDialog" name="Dialog">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>400</width>
10+
<height>300</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Dialog</string>
15+
</property>
16+
<layout class="QVBoxLayout" name="verticalLayout">
17+
<item>
18+
<widget class="QPushButton" name="add_lifecycle_button">
19+
<property name="sizePolicy">
20+
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
21+
<horstretch>0</horstretch>
22+
<verstretch>0</verstretch>
23+
</sizepolicy>
24+
</property>
25+
<property name="text">
26+
<string>Lisää elinkaari</string>
27+
</property>
28+
</widget>
29+
</item>
30+
<item>
31+
<widget class="QDialogButtonBox" name="button_box">
32+
<property name="orientation">
33+
<enum>Qt::Horizontal</enum>
34+
</property>
35+
<property name="standardButtons">
36+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
37+
</property>
38+
</widget>
39+
</item>
40+
</layout>
41+
</widget>
42+
<resources/>
43+
<connections>
44+
<connection>
45+
<sender>button_box</sender>
46+
<signal>accepted()</signal>
47+
<receiver>Dialog</receiver>
48+
<slot>accept()</slot>
49+
<hints>
50+
<hint type="sourcelabel">
51+
<x>248</x>
52+
<y>254</y>
53+
</hint>
54+
<hint type="destinationlabel">
55+
<x>157</x>
56+
<y>274</y>
57+
</hint>
58+
</hints>
59+
</connection>
60+
<connection>
61+
<sender>button_box</sender>
62+
<signal>rejected()</signal>
63+
<receiver>Dialog</receiver>
64+
<slot>reject()</slot>
65+
<hints>
66+
<hint type="sourcelabel">
67+
<x>316</x>
68+
<y>260</y>
69+
</hint>
70+
<hint type="destinationlabel">
71+
<x>286</x>
72+
<y>274</y>
73+
</hint>
74+
</hints>
75+
</connection>
76+
</connections>
77+
</ui>

arho_feature_template/gui/dialogs/plan_attribute_form.py

+1-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from arho_feature_template.gui.components.general_regulation_group_widget import GeneralRegulationGroupWidget
1818

1919
# from arho_feature_template.gui.components.plan_regulation_group_widget import RegulationGroupWidget
20-
from arho_feature_template.gui.components.lifecycle_table_widget import LifecycleTableWidget
2120
from arho_feature_template.gui.components.plan_document_widget import DocumentWidget
2221
from arho_feature_template.project.layers.code_layers import (
2322
LifeCycleStatusLayer,
@@ -55,10 +54,6 @@ class PlanAttributeForm(QDialog, FormClass): # type: ignore
5554
documents_layout: QVBoxLayout
5655
add_document_btn: QPushButton
5756

58-
lifecycle_tab_layout: QVBoxLayout
59-
add_lifecycle_button: QPushButton
60-
# delete_lifecycle: QPushButton
61-
6257
button_box: QDialogButtonBox
6358

6459
def __init__(self, plan: Plan, _regulation_group_libraries: list[RegulationGroupLibrary], parent=None):
@@ -116,13 +111,6 @@ def __init__(self, plan: Plan, _regulation_group_libraries: list[RegulationGroup
116111
self.add_document_btn.clicked.connect(self.add_new_document)
117112
self.add_document_btn.setIcon(QgsApplication.getThemeIcon("mActionAdd.svg"))
118113

119-
# Lifecycle table setup
120-
self.lifecycle_table = LifecycleTableWidget(self.plan.lifecycles)
121-
self.lifecycle_tab_layout.insertWidget(1, self.lifecycle_table)
122-
self.lifecycle_table.table_edited.connect(self._check_required_fields)
123-
self.add_lifecycle_button.clicked.connect(self.lifecycle_table.add_new_lifecycle_row)
124-
# self.delete_lifecycle_button.clicked.connect(self.delete_lifecycle_row)
125-
126114
self.button_box.button(QDialogButtonBox.Ok).setEnabled(False)
127115
self.button_box.accepted.connect(self._on_ok_clicked)
128116

@@ -136,7 +124,6 @@ def _check_required_fields(self) -> None:
136124
and self.organisation_combo_box.value() is not None
137125
and self.lifecycle_status_combo_box.value() is not None
138126
and all(document_widget.is_ok() for document_widget in self.document_widgets)
139-
and self.lifecycle_table.is_ok()
140127
):
141128
ok_button.setEnabled(True)
142129
else:
@@ -213,8 +200,7 @@ def into_model(self) -> Plan:
213200
record_number=self.record_number_line_edit.text() or None,
214201
producers_plan_identifier=self.producers_plan_identifier_line_edit.text() or None,
215202
matter_management_identifier=self.matter_management_identifier_line_edit.text() or None,
216-
lifecycle_status_id=self.lifecycle_status_combo_box.value(), # Need to get the lifecycle with the latest lifecycle
217-
lifecycles=self.lifecycle_table.into_model(),
203+
lifecycle_status_id=self.lifecycle_status_combo_box.value(),
218204
general_regulations=[reg_group_widget.into_model() for reg_group_widget in self.regulation_group_widgets],
219205
documents=[document_widget.into_model() for document_widget in self.document_widgets],
220206
geom=self.plan.geom,

arho_feature_template/gui/dialogs/plan_attribute_form.ui

-20
Original file line numberDiff line numberDiff line change
@@ -338,26 +338,6 @@
338338
</item>
339339
</layout>
340340
</widget>
341-
<widget class="QWidget" name="lifecycles_tab">
342-
<attribute name="title">
343-
<string>Elinkaaret</string>
344-
</attribute>
345-
<layout class="QVBoxLayout" name="lifecycle_tab_layout">
346-
<item>
347-
<widget class="QPushButton" name="add_lifecycle_button">
348-
<property name="sizePolicy">
349-
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
350-
<horstretch>0</horstretch>
351-
<verstretch>0</verstretch>
352-
</sizepolicy>
353-
</property>
354-
<property name="text">
355-
<string>Lisää elinkaari</string>
356-
</property>
357-
</widget>
358-
</item>
359-
</layout>
360-
</widget>
361341
</widget>
362342
</item>
363343
<item>

arho_feature_template/plugin.py

+14
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ def initGui(self) -> None: # noqa N802
227227
status_tip="Tallenna aktiivinen kaava JSON muodossa",
228228
)
229229

230+
self.edit_lifecycles_action = self.add_action(
231+
text="Muokkaa elinkaaria",
232+
icon=QgsApplication.getThemeIcon("mActionFileSaveAs.svg"),
233+
# icon=QIcon(resources_path("icons", "toolbar", "tallenna_jsonina2.svg")),
234+
triggered_callback=self.edit_lifecycles,
235+
add_to_menu=True,
236+
add_to_toolbar=True,
237+
status_tip="Muokkaa kaavan elinkaaria",
238+
)
239+
230240
self.plugin_settings_action = self.add_action(
231241
text="Asetukset",
232242
triggered_callback=self.open_settings,
@@ -254,6 +264,10 @@ def open_settings(self):
254264
settings = PluginSettings()
255265
settings.exec_()
256266

267+
def edit_lifecycles(self):
268+
"""Edit lifecycles of currently active plan."""
269+
self.plan_manager.edit_lifecycles()
270+
257271
def unload(self) -> None:
258272
"""Removes the plugin menu item and icon from QGIS GUI."""
259273
# Handle signals

0 commit comments

Comments
 (0)