4
4
from typing import TYPE_CHECKING
5
5
6
6
from qgis .core import QgsApplication
7
+ from qgis .gui import QgsDateTimeEdit
7
8
from qgis .PyQt import uic
8
- from qgis .PyQt .QtCore import QDate , Qt
9
- from qgis .PyQt .QtGui import QStandardItem , QStandardItemModel
9
+ from qgis .PyQt .QtCore import Qt
10
+ from qgis .PyQt .QtGui import QStandardItem
10
11
from qgis .PyQt .QtWidgets import (
11
12
QComboBox ,
12
13
QDateEdit ,
13
14
QDialog ,
14
15
QDialogButtonBox ,
16
+ QHeaderView ,
15
17
QLineEdit ,
16
18
QPushButton ,
17
19
QSizePolicy ,
18
20
QSpacerItem ,
19
- QStyledItemDelegate ,
20
21
QTableWidget ,
22
+ QTableWidgetItem ,
21
23
QTextEdit ,
22
24
QTreeWidgetItem ,
23
25
QWidget ,
48
50
FormClass , _ = uic .loadUiType (ui_path )
49
51
50
52
51
- class LifecycleTableModel (QStandardItemModel ):
52
- def __init__ (self , status_options , parent = None ):
53
- super ().__init__ (parent )
54
- self .status_options = status_options
55
-
56
- def flags (self , index ):
57
- if index .column () == 0 : # "Elinkaaren tila" - editable combo box
58
- return Qt .ItemIsSelectable | Qt .ItemIsEnabled
59
-
60
- if index .column () in (1 , 2 ): # Dates
61
- return Qt .ItemIsSelectable | Qt .ItemIsEnabled | Qt .ItemIsEditable
62
- return super ().flags (index )
63
-
64
-
65
- class LifecycleDelegate (QStyledItemDelegate ):
66
- def create_editor (self , parent , option , index ):
67
- if index .column () == 0 : # Status column
68
- lifecycle_combo_box = CodeComboBox (parent )
69
- lifecycle_combo_box .populate_from_code_layer (LifeCycleStatusLayer )
70
- return lifecycle_combo_box
71
- if index .column () in (1 , 2 ): # Dates columns
72
- date_edit = QDateEdit (parent )
73
- date_edit .setDisplayFormat ("yyyy-MM-dd" )
74
- date_edit .setCalendarPopup (True )
75
- return date_edit
76
- return super ().createEditor (parent , option , index )
77
-
78
- def set_editor_data (self , editor , index ):
79
- if isinstance (editor , CodeComboBox ) and index .column () == 0 :
80
- value = index .data (Qt .EditRole )
81
- if value is not None :
82
- editor .set_value (value )
83
- elif isinstance (editor , QDateEdit ) and index .column () in (1 , 2 ):
84
- value = index .data (Qt .EditRole )
85
- if value :
86
- editor .setDate (QDate .fromString (value , "yyyy-MM-dd" ))
87
-
88
- def set_model_data (self , editor , model , index ):
89
- if isinstance (editor , CodeComboBox ) and index .column () == 0 :
90
- model .setData (index , editor .value (), Qt .EditRole )
91
- if isinstance (editor , QDateEdit ) and index .column () in (1 , 2 ):
92
- model .setData (index , editor .date ().toString ("yyyy-MM-dd" ), Qt .EditRole )
93
-
94
-
95
53
class PlanAttributeForm (QDialog , FormClass ): # type: ignore
96
54
permanent_identifier_line_edit : QLineEdit
97
55
name_line_edit : QLineEdit
@@ -114,7 +72,7 @@ class PlanAttributeForm(QDialog, FormClass): # type: ignore
114
72
115
73
lifecycle_table : QTableWidget
116
74
add_lifecycle : QPushButton
117
- delete_lifecycle : QPushButton
75
+ # delete_lifecycle: QPushButton
118
76
119
77
button_box : QDialogButtonBox
120
78
@@ -124,6 +82,7 @@ def __init__(self, plan: Plan, _regulation_group_libraries: list[RegulationGroup
124
82
self .setupUi (self )
125
83
126
84
self .plan = plan
85
+ self .lifecycle_models = plan .lifecycles
127
86
128
87
self .plan_type_combo_box .populate_from_code_layer (PlanTypeLayer )
129
88
self .lifecycle_status_combo_box .populate_from_code_layer (LifeCycleStatusLayer )
@@ -173,12 +132,22 @@ def __init__(self, plan: Plan, _regulation_group_libraries: list[RegulationGroup
173
132
self .add_document_btn .setIcon (QgsApplication .getThemeIcon ("mActionAdd.svg" ))
174
133
175
134
# Lifecycle table setup
176
- self .lifecycle_table .setColumnCount (3 ) # Three columns: Status, Start Date, End Date
177
- self .lifecycle_table .setHorizontalHeaderLabels (["Elinkaaren tila" , "Alkupvm" , "Loppupvm" ])
178
- self .lifecycle_table .setRowCount (0 ) # No rows initially
135
+ self .lifecycle_table .setColumnCount (4 )
136
+ self .lifecycle_table .setHorizontalHeaderLabels (["Elinkaaren tila" , "Alkupvm" , "Loppupvm" , "id" ])
137
+ # self.lifecycle_table.setColumnHidden(3, True)
138
+ self .lifecycle_table .setRowCount (0 )
139
+
140
+ header = self .lifecycle_table .horizontalHeader ()
141
+ header .setSectionResizeMode (0 , QHeaderView .Stretch )
142
+ header .setSectionResizeMode (1 , QHeaderView .ResizeToContents )
143
+ header .setSectionResizeMode (2 , QHeaderView .ResizeToContents )
144
+
145
+ self .populate_lifecycle_table ()
146
+ # for lifecycle in plan.lifecycles:
147
+ # self.add_lifecycle_row(lifecycle)
179
148
180
149
self .add_lifecycle_button .clicked .connect (self .add_lifecycle_row )
181
- self .delete_lifecycle_button .clicked .connect (self .delete_lifecycle_row )
150
+ # self.delete_lifecycle_button.clicked.connect(self.delete_lifecycle_row)
182
151
183
152
self .button_box .button (QDialogButtonBox .Ok ).setEnabled (False )
184
153
self .button_box .accepted .connect (self ._on_ok_clicked )
@@ -188,7 +157,6 @@ def __init__(self, plan: Plan, _regulation_group_libraries: list[RegulationGroup
188
157
def _check_required_fields (self ) -> None :
189
158
ok_button = self .button_box .button (QDialogButtonBox .Ok )
190
159
191
- # Check if all required fields are filled and lifecycle table has at least one valid row
192
160
has_valid_lifecycle_row = False
193
161
for row in range (self .lifecycle_table .rowCount ()):
194
162
status_item = self .lifecycle_table .cellWidget (row , 0 )
@@ -198,7 +166,7 @@ def _check_required_fields(self) -> None:
198
166
if (
199
167
status_item
200
168
and status_item .value () is not None
201
- and start_date_item
169
+ # and start_date_item
202
170
and start_date_item .date ()
203
171
and (end_date_item and end_date_item .date () or True )
204
172
):
@@ -279,56 +247,107 @@ def delete_document(self, document_widget: DocumentWidget):
279
247
280
248
# ---
281
249
250
+ def populate_lifecycle_table (self ):
251
+ # kutsu add_lifecycle_row
252
+ self .lifecycle_table .setRowCount (len (self .lifecycle_models ))
253
+
254
+ for row , lifecycle in enumerate (self .lifecycle_models ):
255
+ # Populate the status combobox
256
+ status_combobox = CodeComboBox (self )
257
+ status_combobox .populate_from_code_layer (LifeCycleStatusLayer )
258
+ status_combobox .set_value (lifecycle .status_id )
259
+ self .lifecycle_table .setCellWidget (row , 0 , status_combobox )
260
+
261
+ # Populate the start date edit field
262
+ start_date_edit = QDateEdit (self )
263
+ start_date_edit .setDisplayFormat ("yyyy-MM-dd" )
264
+ start_date_edit .setCalendarPopup (True )
265
+ start_date_edit .setDate (lifecycle .starting_at .date ())
266
+ self .lifecycle_table .setCellWidget (row , 1 , start_date_edit )
267
+
268
+ # Populate the end date edit field
269
+ end_date_edit = QDateEdit (self )
270
+ end_date_edit .setDisplayFormat ("yyyy-MM-dd" )
271
+ end_date_edit .setCalendarPopup (True )
272
+ if lifecycle .ending_at :
273
+ end_date_edit .setDate (lifecycle .ending_at .date ())
274
+ self .lifecycle_table .setCellWidget (row , 2 , end_date_edit )
275
+
276
+ # Populate the ID column
277
+ id_item = QTableWidgetItem (str (lifecycle .id_ ) if lifecycle .id_ else "" )
278
+ id_item .setData (Qt .UserRole , lifecycle .id_ )
279
+ self .lifecycle_table .setItem (row , 3 , id_item )
280
+
282
281
def add_lifecycle_row (self ):
283
282
row_position = self .lifecycle_table .rowCount ()
284
283
self .lifecycle_table .insertRow (row_position )
285
284
286
- status = CodeComboBox (self )
285
+ status = CodeComboBox ()
287
286
status .populate_from_code_layer (LifeCycleStatusLayer )
288
287
self .lifecycle_table .setCellWidget (row_position , 0 , status )
289
288
290
- start_date_edit = QDateEdit ( self )
289
+ start_date_edit = QgsDateTimeEdit ( )
291
290
start_date_edit .setDisplayFormat ("yyyy-MM-dd" )
292
291
start_date_edit .setCalendarPopup (True )
293
292
self .lifecycle_table .setCellWidget (row_position , 1 , start_date_edit )
294
293
295
- end_date_edit = QDateEdit ( self )
294
+ end_date_edit = QgsDateTimeEdit ( )
296
295
end_date_edit .setDisplayFormat ("yyyy-MM-dd" )
297
296
end_date_edit .setCalendarPopup (True )
297
+ end_date_edit .clear ()
298
298
self .lifecycle_table .setCellWidget (row_position , 2 , end_date_edit )
299
299
300
- self .lifecycle_table .resizeRowsToContents ()
301
- self .lifecycle_table .resizeColumnsToContents ()
302
-
303
- def delete_lifecycle_row (self ):
304
- selected_rows = self .lifecycle_table .selectionModel ().selectedRows ()
300
+ """ def add_lifecycle_row(self, lifecycle: LifeCycle | None):
301
+ # Add a new row at the end of the table
302
+ row_position = self.lifecycle_table.rowCount()
303
+ self.lifecycle_table.insertRow(row_position)
305
304
306
- if selected_rows :
307
- row_position = selected_rows [ 0 ]. row ()
308
- self . lifecycle_table . removeRow ( row_position )
309
- self ._check_required_fields ( )
305
+ # Status combobox
306
+ status_combobox = CodeComboBox ()
307
+ status_combobox.populate_from_code_layer(LifeCycleStatusLayer )
308
+ self.lifecycle_table.setCellWidget(row_position, 0, status_combobox )
310
309
311
- def save_lifecycle ( self ):
312
- for row in range ( self . lifecycle_table . rowCount ()):
313
- status = self . lifecycle_table . cellWidget ( row , 0 )
314
- start_date_item = self . lifecycle_table . cellWidget ( row , 1 )
315
- end_date_item = self .lifecycle_table .cellWidget ( row , 2 )
310
+ # Start date field
311
+ start_date_edit = QgsDateTimeEdit()
312
+ start_date_edit.setDisplayFormat("yyyy-MM-dd" )
313
+ start_date_edit.setCalendarPopup(True )
314
+ self.lifecycle_table.setCellWidget(row_position, 1, start_date_edit )
316
315
317
- if status and start_date_item :
318
- status = status .value () if status .value () is not None else ""
319
- start_date = start_date_item .date ().toString ("yyyy-MM-dd" ) if start_date_item .date () else ""
320
- end_date = end_date_item .date ().toString ("yyyy-MM-dd" ) if end_date_item .date () else None
316
+ # End date field
317
+ end_date_edit = QgsDateTimeEdit()
318
+ end_date_edit.setDisplayFormat("yyyy-MM-dd")
319
+ end_date_edit.setCalendarPopup(True)
320
+ self.lifecycle_table.setCellWidget(row_position, 2, end_date_edit)
321
321
322
- lifecycle_model_item = QStandardItem (status )
323
- lifecycle_model_item .setData (status .value (), Qt .UserRole + 1 )
324
- start_date_model_item = QStandardItem (start_date )
325
- end_date_model_item = QStandardItem (end_date if end_date else "" )
322
+ # ID field
323
+ id_item = QTableWidgetItem()
324
+ # If lifecycle exists and has an ID, set it
325
+ if lifecycle and lifecycle.id_:
326
+ id_item.setData(Qt.UserRole, lifecycle.id_)
327
+ id_item.setText(str(lifecycle.id_))
328
+ self.lifecycle_table.setItem(row_position, 3, id_item)
329
+
330
+ # If lifecycle is provided, populate the fields
331
+ if lifecycle:
332
+ # For existing lifecycle, populate its data
333
+ status_combobox.set_value(lifecycle.status_id)
334
+ if lifecycle.starting_at:
335
+ start_date_edit.setDate(lifecycle.starting_at.date())
336
+ if lifecycle.ending_at:
337
+ end_date_edit.setDate(lifecycle.ending_at.date())
338
+ else:
339
+ # For a new lifecycle, leave the fields empty
340
+ status_combobox.set_value(None) # Do not set a default value
341
+ start_date_edit.clear() # No date set initially
342
+ end_date_edit.clear() # No date set initially """
326
343
327
- self .lifecycle_table .model ().appendRow (
328
- [lifecycle_model_item , start_date_model_item , end_date_model_item ]
329
- )
344
+ # def delete_lifecycle_row(self):
345
+ # selected_rows = self.lifecycle_table.selectionModel().selectedRows()
330
346
331
- self ._check_required_fields ()
347
+ # if selected_rows:
348
+ # row_position = selected_rows[0].row()
349
+ # self.lifecycle_table.removeRow(row_position)
350
+ # self._check_required_fields()
332
351
333
352
def into_lifecycle_model (self ) -> list [LifeCycle ]:
334
353
lifecycles = []
@@ -338,13 +357,27 @@ def into_lifecycle_model(self) -> list[LifeCycle]:
338
357
status_item = self .lifecycle_table .cellWidget (row , 0 )
339
358
start_date_item = self .lifecycle_table .cellWidget (row , 1 )
340
359
end_date_item = self .lifecycle_table .cellWidget (row , 2 )
360
+ id_item = self .lifecycle_table .item (row , 3 )
341
361
362
+ # Check if status_item and start_date_item exist, then create the LifeCycle
342
363
if status_item and start_date_item :
343
364
status_id = status_item .value ()
344
365
start_date = start_date_item .date ().toString ("yyyy-MM-dd" ) if start_date_item .date () else ""
345
366
end_date = end_date_item .date ().toString ("yyyy-MM-dd" ) if end_date_item .date () else None
346
367
347
- lifecycles .append (LifeCycle (status_id = status_id , starting_at = start_date , ending_at = end_date ))
368
+ lifecycle_id = None
369
+ if id_item and id_item .text ().strip (): # Check if the ID in the 4th column is not empty
370
+ lifecycle_id = id_item .data (Qt .UserRole )
371
+
372
+ # Create the LifeCycle model and add it to the list
373
+ lifecycles .append (
374
+ LifeCycle (
375
+ status_id = status_id ,
376
+ starting_at = start_date ,
377
+ ending_at = end_date ,
378
+ id_ = lifecycle_id , # Set the ID only if not None
379
+ )
380
+ )
348
381
349
382
return lifecycles
350
383
0 commit comments