55views and models, handling user interactions and data flow.
66"""
77
8+ import csv
9+
810from PySide6 .QtWidgets import QMessageBox
911
1012from ..config import (
@@ -427,6 +429,70 @@ def insert_session_row(self, view) -> None:
427429 animate_button (view .insert_button )
428430 view .session_notes_edit .clear ()
429431
432+ # Enable undo button after successful insert
433+ if hasattr (view , "undo_button" ):
434+ view .undo_button .setEnabled (True )
435+
436+ def undo_last_session_entry (self , view ) -> None :
437+ """
438+ Delete the last block_ID entry from the TSV file.
439+
440+ Args:
441+ view: The Step3View instance
442+ """
443+ if not self .session_data .is_file_open ():
444+ QMessageBox .warning (view , "Error" , "No file is currently open." )
445+ return
446+
447+ # Get the last written block_id (current block_id is the next one to write)
448+ last_written_block_id = self .session_data .block_id - 1
449+
450+ if last_written_block_id < 0 :
451+ QMessageBox .warning (view , "Error" , "No entries to undo." )
452+ return
453+
454+ # Read the TSV file and filter out rows with the last block_id
455+ file_path = self .session_data .file_path
456+ rows_to_keep = []
457+ rows_to_delete = []
458+
459+ with open (file_path , newline = "" , encoding = "utf-8" ) as f :
460+ reader = csv .DictReader (f , delimiter = "\t " )
461+ fieldnames = reader .fieldnames
462+
463+ for row in reader :
464+ block_id = row .get ("block_id" , "" )
465+ try :
466+ if int (block_id ) != last_written_block_id :
467+ rows_to_keep .append (row )
468+ else :
469+ rows_to_delete .append (row )
470+ except ValueError , TypeError :
471+ # If block_id is not a number, keep the row
472+ rows_to_keep .append (row )
473+
474+ if not rows_to_delete :
475+ QMessageBox .warning (
476+ view ,
477+ "Error" ,
478+ f"No entries found with block_id { last_written_block_id } ." ,
479+ )
480+ return
481+
482+ # Decrement block_id to point to the previous entry
483+ self .session_data .block_id = last_written_block_id
484+
485+ # Rewrite the TSV file with the filtered rows
486+ with open (file_path , "w" , newline = "" , encoding = "utf-8" ) as f :
487+ writer = csv .DictWriter (f , fieldnames = fieldnames , delimiter = "\t " )
488+ writer .writeheader ()
489+ writer .writerows (rows_to_keep )
490+
491+ # Disable undo button if no more entries to undo
492+ if self .session_data .block_id == 0 or len (rows_to_keep ) == 0 :
493+ if hasattr (view , "undo_button" ):
494+ view .undo_button .setEnabled (False )
495+
430496 def close_session (self , parent ) -> None :
431497 """
432498 Close the current session and file.
@@ -445,9 +511,6 @@ def close_session(self, parent) -> None:
445511
446512 if reply == QMessageBox .Ok :
447513 self .session_data .close_file ()
448- QMessageBox .information (
449- parent , "Session closed" , "Session closed and file saved."
450- )
451514 parent .close ()
452515
453516 def export_session_word (self , parent , scale_prefs = None , sections = None ) -> None :
0 commit comments