feat(main.py): implement undo functionality for grading actions

This commit is contained in:
2025-08-19 08:05:38 +02:00
parent 8ba99a2053
commit 0b08869483
2 changed files with 46 additions and 0 deletions

45
main.py
View File

@@ -76,6 +76,9 @@ class MediaGrader:
self.window_width = 800
self.window_height = 600
# Undo functionality
self.undo_history = [] # List of (source_path, destination_path, original_index) tuples
def find_media_files(self) -> List[Path]:
"""Find all media files recursively in the directory"""
media_files = []
@@ -365,6 +368,9 @@ class MediaGrader:
shutil.move(str(current_file), str(destination))
print(f"Moved {current_file.name} to grade {grade}")
# Track this move for undo functionality
self.undo_history.append((str(destination), str(current_file), self.current_index))
self.media_files.pop(self.current_index)
if self.current_index >= len(self.media_files):
@@ -379,6 +385,40 @@ class MediaGrader:
return True
def undo_last_action(self):
"""Undo the last grading action by moving file back and restoring to media list"""
if not self.undo_history:
print("No actions to undo!")
return False
# Get the last action
moved_file_path, original_file_path, original_index = self.undo_history.pop()
try:
# Move the file back to its original location
shutil.move(moved_file_path, original_file_path)
# Add the file back to the media list at its original position
original_file = Path(original_file_path)
# Insert the file back at the appropriate position
if original_index <= len(self.media_files):
self.media_files.insert(original_index, original_file)
else:
self.media_files.append(original_file)
# Navigate to the restored file
self.current_index = original_index
print(f"Undone: Moved {original_file.name} back from grade folder")
return True
except Exception as e:
print(f"Error undoing action: {e}")
# If undo failed, put the action back in history
self.undo_history.append((moved_file_path, original_file_path, original_index))
return False
def run(self):
"""Main grading loop"""
self.media_files = self.find_media_files()
@@ -396,6 +436,7 @@ class MediaGrader:
print(" 1-5: Grade and move file")
print(" N: Next file")
print(" P: Previous file")
print(" U: Undo last grading action")
print(" Q/ESC: Quit")
cv2.namedWindow("Media Grader", cv2.WINDOW_NORMAL)
@@ -446,6 +487,10 @@ class MediaGrader:
elif key == ord("p"):
self.current_index = max(0, self.current_index - 1)
break
elif key == ord("u"):
if self.undo_last_action():
# File was restored, reload it
break
elif key in [ord("1"), ord("2"), ord("3"), ord("4"), ord("5")]:
grade = int(chr(key))
if not self.grade_media(grade):