3
3
from importlib import resources
4
4
from typing import TYPE_CHECKING
5
5
6
+ from qgis .gui import QgsDateTimeEdit
6
7
from qgis .PyQt import uic
7
- from qgis .PyQt .QtCore import QDate , Qt
8
- from qgis .PyQt .QtGui import QStandardItem , QStandardItemModel
8
+ from qgis .PyQt .QtCore import Qt
9
+ from qgis .PyQt .QtGui import QStandardItem
9
10
from qgis .PyQt .QtWidgets import (
10
11
QComboBox ,
11
12
QDateEdit ,
12
13
QDialog ,
13
14
QDialogButtonBox ,
15
+ QHeaderView ,
14
16
QLineEdit ,
15
17
QPushButton ,
16
18
QSizePolicy ,
17
19
QSpacerItem ,
18
- QStyledItemDelegate ,
19
20
QTableWidget ,
21
+ QTableWidgetItem ,
20
22
QTextEdit ,
21
23
QTreeWidgetItem ,
22
24
QWidget ,
41
43
FormClass , _ = uic .loadUiType (ui_path )
42
44
43
45
44
- class LifecycleTableModel (QStandardItemModel ):
45
- def __init__ (self , status_options , parent = None ):
46
- super ().__init__ (parent )
47
- self .status_options = status_options
48
-
49
- def flags (self , index ):
50
- if index .column () == 0 : # "Elinkaaren tila" - editable combo box
51
- return Qt .ItemIsSelectable | Qt .ItemIsEnabled
52
-
53
- if index .column () in (1 , 2 ): # Dates
54
- return Qt .ItemIsSelectable | Qt .ItemIsEnabled | Qt .ItemIsEditable
55
- return super ().flags (index )
56
-
57
-
58
- class LifecycleDelegate (QStyledItemDelegate ):
59
- def create_editor (self , parent , option , index ):
60
- if index .column () == 0 : # Status column
61
- lifecycle_combo_box = CodeComboBox (parent )
62
- lifecycle_combo_box .populate_from_code_layer (LifeCycleStatusLayer )
63
- return lifecycle_combo_box
64
- if index .column () in (1 , 2 ): # Dates columns
65
- date_edit = QDateEdit (parent )
66
- date_edit .setDisplayFormat ("yyyy-MM-dd" )
67
- date_edit .setCalendarPopup (True )
68
- return date_edit
69
- return super ().createEditor (parent , option , index )
70
-
71
- def set_editor_data (self , editor , index ):
72
- if isinstance (editor , CodeComboBox ) and index .column () == 0 :
73
- value = index .data (Qt .EditRole )
74
- if value is not None :
75
- editor .set_value (value )
76
- elif isinstance (editor , QDateEdit ) and index .column () in (1 , 2 ):
77
- value = index .data (Qt .EditRole )
78
- if value :
79
- editor .setDate (QDate .fromString (value , "yyyy-MM-dd" ))
80
-
81
- def set_model_data (self , editor , model , index ):
82
- if isinstance (editor , CodeComboBox ) and index .column () == 0 :
83
- model .setData (index , editor .value (), Qt .EditRole )
84
- if isinstance (editor , QDateEdit ) and index .column () in (1 , 2 ):
85
- model .setData (index , editor .date ().toString ("yyyy-MM-dd" ), Qt .EditRole )
86
-
87
-
88
46
class PlanAttributeForm (QDialog , FormClass ): # type: ignore
89
47
permanent_identifier_line_edit : QLineEdit
90
48
name_line_edit : QLineEdit
@@ -102,16 +60,22 @@ class PlanAttributeForm(QDialog, FormClass): # type: ignore
102
60
103
61
lifecycle_table : QTableWidget
104
62
add_lifecycle : QPushButton
105
- delete_lifecycle : QPushButton
63
+ # delete_lifecycle: QPushButton
106
64
107
65
button_box : QDialogButtonBox
108
66
109
- def __init__ (self , plan : Plan , regulation_group_libraries : list [RegulationGroupLibrary ], parent = None ):
67
+ def __init__ (
68
+ self ,
69
+ plan : Plan ,
70
+ regulation_group_libraries : list [RegulationGroupLibrary ],
71
+ parent = None ,
72
+ ):
110
73
super ().__init__ (parent )
111
74
112
75
self .setupUi (self )
113
76
114
77
self .plan = plan
78
+ self .lifecycle_models = plan .lifecycles
115
79
116
80
self .plan_type_combo_box .populate_from_code_layer (PlanTypeLayer )
117
81
self .lifecycle_status_combo_box .populate_from_code_layer (LifeCycleStatusLayer )
@@ -151,12 +115,22 @@ def __init__(self, plan: Plan, regulation_group_libraries: list[RegulationGroupL
151
115
self .add_plan_regulation_group (regulation_group )
152
116
153
117
# Lifecycle table setup
154
- self .lifecycle_table .setColumnCount (3 ) # Three columns: Status, Start Date, End Date
155
- self .lifecycle_table .setHorizontalHeaderLabels (["Elinkaaren tila" , "Alkupvm" , "Loppupvm" ])
156
- self .lifecycle_table .setRowCount (0 ) # No rows initially
118
+ self .lifecycle_table .setColumnCount (4 )
119
+ self .lifecycle_table .setHorizontalHeaderLabels (["Elinkaaren tila" , "Alkupvm" , "Loppupvm" , "id" ])
120
+ # self.lifecycle_table.setColumnHidden(3, True)
121
+ self .lifecycle_table .setRowCount (0 )
122
+
123
+ header = self .lifecycle_table .horizontalHeader ()
124
+ header .setSectionResizeMode (0 , QHeaderView .Stretch )
125
+ header .setSectionResizeMode (1 , QHeaderView .ResizeToContents )
126
+ header .setSectionResizeMode (2 , QHeaderView .ResizeToContents )
127
+
128
+ self .populate_lifecycle_table ()
129
+ # for lifecycle in plan.lifecycles:
130
+ # self.add_lifecycle_row(lifecycle)
157
131
158
132
self .add_lifecycle_button .clicked .connect (self .add_lifecycle_row )
159
- self .delete_lifecycle_button .clicked .connect (self .delete_lifecycle_row )
133
+ # self.delete_lifecycle_button.clicked.connect(self.delete_lifecycle_row)
160
134
161
135
self .button_box .button (QDialogButtonBox .Ok ).setEnabled (False )
162
136
self .button_box .accepted .connect (self ._on_ok_clicked )
@@ -166,7 +140,6 @@ def __init__(self, plan: Plan, regulation_group_libraries: list[RegulationGroupL
166
140
def _check_required_fields (self ) -> None :
167
141
ok_button = self .button_box .button (QDialogButtonBox .Ok )
168
142
169
- # Check if all required fields are filled and lifecycle table has at least one valid row
170
143
has_valid_lifecycle_row = False
171
144
for row in range (self .lifecycle_table .rowCount ()):
172
145
status_item = self .lifecycle_table .cellWidget (row , 0 )
@@ -176,7 +149,7 @@ def _check_required_fields(self) -> None:
176
149
if (
177
150
status_item
178
151
and status_item .value () is not None
179
- and start_date_item
152
+ # and start_date_item
180
153
and start_date_item .date ()
181
154
and (end_date_item and end_date_item .date () or True )
182
155
):
@@ -235,56 +208,107 @@ def init_plan_regulation_group_library(self, library: RegulationGroupLibrary):
235
208
236
209
# ---
237
210
211
+ def populate_lifecycle_table (self ):
212
+ # kutsu add_lifecycle_row
213
+ self .lifecycle_table .setRowCount (len (self .lifecycle_models ))
214
+
215
+ for row , lifecycle in enumerate (self .lifecycle_models ):
216
+ # Populate the status combobox
217
+ status_combobox = CodeComboBox (self )
218
+ status_combobox .populate_from_code_layer (LifeCycleStatusLayer )
219
+ status_combobox .set_value (lifecycle .status_id )
220
+ self .lifecycle_table .setCellWidget (row , 0 , status_combobox )
221
+
222
+ # Populate the start date edit field
223
+ start_date_edit = QDateEdit (self )
224
+ start_date_edit .setDisplayFormat ("yyyy-MM-dd" )
225
+ start_date_edit .setCalendarPopup (True )
226
+ start_date_edit .setDate (lifecycle .starting_at .date ())
227
+ self .lifecycle_table .setCellWidget (row , 1 , start_date_edit )
228
+
229
+ # Populate the end date edit field
230
+ end_date_edit = QDateEdit (self )
231
+ end_date_edit .setDisplayFormat ("yyyy-MM-dd" )
232
+ end_date_edit .setCalendarPopup (True )
233
+ if lifecycle .ending_at :
234
+ end_date_edit .setDate (lifecycle .ending_at .date ())
235
+ self .lifecycle_table .setCellWidget (row , 2 , end_date_edit )
236
+
237
+ # Populate the ID column
238
+ id_item = QTableWidgetItem (str (lifecycle .id_ ) if lifecycle .id_ else "" )
239
+ id_item .setData (Qt .UserRole , lifecycle .id_ )
240
+ self .lifecycle_table .setItem (row , 3 , id_item )
241
+
238
242
def add_lifecycle_row (self ):
239
243
row_position = self .lifecycle_table .rowCount ()
240
244
self .lifecycle_table .insertRow (row_position )
241
245
242
- status = CodeComboBox (self )
246
+ status = CodeComboBox ()
243
247
status .populate_from_code_layer (LifeCycleStatusLayer )
244
248
self .lifecycle_table .setCellWidget (row_position , 0 , status )
245
249
246
- start_date_edit = QDateEdit ( self )
250
+ start_date_edit = QgsDateTimeEdit ( )
247
251
start_date_edit .setDisplayFormat ("yyyy-MM-dd" )
248
252
start_date_edit .setCalendarPopup (True )
249
253
self .lifecycle_table .setCellWidget (row_position , 1 , start_date_edit )
250
254
251
- end_date_edit = QDateEdit ( self )
255
+ end_date_edit = QgsDateTimeEdit ( )
252
256
end_date_edit .setDisplayFormat ("yyyy-MM-dd" )
253
257
end_date_edit .setCalendarPopup (True )
258
+ end_date_edit .clear ()
254
259
self .lifecycle_table .setCellWidget (row_position , 2 , end_date_edit )
255
260
256
- self .lifecycle_table .resizeRowsToContents ()
257
- self .lifecycle_table .resizeColumnsToContents ()
258
-
259
- def delete_lifecycle_row (self ):
260
- selected_rows = self .lifecycle_table .selectionModel ().selectedRows ()
261
+ """ def add_lifecycle_row(self, lifecycle: LifeCycle | None):
262
+ # Add a new row at the end of the table
263
+ row_position = self.lifecycle_table.rowCount()
264
+ self.lifecycle_table.insertRow(row_position)
261
265
262
- if selected_rows :
263
- row_position = selected_rows [ 0 ]. row ()
264
- self . lifecycle_table . removeRow ( row_position )
265
- self ._check_required_fields ( )
266
+ # Status combobox
267
+ status_combobox = CodeComboBox ()
268
+ status_combobox.populate_from_code_layer(LifeCycleStatusLayer )
269
+ self.lifecycle_table.setCellWidget(row_position, 0, status_combobox )
266
270
267
- def save_lifecycle ( self ):
268
- for row in range ( self . lifecycle_table . rowCount ()):
269
- status = self . lifecycle_table . cellWidget ( row , 0 )
270
- start_date_item = self . lifecycle_table . cellWidget ( row , 1 )
271
- end_date_item = self .lifecycle_table .cellWidget ( row , 2 )
271
+ # Start date field
272
+ start_date_edit = QgsDateTimeEdit()
273
+ start_date_edit.setDisplayFormat("yyyy-MM-dd" )
274
+ start_date_edit.setCalendarPopup(True )
275
+ self.lifecycle_table.setCellWidget(row_position, 1, start_date_edit )
272
276
273
- if status and start_date_item :
274
- status = status .value () if status .value () is not None else ""
275
- start_date = start_date_item .date ().toString ("yyyy-MM-dd" ) if start_date_item .date () else ""
276
- end_date = end_date_item .date ().toString ("yyyy-MM-dd" ) if end_date_item .date () else None
277
+ # End date field
278
+ end_date_edit = QgsDateTimeEdit()
279
+ end_date_edit.setDisplayFormat("yyyy-MM-dd")
280
+ end_date_edit.setCalendarPopup(True)
281
+ self.lifecycle_table.setCellWidget(row_position, 2, end_date_edit)
277
282
278
- lifecycle_model_item = QStandardItem (status )
279
- lifecycle_model_item .setData (status .value (), Qt .UserRole + 1 )
280
- start_date_model_item = QStandardItem (start_date )
281
- end_date_model_item = QStandardItem (end_date if end_date else "" )
283
+ # ID field
284
+ id_item = QTableWidgetItem()
285
+ # If lifecycle exists and has an ID, set it
286
+ if lifecycle and lifecycle.id_:
287
+ id_item.setData(Qt.UserRole, lifecycle.id_)
288
+ id_item.setText(str(lifecycle.id_))
289
+ self.lifecycle_table.setItem(row_position, 3, id_item)
290
+
291
+ # If lifecycle is provided, populate the fields
292
+ if lifecycle:
293
+ # For existing lifecycle, populate its data
294
+ status_combobox.set_value(lifecycle.status_id)
295
+ if lifecycle.starting_at:
296
+ start_date_edit.setDate(lifecycle.starting_at.date())
297
+ if lifecycle.ending_at:
298
+ end_date_edit.setDate(lifecycle.ending_at.date())
299
+ else:
300
+ # For a new lifecycle, leave the fields empty
301
+ status_combobox.set_value(None) # Do not set a default value
302
+ start_date_edit.clear() # No date set initially
303
+ end_date_edit.clear() # No date set initially """
282
304
283
- self .lifecycle_table .model ().appendRow (
284
- [lifecycle_model_item , start_date_model_item , end_date_model_item ]
285
- )
305
+ # def delete_lifecycle_row(self):
306
+ # selected_rows = self.lifecycle_table.selectionModel().selectedRows()
286
307
287
- self ._check_required_fields ()
308
+ # if selected_rows:
309
+ # row_position = selected_rows[0].row()
310
+ # self.lifecycle_table.removeRow(row_position)
311
+ # self._check_required_fields()
288
312
289
313
def into_lifecycle_model (self ) -> list [LifeCycle ]:
290
314
lifecycles = []
@@ -294,13 +318,27 @@ def into_lifecycle_model(self) -> list[LifeCycle]:
294
318
status_item = self .lifecycle_table .cellWidget (row , 0 )
295
319
start_date_item = self .lifecycle_table .cellWidget (row , 1 )
296
320
end_date_item = self .lifecycle_table .cellWidget (row , 2 )
321
+ id_item = self .lifecycle_table .item (row , 3 )
297
322
323
+ # Check if status_item and start_date_item exist, then create the LifeCycle
298
324
if status_item and start_date_item :
299
325
status_id = status_item .value ()
300
326
start_date = start_date_item .date ().toString ("yyyy-MM-dd" ) if start_date_item .date () else ""
301
327
end_date = end_date_item .date ().toString ("yyyy-MM-dd" ) if end_date_item .date () else None
302
328
303
- lifecycles .append (LifeCycle (status_id = status_id , starting_at = start_date , ending_at = end_date ))
329
+ lifecycle_id = None
330
+ if id_item and id_item .text ().strip (): # Check if the ID in the 4th column is not empty
331
+ lifecycle_id = id_item .data (Qt .UserRole )
332
+
333
+ # Create the LifeCycle model and add it to the list
334
+ lifecycles .append (
335
+ LifeCycle (
336
+ status_id = status_id ,
337
+ starting_at = start_date ,
338
+ ending_at = end_date ,
339
+ id_ = lifecycle_id , # Set the ID only if not None
340
+ )
341
+ )
304
342
305
343
return lifecycles
306
344
0 commit comments